@lssm/lib.metering 0.0.0-canary-20251217054315 → 0.0.0-canary-20251217060804
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.
- package/dist/aggregation/index.d.ts +155 -0
- package/dist/contracts/index.d.ts +1138 -0
- package/dist/docs/index.d.ts +1 -0
- package/dist/docs/metering.docblock.d.ts +1 -0
- package/dist/entities/index.d.ts +230 -0
- package/dist/events.d.ts +539 -0
- package/dist/index.d.ts +6 -0
- package/dist/metering.feature.d.ts +11 -0
- package/package.json +13 -13
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
//#region src/aggregation/index.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Usage aggregation engine.
|
|
4
|
+
*
|
|
5
|
+
* Provides periodic aggregation of usage records into summaries
|
|
6
|
+
* for efficient billing and reporting queries.
|
|
7
|
+
*/
|
|
8
|
+
type PeriodType = 'HOURLY' | 'DAILY' | 'WEEKLY' | 'MONTHLY' | 'YEARLY';
|
|
9
|
+
type AggregationType = 'COUNT' | 'SUM' | 'AVG' | 'MAX' | 'MIN' | 'LAST';
|
|
10
|
+
interface UsageRecord {
|
|
11
|
+
id: string;
|
|
12
|
+
metricKey: string;
|
|
13
|
+
subjectType: string;
|
|
14
|
+
subjectId: string;
|
|
15
|
+
quantity: number;
|
|
16
|
+
timestamp: Date;
|
|
17
|
+
}
|
|
18
|
+
interface UsageSummary {
|
|
19
|
+
id: string;
|
|
20
|
+
metricKey: string;
|
|
21
|
+
subjectType: string;
|
|
22
|
+
subjectId: string;
|
|
23
|
+
periodType: PeriodType;
|
|
24
|
+
periodStart: Date;
|
|
25
|
+
periodEnd: Date;
|
|
26
|
+
totalQuantity: number;
|
|
27
|
+
recordCount: number;
|
|
28
|
+
minQuantity?: number;
|
|
29
|
+
maxQuantity?: number;
|
|
30
|
+
avgQuantity?: number;
|
|
31
|
+
}
|
|
32
|
+
interface MetricDefinition {
|
|
33
|
+
key: string;
|
|
34
|
+
aggregationType: AggregationType;
|
|
35
|
+
}
|
|
36
|
+
interface UsageStorage {
|
|
37
|
+
/**
|
|
38
|
+
* Get unaggregated records for a period.
|
|
39
|
+
*/
|
|
40
|
+
getUnaggregatedRecords(options: {
|
|
41
|
+
metricKey?: string;
|
|
42
|
+
periodStart: Date;
|
|
43
|
+
periodEnd: Date;
|
|
44
|
+
limit?: number;
|
|
45
|
+
}): Promise<UsageRecord[]>;
|
|
46
|
+
/**
|
|
47
|
+
* Mark records as aggregated.
|
|
48
|
+
*/
|
|
49
|
+
markRecordsAggregated(recordIds: string[], aggregatedAt: Date): Promise<void>;
|
|
50
|
+
/**
|
|
51
|
+
* Get or create a summary record.
|
|
52
|
+
*/
|
|
53
|
+
upsertSummary(summary: Omit<UsageSummary, 'id'>): Promise<UsageSummary>;
|
|
54
|
+
/**
|
|
55
|
+
* Get metric definition.
|
|
56
|
+
*/
|
|
57
|
+
getMetric(key: string): Promise<MetricDefinition | null>;
|
|
58
|
+
/**
|
|
59
|
+
* List all active metrics.
|
|
60
|
+
*/
|
|
61
|
+
listMetrics(): Promise<MetricDefinition[]>;
|
|
62
|
+
}
|
|
63
|
+
interface AggregationOptions {
|
|
64
|
+
/** Storage implementation */
|
|
65
|
+
storage: UsageStorage;
|
|
66
|
+
/** Batch size for processing records */
|
|
67
|
+
batchSize?: number;
|
|
68
|
+
}
|
|
69
|
+
interface AggregateParams {
|
|
70
|
+
/** Period type to aggregate */
|
|
71
|
+
periodType: PeriodType;
|
|
72
|
+
/** Period start time */
|
|
73
|
+
periodStart: Date;
|
|
74
|
+
/** Period end time (optional, defaults to period boundary) */
|
|
75
|
+
periodEnd?: Date;
|
|
76
|
+
/** Specific metric to aggregate (optional, aggregates all if not specified) */
|
|
77
|
+
metricKey?: string;
|
|
78
|
+
}
|
|
79
|
+
interface AggregationResult {
|
|
80
|
+
periodType: PeriodType;
|
|
81
|
+
periodStart: Date;
|
|
82
|
+
periodEnd: Date;
|
|
83
|
+
recordsProcessed: number;
|
|
84
|
+
summariesCreated: number;
|
|
85
|
+
summariesUpdated: number;
|
|
86
|
+
errors: AggregationError[];
|
|
87
|
+
}
|
|
88
|
+
interface AggregationError {
|
|
89
|
+
metricKey: string;
|
|
90
|
+
subjectType: string;
|
|
91
|
+
subjectId: string;
|
|
92
|
+
error: string;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Get the start of a period for a given date.
|
|
96
|
+
*/
|
|
97
|
+
declare function getPeriodStart(date: Date, periodType: PeriodType): Date;
|
|
98
|
+
/**
|
|
99
|
+
* Get the end of a period for a given date.
|
|
100
|
+
*/
|
|
101
|
+
declare function getPeriodEnd(date: Date, periodType: PeriodType): Date;
|
|
102
|
+
/**
|
|
103
|
+
* Format a period key for grouping.
|
|
104
|
+
*/
|
|
105
|
+
declare function formatPeriodKey(date: Date, periodType: PeriodType): string;
|
|
106
|
+
/**
|
|
107
|
+
* Usage aggregator.
|
|
108
|
+
*
|
|
109
|
+
* Aggregates usage records into summaries based on period type.
|
|
110
|
+
*/
|
|
111
|
+
declare class UsageAggregator {
|
|
112
|
+
private storage;
|
|
113
|
+
private batchSize;
|
|
114
|
+
constructor(options: AggregationOptions);
|
|
115
|
+
/**
|
|
116
|
+
* Aggregate usage records for a period.
|
|
117
|
+
*/
|
|
118
|
+
aggregate(params: AggregateParams): Promise<AggregationResult>;
|
|
119
|
+
/**
|
|
120
|
+
* Group records by metric, subject, and period.
|
|
121
|
+
*/
|
|
122
|
+
private groupRecords;
|
|
123
|
+
/**
|
|
124
|
+
* Aggregate a group of records into a summary.
|
|
125
|
+
*/
|
|
126
|
+
private aggregateGroup;
|
|
127
|
+
/**
|
|
128
|
+
* Calculate aggregation values.
|
|
129
|
+
*/
|
|
130
|
+
private calculateAggregation;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* In-memory usage storage for testing.
|
|
134
|
+
*/
|
|
135
|
+
declare class InMemoryUsageStorage implements UsageStorage {
|
|
136
|
+
private records;
|
|
137
|
+
private summaries;
|
|
138
|
+
private metrics;
|
|
139
|
+
addRecord(record: UsageRecord): void;
|
|
140
|
+
addMetric(metric: MetricDefinition): void;
|
|
141
|
+
getUnaggregatedRecords(options: {
|
|
142
|
+
metricKey?: string;
|
|
143
|
+
periodStart: Date;
|
|
144
|
+
periodEnd: Date;
|
|
145
|
+
limit?: number;
|
|
146
|
+
}): Promise<UsageRecord[]>;
|
|
147
|
+
markRecordsAggregated(recordIds: string[]): Promise<void>;
|
|
148
|
+
upsertSummary(summary: Omit<UsageSummary, 'id'>): Promise<UsageSummary>;
|
|
149
|
+
getMetric(key: string): Promise<MetricDefinition | null>;
|
|
150
|
+
listMetrics(): Promise<MetricDefinition[]>;
|
|
151
|
+
getSummaries(): UsageSummary[];
|
|
152
|
+
clear(): void;
|
|
153
|
+
}
|
|
154
|
+
//#endregion
|
|
155
|
+
export { AggregateParams, AggregationError, AggregationOptions, AggregationResult, AggregationType, InMemoryUsageStorage, MetricDefinition, PeriodType, UsageAggregator, UsageRecord, UsageStorage, UsageSummary, formatPeriodKey, getPeriodEnd, getPeriodStart };
|