-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ucan stream consumer provider add count
- Loading branch information
1 parent
abe2410
commit 0af0278
Showing
10 changed files
with
359 additions
and
12,058 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import * as Sentry from '@sentry/serverless' | ||
|
||
import { hasOkReceipt } from '../utils/receipt.js' | ||
import { createMetricsTable } from '../tables/metrics.js' | ||
import { parseKinesisEvent } from '../utils/parse-kinesis-event.js' | ||
import { PROVIDER_ADD } from '../constants.js' | ||
|
||
Sentry.AWSLambda.init({ | ||
environment: process.env.SST_STAGE, | ||
dsn: process.env.SENTRY_DSN, | ||
tracesSampleRate: 1.0, | ||
}) | ||
|
||
const AWS_REGION = process.env.AWS_REGION || 'us-west-2' | ||
|
||
/** | ||
* @param {import('aws-lambda').KinesisStreamEvent} event | ||
*/ | ||
async function handler(event) { | ||
const ucanInvocations = parseKinesisEvent(event) | ||
|
||
const { | ||
TABLE_NAME: tableName = '', | ||
// set for testing | ||
DYNAMO_DB_ENDPOINT: dbEndpoint, | ||
} = process.env | ||
|
||
await updateProviderAddTotal(ucanInvocations, { | ||
metricsTable: createMetricsTable(AWS_REGION, tableName, { | ||
endpoint: dbEndpoint | ||
}) | ||
}) | ||
} | ||
|
||
/** | ||
* @param {import('../types').UcanInvocation[]} ucanInvocations | ||
* @param {import('../types').TotalSizeCtx} ctx | ||
*/ | ||
export async function updateProviderAddTotal (ucanInvocations, ctx) { | ||
const invocationsWithConsumerAdd = ucanInvocations.filter( | ||
inv => inv.value.att.find(a => a.can === PROVIDER_ADD) && hasOkReceipt(inv) | ||
).flatMap(inv => inv.value.att) | ||
|
||
await ctx.metricsTable.incrementProviderAddTotal(invocationsWithConsumerAdd) | ||
} | ||
|
||
export const consumer = Sentry.AWSLambda.wrapHandler(handler) |
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
225 changes: 225 additions & 0 deletions
225
ucan-invocation/test/functions/metrics-provider-add-total.test.js
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,225 @@ | ||
import { testConsumer as test } from '../helpers/context.js' | ||
|
||
import * as Signer from '@ucanto/principal/ed25519' | ||
import * as ProviderCapabilities from '@web3-storage/capabilities/provider' | ||
|
||
import { createDynamodDb } from '../helpers/resources.js' | ||
import { createSpace } from '../helpers/ucanto.js' | ||
import { randomCAR } from '../helpers/random.js' | ||
import { createDynamoTable, getItemFromTable} from '../helpers/tables.js' | ||
import { adminMetricsTableProps } from '../../tables/index.js' | ||
|
||
import { updateUploadAddTotal } from '../../functions/metrics-upload-add-total.js' | ||
import { createMetricsTable } from '../../tables/metrics.js' | ||
import { METRICS_NAMES, STREAM_TYPE } from '../../constants.js' | ||
|
||
const REGION = 'us-west-2' | ||
|
||
test.before(async t => { | ||
// Dynamo DB | ||
const { | ||
client: dynamo, | ||
endpoint: dbEndpoint | ||
} = await createDynamodDb({ port: 8000 }) | ||
|
||
t.context.dbEndpoint = dbEndpoint | ||
t.context.dynamoClient = dynamo | ||
}) | ||
|
||
test('handles a batch of single invocation with provider/add', async t => { | ||
const { tableName } = await prepareResources(t.context.dynamoClient) | ||
const uploadService = await Signer.generate() | ||
const alice = await Signer.generate() | ||
const car = await randomCAR(128) | ||
|
||
const metricsTable = createMetricsTable(REGION, tableName, { | ||
endpoint: t.context.dbEndpoint | ||
}) | ||
|
||
// const invocations = [{ | ||
// carCid: car.cid.toString(), | ||
// value: { | ||
// att: [ | ||
// ProviderCapabilities.add.create({ | ||
// with: alice.did(), | ||
// nb: { | ||
// root: car.cid, | ||
// shards: [car.cid] | ||
// } | ||
// }) | ||
// ], | ||
// aud: uploadService.did(), | ||
// iss: alice.did() | ||
// }, | ||
// type: STREAM_TYPE.RECEIPT, | ||
// out: { | ||
// ok: true | ||
// }, | ||
// ts: Date.now() | ||
// }] | ||
|
||
// // @ts-expect-error | ||
// await updateUploadAddTotal(invocations, { | ||
// metricsTable | ||
// }) | ||
|
||
// const item = await getItemFromTable(t.context.dynamoClient, tableName, { | ||
// name: METRICS_NAMES.UPLOAD_ADD_TOTAL | ||
// }) | ||
// t.truthy(item) | ||
// t.is(item?.name, METRICS_NAMES.UPLOAD_ADD_TOTAL) | ||
// t.is(item?.value, 1) | ||
|
||
t.is(1, 1) | ||
}) | ||
|
||
// test('handles batch of single invocations with multiple upload/add attributes', async t => { | ||
// const { tableName } = await prepareResources(t.context.dynamoClient) | ||
// const uploadService = await Signer.generate() | ||
// const alice = await Signer.generate() | ||
// const { spaceDid } = await createSpace(alice) | ||
|
||
// const cars = await Promise.all( | ||
// Array.from({ length: 10 }).map(() => randomCAR(128)) | ||
// ) | ||
|
||
// const metricsTable = createMetricsTable(REGION, tableName, { | ||
// endpoint: t.context.dbEndpoint | ||
// }) | ||
|
||
// const invocations = [{ | ||
// carCid: cars[0].cid.toString(), | ||
// value: { | ||
// att: cars.map((car) => UploadCapabilities.add.create({ | ||
// with: spaceDid, | ||
// nb: { | ||
// root: car.cid, | ||
// shards: [car.cid] | ||
// } | ||
// })), | ||
// aud: uploadService.did(), | ||
// iss: alice.did() | ||
// }, | ||
// type: STREAM_TYPE.RECEIPT, | ||
// out: { | ||
// ok: true | ||
// }, | ||
// ts: Date.now() | ||
// }] | ||
|
||
// // @ts-expect-error | ||
// await updateUploadAddTotal(invocations, { | ||
// metricsTable | ||
// }) | ||
|
||
// const item = await getItemFromTable(t.context.dynamoClient, tableName, { | ||
// name: METRICS_NAMES.UPLOAD_ADD_TOTAL | ||
// }) | ||
|
||
// t.truthy(item) | ||
// t.is(item?.name, METRICS_NAMES.UPLOAD_ADD_TOTAL) | ||
// t.is(item?.value, cars.length) | ||
// }) | ||
|
||
// test('handles a batch of single invocation without upload/add', async t => { | ||
// const { tableName } = await prepareResources(t.context.dynamoClient) | ||
// const uploadService = await Signer.generate() | ||
// const alice = await Signer.generate() | ||
// const { spaceDid } = await createSpace(alice) | ||
// const car = await randomCAR(128) | ||
|
||
// const metricsTable = createMetricsTable(REGION, tableName, { | ||
// endpoint: t.context.dbEndpoint | ||
// }) | ||
|
||
// const invocations = [{ | ||
// carCid: car.cid.toString(), | ||
// value: { | ||
// att: [ | ||
// UploadCapabilities.remove.create({ | ||
// with: spaceDid, | ||
// nb: { | ||
// root: car.cid, | ||
// } | ||
// }) | ||
// ], | ||
// aud: uploadService.did(), | ||
// iss: alice.did() | ||
// }, | ||
// type: STREAM_TYPE.RECEIPT, | ||
// out: { | ||
// ok: true | ||
// }, | ||
// ts: Date.now() | ||
// }] | ||
|
||
// // @ts-expect-error | ||
// await updateUploadAddTotal(invocations, { | ||
// metricsTable | ||
// }) | ||
|
||
// const item = await getItemFromTable(t.context.dynamoClient, tableName, { | ||
// name: METRICS_NAMES.UPLOAD_ADD_TOTAL | ||
// }) | ||
|
||
// t.truthy(item) | ||
// t.is(item?.name, METRICS_NAMES.UPLOAD_ADD_TOTAL) | ||
// t.is(item?.value, 0) | ||
// }) | ||
|
||
// test('handles a batch of single invocation without receipts', async t => { | ||
// const { tableName } = await prepareResources(t.context.dynamoClient) | ||
// const uploadService = await Signer.generate() | ||
// const alice = await Signer.generate() | ||
// const { spaceDid } = await createSpace(alice) | ||
// const car = await randomCAR(128) | ||
|
||
// const metricsTable = createMetricsTable(REGION, tableName, { | ||
// endpoint: t.context.dbEndpoint | ||
// }) | ||
|
||
// const invocations = [{ | ||
// carCid: car.cid.toString(), | ||
// value: { | ||
// att: [ | ||
// UploadCapabilities.add.create({ | ||
// with: spaceDid, | ||
// nb: { | ||
// root: car.cid, | ||
// shards: [car.cid] | ||
// } | ||
// }) | ||
// ], | ||
// aud: uploadService.did(), | ||
// iss: alice.did() | ||
// }, | ||
// type: STREAM_TYPE.WORKFLOW, | ||
// ts: Date.now() | ||
// }] | ||
|
||
// // @ts-expect-error | ||
// await updateUploadAddTotal(invocations, { | ||
// metricsTable | ||
// }) | ||
|
||
// const item = await getItemFromTable(t.context.dynamoClient, tableName, { | ||
// name: METRICS_NAMES.UPLOAD_ADD_TOTAL | ||
// }) | ||
|
||
// t.truthy(item) | ||
// t.is(item?.name, METRICS_NAMES.UPLOAD_ADD_TOTAL) | ||
// t.is(item?.value, 0) | ||
// }) | ||
|
||
/** | ||
* @param {import('@aws-sdk/client-dynamodb').DynamoDBClient} dynamoClient | ||
*/ | ||
async function prepareResources (dynamoClient) { | ||
const [ tableName ] = await Promise.all([ | ||
createDynamoTable(dynamoClient, adminMetricsTableProps), | ||
]) | ||
|
||
return { | ||
tableName | ||
} | ||
} |
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
Oops, something went wrong.