Skip to content

Commit

Permalink
chore: appease linter
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan Shaw committed Oct 25, 2023
1 parent 3f0af82 commit 4331061
Show file tree
Hide file tree
Showing 14 changed files with 39 additions and 64 deletions.
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
dist/
node_modules/
node_modules/
coverage/
1 change: 0 additions & 1 deletion billing/functions/space-billing-queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ Sentry.AWSLambda.init({
* spaceDiffTable?: string
* spaceSnapshotTable?: string
* usageTable?: string
* dbEndpoint?: URL
* region?: 'us-west-2'|'us-east-2'
* }} CustomHandlerContext
*/
Expand Down
1 change: 0 additions & 1 deletion billing/functions/ucan-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ Sentry.AWSLambda.init({
* spaceDiffTable?: string
* subscriptionTable?: string
* consumerTable?: string
* dbEndpoint?: URL
* region?: 'us-west-2'|'us-east-2'
* }} CustomHandlerContext
*/
Expand Down
61 changes: 18 additions & 43 deletions billing/functions/usage-table.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import * as Sentry from '@sentry/serverless'
import { toString, fromString } from 'uint8arrays'
import * as Link from 'multiformats/link'
import { createSpaceDiffStore } from '../tables/space-diff.js'
import { createSubscriptionStore } from '../tables/subscription.js'
import { createConsumerStore } from '../tables/consumer.js'
import { notNully } from './lib.js'
import { handleUsageInsert } from '../lib/usage-table.js'
import { unmarshall } from '@aws-sdk/util-dynamodb'
import * as Usage from '../data/usage.js'

