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 111 112 113 114 115 116 117 118 | 20x 20x 20x 160x 20x 20x 20x 441x 441x 31x 31x 31x 3x 2x 31x 31x 31x 18x 18x 20x | const { ScanCommand } = require('@aws-sdk/lib-dynamodb')
/**
* Single source of truth for the credit cost of every metered operation —
* see docs/architecture/USAGE_METERING.md. Each row is editable at runtime
* by a platform admin (GET/PUT /v1/platform-admin/pricing, backed by
* PricingConfigTable); `defaultValue` is only the fallback used when no
* PricingConfigTable row overrides it yet (e.g. right after a fresh deploy).
*
* Every metering lambda reads its rate(s) through getPricingConfig() below
* instead of a hardcoded constant, so a price change takes effect for every
* caller (subject to CACHE_TTL_MS staleness) without a redeploy.
*/
const PRICING_DEFINITIONS = [
{
id: 'dynamodb_storage_credits_per_kb',
defaultValue: 2,
unit: 'credits / KB',
description: 'DynamoDB item storage (entities, items, flow settings)'
},
{
id: 'dynamodb_throughput_credits_per_wru',
defaultValue: 0.0483,
unit: 'credits / WRU',
description: 'DynamoDB write throughput consumed by a write'
},
{
id: 'dynamodb_read_credits_per_ru',
defaultValue: 0.0097,
unit: 'credits / RU',
description: 'DynamoDB read throughput consumed by API traffic (daily usage aggregation)'
},
{
id: 's3_storage_credits_per_mb',
defaultValue: 150,
unit: 'credits / MB',
description: 'S3 storage for uploaded images and files, per object (1-credit minimum)'
},
{
id: 'lambda_compute_credits_per_gb_second',
defaultValue: 1,
unit: 'credits / GB-second',
description: 'Lambda compute time (daily usage aggregation)'
},
{
id: 'api_gateway_credits_per_call',
defaultValue: 0.255,
unit: 'credits / call',
description: 'API Gateway request volume (daily usage aggregation)'
},
{
id: 'automation_wake_up_credits',
defaultValue: 1,
unit: 'credits / wake-up',
description:
'Wake-up of an item by a timer of an automatic workflow transition (scheduler + compute), paid by the flow owner'
},
{
id: 'automation_email_credits',
defaultValue: 1,
unit: 'credits / email',
description:
'Email sent by an Email action of a workflow transition (queue + compute + SES), paid by the flow owner'
}
]
const DEFAULT_PRICING = Object.fromEntries(
PRICING_DEFINITIONS.map((def) => [def.id, def.defaultValue])
)
const CACHE_TTL_MS = 60 * 60 * 1000
let cachedConfig = null
let cacheExpiresAt = 0
/**
* Returns the current effective credit pricing — PricingConfigTable's rows
* layered onto DEFAULT_PRICING, so an operation nobody has ever edited still
* prices correctly. Cached per Lambda execution environment for
* CACHE_TTL_MS: pricing rarely changes and this runs on the hot path of
* every metered write, so it isn't worth a DynamoDB read on every
* invocation — an admin's edit is picked up by each warm container within
* one TTL window rather than instantly.
*
* @param {import('@aws-sdk/lib-dynamodb').DynamoDBDocumentClient} docClient
* @param {string} tableName
* @returns {Promise<Record<string, number>>}
*/
async function getPricingConfig(docClient, tableName) {
const now = Date.now()
if (cachedConfig && now < cacheExpiresAt) return cachedConfig
const result = await docClient.send(new ScanCommand({ TableName: tableName }))
const config = { ...DEFAULT_PRICING }
for (const item of result.Items || []) {
if (typeof item.value === 'number' && Number.isFinite(item.value)) {
config[item.id] = item.value
}
}
cachedConfig = config
cacheExpiresAt = now + CACHE_TTL_MS
return config
}
/** Test-only: forces the next getPricingConfig() call to re-read the table. */
function clearPricingConfigCache() {
cachedConfig = null
cacheExpiresAt = 0
}
module.exports = {
PRICING_DEFINITIONS,
DEFAULT_PRICING,
getPricingConfig,
clearPricingConfigCache
}
|