-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: usage/record capability definition (#1562)
### Add `usage/record` Capability definition for Egress Traffic Tracking **Summary:** - Introduced a new capability `usage/record` definition to track egress traffic metrics. - The `w3upInfra` upload-api will be updated to implement this new capability by leveraging Stripe's Usage Records API for accurate tracking and billing. RFC: storacha/RFC#37
- Loading branch information
Showing
11 changed files
with
251 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
import { provide } from './usage/report.js' | ||
import { provide as provideReport } from './usage/report.js' | ||
import { provide as provideRecord } from './usage/record.js' | ||
|
||
/** @param {import('./types.js').UsageServiceContext} context */ | ||
export const createService = (context) => ({ report: provide(context) }) | ||
export const createService = (context) => ({ | ||
report: provideReport(context), | ||
record: provideRecord(context), | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import * as API from '../types.js' | ||
import * as Provider from '@ucanto/server' | ||
import { Usage } from '@web3-storage/capabilities' | ||
|
||
/** @param {API.UsageServiceContext} context */ | ||
export const provide = (context) => | ||
Provider.provide(Usage.record, (input) => record(input, context)) | ||
|
||
/** | ||
* @param {API.Input<Usage.record>} input | ||
* @param {API.UsageServiceContext} context | ||
* @returns {Promise<API.Result<API.EgressRecordSuccess, API.EgressRecordFailure>>} | ||
*/ | ||
const record = async ({ capability, invocation }, context) => { | ||
const provider = /** @type {`did:web:${string}`} */ ( | ||
invocation.audience.did() | ||
) | ||
const consumerResponse = await context.provisionsStorage.getConsumer( | ||
provider, | ||
capability.with | ||
) | ||
if (consumerResponse.error) { | ||
return consumerResponse | ||
} | ||
const consumer = consumerResponse.ok | ||
const res = await context.usageStorage.record( | ||
// The space which contains the resource that was served. | ||
capability.with, | ||
// The customer that is being billed for the egress traffic. | ||
consumer.customer, | ||
// CID of the resource that was served. | ||
capability.nb.resource, | ||
// Number of bytes that were served. | ||
capability.nb.bytes, | ||
// Date and time when the resource was served. | ||
new Date(capability.nb.servedAt * 1000), | ||
// Link to the invocation that caused the egress traffic. | ||
invocation.cid | ||
) | ||
return res | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters