-
Notifications
You must be signed in to change notification settings - Fork 7
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 space store add and remove count (#160)
Part of metrics work #117 Adds space counters for `store/add` and `store/remove`.
- Loading branch information
1 parent
370651c
commit 7728718
Showing
16 changed files
with
812 additions
and
59 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
52 changes: 52 additions & 0 deletions
52
ucan-invocation/functions/space-metrics-store-add-total.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,52 @@ | ||
import * as Sentry from '@sentry/serverless' | ||
|
||
import { createSpaceMetricsTable } from '../tables/space-metrics.js' | ||
import { parseKinesisEvent } from '../utils/parse-kinesis-event.js' | ||
|
||
Sentry.AWSLambda.init({ | ||
environment: process.env.SST_STAGE, | ||
dsn: process.env.SENTRY_DSN, | ||
tracesSampleRate: 1.0, | ||
}) | ||
|
||
const STORE_ADD = 'store/add' | ||
const AWS_REGION = process.env.AWS_REGION || 'us-west-2' | ||
|
||
/** | ||
* @typedef {object} IncrementInput | ||
* @property {`did:${string}:${string}`} space | ||
* @property {number} count | ||
*/ | ||
|
||
/** | ||
* @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 updateStoreCount(ucanInvocations, { | ||
spaceMetricsTable: createSpaceMetricsTable(AWS_REGION, tableName, { | ||
endpoint: dbEndpoint | ||
}) | ||
}) | ||
} | ||
|
||
/** | ||
* @param {import('../types').UcanInvocation[]} ucanInvocations | ||
* @param {import('../types').SpaceMetricsTableCtx} ctx | ||
*/ | ||
export async function updateStoreCount (ucanInvocations, ctx) { | ||
const invocationsWithStoreAdd = ucanInvocations.filter( | ||
inv => inv.value.att.find(a => a.can === STORE_ADD) | ||
).flatMap(inv => inv.value.att) | ||
|
||
await ctx.spaceMetricsTable.incrementStoreAddCount(invocationsWithStoreAdd) | ||
} | ||
|
||
export const consumer = Sentry.AWSLambda.wrapHandler(handler) |
52 changes: 52 additions & 0 deletions
52
ucan-invocation/functions/space-metrics-store-remove-total.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,52 @@ | ||
import * as Sentry from '@sentry/serverless' | ||
|
||
import { createSpaceMetricsTable } from '../tables/space-metrics.js' | ||
import { parseKinesisEvent } from '../utils/parse-kinesis-event.js' | ||
import { STORE_REMOVE } 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' | ||
|
||
/** | ||
* @typedef {object} IncrementInput | ||
* @property {`did:${string}:${string}`} space | ||
* @property {number} count | ||
*/ | ||
|
||
/** | ||
* @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 updateStoreCount(ucanInvocations, { | ||
spaceMetricsTable: createSpaceMetricsTable(AWS_REGION, tableName, { | ||
endpoint: dbEndpoint | ||
}) | ||
}) | ||
} | ||
|
||
/** | ||
* @param {import('../types').UcanInvocation[]} ucanInvocations | ||
* @param {import('../types').SpaceMetricsTableCtx} ctx | ||
*/ | ||
export async function updateStoreCount (ucanInvocations, ctx) { | ||
const invocationsWithStoreRemove = ucanInvocations.filter( | ||
inv => inv.value.att.find(a => a.can === STORE_REMOVE) | ||
).flatMap(inv => inv.value.att) | ||
|
||
await ctx.spaceMetricsTable.incrementStoreRemoveCount(invocationsWithStoreRemove) | ||
} | ||
|
||
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
Oops, something went wrong.