Sentry.AWSLambda.init({
environment: process.env.SST_STAGE,
Expand All @@ -14,13 +9,7 @@ Sentry.AWSLambda.init({
})

/**
* @typedef {{
* spaceDiffTable?: string
* subscriptionTable?: string
* consumerTable?: string
* dbEndpoint?: URL
* region?: 'us-west-2'|'us-east-2'
* }} CustomHandlerContext
* @typedef {{}} CustomHandlerContext
*/

export const handler = Sentry.AWSLambda.wrapHandler(
Expand All @@ -29,47 +18,33 @@ export const handler = Sentry.AWSLambda.wrapHandler(
* @param {import('aws-lambda').Context} context
*/
async (event, context) => {
/** @type {CustomHandlerContext|undefined} */
const customContext = context?.clientContext?.Custom
const spaceDiffTable = customContext?.spaceDiffTable ?? notNully(process.env, 'SPACE_DIFF_TABLE_NAME')
const subscriptionTable = customContext?.subscriptionTable ?? notNully(process.env, 'SUBSCRIPTION_TABLE_NAME')
const consumerTable = customContext?.consumerTable ?? notNully(process.env, 'CONSUMER_TABLE_NAME')
const dbEndpoint = new URL(customContext?.dbEndpoint ?? notNully(process.env, 'DB_ENDPOINT'))
const region = customContext?.region ?? notNully(process.env, 'AWS_REGION')
// /** @type {CustomHandlerContext|undefined} */
// const customContext = context?.clientContext?.Custom
// TODO: stripe publishable key or something?

const records = parseUsageInsertEvent(event)
if (!records.length) return
const usages = parseUsageInsertEvent(event)
if (!usages.length) return

const storeOptions = { endpoint: dbEndpoint }
const ctx = {
spaceDiffStore: createSpaceDiffStore(region, spaceDiffTable, storeOptions),
subscriptionStore: createSubscriptionStore(region, subscriptionTable, storeOptions),
consumerStore: createConsumerStore(region, consumerTable, storeOptions)
for (const u of usages) {
// TODO: send request to stripe
console.log(u)
}
const results = await Promise.all(records.map(m => handleUsageInsert(m, ctx)))
for (const r of results) if (r.error) throw r.error
}
)

/**
* @param {import('aws-lambda').DynamoDBStreamEvent} event
* @returns {import('../lib/api').Usage[]}
*/
const parseUsageInsertEvent = event => {
const usages = []
for (const r of event.Records) {
if (r.eventName !== 'INSERT') continue
usages.push(r.dynamodb)
if (!r.dynamodb) continue
if (!r.dynamodb.NewImage) throw new Error('missing "NEW_IMAGE" in stream event')
// @ts-expect-error IDK why this is not Record<string, AttributeValue>
const { ok: usage, error } = Usage.decode(unmarshall(r.dynamodb.NewImage))
if (error) throw error
usages.push(usage)
}

const batch = event.Records.map(r => fromString(r.kinesis.data, 'base64'))
return batch.map(b => {
const json = JSON.parse(toString(b, 'utf8'))
return {
...json,
carCid: Link.parse(json.carCid),
invocationCid: Link.parse(json.invocationCid),
ts: new Date(json.ts)
}
})
return usages
}
1 change: 1 addition & 0 deletions billing/lib/billing-cron.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const handleCronTick = async ctx => {
const from = startOfLastMonth()
const to = startOfMonth()

/** @type {string|undefined} */
let cursor
while (true) {
const customerList = await ctx.customerStore.list({}, { cursor, size: 1000 })
Expand Down
1 change: 1 addition & 0 deletions billing/lib/customer-billing-queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const handleCustomerBillingInstruction = async (instruction, ctx) => {
console.log(`Processing customer billing instruction for: ${instruction.customer}`)
console.log(`Period: ${instruction.from.toISOString()} - ${instruction.to.toISOString()}`)

/** @type {string|undefined} */
let cursor
while (true) {
const subsList = await ctx.subscriptionStore.list({ customer: instruction.customer }, { cursor })
Expand Down
1 change: 1 addition & 0 deletions billing/lib/space-billing-queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const handleSpaceBillingInstruction = async (instruction, ctx) => {
let size = snap.size
let usage = size * BigInt(instruction.to.getTime() - instruction.from.getTime())

/** @type {string|undefined} */
let cursor
while (true) {
const spaceDiffList = await ctx.spaceDiffStore.listBetween(
Expand Down
7 changes: 0 additions & 7 deletions billing/lib/usage-table.js

This file was deleted.

10 changes: 4 additions & 6 deletions filecoin/test/helpers/resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export async function createDynamodDb(opts = {}) {
*
* @typedef {import('@aws-sdk/client-dynamodb').CreateTableCommandInput} CreateTableCommandInput
* @typedef {import('@serverless-stack/resources').TableProps} TableProps
*
* @param {TableProps} props
* @returns {Pick<CreateTableCommandInput, 'AttributeDefinitions' | 'KeySchema' | 'GlobalSecondaryIndexes'>}
*/
Expand All @@ -47,11 +46,11 @@ export function dynamoDBTableConfig ({ fields, primaryIndex, globalIndexes = {}
.filter(([k]) => attributes.includes(k)) // 'The number of attributes in key schema must match the number of attributes defined in attribute definitions'
.map(([k, v]) => ({
AttributeName: k,
AttributeType: v[0].toUpperCase()
AttributeType: /** @type {import('@aws-sdk/client-dynamodb').ScalarAttributeType} */ (v[0].toUpperCase())
}))
const KeySchema = toKeySchema(primaryIndex)
const GlobalSecondaryIndexes = Object.entries(globalIndexes)
.map(([IndexName, val]) => ({
.map(([IndexName, val]) => /** @type {import('@aws-sdk/client-dynamodb').GlobalSecondaryIndex} */ ({
IndexName,
KeySchema: toKeySchema(val),
Projection: { ProjectionType: 'ALL' },
Expand All @@ -74,9 +73,8 @@ export function dynamoDBTableConfig ({ fields, primaryIndex, globalIndexes = {}
* @param {string} [index.sortKey]
*/
function toKeySchema ({partitionKey, sortKey}) {
const KeySchema = [
{ AttributeName: partitionKey, KeyType: 'HASH' }
]
/** @type {import('@aws-sdk/client-dynamodb').KeySchemaElement[]} */
const KeySchema = [{ AttributeName: partitionKey, KeyType: 'HASH' }]
if (sortKey) {
KeySchema.push(
{ AttributeName: sortKey, KeyType: 'RANGE' }
Expand Down
5 changes: 4 additions & 1 deletion stacks/billing-db-stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ export const BillingDbStack = ({ stack }) => {
const customerTable = new Table(stack, 'customer', customerTableProps)
const spaceSnapshotTable = new Table(stack, 'space-snapshot', spaceSnapshotTableProps)
const spaceDiffTable = new Table(stack, 'space-diff', spaceDiffTableProps)
const usageTable = new Table(stack, 'usage', usageTableProps)
const usageTable = new Table(stack, 'usage', {
...usageTableProps,
stream: 'new_image'
})
return { customerTable, spaceSnapshotTable, spaceDiffTable, usageTable }
}
3 changes: 2 additions & 1 deletion stacks/billing-stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ export function BillingStack ({ stack, app }) {
batchSize: 1,
startingPosition: StartingPosition.LATEST
}
}
},
filters: [{ eventName: ['INSERT'] }]
}
})

Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@
"stripInternal": true,
"resolveJsonModule": true,
},
"include": [ "billing", "upload-api", "stacks", "carpark", "replicator", "satnav", "ucan-invocation", "filecoin", "tools" ]
"include": [ "billing", "upload-api", "stacks", "carpark", "replicator", "satnav", "ucan-invocation", "filecoin", "tools" ],
"exclude": [ "billing/coverage" ]
}
3 changes: 2 additions & 1 deletion ucan-invocation/test/helpers/resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ export function dynamoDBTableConfig ({ fields, primaryIndex }) {
.filter(([k]) => attributes.includes(k)) // 'The number of attributes in key schema must match the number of attributes defined in attribute definitions'
.map(([k, v]) => ({
AttributeName: k,
AttributeType: v[0].toUpperCase()
AttributeType: /** @type {import('@aws-sdk/client-dynamodb').ScalarAttributeType} */ (v[0].toUpperCase())
}))
/** @type {import('@aws-sdk/client-dynamodb').KeySchemaElement[]} */
const KeySchema = [
{ AttributeName: primaryIndex.partitionKey, KeyType: 'HASH' }
]
Expand Down
3 changes: 2 additions & 1 deletion upload-api/test/helpers/resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,9 @@ export function dynamoDBTableConfig ({ fields, primaryIndex, globalIndexes }) {
.filter(([k]) => attributes.includes(k)) // 'The number of attributes in key schema must match the number of attributes defined in attribute definitions'
.map(([k, v]) => ({
AttributeName: k,
AttributeType: v[0].toUpperCase(),
AttributeType: /** @type {import('@aws-sdk/client-dynamodb').ScalarAttributeType} */ (v[0].toUpperCase()),
}))
/** @type {import('@aws-sdk/client-dynamodb').KeySchemaElement[]} */
const KeySchema = [
{ AttributeName: primaryIndex.partitionKey, KeyType: 'HASH' },
]
Expand Down

0 comments on commit 4331061

Please sign in to comment.