All files / admin lambdaCreditPricing.js

100% Statements 13/13
100% Branches 6/6
100% Functions 4/4
100% Lines 12/12

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 993x                                                             3x           3x 3x         3x                                                                 24x     31x 31x                   22x     31x 22x     3x  
const { PRICING_DEFINITIONS } = require('../utils/pricingConfig')
 
/**
 * Describes which credit pools a Lambda's invocations can debit, for display
 * on the platform-admin Lambda Stats page (see lambda-stats.js). Two layers:
 *
 * 1. BASELINE_POOLS — the usage-based pools debited daily from every
 *    authenticated invocation's usage_event log (see usage/aggregate.js):
 *    compute time, the API Gateway call, and DynamoDB reads. Whether
 *    a given function's route can produce these at all ('always' /
 *    'conditional' / 'never') is derived straight from the OpenAPI spec's
 *    `security` requirement — see api-stack.ts's classifyBilling — and
 *    travels with each row as `billing` (set by shared/lambda-stats.js).
 *
 * 2. EXTRA_CREDIT_CHARGES — additional, operation-specific charges that a
 *    handful of handlers debit synchronously via
 *    utils/creditsLedger.js's debitCredits/debitCreditsIdempotent (DynamoDB
 *    storage/write-throughput for a create/update, S3 storage for an
 *    upload). Unlike the baseline pools, *which* lambda charges *which* pool
 *    is business logic embedded in each handler file with no single
 *    declarative source to read it from, so this mapping is hand-maintained.
 *
 * Neither layer stores a rate: rates are admin-editable at runtime (see
 * utils/pricingConfig.js), so each entry only names the pricing key it is
 * charged at, and creditsFor() resolves it to the *current* effective value.
 * This is also how a worker with no HTTP route at all (entities/import-worker,
 * entities/fork-worker) — whose `billing` is always 'never', since there is
 * no API Gateway call and no request-time identity to attribute the baseline
 * pools to — still shows the credits it really does charge, via the job's
 * own resolved userId.
 */
const BASELINE_POOLS = [
  { pool: 'lambda_compute', pricingId: 'lambda_compute_credits_per_gb_second' },
  { pool: 'api_gateway', pricingId: 'api_gateway_credits_per_call' },
  { pool: 'dynamodb_throughput', pricingId: 'dynamodb_read_credits_per_ru' }
]
 
const STORAGE = { pool: 'dynamodb_storage', pricingId: 'dynamodb_storage_credits_per_kb' }
const WRITE_THROUGHPUT = {
  pool: 'dynamodb_throughput',
  pricingId: 'dynamodb_throughput_credits_per_wru'
}
 
const EXTRA_CREDIT_CHARGES = {
  'flows/create': [STORAGE, WRITE_THROUGHPUT],
  'items/create': [STORAGE, WRITE_THROUGHPUT],
  // Unlike every other entry, marking comments/create free also waives these
  // (a user with no credits must still be able to comment) — see comments/create.js.
  'comments/create': [
    { ...STORAGE, note: 'waived when marked free' },
    { ...WRITE_THROUGHPUT, note: 'waived when marked free' }
  ],
  'items/update': [{ ...STORAGE, note: 'growth only' }, WRITE_THROUGHPUT],
  // Approving a membership request creates the membership item, charged to the approver
  'membership-requests/decide': [
    { ...STORAGE, note: 'approval, approver' },
    { ...WRITE_THROUGHPUT, note: 'approval, approver' }
  ],
  'entities/create': [STORAGE, WRITE_THROUGHPUT],
  'entities/update': [{ ...STORAGE, note: 'growth only' }, WRITE_THROUGHPUT],
  'entities/import-worker': [STORAGE, WRITE_THROUGHPUT],
  'entities/fork-worker': [{ ...STORAGE, note: 'entity + items' }, WRITE_THROUGHPUT],
  'uploads/s3-usage-consumer': [
    { pool: 's3_storage', pricingId: 's3_storage_credits_per_mb', note: '1 credit minimum' }
  ],
  // A timer's wake-up, charged to the flow owner (UC-WORKFLOW-009.1)
  'items/timer': [
    { pool: 'lambda_compute', pricingId: 'automation_wake_up_credits', note: 'flow owner' },
    { ...WRITE_THROUGHPUT, note: 'flow owner' }
  ],
  // Emails of transitions' Email actions, charged to the flow owner (UC-WORKFLOW-011.1)
  'automation/email-consumer': [
    { pool: 'lambda_compute', pricingId: 'automation_email_credits', note: 'flow owner, per email' }
  ]
}
 
const UNIT_BY_PRICING_ID = Object.fromEntries(PRICING_DEFINITIONS.map((def) => [def.id, def.unit]))
 
function toPoolRate({ pool, pricingId, note }, pricing) {
  const rate = `${pricing[pricingId]} ${UNIT_BY_PRICING_ID[pricingId]}`
  return { pool, rate: note ? `${rate} (${note})` : rate }
}
 
/**
 * @param {string} functionKey `${resource}/${operation}`
 * @param {'always' | 'conditional' | 'never'} billing
 * @param {Record<string, number>} pricing the effective pricing, as returned by getPricingConfig()
 * @returns {{ billing: string, billed: boolean, pools: Array<{ pool: string, rate: string }> }}
 */
function creditsFor(functionKey, billing, pricing) {
  const pools = [
    ...(billing === 'never' ? [] : BASELINE_POOLS),
    ...(EXTRA_CREDIT_CHARGES[functionKey] ?? [])
  ].map((entry) => toPoolRate(entry, pricing))
  return { billing, billed: pools.length > 0, pools }
}
 
module.exports = { creditsFor, BASELINE_POOLS, EXTRA_CREDIT_CHARGES }