Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | 1x 1x 1x 1x 1x 1x 1x 11x 11x 2x 11x 11x 12x 12x 12x 11x 15x 15x 15x 15x 15x 15x 11x 15x 11x 15x 8x 11x 1x | const { DynamoDBClient } = require('@aws-sdk/client-dynamodb')
const { DynamoDBDocumentClient, QueryCommand, ScanCommand } = require('@aws-sdk/lib-dynamodb')
const { requirePlatformAdmin } = require('../utils/requirePlatformAdmin')
const { getPricingConfig } = require('../utils/pricingConfig')
const { creditsFor } = require('./lambdaCreditPricing')
const docClient = DynamoDBDocumentClient.from(new DynamoDBClient({}))
const corsHeaders = {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
}
/**
* `LambdaBillingTable` holds one row per function an admin has marked
* "always free" (see utils/lambdaBillingConfig.js) — a handful of rows at
* most, so a plain Scan (no GSI) mirrors PricingConfigTable's own listing.
* Returns a Map so toStatsEntry can also surface who overrode a function and
* when, which the metering-side cache (a bare Set of keys) doesn't need.
*/
async function fetchFreeOverrides(tableName) {
const result = await docClient.send(new ScanCommand({ TableName: tableName }))
return new Map(
(result.Items || []).map((item) => [
item.functionKey,
{ updatedAt: item.updatedAt || null, updatedBy: item.updatedBy || null }
])
)
}
/**
* Every row shares the same `gsiPk` ('LAMBDA_STAT', set by
* shared/lambda-stats.js), so listing every function is a Query on
* `platformStatsIndex`, never a table Scan — same pattern as the
* users/flows/entities platform-admin lists.
*/
async function fetchAllRows(tableName) {
const rows = []
let ExclusiveStartKey
do {
const result = await docClient.send(
new QueryCommand({
TableName: tableName,
IndexName: 'platformStatsIndex',
KeyConditionExpression: 'gsiPk = :gsiPk',
ExpressionAttributeValues: { ':gsiPk': 'LAMBDA_STAT' },
ExclusiveStartKey
})
)
rows.push(...(result.Items ?? []))
ExclusiveStartKey = result.LastEvaluatedKey
} while (ExclusiveStartKey)
return rows
}
/**
* invocationCount/totalDurationMs are both extrapolated from sampled writes
* (see shared/lambda-stats.js) — the average is exact regardless of the
* sample rate, only the totals are estimates. `timeSharePercent` is this
* function's share of the time spent by all functions combined (itself
* included), so it is a ratio of two estimates drawn from the same sampling.
*/
function toStatsEntry(row, pricing, grandTotalDurationMs, freeOverrides) {
const [resource, operation] = String(row.functionKey).split('/')
const invocationCount = row.invocationCount ?? 0
const totalDurationMs = row.totalDurationMs ?? 0
const credits = creditsFor(row.functionKey, row.billing || 'never', pricing)
const override = freeOverrides.get(row.functionKey) || null
return {
functionKey: row.functionKey,
resource,
operation,
invocationCount,
avgDurationMs: invocationCount > 0 ? Math.round(totalDurationMs / invocationCount) : 0,
totalDurationMs: Math.round(totalDurationMs),
timeSharePercent:
grandTotalDurationMs > 0
? Math.round((totalDurationMs / grandTotalDurationMs) * 10000) / 100
: 0,
lastSeenAt: row.lastSeenAt ?? null,
// An admin override always wins over what the route would otherwise
// charge: `pools` is left as-is (what it would cost without the
// override, for context), but `billed` reflects what actually happens.
credits: { ...credits, billed: override ? false : credits.billed },
freeOverride: override
}
}
async function lambdaStatsHandler() {
const [rows, pricing, freeOverrides] = await Promise.all([
fetchAllRows(process.env.LAMBDA_STATS_TABLE_NAME),
// The effective (admin-editable) rates, so the displayed credit costs
// never drift from what is actually charged.
getPricingConfig(docClient, process.env.PRICING_CONFIG_TABLE_NAME),
fetchFreeOverrides(process.env.LAMBDA_BILLING_TABLE_NAME)
])
const grandTotalDurationMs = rows.reduce((sum, row) => sum + (row.totalDurationMs ?? 0), 0)
const functions = rows
.map((row) => toStatsEntry(row, pricing, grandTotalDurationMs, freeOverrides))
.sort((a, b) => b.invocationCount - a.invocationCount)
return {
statusCode: 200,
headers: corsHeaders,
body: JSON.stringify({ functions })
}
}
exports.handler = requirePlatformAdmin(lambdaStatsHandler)
|