@contractspec/lib.metering 1.56.1 → 1.58.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. package/dist/aggregation/index.d.ts +119 -122
  2. package/dist/aggregation/index.d.ts.map +1 -1
  3. package/dist/aggregation/index.js +257 -265
  4. package/dist/analytics/posthog-metering-reader.d.ts +39 -0
  5. package/dist/analytics/posthog-metering-reader.d.ts.map +1 -0
  6. package/dist/analytics/posthog-metering-reader.js +267 -0
  7. package/dist/analytics/posthog-metering.d.ts +35 -0
  8. package/dist/analytics/posthog-metering.d.ts.map +1 -0
  9. package/dist/analytics/posthog-metering.js +46 -0
  10. package/dist/browser/aggregation/index.js +265 -0
  11. package/dist/browser/analytics/posthog-metering-reader.js +266 -0
  12. package/dist/browser/analytics/posthog-metering.js +45 -0
  13. package/dist/browser/contracts/index.js +617 -0
  14. package/dist/browser/docs/index.js +62 -0
  15. package/dist/browser/docs/metering.docblock.js +62 -0
  16. package/dist/browser/entities/index.js +350 -0
  17. package/dist/browser/events.js +228 -0
  18. package/dist/browser/index.js +1879 -0
  19. package/dist/browser/metering.capability.js +28 -0
  20. package/dist/browser/metering.feature.js +52 -0
  21. package/dist/contracts/index.d.ts +1076 -1082
  22. package/dist/contracts/index.d.ts.map +1 -1
  23. package/dist/contracts/index.js +616 -1030
  24. package/dist/docs/index.d.ts +2 -1
  25. package/dist/docs/index.d.ts.map +1 -0
  26. package/dist/docs/index.js +63 -1
  27. package/dist/docs/metering.docblock.d.ts +2 -1
  28. package/dist/docs/metering.docblock.d.ts.map +1 -0
  29. package/dist/docs/metering.docblock.js +18 -22
  30. package/dist/entities/index.d.ts +186 -191
  31. package/dist/entities/index.d.ts.map +1 -1
  32. package/dist/entities/index.js +340 -407
  33. package/dist/events.d.ts +411 -417
  34. package/dist/events.d.ts.map +1 -1
  35. package/dist/events.js +226 -414
  36. package/dist/index.d.ts +9 -6
  37. package/dist/index.d.ts.map +1 -0
  38. package/dist/index.js +1880 -8
  39. package/dist/metering.capability.d.ts +2 -7
  40. package/dist/metering.capability.d.ts.map +1 -1
  41. package/dist/metering.capability.js +29 -33
  42. package/dist/metering.feature.d.ts +1 -7
  43. package/dist/metering.feature.d.ts.map +1 -1
  44. package/dist/metering.feature.js +51 -132
  45. package/dist/node/aggregation/index.js +265 -0
  46. package/dist/node/analytics/posthog-metering-reader.js +266 -0
  47. package/dist/node/analytics/posthog-metering.js +45 -0
  48. package/dist/node/contracts/index.js +617 -0
  49. package/dist/node/docs/index.js +62 -0
  50. package/dist/node/docs/metering.docblock.js +62 -0
  51. package/dist/node/entities/index.js +350 -0
  52. package/dist/node/events.js +228 -0
  53. package/dist/node/index.js +1879 -0
  54. package/dist/node/metering.capability.js +28 -0
  55. package/dist/node/metering.feature.js +52 -0
  56. package/package.json +133 -30
  57. package/dist/aggregation/index.js.map +0 -1
  58. package/dist/contracts/index.js.map +0 -1
  59. package/dist/docs/metering.docblock.js.map +0 -1
  60. package/dist/entities/index.js.map +0 -1
  61. package/dist/events.js.map +0 -1
  62. package/dist/metering.capability.js.map +0 -1
  63. package/dist/metering.feature.js.map +0 -1
@@ -0,0 +1,1879 @@
1
+ // src/aggregation/index.ts
2
+ function getPeriodStart(date, periodType) {
3
+ const d = new Date(date);
4
+ switch (periodType) {
5
+ case "HOURLY":
6
+ d.setMinutes(0, 0, 0);
7
+ return d;
8
+ case "DAILY":
9
+ d.setHours(0, 0, 0, 0);
10
+ return d;
11
+ case "WEEKLY": {
12
+ d.setHours(0, 0, 0, 0);
13
+ const day = d.getDay();
14
+ d.setDate(d.getDate() - day);
15
+ return d;
16
+ }
17
+ case "MONTHLY":
18
+ d.setHours(0, 0, 0, 0);
19
+ d.setDate(1);
20
+ return d;
21
+ case "YEARLY":
22
+ d.setHours(0, 0, 0, 0);
23
+ d.setMonth(0, 1);
24
+ return d;
25
+ }
26
+ }
27
+ function getPeriodEnd(date, periodType) {
28
+ const start = getPeriodStart(date, periodType);
29
+ switch (periodType) {
30
+ case "HOURLY":
31
+ return new Date(start.getTime() + 60 * 60 * 1000);
32
+ case "DAILY":
33
+ return new Date(start.getTime() + 24 * 60 * 60 * 1000);
34
+ case "WEEKLY":
35
+ return new Date(start.getTime() + 7 * 24 * 60 * 60 * 1000);
36
+ case "MONTHLY": {
37
+ const end = new Date(start);
38
+ end.setMonth(end.getMonth() + 1);
39
+ return end;
40
+ }
41
+ case "YEARLY": {
42
+ const end = new Date(start);
43
+ end.setFullYear(end.getFullYear() + 1);
44
+ return end;
45
+ }
46
+ }
47
+ }
48
+ function formatPeriodKey(date, periodType) {
49
+ const start = getPeriodStart(date, periodType);
50
+ const year = start.getFullYear();
51
+ const month = String(start.getMonth() + 1).padStart(2, "0");
52
+ const day = String(start.getDate()).padStart(2, "0");
53
+ const hour = String(start.getHours()).padStart(2, "0");
54
+ switch (periodType) {
55
+ case "HOURLY":
56
+ return `${year}-${month}-${day}T${hour}`;
57
+ case "DAILY":
58
+ return `${year}-${month}-${day}`;
59
+ case "WEEKLY":
60
+ return `${year}-W${getWeekNumber(start)}`;
61
+ case "MONTHLY":
62
+ return `${year}-${month}`;
63
+ case "YEARLY":
64
+ return `${year}`;
65
+ }
66
+ }
67
+ function getWeekNumber(date) {
68
+ const d = new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()));
69
+ const dayNum = d.getUTCDay() || 7;
70
+ d.setUTCDate(d.getUTCDate() + 4 - dayNum);
71
+ const yearStart = new Date(Date.UTC(d.getUTCFullYear(), 0, 1));
72
+ const weekNum = Math.ceil(((d.getTime() - yearStart.getTime()) / 86400000 + 1) / 7);
73
+ return String(weekNum).padStart(2, "0");
74
+ }
75
+
76
+ class UsageAggregator {
77
+ storage;
78
+ batchSize;
79
+ constructor(options) {
80
+ this.storage = options.storage;
81
+ this.batchSize = options.batchSize || 1000;
82
+ }
83
+ async aggregate(params) {
84
+ const { periodType, periodStart, metricKey } = params;
85
+ const periodEnd = params.periodEnd || getPeriodEnd(periodStart, periodType);
86
+ const result = {
87
+ periodType,
88
+ periodStart,
89
+ periodEnd,
90
+ recordsProcessed: 0,
91
+ summariesCreated: 0,
92
+ summariesUpdated: 0,
93
+ errors: []
94
+ };
95
+ const records = await this.storage.getUnaggregatedRecords({
96
+ metricKey,
97
+ periodStart,
98
+ periodEnd,
99
+ limit: this.batchSize
100
+ });
101
+ if (records.length === 0) {
102
+ return result;
103
+ }
104
+ const groups = this.groupRecords(records, periodType);
105
+ for (const [groupKey, groupRecords] of groups.entries()) {
106
+ try {
107
+ await this.aggregateGroup(groupKey, groupRecords, periodType, result);
108
+ } catch (error) {
109
+ const [metricKey2, subjectType, subjectId] = groupKey.split("::");
110
+ result.errors.push({
111
+ metricKey: metricKey2 ?? "unknown",
112
+ subjectType: subjectType ?? "unknown",
113
+ subjectId: subjectId ?? "unknown",
114
+ error: error instanceof Error ? error.message : String(error)
115
+ });
116
+ }
117
+ }
118
+ const recordIds = records.map((r) => r.id);
119
+ await this.storage.markRecordsAggregated(recordIds, new Date);
120
+ result.recordsProcessed = records.length;
121
+ return result;
122
+ }
123
+ groupRecords(records, periodType) {
124
+ const groups = new Map;
125
+ for (const record of records) {
126
+ const periodKey = formatPeriodKey(record.timestamp, periodType);
127
+ const groupKey = `${record.metricKey}::${record.subjectType}::${record.subjectId}::${periodKey}`;
128
+ const existing = groups.get(groupKey) || [];
129
+ existing.push(record);
130
+ groups.set(groupKey, existing);
131
+ }
132
+ return groups;
133
+ }
134
+ async aggregateGroup(groupKey, records, periodType, result) {
135
+ const [metricKey, subjectType, subjectId] = groupKey.split("::");
136
+ if (!metricKey || !subjectType || !subjectId || records.length === 0) {
137
+ return;
138
+ }
139
+ const firstRecord = records[0];
140
+ if (!firstRecord)
141
+ return;
142
+ const periodStart = getPeriodStart(firstRecord.timestamp, periodType);
143
+ const periodEnd = getPeriodEnd(firstRecord.timestamp, periodType);
144
+ const metric = await this.storage.getMetric(metricKey);
145
+ const aggregationType = metric?.aggregationType || "SUM";
146
+ const quantities = records.map((r) => r.quantity);
147
+ const aggregated = this.calculateAggregation(quantities, aggregationType);
148
+ await this.storage.upsertSummary({
149
+ metricKey,
150
+ subjectType,
151
+ subjectId,
152
+ periodType,
153
+ periodStart,
154
+ periodEnd,
155
+ totalQuantity: aggregated.total,
156
+ recordCount: records.length,
157
+ minQuantity: aggregated.min,
158
+ maxQuantity: aggregated.max,
159
+ avgQuantity: aggregated.avg
160
+ });
161
+ result.summariesCreated++;
162
+ }
163
+ calculateAggregation(quantities, aggregationType) {
164
+ if (quantities.length === 0) {
165
+ return { total: 0, min: 0, max: 0, avg: 0 };
166
+ }
167
+ const min = Math.min(...quantities);
168
+ const max = Math.max(...quantities);
169
+ const sum = quantities.reduce((a, b) => a + b, 0);
170
+ const avg = sum / quantities.length;
171
+ const count = quantities.length;
172
+ let total;
173
+ switch (aggregationType) {
174
+ case "COUNT":
175
+ total = count;
176
+ break;
177
+ case "SUM":
178
+ total = sum;
179
+ break;
180
+ case "AVG":
181
+ total = avg;
182
+ break;
183
+ case "MAX":
184
+ total = max;
185
+ break;
186
+ case "MIN":
187
+ total = min;
188
+ break;
189
+ case "LAST":
190
+ total = quantities[quantities.length - 1] ?? 0;
191
+ break;
192
+ default:
193
+ total = sum;
194
+ }
195
+ return { total, min, max, avg };
196
+ }
197
+ }
198
+
199
+ class InMemoryUsageStorage {
200
+ records = [];
201
+ summaries = new Map;
202
+ metrics = new Map;
203
+ addRecord(record) {
204
+ this.records.push(record);
205
+ }
206
+ addMetric(metric) {
207
+ this.metrics.set(metric.key, metric);
208
+ }
209
+ async getUnaggregatedRecords(options) {
210
+ let records = this.records.filter((r) => {
211
+ const inPeriod = r.timestamp >= options.periodStart && r.timestamp < options.periodEnd;
212
+ const matchesMetric = !options.metricKey || r.metricKey === options.metricKey;
213
+ return inPeriod && matchesMetric;
214
+ });
215
+ if (options.limit) {
216
+ records = records.slice(0, options.limit);
217
+ }
218
+ return records;
219
+ }
220
+ async markRecordsAggregated(recordIds) {
221
+ this.records = this.records.filter((r) => !recordIds.includes(r.id));
222
+ }
223
+ async upsertSummary(summary) {
224
+ const key = `${summary.metricKey}::${summary.subjectType}::${summary.subjectId}::${summary.periodType}::${summary.periodStart.toISOString()}`;
225
+ const existing = this.summaries.get(key);
226
+ if (existing) {
227
+ existing.totalQuantity += summary.totalQuantity;
228
+ existing.recordCount += summary.recordCount;
229
+ if (summary.minQuantity !== undefined) {
230
+ existing.minQuantity = Math.min(existing.minQuantity ?? Infinity, summary.minQuantity);
231
+ }
232
+ if (summary.maxQuantity !== undefined) {
233
+ existing.maxQuantity = Math.max(existing.maxQuantity ?? -Infinity, summary.maxQuantity);
234
+ }
235
+ return existing;
236
+ }
237
+ const newSummary = {
238
+ id: `summary-${Date.now()}-${Math.random().toString(36).slice(2)}`,
239
+ ...summary
240
+ };
241
+ this.summaries.set(key, newSummary);
242
+ return newSummary;
243
+ }
244
+ async getMetric(key) {
245
+ return this.metrics.get(key) || null;
246
+ }
247
+ async listMetrics() {
248
+ return Array.from(this.metrics.values());
249
+ }
250
+ getSummaries() {
251
+ return Array.from(this.summaries.values());
252
+ }
253
+ clear() {
254
+ this.records = [];
255
+ this.summaries.clear();
256
+ this.metrics.clear();
257
+ }
258
+ }
259
+
260
+ // src/analytics/posthog-metering-reader.ts
261
+ class PosthogMeteringReader {
262
+ reader;
263
+ eventPrefix;
264
+ constructor(reader, options = {}) {
265
+ this.reader = reader;
266
+ this.eventPrefix = options.eventPrefix ?? "metering";
267
+ }
268
+ async getUsageByMetric(input) {
269
+ const result = await this.queryHogQL({
270
+ query: [
271
+ "select",
272
+ " properties.recordId as recordId,",
273
+ " properties.metricKey as metricKey,",
274
+ " properties.subjectType as subjectType,",
275
+ " distinct_id as subjectId,",
276
+ " properties.quantity as quantity,",
277
+ " properties.source as source,",
278
+ " timestamp as timestamp",
279
+ "from events",
280
+ `where ${buildUsageWhereClause(this.eventPrefix, input)}`,
281
+ "order by timestamp desc",
282
+ `limit ${input.limit ?? 200}`
283
+ ].join(`
284
+ `),
285
+ values: buildUsageValues(input)
286
+ });
287
+ return mapUsageRecords(result);
288
+ }
289
+ async getUsageSummary(input) {
290
+ const result = await this.queryHogQL({
291
+ query: [
292
+ "select",
293
+ " properties.summaryId as summaryId,",
294
+ " properties.metricKey as metricKey,",
295
+ " properties.subjectType as subjectType,",
296
+ " distinct_id as subjectId,",
297
+ " properties.periodType as periodType,",
298
+ " properties.periodStart as periodStart,",
299
+ " properties.periodEnd as periodEnd,",
300
+ " properties.totalQuantity as totalQuantity,",
301
+ " properties.recordCount as recordCount,",
302
+ " timestamp as aggregatedAt",
303
+ "from events",
304
+ `where ${buildSummaryWhereClause(this.eventPrefix, input)}`,
305
+ "order by timestamp desc",
306
+ `limit ${input.limit ?? 200}`
307
+ ].join(`
308
+ `),
309
+ values: buildSummaryValues(input)
310
+ });
311
+ return mapUsageSummaries(result);
312
+ }
313
+ async getUsageTrend(input) {
314
+ const result = await this.queryHogQL({
315
+ query: [
316
+ "select",
317
+ ` ${bucketExpression(input.bucket)} as bucketStart,`,
318
+ " sum(properties.quantity) as totalQuantity,",
319
+ " count() as recordCount",
320
+ "from events",
321
+ `where ${buildUsageWhereClause(this.eventPrefix, input)}`,
322
+ "group by bucketStart",
323
+ "order by bucketStart asc"
324
+ ].join(`
325
+ `),
326
+ values: buildUsageValues(input)
327
+ });
328
+ return mapUsageTrend(result);
329
+ }
330
+ async queryHogQL(input) {
331
+ if (!this.reader.queryHogQL) {
332
+ throw new Error("Analytics reader does not support HogQL queries.");
333
+ }
334
+ return this.reader.queryHogQL(input);
335
+ }
336
+ }
337
+ function buildUsageWhereClause(eventPrefix, input) {
338
+ const clauses = [
339
+ `event = '${eventPrefix}.usage_recorded'`,
340
+ `properties.metricKey = {metricKey}`
341
+ ];
342
+ if (input.subjectId) {
343
+ clauses.push("distinct_id = {subjectId}");
344
+ }
345
+ if (input.dateRange?.from) {
346
+ clauses.push("timestamp >= {dateFrom}");
347
+ }
348
+ if (input.dateRange?.to) {
349
+ clauses.push("timestamp < {dateTo}");
350
+ }
351
+ return clauses.join(" and ");
352
+ }
353
+ function buildSummaryWhereClause(eventPrefix, input) {
354
+ const clauses = [
355
+ `event = '${eventPrefix}.usage_aggregated'`,
356
+ `properties.metricKey = {metricKey}`
357
+ ];
358
+ if (input.subjectId) {
359
+ clauses.push("distinct_id = {subjectId}");
360
+ }
361
+ if (input.periodType) {
362
+ clauses.push("properties.periodType = {periodType}");
363
+ }
364
+ if (input.dateRange?.from) {
365
+ clauses.push("timestamp >= {dateFrom}");
366
+ }
367
+ if (input.dateRange?.to) {
368
+ clauses.push("timestamp < {dateTo}");
369
+ }
370
+ return clauses.join(" and ");
371
+ }
372
+ function buildUsageValues(input) {
373
+ return {
374
+ metricKey: input.metricKey,
375
+ subjectId: input.subjectId,
376
+ dateFrom: toIsoString(input.dateRange?.from),
377
+ dateTo: toIsoString(input.dateRange?.to)
378
+ };
379
+ }
380
+ function buildSummaryValues(input) {
381
+ return {
382
+ metricKey: input.metricKey,
383
+ subjectId: input.subjectId,
384
+ periodType: input.periodType,
385
+ dateFrom: toIsoString(input.dateRange?.from),
386
+ dateTo: toIsoString(input.dateRange?.to)
387
+ };
388
+ }
389
+ function bucketExpression(bucket) {
390
+ switch (bucket) {
391
+ case "hour":
392
+ return "toStartOfHour(timestamp)";
393
+ case "week":
394
+ return "toStartOfWeek(timestamp)";
395
+ case "month":
396
+ return "toStartOfMonth(timestamp)";
397
+ case "day":
398
+ default:
399
+ return "toStartOfDay(timestamp)";
400
+ }
401
+ }
402
+ function mapUsageRecords(result) {
403
+ const rows = mapRows(result);
404
+ return rows.flatMap((row) => {
405
+ const metricKey = asString(row.metricKey);
406
+ const subjectType = asString(row.subjectType);
407
+ const subjectId = asString(row.subjectId);
408
+ const timestamp = asDate(row.timestamp);
409
+ if (!metricKey || !subjectType || !subjectId || !timestamp) {
410
+ return [];
411
+ }
412
+ return [
413
+ {
414
+ recordId: asOptionalString(row.recordId),
415
+ metricKey,
416
+ subjectType,
417
+ subjectId,
418
+ quantity: asNumber(row.quantity),
419
+ source: asOptionalString(row.source),
420
+ timestamp
421
+ }
422
+ ];
423
+ });
424
+ }
425
+ function mapUsageSummaries(result) {
426
+ const rows = mapRows(result);
427
+ return rows.flatMap((row) => {
428
+ const metricKey = asString(row.metricKey);
429
+ const subjectType = asString(row.subjectType);
430
+ const subjectId = asString(row.subjectId);
431
+ const periodType = asString(row.periodType);
432
+ const periodStart = asDate(row.periodStart);
433
+ const periodEnd = asDate(row.periodEnd);
434
+ const aggregatedAt = asDate(row.aggregatedAt);
435
+ if (!metricKey || !subjectType || !subjectId || !periodType || !periodStart || !periodEnd || !aggregatedAt) {
436
+ return [];
437
+ }
438
+ return [
439
+ {
440
+ summaryId: asOptionalString(row.summaryId),
441
+ metricKey,
442
+ subjectType,
443
+ subjectId,
444
+ periodType,
445
+ periodStart,
446
+ periodEnd,
447
+ totalQuantity: asNumber(row.totalQuantity),
448
+ recordCount: asNumber(row.recordCount),
449
+ aggregatedAt
450
+ }
451
+ ];
452
+ });
453
+ }
454
+ function mapUsageTrend(result) {
455
+ const rows = mapRows(result);
456
+ return rows.flatMap((row) => {
457
+ const bucketStart = asString(row.bucketStart);
458
+ if (!bucketStart)
459
+ return [];
460
+ return [
461
+ {
462
+ bucketStart,
463
+ totalQuantity: asNumber(row.totalQuantity),
464
+ recordCount: asNumber(row.recordCount)
465
+ }
466
+ ];
467
+ });
468
+ }
469
+ function mapRows(result) {
470
+ if (!Array.isArray(result.results) || !Array.isArray(result.columns)) {
471
+ return [];
472
+ }
473
+ const columns = result.columns;
474
+ return result.results.flatMap((row) => {
475
+ if (!Array.isArray(row))
476
+ return [];
477
+ const record = {};
478
+ columns.forEach((column, index) => {
479
+ record[column] = row[index];
480
+ });
481
+ return [record];
482
+ });
483
+ }
484
+ function asString(value) {
485
+ if (typeof value === "string" && value.trim())
486
+ return value;
487
+ if (typeof value === "number")
488
+ return String(value);
489
+ return null;
490
+ }
491
+ function asOptionalString(value) {
492
+ if (typeof value === "string")
493
+ return value;
494
+ if (typeof value === "number")
495
+ return String(value);
496
+ return;
497
+ }
498
+ function asNumber(value) {
499
+ if (typeof value === "number" && Number.isFinite(value))
500
+ return value;
501
+ if (typeof value === "string" && value.trim()) {
502
+ const parsed = Number(value);
503
+ if (Number.isFinite(parsed))
504
+ return parsed;
505
+ }
506
+ return 0;
507
+ }
508
+ function asDate(value) {
509
+ if (value instanceof Date)
510
+ return value;
511
+ if (typeof value === "string" || typeof value === "number") {
512
+ const date = new Date(value);
513
+ if (!Number.isNaN(date.getTime()))
514
+ return date;
515
+ }
516
+ return null;
517
+ }
518
+ function toIsoString(value) {
519
+ if (!value)
520
+ return;
521
+ return typeof value === "string" ? value : value.toISOString();
522
+ }
523
+
524
+ // src/analytics/posthog-metering.ts
525
+ class PosthogMeteringReporter {
526
+ provider;
527
+ eventPrefix;
528
+ includeSource;
529
+ constructor(provider, options = {}) {
530
+ this.provider = provider;
531
+ this.eventPrefix = options.eventPrefix ?? "metering";
532
+ this.includeSource = options.includeSource ?? true;
533
+ }
534
+ async captureUsageRecorded(record) {
535
+ await this.provider.capture({
536
+ distinctId: record.subjectId,
537
+ event: `${this.eventPrefix}.usage_recorded`,
538
+ timestamp: record.timestamp,
539
+ properties: {
540
+ recordId: record.recordId ?? null,
541
+ metricKey: record.metricKey,
542
+ subjectType: record.subjectType,
543
+ quantity: record.quantity,
544
+ ...this.includeSource && record.source ? { source: record.source } : {}
545
+ }
546
+ });
547
+ }
548
+ async captureUsageAggregated(summary) {
549
+ await this.provider.capture({
550
+ distinctId: summary.subjectId,
551
+ event: `${this.eventPrefix}.usage_aggregated`,
552
+ timestamp: summary.aggregatedAt,
553
+ properties: {
554
+ summaryId: summary.summaryId ?? null,
555
+ metricKey: summary.metricKey,
556
+ subjectType: summary.subjectType,
557
+ periodType: summary.periodType,
558
+ periodStart: summary.periodStart.toISOString(),
559
+ periodEnd: summary.periodEnd.toISOString(),
560
+ totalQuantity: summary.totalQuantity,
561
+ recordCount: summary.recordCount
562
+ }
563
+ });
564
+ }
565
+ }
566
+
567
+ // src/contracts/index.ts
568
+ import { ScalarTypeEnum, defineSchemaModel } from "@contractspec/lib.schema";
569
+ import { defineCommand, defineQuery } from "@contractspec/lib.contracts";
570
+ var OWNERS = ["platform.metering"];
571
+ var MetricDefinitionModel = defineSchemaModel({
572
+ name: "MetricDefinition",
573
+ description: "Represents a metric definition",
574
+ fields: {
575
+ id: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
576
+ key: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
577
+ name: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
578
+ description: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
579
+ unit: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
580
+ aggregationType: {
581
+ type: ScalarTypeEnum.String_unsecure(),
582
+ isOptional: false
583
+ },
584
+ resetPeriod: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
585
+ category: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
586
+ isActive: { type: ScalarTypeEnum.Boolean(), isOptional: false },
587
+ orgId: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
588
+ createdAt: { type: ScalarTypeEnum.DateTime(), isOptional: false },
589
+ updatedAt: { type: ScalarTypeEnum.DateTime(), isOptional: false }
590
+ }
591
+ });
592
+ var UsageRecordModel = defineSchemaModel({
593
+ name: "UsageRecord",
594
+ description: "Represents a usage record",
595
+ fields: {
596
+ id: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
597
+ metricKey: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
598
+ subjectType: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
599
+ subjectId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
600
+ quantity: { type: ScalarTypeEnum.Float_unsecure(), isOptional: false },
601
+ source: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
602
+ resourceId: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
603
+ resourceType: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
604
+ metadata: { type: ScalarTypeEnum.JSON(), isOptional: true },
605
+ timestamp: { type: ScalarTypeEnum.DateTime(), isOptional: false },
606
+ createdAt: { type: ScalarTypeEnum.DateTime(), isOptional: false }
607
+ }
608
+ });
609
+ var UsageSummaryModel = defineSchemaModel({
610
+ name: "UsageSummary",
611
+ description: "Represents aggregated usage",
612
+ fields: {
613
+ id: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
614
+ metricKey: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
615
+ subjectType: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
616
+ subjectId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
617
+ periodType: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
618
+ periodStart: { type: ScalarTypeEnum.DateTime(), isOptional: false },
619
+ periodEnd: { type: ScalarTypeEnum.DateTime(), isOptional: false },
620
+ totalQuantity: { type: ScalarTypeEnum.Float_unsecure(), isOptional: false },
621
+ recordCount: { type: ScalarTypeEnum.Int_unsecure(), isOptional: false },
622
+ minQuantity: { type: ScalarTypeEnum.Float_unsecure(), isOptional: true },
623
+ maxQuantity: { type: ScalarTypeEnum.Float_unsecure(), isOptional: true },
624
+ avgQuantity: { type: ScalarTypeEnum.Float_unsecure(), isOptional: true }
625
+ }
626
+ });
627
+ var UsageThresholdModel = defineSchemaModel({
628
+ name: "UsageThreshold",
629
+ description: "Represents a usage threshold",
630
+ fields: {
631
+ id: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
632
+ metricKey: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
633
+ subjectType: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
634
+ subjectId: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
635
+ name: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
636
+ threshold: { type: ScalarTypeEnum.Float_unsecure(), isOptional: false },
637
+ warnThreshold: { type: ScalarTypeEnum.Float_unsecure(), isOptional: true },
638
+ periodType: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
639
+ action: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
640
+ currentValue: { type: ScalarTypeEnum.Float_unsecure(), isOptional: false },
641
+ isActive: { type: ScalarTypeEnum.Boolean(), isOptional: false },
642
+ createdAt: { type: ScalarTypeEnum.DateTime(), isOptional: false }
643
+ }
644
+ });
645
+ var DefineMetricInput = defineSchemaModel({
646
+ name: "DefineMetricInput",
647
+ description: "Input for defining a metric",
648
+ fields: {
649
+ key: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
650
+ name: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
651
+ description: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
652
+ unit: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
653
+ aggregationType: {
654
+ type: ScalarTypeEnum.String_unsecure(),
655
+ isOptional: true
656
+ },
657
+ resetPeriod: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
658
+ category: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
659
+ orgId: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
660
+ metadata: { type: ScalarTypeEnum.JSON(), isOptional: true }
661
+ }
662
+ });
663
+ var UpdateMetricInput = defineSchemaModel({
664
+ name: "UpdateMetricInput",
665
+ description: "Input for updating a metric",
666
+ fields: {
667
+ metricId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
668
+ name: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
669
+ description: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
670
+ category: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
671
+ isActive: { type: ScalarTypeEnum.Boolean(), isOptional: true },
672
+ metadata: { type: ScalarTypeEnum.JSON(), isOptional: true }
673
+ }
674
+ });
675
+ var DeleteMetricInput = defineSchemaModel({
676
+ name: "DeleteMetricInput",
677
+ description: "Input for deleting a metric",
678
+ fields: {
679
+ metricId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false }
680
+ }
681
+ });
682
+ var GetMetricInput = defineSchemaModel({
683
+ name: "GetMetricInput",
684
+ description: "Input for getting a metric",
685
+ fields: {
686
+ key: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
687
+ orgId: { type: ScalarTypeEnum.String_unsecure(), isOptional: true }
688
+ }
689
+ });
690
+ var ListMetricsInput = defineSchemaModel({
691
+ name: "ListMetricsInput",
692
+ description: "Input for listing metrics",
693
+ fields: {
694
+ orgId: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
695
+ category: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
696
+ isActive: { type: ScalarTypeEnum.Boolean(), isOptional: true },
697
+ limit: { type: ScalarTypeEnum.Int_unsecure(), isOptional: true },
698
+ offset: { type: ScalarTypeEnum.Int_unsecure(), isOptional: true }
699
+ }
700
+ });
701
+ var ListMetricsOutput = defineSchemaModel({
702
+ name: "ListMetricsOutput",
703
+ description: "Output for listing metrics",
704
+ fields: {
705
+ metrics: { type: MetricDefinitionModel, isArray: true, isOptional: false },
706
+ total: { type: ScalarTypeEnum.Int_unsecure(), isOptional: false }
707
+ }
708
+ });
709
+ var RecordUsageInput = defineSchemaModel({
710
+ name: "RecordUsageInput",
711
+ description: "Input for recording usage",
712
+ fields: {
713
+ metricKey: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
714
+ subjectType: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
715
+ subjectId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
716
+ quantity: { type: ScalarTypeEnum.Float_unsecure(), isOptional: false },
717
+ timestamp: { type: ScalarTypeEnum.DateTime(), isOptional: true },
718
+ source: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
719
+ resourceId: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
720
+ resourceType: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
721
+ metadata: { type: ScalarTypeEnum.JSON(), isOptional: true },
722
+ idempotencyKey: {
723
+ type: ScalarTypeEnum.String_unsecure(),
724
+ isOptional: true
725
+ }
726
+ }
727
+ });
728
+ var RecordBatchUsageInput = defineSchemaModel({
729
+ name: "RecordBatchUsageInput",
730
+ description: "Input for recording batch usage",
731
+ fields: {
732
+ records: { type: RecordUsageInput, isArray: true, isOptional: false }
733
+ }
734
+ });
735
+ var RecordBatchUsageOutput = defineSchemaModel({
736
+ name: "RecordBatchUsageOutput",
737
+ description: "Output for recording batch usage",
738
+ fields: {
739
+ recordedCount: { type: ScalarTypeEnum.Int_unsecure(), isOptional: false },
740
+ skippedCount: { type: ScalarTypeEnum.Int_unsecure(), isOptional: false },
741
+ recordIds: { type: ScalarTypeEnum.JSON(), isOptional: false }
742
+ }
743
+ });
744
+ var GetUsageInput = defineSchemaModel({
745
+ name: "GetUsageInput",
746
+ description: "Input for getting usage",
747
+ fields: {
748
+ metricKey: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
749
+ subjectType: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
750
+ subjectId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
751
+ startDate: { type: ScalarTypeEnum.DateTime(), isOptional: false },
752
+ endDate: { type: ScalarTypeEnum.DateTime(), isOptional: false },
753
+ limit: { type: ScalarTypeEnum.Int_unsecure(), isOptional: true },
754
+ offset: { type: ScalarTypeEnum.Int_unsecure(), isOptional: true }
755
+ }
756
+ });
757
+ var GetUsageOutput = defineSchemaModel({
758
+ name: "GetUsageOutput",
759
+ description: "Output for getting usage",
760
+ fields: {
761
+ records: { type: UsageRecordModel, isArray: true, isOptional: false },
762
+ total: { type: ScalarTypeEnum.Int_unsecure(), isOptional: false },
763
+ totalQuantity: { type: ScalarTypeEnum.Float_unsecure(), isOptional: false }
764
+ }
765
+ });
766
+ var GetUsageSummaryInput = defineSchemaModel({
767
+ name: "GetUsageSummaryInput",
768
+ description: "Input for getting usage summary",
769
+ fields: {
770
+ metricKey: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
771
+ subjectType: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
772
+ subjectId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
773
+ periodType: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
774
+ startDate: { type: ScalarTypeEnum.DateTime(), isOptional: false },
775
+ endDate: { type: ScalarTypeEnum.DateTime(), isOptional: true }
776
+ }
777
+ });
778
+ var GetUsageSummaryOutput = defineSchemaModel({
779
+ name: "GetUsageSummaryOutput",
780
+ description: "Output for getting usage summary",
781
+ fields: {
782
+ summaries: { type: UsageSummaryModel, isArray: true, isOptional: false },
783
+ total: { type: ScalarTypeEnum.Int_unsecure(), isOptional: false }
784
+ }
785
+ });
786
+ var CreateThresholdInput = defineSchemaModel({
787
+ name: "CreateThresholdInput",
788
+ description: "Input for creating a threshold",
789
+ fields: {
790
+ metricKey: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
791
+ subjectType: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
792
+ subjectId: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
793
+ name: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
794
+ threshold: { type: ScalarTypeEnum.Float_unsecure(), isOptional: false },
795
+ warnThreshold: { type: ScalarTypeEnum.Float_unsecure(), isOptional: true },
796
+ periodType: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
797
+ action: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
798
+ notifyEmails: { type: ScalarTypeEnum.JSON(), isOptional: true },
799
+ notifyWebhook: { type: ScalarTypeEnum.String_unsecure(), isOptional: true }
800
+ }
801
+ });
802
+ var UpdateThresholdInput = defineSchemaModel({
803
+ name: "UpdateThresholdInput",
804
+ description: "Input for updating a threshold",
805
+ fields: {
806
+ thresholdId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
807
+ name: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
808
+ threshold: { type: ScalarTypeEnum.Float_unsecure(), isOptional: true },
809
+ warnThreshold: { type: ScalarTypeEnum.Float_unsecure(), isOptional: true },
810
+ action: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
811
+ notifyEmails: { type: ScalarTypeEnum.JSON(), isOptional: true },
812
+ notifyWebhook: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
813
+ isActive: { type: ScalarTypeEnum.Boolean(), isOptional: true }
814
+ }
815
+ });
816
+ var DeleteThresholdInput = defineSchemaModel({
817
+ name: "DeleteThresholdInput",
818
+ description: "Input for deleting a threshold",
819
+ fields: {
820
+ thresholdId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false }
821
+ }
822
+ });
823
+ var ListThresholdsInput = defineSchemaModel({
824
+ name: "ListThresholdsInput",
825
+ description: "Input for listing thresholds",
826
+ fields: {
827
+ metricKey: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
828
+ subjectType: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
829
+ subjectId: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
830
+ isActive: { type: ScalarTypeEnum.Boolean(), isOptional: true }
831
+ }
832
+ });
833
+ var ListThresholdsOutput = defineSchemaModel({
834
+ name: "ListThresholdsOutput",
835
+ description: "Output for listing thresholds",
836
+ fields: {
837
+ thresholds: { type: UsageThresholdModel, isArray: true, isOptional: false },
838
+ total: { type: ScalarTypeEnum.Int_unsecure(), isOptional: false }
839
+ }
840
+ });
841
+ var SuccessOutput = defineSchemaModel({
842
+ name: "SuccessOutput",
843
+ description: "Generic success output",
844
+ fields: {
845
+ success: { type: ScalarTypeEnum.Boolean(), isOptional: false }
846
+ }
847
+ });
848
+ var DefineMetricContract = defineCommand({
849
+ meta: {
850
+ key: "metric.define",
851
+ version: "1.0.0",
852
+ stability: "stable",
853
+ owners: [...OWNERS],
854
+ tags: ["metering", "metric", "define"],
855
+ description: "Define a new usage metric.",
856
+ goal: "Create a new metric for tracking usage.",
857
+ context: "Called when setting up metering."
858
+ },
859
+ io: {
860
+ input: DefineMetricInput,
861
+ output: MetricDefinitionModel,
862
+ errors: {
863
+ METRIC_KEY_EXISTS: {
864
+ description: "Metric key already exists",
865
+ http: 409,
866
+ gqlCode: "METRIC_KEY_EXISTS",
867
+ when: "A metric with this key already exists"
868
+ }
869
+ }
870
+ },
871
+ policy: {
872
+ auth: "admin"
873
+ }
874
+ });
875
+ var UpdateMetricContract = defineCommand({
876
+ meta: {
877
+ key: "metric.update",
878
+ version: "1.0.0",
879
+ stability: "stable",
880
+ owners: [...OWNERS],
881
+ tags: ["metering", "metric", "update"],
882
+ description: "Update a metric definition.",
883
+ goal: "Modify metric configuration.",
884
+ context: "Called when updating metric settings."
885
+ },
886
+ io: {
887
+ input: UpdateMetricInput,
888
+ output: MetricDefinitionModel,
889
+ errors: {
890
+ METRIC_NOT_FOUND: {
891
+ description: "Metric does not exist",
892
+ http: 404,
893
+ gqlCode: "METRIC_NOT_FOUND",
894
+ when: "Metric ID is invalid"
895
+ }
896
+ }
897
+ },
898
+ policy: {
899
+ auth: "admin"
900
+ }
901
+ });
902
+ var DeleteMetricContract = defineCommand({
903
+ meta: {
904
+ key: "metric.delete",
905
+ version: "1.0.0",
906
+ stability: "stable",
907
+ owners: [...OWNERS],
908
+ tags: ["metering", "metric", "delete"],
909
+ description: "Delete a metric definition.",
910
+ goal: "Remove a metric and its data.",
911
+ context: "Called when removing a metric."
912
+ },
913
+ io: {
914
+ input: DeleteMetricInput,
915
+ output: SuccessOutput,
916
+ errors: {
917
+ METRIC_NOT_FOUND: {
918
+ description: "Metric does not exist",
919
+ http: 404,
920
+ gqlCode: "METRIC_NOT_FOUND",
921
+ when: "Metric ID is invalid"
922
+ }
923
+ }
924
+ },
925
+ policy: {
926
+ auth: "admin"
927
+ }
928
+ });
929
+ var GetMetricContract = defineQuery({
930
+ meta: {
931
+ key: "metric.get",
932
+ version: "1.0.0",
933
+ stability: "stable",
934
+ owners: [...OWNERS],
935
+ tags: ["metering", "metric", "get"],
936
+ description: "Get a metric by key.",
937
+ goal: "Retrieve metric definition.",
938
+ context: "Called to inspect metric details."
939
+ },
940
+ io: {
941
+ input: GetMetricInput,
942
+ output: MetricDefinitionModel,
943
+ errors: {
944
+ METRIC_NOT_FOUND: {
945
+ description: "Metric does not exist",
946
+ http: 404,
947
+ gqlCode: "METRIC_NOT_FOUND",
948
+ when: "Metric key is invalid"
949
+ }
950
+ }
951
+ },
952
+ policy: {
953
+ auth: "user"
954
+ }
955
+ });
956
+ var ListMetricsContract = defineQuery({
957
+ meta: {
958
+ key: "metric.list",
959
+ version: "1.0.0",
960
+ stability: "stable",
961
+ owners: [...OWNERS],
962
+ tags: ["metering", "metric", "list"],
963
+ description: "List all metrics.",
964
+ goal: "View configured metrics.",
965
+ context: "Called to browse metrics."
966
+ },
967
+ io: {
968
+ input: ListMetricsInput,
969
+ output: ListMetricsOutput
970
+ },
971
+ policy: {
972
+ auth: "user"
973
+ }
974
+ });
975
+ var RecordUsageContract = defineCommand({
976
+ meta: {
977
+ key: "usage.record",
978
+ version: "1.0.0",
979
+ stability: "stable",
980
+ owners: [...OWNERS],
981
+ tags: ["metering", "usage", "record"],
982
+ description: "Record a usage event.",
983
+ goal: "Track usage for billing and monitoring.",
984
+ context: "Called when usage occurs."
985
+ },
986
+ io: {
987
+ input: RecordUsageInput,
988
+ output: UsageRecordModel,
989
+ errors: {
990
+ METRIC_NOT_FOUND: {
991
+ description: "Metric does not exist",
992
+ http: 404,
993
+ gqlCode: "METRIC_NOT_FOUND",
994
+ when: "Metric key is invalid"
995
+ },
996
+ DUPLICATE_RECORD: {
997
+ description: "Record already exists",
998
+ http: 409,
999
+ gqlCode: "DUPLICATE_RECORD",
1000
+ when: "Idempotency key already used"
1001
+ }
1002
+ }
1003
+ },
1004
+ policy: {
1005
+ auth: "admin"
1006
+ }
1007
+ });
1008
+ var RecordBatchUsageContract = defineCommand({
1009
+ meta: {
1010
+ key: "usage.recordBatch",
1011
+ version: "1.0.0",
1012
+ stability: "stable",
1013
+ owners: [...OWNERS],
1014
+ tags: ["metering", "usage", "batch"],
1015
+ description: "Record multiple usage events.",
1016
+ goal: "Efficiently track bulk usage.",
1017
+ context: "Called for batch processing."
1018
+ },
1019
+ io: {
1020
+ input: RecordBatchUsageInput,
1021
+ output: RecordBatchUsageOutput
1022
+ },
1023
+ policy: {
1024
+ auth: "admin"
1025
+ }
1026
+ });
1027
+ var GetUsageContract = defineQuery({
1028
+ meta: {
1029
+ key: "usage.get",
1030
+ version: "1.0.0",
1031
+ stability: "stable",
1032
+ owners: [...OWNERS],
1033
+ tags: ["metering", "usage", "get"],
1034
+ description: "Get usage records for a subject.",
1035
+ goal: "View detailed usage history.",
1036
+ context: "Called to analyze usage."
1037
+ },
1038
+ io: {
1039
+ input: GetUsageInput,
1040
+ output: GetUsageOutput
1041
+ },
1042
+ policy: {
1043
+ auth: "user"
1044
+ }
1045
+ });
1046
+ var GetUsageSummaryContract = defineQuery({
1047
+ meta: {
1048
+ key: "usage.getSummary",
1049
+ version: "1.0.0",
1050
+ stability: "stable",
1051
+ owners: [...OWNERS],
1052
+ tags: ["metering", "usage", "summary"],
1053
+ description: "Get aggregated usage summary.",
1054
+ goal: "View usage totals for billing.",
1055
+ context: "Called for billing and reporting."
1056
+ },
1057
+ io: {
1058
+ input: GetUsageSummaryInput,
1059
+ output: GetUsageSummaryOutput
1060
+ },
1061
+ policy: {
1062
+ auth: "user"
1063
+ }
1064
+ });
1065
+ var CreateThresholdContract = defineCommand({
1066
+ meta: {
1067
+ key: "threshold.create",
1068
+ version: "1.0.0",
1069
+ stability: "stable",
1070
+ owners: [...OWNERS],
1071
+ tags: ["metering", "threshold", "create"],
1072
+ description: "Create a usage threshold.",
1073
+ goal: "Set up usage limits and alerts.",
1074
+ context: "Called when configuring limits."
1075
+ },
1076
+ io: {
1077
+ input: CreateThresholdInput,
1078
+ output: UsageThresholdModel,
1079
+ errors: {
1080
+ METRIC_NOT_FOUND: {
1081
+ description: "Metric does not exist",
1082
+ http: 404,
1083
+ gqlCode: "METRIC_NOT_FOUND",
1084
+ when: "Metric key is invalid"
1085
+ }
1086
+ }
1087
+ },
1088
+ policy: {
1089
+ auth: "admin"
1090
+ }
1091
+ });
1092
+ var UpdateThresholdContract = defineCommand({
1093
+ meta: {
1094
+ key: "threshold.update",
1095
+ version: "1.0.0",
1096
+ stability: "stable",
1097
+ owners: [...OWNERS],
1098
+ tags: ["metering", "threshold", "update"],
1099
+ description: "Update a threshold.",
1100
+ goal: "Modify threshold configuration.",
1101
+ context: "Called when adjusting limits."
1102
+ },
1103
+ io: {
1104
+ input: UpdateThresholdInput,
1105
+ output: UsageThresholdModel,
1106
+ errors: {
1107
+ THRESHOLD_NOT_FOUND: {
1108
+ description: "Threshold does not exist",
1109
+ http: 404,
1110
+ gqlCode: "THRESHOLD_NOT_FOUND",
1111
+ when: "Threshold ID is invalid"
1112
+ }
1113
+ }
1114
+ },
1115
+ policy: {
1116
+ auth: "admin"
1117
+ }
1118
+ });
1119
+ var DeleteThresholdContract = defineCommand({
1120
+ meta: {
1121
+ key: "threshold.delete",
1122
+ version: "1.0.0",
1123
+ stability: "stable",
1124
+ owners: [...OWNERS],
1125
+ tags: ["metering", "threshold", "delete"],
1126
+ description: "Delete a threshold.",
1127
+ goal: "Remove a usage threshold.",
1128
+ context: "Called when removing limits."
1129
+ },
1130
+ io: {
1131
+ input: DeleteThresholdInput,
1132
+ output: SuccessOutput,
1133
+ errors: {
1134
+ THRESHOLD_NOT_FOUND: {
1135
+ description: "Threshold does not exist",
1136
+ http: 404,
1137
+ gqlCode: "THRESHOLD_NOT_FOUND",
1138
+ when: "Threshold ID is invalid"
1139
+ }
1140
+ }
1141
+ },
1142
+ policy: {
1143
+ auth: "admin"
1144
+ }
1145
+ });
1146
+ var ListThresholdsContract = defineQuery({
1147
+ meta: {
1148
+ key: "threshold.list",
1149
+ version: "1.0.0",
1150
+ stability: "stable",
1151
+ owners: [...OWNERS],
1152
+ tags: ["metering", "threshold", "list"],
1153
+ description: "List usage thresholds.",
1154
+ goal: "View configured limits.",
1155
+ context: "Called to browse thresholds."
1156
+ },
1157
+ io: {
1158
+ input: ListThresholdsInput,
1159
+ output: ListThresholdsOutput
1160
+ },
1161
+ policy: {
1162
+ auth: "user"
1163
+ }
1164
+ });
1165
+
1166
+ // src/docs/metering.docblock.ts
1167
+ import { registerDocBlocks } from "@contractspec/lib.contracts/docs";
1168
+ var meteringDocBlocks = [
1169
+ {
1170
+ id: "docs.metering.usage",
1171
+ title: "Usage Metering & Billing Core",
1172
+ summary: "Reusable usage/metering layer with metric definitions, usage ingestion, aggregation, thresholds, and alerts for billing or quotas.",
1173
+ kind: "reference",
1174
+ visibility: "public",
1175
+ route: "/docs/metering/usage",
1176
+ tags: ["metering", "usage", "billing", "quotas"],
1177
+ body: `## Capabilities
1178
+
1179
+ - **Entities**: MetricDefinition, UsageRecord, UsageSummary, UsageThreshold, UsageAlert.
1180
+ - **Contracts**: define/list metrics; record usage (batch + idempotent); retrieve usage by subject; manage thresholds and alerts.
1181
+ - **Aggregation**: hourly/daily/weekly/monthly rollups with SUM/COUNT/AVG/MIN/MAX/LAST strategies.
1182
+ - **Events**: usage.recorded, usage.aggregated, threshold.exceeded (see events export).
1183
+
1184
+ ## Usage
1185
+
1186
+ 1) Compose schema
1187
+ - Add \`meteringSchemaContribution\` to your schema composition.
1188
+
1189
+ 2) Register contracts/events
1190
+ - Import from \`@contractspec/lib.metering\` into your spec registry.
1191
+
1192
+ 3) Ingest usage
1193
+ - Use \`recordUsage\` contract (see contracts export) from application services whenever a billable/important action happens (e.g., agent run, API call).
1194
+
1195
+ 4) Aggregate + thresholds
1196
+ - Run scheduled jobs using \`aggregateUsage\` helper to roll up summaries.
1197
+ - Configure \`UsageThreshold\` to emit alerts or blocking actions; pipe alerts into Notifications/Audit.
1198
+ - Use \`PosthogMeteringReader\` to backfill or validate usage from PostHog events.
1199
+
1200
+ ## Example
1201
+
1202
+ ${"```"}ts
1203
+ import { meteringSchemaContribution } from '@contractspec/lib.metering';
1204
+ import { aggregateUsage } from '@contractspec/lib.metering/aggregation';
1205
+
1206
+ // schema composition
1207
+ const schema = {
1208
+ modules: [meteringSchemaContribution],
1209
+ };
1210
+
1211
+ // aggregation job
1212
+ await aggregateUsage({
1213
+ metrics: metricRepository,
1214
+ usage: usageRepository,
1215
+ period: 'DAILY',
1216
+ });
1217
+ ${"```"},
1218
+
1219
+ ## Guardrails
1220
+
1221
+ - Keep metric keys stable; store quantities as decimals for currency/units.
1222
+ - Use idempotency keys for external ingestion; avoid PII in metric metadata.
1223
+ - Scope by org/user for multi-tenant isolation; emit audit + analytics events on changes.
1224
+ `
1225
+ }
1226
+ ];
1227
+ registerDocBlocks(meteringDocBlocks);
1228
+ // src/entities/index.ts
1229
+ import {
1230
+ defineEntity,
1231
+ defineEntityEnum,
1232
+ field,
1233
+ index
1234
+ } from "@contractspec/lib.schema";
1235
+ var AggregationTypeEnum = defineEntityEnum({
1236
+ name: "AggregationType",
1237
+ values: ["COUNT", "SUM", "AVG", "MAX", "MIN", "LAST"],
1238
+ schema: "lssm_metering",
1239
+ description: "How to aggregate metric values."
1240
+ });
1241
+ var ResetPeriodEnum = defineEntityEnum({
1242
+ name: "ResetPeriod",
1243
+ values: ["NEVER", "HOURLY", "DAILY", "WEEKLY", "MONTHLY", "YEARLY"],
1244
+ schema: "lssm_metering",
1245
+ description: "When to reset metric counters."
1246
+ });
1247
+ var PeriodTypeEnum = defineEntityEnum({
1248
+ name: "PeriodType",
1249
+ values: ["HOURLY", "DAILY", "WEEKLY", "MONTHLY", "YEARLY"],
1250
+ schema: "lssm_metering",
1251
+ description: "Time period for aggregation."
1252
+ });
1253
+ var ThresholdActionEnum = defineEntityEnum({
1254
+ name: "ThresholdAction",
1255
+ values: ["NONE", "ALERT", "WARN", "BLOCK", "DOWNGRADE"],
1256
+ schema: "lssm_metering",
1257
+ description: "Action to take when threshold is exceeded."
1258
+ });
1259
+ var MetricDefinitionEntity = defineEntity({
1260
+ name: "MetricDefinition",
1261
+ description: "Definition of a usage metric.",
1262
+ schema: "lssm_metering",
1263
+ map: "metric_definition",
1264
+ fields: {
1265
+ id: field.id({ description: "Unique identifier" }),
1266
+ key: field.string({
1267
+ isUnique: true,
1268
+ description: "Metric key (e.g., api_calls, storage_gb)"
1269
+ }),
1270
+ name: field.string({ description: "Human-readable name" }),
1271
+ description: field.string({
1272
+ isOptional: true,
1273
+ description: "Metric description"
1274
+ }),
1275
+ unit: field.string({
1276
+ description: "Unit of measurement (calls, bytes, etc.)"
1277
+ }),
1278
+ aggregationType: field.enum("AggregationType", {
1279
+ default: "SUM",
1280
+ description: "How to aggregate values"
1281
+ }),
1282
+ resetPeriod: field.enum("ResetPeriod", {
1283
+ default: "MONTHLY",
1284
+ description: "When to reset counters"
1285
+ }),
1286
+ precision: field.int({ default: 2, description: "Decimal precision" }),
1287
+ orgId: field.string({
1288
+ isOptional: true,
1289
+ description: "Organization scope (null = global metric)"
1290
+ }),
1291
+ category: field.string({
1292
+ isOptional: true,
1293
+ description: "Category for grouping"
1294
+ }),
1295
+ displayOrder: field.int({ default: 0, description: "Order for display" }),
1296
+ metadata: field.json({
1297
+ isOptional: true,
1298
+ description: "Additional metadata"
1299
+ }),
1300
+ isActive: field.boolean({
1301
+ default: true,
1302
+ description: "Whether metric is active"
1303
+ }),
1304
+ createdAt: field.createdAt(),
1305
+ updatedAt: field.updatedAt(),
1306
+ usageRecords: field.hasMany("UsageRecord"),
1307
+ usageSummaries: field.hasMany("UsageSummary"),
1308
+ thresholds: field.hasMany("UsageThreshold")
1309
+ },
1310
+ indexes: [
1311
+ index.on(["orgId", "key"]),
1312
+ index.on(["category"]),
1313
+ index.on(["isActive"])
1314
+ ],
1315
+ enums: [AggregationTypeEnum, ResetPeriodEnum]
1316
+ });
1317
+ var UsageRecordEntity = defineEntity({
1318
+ name: "UsageRecord",
1319
+ description: "A single usage event.",
1320
+ schema: "lssm_metering",
1321
+ map: "usage_record",
1322
+ fields: {
1323
+ id: field.id({ description: "Unique identifier" }),
1324
+ metricKey: field.string({ description: "Metric being recorded" }),
1325
+ metricId: field.string({
1326
+ isOptional: true,
1327
+ description: "Metric ID (for FK)"
1328
+ }),
1329
+ subjectType: field.string({
1330
+ description: "Subject type (org, user, project)"
1331
+ }),
1332
+ subjectId: field.string({ description: "Subject identifier" }),
1333
+ quantity: field.decimal({ description: "Usage quantity" }),
1334
+ source: field.string({
1335
+ isOptional: true,
1336
+ description: "Source of usage (endpoint, feature, etc.)"
1337
+ }),
1338
+ resourceId: field.string({
1339
+ isOptional: true,
1340
+ description: "Related resource ID"
1341
+ }),
1342
+ resourceType: field.string({
1343
+ isOptional: true,
1344
+ description: "Related resource type"
1345
+ }),
1346
+ metadata: field.json({
1347
+ isOptional: true,
1348
+ description: "Additional context"
1349
+ }),
1350
+ idempotencyKey: field.string({
1351
+ isOptional: true,
1352
+ description: "Idempotency key for deduplication"
1353
+ }),
1354
+ timestamp: field.dateTime({ description: "When usage occurred" }),
1355
+ createdAt: field.createdAt(),
1356
+ aggregated: field.boolean({
1357
+ default: false,
1358
+ description: "Whether included in summary"
1359
+ }),
1360
+ aggregatedAt: field.dateTime({
1361
+ isOptional: true,
1362
+ description: "When aggregated"
1363
+ })
1364
+ },
1365
+ indexes: [
1366
+ index.on(["metricKey", "subjectType", "subjectId", "timestamp"]),
1367
+ index.on(["subjectType", "subjectId", "timestamp"]),
1368
+ index.on(["timestamp"]),
1369
+ index.on(["aggregated", "timestamp"]),
1370
+ index.unique(["idempotencyKey"], { name: "usage_record_idempotency" })
1371
+ ]
1372
+ });
1373
+ var UsageSummaryEntity = defineEntity({
1374
+ name: "UsageSummary",
1375
+ description: "Pre-aggregated usage summary.",
1376
+ schema: "lssm_metering",
1377
+ map: "usage_summary",
1378
+ fields: {
1379
+ id: field.id({ description: "Unique identifier" }),
1380
+ metricKey: field.string({ description: "Metric key" }),
1381
+ metricId: field.string({
1382
+ isOptional: true,
1383
+ description: "Metric ID (for FK)"
1384
+ }),
1385
+ subjectType: field.string({ description: "Subject type" }),
1386
+ subjectId: field.string({ description: "Subject identifier" }),
1387
+ periodType: field.enum("PeriodType", { description: "Period type" }),
1388
+ periodStart: field.dateTime({ description: "Period start time" }),
1389
+ periodEnd: field.dateTime({ description: "Period end time" }),
1390
+ totalQuantity: field.decimal({ description: "Total/aggregated quantity" }),
1391
+ recordCount: field.int({
1392
+ default: 0,
1393
+ description: "Number of records aggregated"
1394
+ }),
1395
+ minQuantity: field.decimal({
1396
+ isOptional: true,
1397
+ description: "Minimum value"
1398
+ }),
1399
+ maxQuantity: field.decimal({
1400
+ isOptional: true,
1401
+ description: "Maximum value"
1402
+ }),
1403
+ avgQuantity: field.decimal({
1404
+ isOptional: true,
1405
+ description: "Average value"
1406
+ }),
1407
+ metadata: field.json({
1408
+ isOptional: true,
1409
+ description: "Additional metadata"
1410
+ }),
1411
+ createdAt: field.createdAt(),
1412
+ updatedAt: field.updatedAt()
1413
+ },
1414
+ indexes: [
1415
+ index.unique(["metricKey", "subjectType", "subjectId", "periodType", "periodStart"], { name: "usage_summary_unique" }),
1416
+ index.on(["subjectType", "subjectId", "periodType", "periodStart"]),
1417
+ index.on(["metricKey", "periodType", "periodStart"])
1418
+ ],
1419
+ enums: [PeriodTypeEnum]
1420
+ });
1421
+ var UsageThresholdEntity = defineEntity({
1422
+ name: "UsageThreshold",
1423
+ description: "Usage threshold configuration.",
1424
+ schema: "lssm_metering",
1425
+ map: "usage_threshold",
1426
+ fields: {
1427
+ id: field.id({ description: "Unique identifier" }),
1428
+ metricKey: field.string({ description: "Metric to monitor" }),
1429
+ metricId: field.string({
1430
+ isOptional: true,
1431
+ description: "Metric ID (for FK)"
1432
+ }),
1433
+ subjectType: field.string({
1434
+ isOptional: true,
1435
+ description: "Subject type"
1436
+ }),
1437
+ subjectId: field.string({
1438
+ isOptional: true,
1439
+ description: "Subject identifier"
1440
+ }),
1441
+ name: field.string({ description: "Threshold name" }),
1442
+ threshold: field.decimal({ description: "Threshold value" }),
1443
+ warnThreshold: field.decimal({
1444
+ isOptional: true,
1445
+ description: "Warning threshold (e.g., 80%)"
1446
+ }),
1447
+ periodType: field.enum("PeriodType", {
1448
+ default: "MONTHLY",
1449
+ description: "Period to evaluate"
1450
+ }),
1451
+ action: field.enum("ThresholdAction", {
1452
+ default: "ALERT",
1453
+ description: "Action when exceeded"
1454
+ }),
1455
+ notifyEmails: field.json({
1456
+ isOptional: true,
1457
+ description: "Email addresses to notify"
1458
+ }),
1459
+ notifyWebhook: field.string({
1460
+ isOptional: true,
1461
+ description: "Webhook URL to call"
1462
+ }),
1463
+ currentValue: field.decimal({
1464
+ default: 0,
1465
+ description: "Current usage value"
1466
+ }),
1467
+ lastCheckedAt: field.dateTime({
1468
+ isOptional: true,
1469
+ description: "Last threshold check"
1470
+ }),
1471
+ lastExceededAt: field.dateTime({
1472
+ isOptional: true,
1473
+ description: "Last time threshold was exceeded"
1474
+ }),
1475
+ isActive: field.boolean({
1476
+ default: true,
1477
+ description: "Whether threshold is active"
1478
+ }),
1479
+ metadata: field.json({
1480
+ isOptional: true,
1481
+ description: "Additional metadata"
1482
+ }),
1483
+ createdAt: field.createdAt(),
1484
+ updatedAt: field.updatedAt()
1485
+ },
1486
+ indexes: [
1487
+ index.on(["metricKey"]),
1488
+ index.on(["subjectType", "subjectId"]),
1489
+ index.on(["isActive", "metricKey"])
1490
+ ],
1491
+ enums: [ThresholdActionEnum]
1492
+ });
1493
+ var UsageAlertEntity = defineEntity({
1494
+ name: "UsageAlert",
1495
+ description: "Alert generated when threshold is exceeded.",
1496
+ schema: "lssm_metering",
1497
+ map: "usage_alert",
1498
+ fields: {
1499
+ id: field.id({ description: "Unique identifier" }),
1500
+ thresholdId: field.foreignKey({
1501
+ description: "Threshold that triggered alert"
1502
+ }),
1503
+ metricKey: field.string({ description: "Metric key" }),
1504
+ subjectType: field.string({
1505
+ isOptional: true,
1506
+ description: "Subject type"
1507
+ }),
1508
+ subjectId: field.string({
1509
+ isOptional: true,
1510
+ description: "Subject identifier"
1511
+ }),
1512
+ alertType: field.string({ description: "Alert type (warn, exceed, etc.)" }),
1513
+ threshold: field.decimal({ description: "Threshold value" }),
1514
+ actualValue: field.decimal({ description: "Actual usage value" }),
1515
+ percentageUsed: field.decimal({
1516
+ description: "Percentage of threshold used"
1517
+ }),
1518
+ status: field.string({
1519
+ default: '"pending"',
1520
+ description: "Alert status (pending, acknowledged, resolved)"
1521
+ }),
1522
+ acknowledgedBy: field.string({
1523
+ isOptional: true,
1524
+ description: "User who acknowledged"
1525
+ }),
1526
+ acknowledgedAt: field.dateTime({
1527
+ isOptional: true,
1528
+ description: "When acknowledged"
1529
+ }),
1530
+ resolvedAt: field.dateTime({
1531
+ isOptional: true,
1532
+ description: "When resolved"
1533
+ }),
1534
+ notificationsSent: field.json({
1535
+ isOptional: true,
1536
+ description: "Notifications sent"
1537
+ }),
1538
+ triggeredAt: field.dateTime({ description: "When alert was triggered" }),
1539
+ createdAt: field.createdAt(),
1540
+ thresholdRelation: field.belongsTo("UsageThreshold", ["thresholdId"], ["id"], { onDelete: "Cascade" })
1541
+ },
1542
+ indexes: [
1543
+ index.on(["thresholdId", "status"]),
1544
+ index.on(["metricKey", "triggeredAt"]),
1545
+ index.on(["status", "triggeredAt"])
1546
+ ]
1547
+ });
1548
+ var meteringEntities = [
1549
+ MetricDefinitionEntity,
1550
+ UsageRecordEntity,
1551
+ UsageSummaryEntity,
1552
+ UsageThresholdEntity,
1553
+ UsageAlertEntity
1554
+ ];
1555
+ var meteringSchemaContribution = {
1556
+ moduleId: "@contractspec/lib.metering",
1557
+ entities: meteringEntities,
1558
+ enums: [
1559
+ AggregationTypeEnum,
1560
+ ResetPeriodEnum,
1561
+ PeriodTypeEnum,
1562
+ ThresholdActionEnum
1563
+ ]
1564
+ };
1565
+
1566
+ // src/events.ts
1567
+ import { ScalarTypeEnum as ScalarTypeEnum2, defineSchemaModel as defineSchemaModel2 } from "@contractspec/lib.schema";
1568
+ import { defineEvent } from "@contractspec/lib.contracts";
1569
+ var MetricDefinedPayload = defineSchemaModel2({
1570
+ name: "MetricDefinedEventPayload",
1571
+ description: "Payload when a metric is defined",
1572
+ fields: {
1573
+ metricId: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
1574
+ key: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
1575
+ name: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
1576
+ unit: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
1577
+ aggregationType: {
1578
+ type: ScalarTypeEnum2.String_unsecure(),
1579
+ isOptional: false
1580
+ },
1581
+ orgId: { type: ScalarTypeEnum2.String_unsecure(), isOptional: true },
1582
+ createdBy: { type: ScalarTypeEnum2.String_unsecure(), isOptional: true },
1583
+ createdAt: { type: ScalarTypeEnum2.DateTime(), isOptional: false }
1584
+ }
1585
+ });
1586
+ var MetricUpdatedPayload = defineSchemaModel2({
1587
+ name: "MetricUpdatedEventPayload",
1588
+ description: "Payload when a metric is updated",
1589
+ fields: {
1590
+ metricId: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
1591
+ key: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
1592
+ changes: { type: ScalarTypeEnum2.JSON(), isOptional: false },
1593
+ updatedBy: { type: ScalarTypeEnum2.String_unsecure(), isOptional: true },
1594
+ updatedAt: { type: ScalarTypeEnum2.DateTime(), isOptional: false }
1595
+ }
1596
+ });
1597
+ var UsageRecordedPayload = defineSchemaModel2({
1598
+ name: "UsageRecordedEventPayload",
1599
+ description: "Payload when usage is recorded",
1600
+ fields: {
1601
+ recordId: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
1602
+ metricKey: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
1603
+ subjectType: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
1604
+ subjectId: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
1605
+ quantity: { type: ScalarTypeEnum2.Float_unsecure(), isOptional: false },
1606
+ source: { type: ScalarTypeEnum2.String_unsecure(), isOptional: true },
1607
+ timestamp: { type: ScalarTypeEnum2.DateTime(), isOptional: false }
1608
+ }
1609
+ });
1610
+ var UsageBatchRecordedPayload = defineSchemaModel2({
1611
+ name: "UsageBatchRecordedEventPayload",
1612
+ description: "Payload when a batch of usage is recorded",
1613
+ fields: {
1614
+ recordCount: { type: ScalarTypeEnum2.Int_unsecure(), isOptional: false },
1615
+ metricKeys: { type: ScalarTypeEnum2.JSON(), isOptional: false },
1616
+ totalQuantity: { type: ScalarTypeEnum2.Float_unsecure(), isOptional: false },
1617
+ timestamp: { type: ScalarTypeEnum2.DateTime(), isOptional: false }
1618
+ }
1619
+ });
1620
+ var UsageAggregatedPayload = defineSchemaModel2({
1621
+ name: "UsageAggregatedEventPayload",
1622
+ description: "Payload when usage is aggregated",
1623
+ fields: {
1624
+ summaryId: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
1625
+ metricKey: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
1626
+ subjectType: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
1627
+ subjectId: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
1628
+ periodType: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
1629
+ periodStart: { type: ScalarTypeEnum2.DateTime(), isOptional: false },
1630
+ periodEnd: { type: ScalarTypeEnum2.DateTime(), isOptional: false },
1631
+ totalQuantity: { type: ScalarTypeEnum2.Float_unsecure(), isOptional: false },
1632
+ recordCount: { type: ScalarTypeEnum2.Int_unsecure(), isOptional: false },
1633
+ aggregatedAt: { type: ScalarTypeEnum2.DateTime(), isOptional: false }
1634
+ }
1635
+ });
1636
+ var ThresholdCreatedPayload = defineSchemaModel2({
1637
+ name: "ThresholdCreatedEventPayload",
1638
+ description: "Payload when a threshold is created",
1639
+ fields: {
1640
+ thresholdId: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
1641
+ metricKey: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
1642
+ subjectType: { type: ScalarTypeEnum2.String_unsecure(), isOptional: true },
1643
+ subjectId: { type: ScalarTypeEnum2.String_unsecure(), isOptional: true },
1644
+ threshold: { type: ScalarTypeEnum2.Float_unsecure(), isOptional: false },
1645
+ action: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
1646
+ createdAt: { type: ScalarTypeEnum2.DateTime(), isOptional: false }
1647
+ }
1648
+ });
1649
+ var ThresholdExceededPayload = defineSchemaModel2({
1650
+ name: "ThresholdExceededEventPayload",
1651
+ description: "Payload when a threshold is exceeded",
1652
+ fields: {
1653
+ alertId: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
1654
+ thresholdId: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
1655
+ metricKey: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
1656
+ subjectType: { type: ScalarTypeEnum2.String_unsecure(), isOptional: true },
1657
+ subjectId: { type: ScalarTypeEnum2.String_unsecure(), isOptional: true },
1658
+ threshold: { type: ScalarTypeEnum2.Float_unsecure(), isOptional: false },
1659
+ actualValue: { type: ScalarTypeEnum2.Float_unsecure(), isOptional: false },
1660
+ percentageUsed: {
1661
+ type: ScalarTypeEnum2.Float_unsecure(),
1662
+ isOptional: false
1663
+ },
1664
+ action: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
1665
+ triggeredAt: { type: ScalarTypeEnum2.DateTime(), isOptional: false }
1666
+ }
1667
+ });
1668
+ var ThresholdApproachingPayload = defineSchemaModel2({
1669
+ name: "ThresholdApproachingEventPayload",
1670
+ description: "Payload when usage is approaching a threshold",
1671
+ fields: {
1672
+ thresholdId: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
1673
+ metricKey: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
1674
+ subjectType: { type: ScalarTypeEnum2.String_unsecure(), isOptional: true },
1675
+ subjectId: { type: ScalarTypeEnum2.String_unsecure(), isOptional: true },
1676
+ threshold: { type: ScalarTypeEnum2.Float_unsecure(), isOptional: false },
1677
+ currentValue: { type: ScalarTypeEnum2.Float_unsecure(), isOptional: false },
1678
+ percentageUsed: {
1679
+ type: ScalarTypeEnum2.Float_unsecure(),
1680
+ isOptional: false
1681
+ },
1682
+ triggeredAt: { type: ScalarTypeEnum2.DateTime(), isOptional: false }
1683
+ }
1684
+ });
1685
+ var MetricDefinedEvent = defineEvent({
1686
+ meta: {
1687
+ key: "metric.defined",
1688
+ version: "1.0.0",
1689
+ description: "A metric has been defined.",
1690
+ stability: "stable",
1691
+ owners: ["@platform.metering"],
1692
+ tags: ["metering", "metric"]
1693
+ },
1694
+ payload: MetricDefinedPayload
1695
+ });
1696
+ var MetricUpdatedEvent = defineEvent({
1697
+ meta: {
1698
+ key: "metric.updated",
1699
+ version: "1.0.0",
1700
+ description: "A metric has been updated.",
1701
+ stability: "stable",
1702
+ owners: ["@platform.metering"],
1703
+ tags: ["metering", "metric"]
1704
+ },
1705
+ payload: MetricUpdatedPayload
1706
+ });
1707
+ var UsageRecordedEvent = defineEvent({
1708
+ meta: {
1709
+ key: "usage.recorded",
1710
+ version: "1.0.0",
1711
+ description: "Usage has been recorded.",
1712
+ stability: "stable",
1713
+ owners: ["@platform.metering"],
1714
+ tags: ["metering", "usage"]
1715
+ },
1716
+ payload: UsageRecordedPayload
1717
+ });
1718
+ var UsageBatchRecordedEvent = defineEvent({
1719
+ meta: {
1720
+ key: "usage.batch_recorded",
1721
+ version: "1.0.0",
1722
+ description: "A batch of usage has been recorded.",
1723
+ stability: "stable",
1724
+ owners: ["@platform.metering"],
1725
+ tags: ["metering", "usage"]
1726
+ },
1727
+ payload: UsageBatchRecordedPayload
1728
+ });
1729
+ var UsageAggregatedEvent = defineEvent({
1730
+ meta: {
1731
+ key: "usage.aggregated",
1732
+ version: "1.0.0",
1733
+ description: "Usage has been aggregated into a summary.",
1734
+ stability: "stable",
1735
+ owners: ["@platform.metering"],
1736
+ tags: ["metering", "usage"]
1737
+ },
1738
+ payload: UsageAggregatedPayload
1739
+ });
1740
+ var ThresholdCreatedEvent = defineEvent({
1741
+ meta: {
1742
+ key: "threshold.created",
1743
+ version: "1.0.0",
1744
+ description: "A usage threshold has been created.",
1745
+ stability: "stable",
1746
+ owners: ["@platform.metering"],
1747
+ tags: ["metering", "threshold"]
1748
+ },
1749
+ payload: ThresholdCreatedPayload
1750
+ });
1751
+ var ThresholdExceededEvent = defineEvent({
1752
+ meta: {
1753
+ key: "threshold.exceeded",
1754
+ version: "1.0.0",
1755
+ description: "Usage has exceeded a threshold.",
1756
+ stability: "stable",
1757
+ owners: ["@platform.metering"],
1758
+ tags: ["metering", "threshold"]
1759
+ },
1760
+ payload: ThresholdExceededPayload
1761
+ });
1762
+ var ThresholdApproachingEvent = defineEvent({
1763
+ meta: {
1764
+ key: "threshold.approaching",
1765
+ version: "1.0.0",
1766
+ description: "Usage is approaching a threshold.",
1767
+ stability: "stable",
1768
+ owners: ["@platform.metering"],
1769
+ tags: ["metering", "threshold"]
1770
+ },
1771
+ payload: ThresholdApproachingPayload
1772
+ });
1773
+ var MeteringEvents = {
1774
+ MetricDefinedEvent,
1775
+ MetricUpdatedEvent,
1776
+ UsageRecordedEvent,
1777
+ UsageBatchRecordedEvent,
1778
+ UsageAggregatedEvent,
1779
+ ThresholdCreatedEvent,
1780
+ ThresholdExceededEvent,
1781
+ ThresholdApproachingEvent
1782
+ };
1783
+
1784
+ // src/metering.feature.ts
1785
+ import { defineFeature } from "@contractspec/lib.contracts";
1786
+ var MeteringFeature = defineFeature({
1787
+ meta: {
1788
+ key: "metrics",
1789
+ version: "1.0.0",
1790
+ title: "Usage Metering",
1791
+ description: "Usage metering, metric definitions, and threshold alerting",
1792
+ domain: "platform",
1793
+ owners: ["@platform.metering"],
1794
+ tags: ["metering", "usage", "billing", "thresholds"],
1795
+ stability: "stable"
1796
+ },
1797
+ operations: [
1798
+ { key: "metric.define", version: "1.0.0" },
1799
+ { key: "metric.update", version: "1.0.0" },
1800
+ { key: "metric.delete", version: "1.0.0" },
1801
+ { key: "metric.get", version: "1.0.0" },
1802
+ { key: "metric.list", version: "1.0.0" },
1803
+ { key: "usage.record", version: "1.0.0" },
1804
+ { key: "usage.recordBatch", version: "1.0.0" },
1805
+ { key: "usage.get", version: "1.0.0" },
1806
+ { key: "usage.getSummary", version: "1.0.0" },
1807
+ { key: "threshold.create", version: "1.0.0" },
1808
+ { key: "threshold.update", version: "1.0.0" },
1809
+ { key: "threshold.delete", version: "1.0.0" },
1810
+ { key: "threshold.list", version: "1.0.0" }
1811
+ ],
1812
+ events: [
1813
+ { key: "metric.defined", version: "1.0.0" },
1814
+ { key: "metric.updated", version: "1.0.0" },
1815
+ { key: "usage.recorded", version: "1.0.0" },
1816
+ { key: "usage.batch_recorded", version: "1.0.0" },
1817
+ { key: "usage.aggregated", version: "1.0.0" },
1818
+ { key: "threshold.created", version: "1.0.0" },
1819
+ { key: "threshold.exceeded", version: "1.0.0" },
1820
+ { key: "threshold.approaching", version: "1.0.0" }
1821
+ ],
1822
+ presentations: [],
1823
+ opToPresentation: [],
1824
+ presentationsTargets: [],
1825
+ capabilities: {
1826
+ provides: [
1827
+ { key: "metering", version: "1.0.0" },
1828
+ { key: "thresholds", version: "1.0.0" }
1829
+ ],
1830
+ requires: []
1831
+ }
1832
+ });
1833
+ export {
1834
+ meteringSchemaContribution,
1835
+ meteringEntities,
1836
+ getPeriodStart,
1837
+ getPeriodEnd,
1838
+ formatPeriodKey,
1839
+ UsageThresholdModel,
1840
+ UsageThresholdEntity,
1841
+ UsageSummaryModel,
1842
+ UsageSummaryEntity,
1843
+ UsageRecordedEvent,
1844
+ UsageRecordModel,
1845
+ UsageRecordEntity,
1846
+ UsageBatchRecordedEvent,
1847
+ UsageAlertEntity,
1848
+ UsageAggregator,
1849
+ UsageAggregatedEvent,
1850
+ UpdateThresholdContract,
1851
+ UpdateMetricContract,
1852
+ ThresholdExceededEvent,
1853
+ ThresholdCreatedEvent,
1854
+ ThresholdApproachingEvent,
1855
+ ThresholdActionEnum,
1856
+ ResetPeriodEnum,
1857
+ RecordUsageContract,
1858
+ RecordBatchUsageContract,
1859
+ PosthogMeteringReporter,
1860
+ PosthogMeteringReader,
1861
+ PeriodTypeEnum,
1862
+ MetricUpdatedEvent,
1863
+ MetricDefinitionModel,
1864
+ MetricDefinitionEntity,
1865
+ MetricDefinedEvent,
1866
+ MeteringFeature,
1867
+ MeteringEvents,
1868
+ ListThresholdsContract,
1869
+ ListMetricsContract,
1870
+ InMemoryUsageStorage,
1871
+ GetUsageSummaryContract,
1872
+ GetUsageContract,
1873
+ GetMetricContract,
1874
+ DeleteThresholdContract,
1875
+ DeleteMetricContract,
1876
+ DefineMetricContract,
1877
+ CreateThresholdContract,
1878
+ AggregationTypeEnum
1879
+ };