@x12i/countex 1.0.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.
- package/LICENSE +21 -0
- package/README.md +520 -0
- package/dist/backup/export.d.ts +19 -0
- package/dist/backup/export.d.ts.map +1 -0
- package/dist/backup/export.js +36 -0
- package/dist/backup/export.js.map +1 -0
- package/dist/core/buffer.d.ts +27 -0
- package/dist/core/buffer.d.ts.map +1 -0
- package/dist/core/buffer.js +63 -0
- package/dist/core/buffer.js.map +1 -0
- package/dist/core/cardinality.d.ts +43 -0
- package/dist/core/cardinality.d.ts.map +1 -0
- package/dist/core/cardinality.js +93 -0
- package/dist/core/cardinality.js.map +1 -0
- package/dist/core/countex.d.ts +49 -0
- package/dist/core/countex.d.ts.map +1 -0
- package/dist/core/countex.js +331 -0
- package/dist/core/countex.js.map +1 -0
- package/dist/core/extraction.d.ts +39 -0
- package/dist/core/extraction.d.ts.map +1 -0
- package/dist/core/extraction.js +176 -0
- package/dist/core/extraction.js.map +1 -0
- package/dist/core/mapping.d.ts +8 -0
- package/dist/core/mapping.d.ts.map +1 -0
- package/dist/core/mapping.js +90 -0
- package/dist/core/mapping.js.map +1 -0
- package/dist/core/normalization.d.ts +15 -0
- package/dist/core/normalization.d.ts.map +1 -0
- package/dist/core/normalization.js +43 -0
- package/dist/core/normalization.js.map +1 -0
- package/dist/core/timeframe.d.ts +30 -0
- package/dist/core/timeframe.d.ts.map +1 -0
- package/dist/core/timeframe.js +151 -0
- package/dist/core/timeframe.js.map +1 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +28 -0
- package/dist/index.js.map +1 -0
- package/dist/query/formatter.d.ts +9 -0
- package/dist/query/formatter.d.ts.map +1 -0
- package/dist/query/formatter.js +119 -0
- package/dist/query/formatter.js.map +1 -0
- package/dist/query/planner.d.ts +14 -0
- package/dist/query/planner.d.ts.map +1 -0
- package/dist/query/planner.js +32 -0
- package/dist/query/planner.js.map +1 -0
- package/dist/storage/memory.d.ts +36 -0
- package/dist/storage/memory.d.ts.map +1 -0
- package/dist/storage/memory.js +150 -0
- package/dist/storage/memory.js.map +1 -0
- package/dist/storage/mongo.d.ts +56 -0
- package/dist/storage/mongo.d.ts.map +1 -0
- package/dist/storage/mongo.js +323 -0
- package/dist/storage/mongo.js.map +1 -0
- package/dist/storage/redis.d.ts +44 -0
- package/dist/storage/redis.d.ts.map +1 -0
- package/dist/storage/redis.js +193 -0
- package/dist/storage/redis.js.map +1 -0
- package/dist/types/index.d.ts +317 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +9 -0
- package/dist/types/index.js.map +1 -0
- package/dist/utils/errors.d.ts +20 -0
- package/dist/utils/errors.d.ts.map +1 -0
- package/dist/utils/errors.js +44 -0
- package/dist/utils/errors.js.map +1 -0
- package/dist/utils/hash.d.ts +30 -0
- package/dist/utils/hash.d.ts.map +1 -0
- package/dist/utils/hash.js +61 -0
- package/dist/utils/hash.js.map +1 -0
- package/dist/utils/jsonpath.d.ts +40 -0
- package/dist/utils/jsonpath.d.ts.map +1 -0
- package/dist/utils/jsonpath.js +162 -0
- package/dist/utils/jsonpath.js.map +1 -0
- package/dist/utils/logger.d.ts +7 -0
- package/dist/utils/logger.d.ts.map +1 -0
- package/dist/utils/logger.js +24 -0
- package/dist/utils/logger.js.map +1 -0
- package/docs/DEPLOYMENT.md +244 -0
- package/docs/END_TO_END_EXAMPLE.md +316 -0
- package/package.json +91 -0
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Local delta buffer.
|
|
4
|
+
*
|
|
5
|
+
* Each app instance keeps just a Map<cellKey, delta>, not full state. On flush
|
|
6
|
+
* we drain the buffer into a snapshot, send it to durable storage, and only
|
|
7
|
+
* then forget those deltas. If the durable write fails, we merge the snapshot
|
|
8
|
+
* back into the buffer so no counts are lost.
|
|
9
|
+
*/
|
|
10
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
+
exports.DeltaBuffer = void 0;
|
|
12
|
+
const hash_js_1 = require("../utils/hash.js");
|
|
13
|
+
class DeltaBuffer {
|
|
14
|
+
map = new Map();
|
|
15
|
+
/** Number of source events accumulated since last flush (for flush triggers). */
|
|
16
|
+
events = 0;
|
|
17
|
+
size() {
|
|
18
|
+
return this.map.size;
|
|
19
|
+
}
|
|
20
|
+
eventCount() {
|
|
21
|
+
return this.events;
|
|
22
|
+
}
|
|
23
|
+
noteEvent() {
|
|
24
|
+
this.events++;
|
|
25
|
+
}
|
|
26
|
+
/** Add a delta to the cell identified by `identity`. */
|
|
27
|
+
add(identity, delta) {
|
|
28
|
+
if (delta === 0)
|
|
29
|
+
return;
|
|
30
|
+
const key = (0, hash_js_1.cellKey)(identity);
|
|
31
|
+
const cur = this.map.get(key);
|
|
32
|
+
if (cur) {
|
|
33
|
+
cur.delta += delta;
|
|
34
|
+
// If they cancel out, drop the entry to keep the buffer compact.
|
|
35
|
+
if (cur.delta === 0)
|
|
36
|
+
this.map.delete(key);
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
this.map.set(key, { identity, delta });
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Take the current contents as a flush snapshot. The buffer is cleared
|
|
44
|
+
* atomically — if you need to re-merge on failure, call `mergeBack`.
|
|
45
|
+
*/
|
|
46
|
+
drain() {
|
|
47
|
+
const out = [];
|
|
48
|
+
for (const entry of this.map.values()) {
|
|
49
|
+
out.push({ identity: entry.identity, delta: entry.delta });
|
|
50
|
+
}
|
|
51
|
+
this.map.clear();
|
|
52
|
+
this.events = 0;
|
|
53
|
+
return out;
|
|
54
|
+
}
|
|
55
|
+
/** Re-merge a previously drained snapshot back into the buffer (failure recovery). */
|
|
56
|
+
mergeBack(snapshot) {
|
|
57
|
+
for (const d of snapshot) {
|
|
58
|
+
this.add(d.identity, d.delta);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
exports.DeltaBuffer = DeltaBuffer;
|
|
63
|
+
//# sourceMappingURL=buffer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"buffer.js","sourceRoot":"","sources":["../../src/core/buffer.ts"],"names":[],"mappings":";AAAA;;;;;;;GAOG;;;AAGH,8CAA2C;AAO3C,MAAa,WAAW;IACL,GAAG,GAA+B,IAAI,GAAG,EAAE,CAAC;IAC7D,iFAAiF;IACzE,MAAM,GAAG,CAAC,CAAC;IAEnB,IAAI;QACF,OAAO,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC;IACvB,CAAC;IAED,UAAU;QACR,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;IAED,SAAS;QACP,IAAI,CAAC,MAAM,EAAE,CAAC;IAChB,CAAC;IAED,wDAAwD;IACxD,GAAG,CAAC,QAAsB,EAAE,KAAa;QACvC,IAAI,KAAK,KAAK,CAAC;YAAE,OAAO;QACxB,MAAM,GAAG,GAAG,IAAA,iBAAO,EAAC,QAAQ,CAAC,CAAC;QAC9B,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QAC9B,IAAI,GAAG,EAAE,CAAC;YACR,GAAG,CAAC,KAAK,IAAI,KAAK,CAAC;YACnB,iEAAiE;YACjE,IAAI,GAAG,CAAC,KAAK,KAAK,CAAC;gBAAE,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QAC5C,CAAC;aAAM,CAAC;YACN,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,EAAE,EAAE,QAAQ,EAAE,KAAK,EAAE,CAAC,CAAC;QACzC,CAAC;IACH,CAAC;IAED;;;OAGG;IACH,KAAK;QACH,MAAM,GAAG,GAAmB,EAAE,CAAC;QAC/B,KAAK,MAAM,KAAK,IAAI,IAAI,CAAC,GAAG,CAAC,MAAM,EAAE,EAAE,CAAC;YACtC,GAAG,CAAC,IAAI,CAAC,EAAE,QAAQ,EAAE,KAAK,CAAC,QAAQ,EAAE,KAAK,EAAE,KAAK,CAAC,KAAK,EAAE,CAAC,CAAC;QAC7D,CAAC;QACD,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,CAAC;QACjB,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC;QAChB,OAAO,GAAG,CAAC;IACb,CAAC;IAED,sFAAsF;IACtF,SAAS,CAAC,QAAwB;QAChC,KAAK,MAAM,CAAC,IAAI,QAAQ,EAAE,CAAC;YACzB,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,QAAQ,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC;QAChC,CAAC;IACH,CAAC;CACF;AAnDD,kCAmDC"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cardinality protection.
|
|
3
|
+
*
|
|
4
|
+
* A bad mapping (e.g. a dimension that pulls a free-form URL) can blow up the
|
|
5
|
+
* dictionary in seconds. We track per-(scope, kind, qualifier) how many distinct
|
|
6
|
+
* source-keys we've allocated. When a limit fires we either:
|
|
7
|
+
*
|
|
8
|
+
* - "overflow" → rewrite the source-key to a sentinel like "__OTHER__"
|
|
9
|
+
* - "drop" → return null, the increment is skipped
|
|
10
|
+
* - "error" → throw; the surrounding ingest will record it and move on
|
|
11
|
+
*
|
|
12
|
+
* Counts are kept in-process. Over many workers this is approximate, which is
|
|
13
|
+
* fine — the goal is to stop a runaway scenario, not enforce a hard quota.
|
|
14
|
+
*/
|
|
15
|
+
import type { CardinalityPolicy, DictionaryKind } from "../types/index.js";
|
|
16
|
+
export type CardinalityWarning = {
|
|
17
|
+
scopeId: string;
|
|
18
|
+
kind: DictionaryKind;
|
|
19
|
+
qualifier?: string;
|
|
20
|
+
count: number;
|
|
21
|
+
limit: number;
|
|
22
|
+
};
|
|
23
|
+
export type OverflowEvent = {
|
|
24
|
+
scopeId: string;
|
|
25
|
+
kind: DictionaryKind;
|
|
26
|
+
qualifier?: string;
|
|
27
|
+
sourceKey: string;
|
|
28
|
+
};
|
|
29
|
+
export declare class CardinalityGuard {
|
|
30
|
+
private readonly counters;
|
|
31
|
+
private readonly policy;
|
|
32
|
+
constructor(policy?: CardinalityPolicy);
|
|
33
|
+
/**
|
|
34
|
+
* Check (and record) a source key. Returns:
|
|
35
|
+
* - the same source-key (or the overflow sentinel) on success
|
|
36
|
+
* - null when policy is "drop" and a limit is hit
|
|
37
|
+
* Throws when policy is "error".
|
|
38
|
+
*/
|
|
39
|
+
check(scopeId: string, kind: DictionaryKind, qualifier: string | undefined, sourceKey: string, onWarning?: (w: CardinalityWarning) => void, onOverflow?: (o: OverflowEvent) => void): string | null;
|
|
40
|
+
private limitFor;
|
|
41
|
+
private keyFor;
|
|
42
|
+
}
|
|
43
|
+
//# sourceMappingURL=cardinality.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cardinality.d.ts","sourceRoot":"","sources":["../../src/core/cardinality.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,KAAK,EACV,iBAAiB,EACjB,cAAc,EACf,MAAM,mBAAmB,CAAC;AAiB3B,MAAM,MAAM,kBAAkB,GAAG;IAC/B,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,cAAc,CAAC;IACrB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;CACf,CAAC;AAEF,MAAM,MAAM,aAAa,GAAG;IAC1B,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,cAAc,CAAC;IACrB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,SAAS,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF,qBAAa,gBAAgB;IAC3B,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAmC;IAC5D,OAAO,CAAC,QAAQ,CAAC,MAAM,CAA8B;gBAEzC,MAAM,CAAC,EAAE,iBAAiB;IAItC;;;;;OAKG;IACH,KAAK,CACH,OAAO,EAAE,MAAM,EACf,IAAI,EAAE,cAAc,EACpB,SAAS,EAAE,MAAM,GAAG,SAAS,EAC7B,SAAS,EAAE,MAAM,EACjB,SAAS,CAAC,EAAE,CAAC,CAAC,EAAE,kBAAkB,KAAK,IAAI,EAC3C,UAAU,CAAC,EAAE,CAAC,CAAC,EAAE,aAAa,KAAK,IAAI,GACtC,MAAM,GAAG,IAAI;IA8ChB,OAAO,CAAC,QAAQ;IAUhB,OAAO,CAAC,MAAM;CAGf"}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Cardinality protection.
|
|
4
|
+
*
|
|
5
|
+
* A bad mapping (e.g. a dimension that pulls a free-form URL) can blow up the
|
|
6
|
+
* dictionary in seconds. We track per-(scope, kind, qualifier) how many distinct
|
|
7
|
+
* source-keys we've allocated. When a limit fires we either:
|
|
8
|
+
*
|
|
9
|
+
* - "overflow" → rewrite the source-key to a sentinel like "__OTHER__"
|
|
10
|
+
* - "drop" → return null, the increment is skipped
|
|
11
|
+
* - "error" → throw; the surrounding ingest will record it and move on
|
|
12
|
+
*
|
|
13
|
+
* Counts are kept in-process. Over many workers this is approximate, which is
|
|
14
|
+
* fine — the goal is to stop a runaway scenario, not enforce a hard quota.
|
|
15
|
+
*/
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.CardinalityGuard = void 0;
|
|
18
|
+
const errors_js_1 = require("../utils/errors.js");
|
|
19
|
+
const DEFAULT_POLICY = {
|
|
20
|
+
maxDimensionValuesPerScope: 1_000_000,
|
|
21
|
+
maxDimensionValuesPerName: 100_000,
|
|
22
|
+
maxSubjectsPerScope: 10_000_000,
|
|
23
|
+
overflowValue: "__OTHER__",
|
|
24
|
+
onLimit: "overflow",
|
|
25
|
+
highCardinalityWarnings: true,
|
|
26
|
+
};
|
|
27
|
+
class CardinalityGuard {
|
|
28
|
+
counters = new Map();
|
|
29
|
+
policy;
|
|
30
|
+
constructor(policy) {
|
|
31
|
+
this.policy = { ...DEFAULT_POLICY, ...(policy ?? {}) };
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Check (and record) a source key. Returns:
|
|
35
|
+
* - the same source-key (or the overflow sentinel) on success
|
|
36
|
+
* - null when policy is "drop" and a limit is hit
|
|
37
|
+
* Throws when policy is "error".
|
|
38
|
+
*/
|
|
39
|
+
check(scopeId, kind, qualifier, sourceKey, onWarning, onOverflow) {
|
|
40
|
+
const limit = this.limitFor(kind);
|
|
41
|
+
const key = this.keyFor(scopeId, kind, qualifier);
|
|
42
|
+
let c = this.counters.get(key);
|
|
43
|
+
if (!c) {
|
|
44
|
+
c = { values: new Set(), warned: false };
|
|
45
|
+
this.counters.set(key, c);
|
|
46
|
+
}
|
|
47
|
+
if (c.values.has(sourceKey) || c.values.has(this.policy.overflowValue)) {
|
|
48
|
+
return c.values.has(sourceKey) ? sourceKey : this.policy.overflowValue;
|
|
49
|
+
}
|
|
50
|
+
if (c.values.size < limit) {
|
|
51
|
+
c.values.add(sourceKey);
|
|
52
|
+
// Warning at 80%
|
|
53
|
+
if (this.policy.highCardinalityWarnings &&
|
|
54
|
+
!c.warned &&
|
|
55
|
+
c.values.size > Math.floor(limit * 0.8)) {
|
|
56
|
+
c.warned = true;
|
|
57
|
+
onWarning?.({ scopeId, kind, qualifier, count: c.values.size, limit });
|
|
58
|
+
}
|
|
59
|
+
return sourceKey;
|
|
60
|
+
}
|
|
61
|
+
// Over the limit
|
|
62
|
+
switch (this.policy.onLimit) {
|
|
63
|
+
case "drop":
|
|
64
|
+
return null;
|
|
65
|
+
case "error":
|
|
66
|
+
throw new errors_js_1.CardinalityLimitError("Cardinality limit exceeded", {
|
|
67
|
+
scopeId,
|
|
68
|
+
kind,
|
|
69
|
+
qualifier,
|
|
70
|
+
limit,
|
|
71
|
+
});
|
|
72
|
+
case "overflow":
|
|
73
|
+
default:
|
|
74
|
+
c.values.add(this.policy.overflowValue);
|
|
75
|
+
onOverflow?.({ scopeId, kind, qualifier, sourceKey });
|
|
76
|
+
return this.policy.overflowValue;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
limitFor(kind) {
|
|
80
|
+
switch (kind) {
|
|
81
|
+
case "subject":
|
|
82
|
+
return this.policy.maxSubjectsPerScope;
|
|
83
|
+
case "metric":
|
|
84
|
+
case "dimensionValue":
|
|
85
|
+
return this.policy.maxDimensionValuesPerName;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
keyFor(scopeId, kind, qualifier) {
|
|
89
|
+
return `${scopeId}|${kind}|${qualifier ?? ""}`;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
exports.CardinalityGuard = CardinalityGuard;
|
|
93
|
+
//# sourceMappingURL=cardinality.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cardinality.js","sourceRoot":"","sources":["../../src/core/cardinality.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;GAaG;;;AAMH,kDAA2D;AAO3D,MAAM,cAAc,GAAgC;IAClD,0BAA0B,EAAE,SAAS;IACrC,yBAAyB,EAAE,OAAO;IAClC,mBAAmB,EAAE,UAAU;IAC/B,aAAa,EAAE,WAAW;IAC1B,OAAO,EAAE,UAAU;IACnB,uBAAuB,EAAE,IAAI;CAC9B,CAAC;AAiBF,MAAa,gBAAgB;IACV,QAAQ,GAAyB,IAAI,GAAG,EAAE,CAAC;IAC3C,MAAM,CAA8B;IAErD,YAAY,MAA0B;QACpC,IAAI,CAAC,MAAM,GAAG,EAAE,GAAG,cAAc,EAAE,GAAG,CAAC,MAAM,IAAI,EAAE,CAAC,EAAE,CAAC;IACzD,CAAC;IAED;;;;;OAKG;IACH,KAAK,CACH,OAAe,EACf,IAAoB,EACpB,SAA6B,EAC7B,SAAiB,EACjB,SAA2C,EAC3C,UAAuC;QAEvC,MAAM,KAAK,GAAG,I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AC;YACD,OAAO,SAAS,CAAC;QACnB,CAAC;QAED,iBAAiB;QACjB,QAAQ,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;YAC5B,KAAK,MAAM;gBACT,OAAO,IAAI,CAAC;YACd,KAAK,OAAO;gBACV,MAAM,IAAI,iCAAqB,CAAC,4BAA4B,EAAE;oBAC5D,OAAO;oBACP,IAAI;oBACJ,SAAS;oBACT,KAAK;iBACN,CAAC,CAAC;YACL,KAAK,UAAU,CAAC;YAChB;gBACE,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC;gBACxC,UAAU,EAAE,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,SAAS,EAAE,CAAC,CAAC;gBACtD,OAAO,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC;QACrC,CAAC;IACH,CAAC;IAEO,QAAQ,CAAC,IAAoB;QACnC,QAAQ,IAAI,EAAE,CAAC;YACb,KAAK,SAAS;gBACZ,OAAO,IAAI,CAAC,MAAM,CAAC,mBAAmB,CAAC;YACzC,KAAK,QAAQ,CAAC;YACd,KAAK,gBAAgB;gBACnB,OAAO,IAAI,CAAC,MAAM,CAAC,yBAAyB,CAAC;QACjD,CAAC;IACH,CAAC;IAEO,MAAM,CAAC,OAAe,EAAE,IAAoB,EAAE,SAAkB;QACtE,OAAO,GAAG,OAAO,IAAI,IAAI,IAAI,SAAS,IAAI,EAAE,EAAE,CAAC;IACjD,CAAC;CACF;AAhFD,4CAgFC"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Countex client.
|
|
3
|
+
*
|
|
4
|
+
* Orchestrates: extract → resolve ids → buffer → flush → query.
|
|
5
|
+
*
|
|
6
|
+
* Lifecycle:
|
|
7
|
+
* ingest(event) buffer-only, never blocks on I/O except dictionary
|
|
8
|
+
* ingestBatch(events) same, batched dictionary lookups across events
|
|
9
|
+
* flush() drain buffer → durable, then cache
|
|
10
|
+
* query(spec) read path (cache-first, durable fallback)
|
|
11
|
+
* shutdown() flush-on-close, stop timers
|
|
12
|
+
*
|
|
13
|
+
* Concurrency model:
|
|
14
|
+
* - Buffer mutations are synchronous in JS (single-threaded), so adds are safe.
|
|
15
|
+
* - At most one flush runs at a time (guarded by a promise lock). New deltas
|
|
16
|
+
* accumulate while a flush is in flight.
|
|
17
|
+
*/
|
|
18
|
+
import type { CountexConfig, CountexEvents, QueryResult, QuerySpec } from "../types/index.js";
|
|
19
|
+
export declare class Countex {
|
|
20
|
+
private readonly cfg;
|
|
21
|
+
private readonly buffer;
|
|
22
|
+
private readonly cardinality;
|
|
23
|
+
private readonly logger;
|
|
24
|
+
private readonly emitter;
|
|
25
|
+
private flushTimer;
|
|
26
|
+
private flushing;
|
|
27
|
+
private closed;
|
|
28
|
+
constructor(config: CountexConfig);
|
|
29
|
+
init(): Promise<void>;
|
|
30
|
+
shutdown(): Promise<void>;
|
|
31
|
+
on<K extends keyof CountexEvents>(event: K, listener: (payload: CountexEvents[K]) => void): this;
|
|
32
|
+
off<K extends keyof CountexEvents>(event: K, listener: (payload: CountexEvents[K]) => void): this;
|
|
33
|
+
private emit;
|
|
34
|
+
ingest(event: unknown): Promise<void>;
|
|
35
|
+
ingestBatch(events: unknown[]): Promise<void>;
|
|
36
|
+
/**
|
|
37
|
+
* Flush buffered deltas. At most one flush runs at a time; overlapping
|
|
38
|
+
* callers get the same in-flight promise.
|
|
39
|
+
*/
|
|
40
|
+
flush(): Promise<void>;
|
|
41
|
+
query(spec: QuerySpec): Promise<QueryResult>;
|
|
42
|
+
diag(): {
|
|
43
|
+
bufferCells: number;
|
|
44
|
+
bufferEvents: number;
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
/** Factory matching the spec's public API. */
|
|
48
|
+
export declare function createCountex(config: CountexConfig): Countex;
|
|
49
|
+
//# sourceMappingURL=countex.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version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AAC;IAMlD,IAAI,IAAI;QAAE,WAAW,EAAE,MAAM,CAAC;QAAC,YAAY,EAAE,MAAM,CAAA;KAAE;CAMtD;AAED,8CAA8C;AAC9C,wBAAgB,aAAa,CAAC,MAAM,EAAE,aAAa,GAAG,OAAO,CAE5D"}
|
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Countex client.
|
|
4
|
+
*
|
|
5
|
+
* Orchestrates: extract → resolve ids → buffer → flush → query.
|
|
6
|
+
*
|
|
7
|
+
* Lifecycle:
|
|
8
|
+
* ingest(event) buffer-only, never blocks on I/O except dictionary
|
|
9
|
+
* ingestBatch(events) same, batched dictionary lookups across events
|
|
10
|
+
* flush() drain buffer → durable, then cache
|
|
11
|
+
* query(spec) read path (cache-first, durable fallback)
|
|
12
|
+
* shutdown() flush-on-close, stop timers
|
|
13
|
+
*
|
|
14
|
+
* Concurrency model:
|
|
15
|
+
* - Buffer mutations are synchronous in JS (single-threaded), so adds are safe.
|
|
16
|
+
* - At most one flush runs at a time (guarded by a promise lock). New deltas
|
|
17
|
+
* accumulate while a flush is in flight.
|
|
18
|
+
*/
|
|
19
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
+
exports.Countex = void 0;
|
|
21
|
+
exports.createCountex = createCountex;
|
|
22
|
+
const node_events_1 = require("node:events");
|
|
23
|
+
const extraction_js_1 = require("./extraction.js");
|
|
24
|
+
const mapping_js_1 = require("./mapping.js");
|
|
25
|
+
const buffer_js_1 = require("./buffer.js");
|
|
26
|
+
const cardinality_js_1 = require("./cardinality.js");
|
|
27
|
+
const timeframe_js_1 = require("./timeframe.js");
|
|
28
|
+
const hash_js_1 = require("../utils/hash.js");
|
|
29
|
+
const logger_js_1 = require("../utils/logger.js");
|
|
30
|
+
const planner_js_1 = require("../query/planner.js");
|
|
31
|
+
class Countex {
|
|
32
|
+
cfg;
|
|
33
|
+
buffer = new buffer_js_1.DeltaBuffer();
|
|
34
|
+
cardinality;
|
|
35
|
+
logger;
|
|
36
|
+
emitter = new node_events_1.EventEmitter();
|
|
37
|
+
flushTimer = null;
|
|
38
|
+
flushing = null;
|
|
39
|
+
closed = false;
|
|
40
|
+
constructor(config) {
|
|
41
|
+
(0, mapping_js_1.validateMaps)(config.maps);
|
|
42
|
+
const mapsByCounterId = new Map();
|
|
43
|
+
for (const m of config.maps)
|
|
44
|
+
mapsByCounterId.set(m.counterId, m);
|
|
45
|
+
this.cfg = {
|
|
46
|
+
maps: mapsByCounterId,
|
|
47
|
+
durable: config.durable,
|
|
48
|
+
cache: config.cache,
|
|
49
|
+
flush: {
|
|
50
|
+
everyMs: config.flush?.everyMs ?? 60_000,
|
|
51
|
+
maxBufferedCells: config.flush?.maxBufferedCells ?? 100_000,
|
|
52
|
+
maxBufferedEvents: config.flush?.maxBufferedEvents ?? 50_000,
|
|
53
|
+
flushOnShutdown: config.flush?.flushOnShutdown ?? true,
|
|
54
|
+
onOverflow: config.flush?.onOverflow ?? "block",
|
|
55
|
+
},
|
|
56
|
+
defaultIdempotency: config.idempotency,
|
|
57
|
+
defaultCardinality: config.cardinality,
|
|
58
|
+
now: config.now ?? (() => new Date()),
|
|
59
|
+
};
|
|
60
|
+
this.cardinality = new cardinality_js_1.CardinalityGuard(config.cardinality);
|
|
61
|
+
this.logger = (0, logger_js_1.resolveLogger)(config.logger);
|
|
62
|
+
if (this.cfg.flush.everyMs > 0) {
|
|
63
|
+
this.flushTimer = setInterval(() => {
|
|
64
|
+
this.flush().catch((err) => {
|
|
65
|
+
this.logger.error("scheduled flush failed", { err: String(err) });
|
|
66
|
+
});
|
|
67
|
+
}, this.cfg.flush.everyMs);
|
|
68
|
+
// Don't keep the event loop alive for the timer alone.
|
|
69
|
+
this.flushTimer.unref?.();
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
// ── Lifecycle ─────────────────────────────────────────────────────────────
|
|
73
|
+
async init() {
|
|
74
|
+
await this.cfg.durable.init?.();
|
|
75
|
+
await this.cfg.cache?.init?.();
|
|
76
|
+
}
|
|
77
|
+
async shutdown() {
|
|
78
|
+
this.closed = true;
|
|
79
|
+
if (this.flushTimer) {
|
|
80
|
+
clearInterval(this.flushTimer);
|
|
81
|
+
this.flushTimer = null;
|
|
82
|
+
}
|
|
83
|
+
if (this.cfg.flush.flushOnShutdown) {
|
|
84
|
+
try {
|
|
85
|
+
await this.flush();
|
|
86
|
+
}
|
|
87
|
+
catch (err) {
|
|
88
|
+
this.logger.error("shutdown flush failed", { err: String(err) });
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
await this.cfg.durable.close?.();
|
|
92
|
+
await this.cfg.cache?.close?.();
|
|
93
|
+
}
|
|
94
|
+
// ── Events ────────────────────────────────────────────────────────────────
|
|
95
|
+
on(event, listener) {
|
|
96
|
+
this.emitter.on(event, listener);
|
|
97
|
+
return this;
|
|
98
|
+
}
|
|
99
|
+
off(event, listener) {
|
|
100
|
+
this.emitter.off(event, listener);
|
|
101
|
+
return this;
|
|
102
|
+
}
|
|
103
|
+
emit(event, payload) {
|
|
104
|
+
this.emitter.emit(event, payload);
|
|
105
|
+
}
|
|
106
|
+
// ── Ingestion ─────────────────────────────────────────────────────────────
|
|
107
|
+
async ingest(event) {
|
|
108
|
+
return this.ingestBatch([event]);
|
|
109
|
+
}
|
|
110
|
+
async ingestBatch(events) {
|
|
111
|
+
if (this.closed)
|
|
112
|
+
throw new Error("Countex is closed");
|
|
113
|
+
if (events.length === 0)
|
|
114
|
+
return;
|
|
115
|
+
const perMapRaws = [];
|
|
116
|
+
const dedupeChecks = [];
|
|
117
|
+
for (const ev of events) {
|
|
118
|
+
for (const map of this.cfg.maps.values()) {
|
|
119
|
+
const result = (0, extraction_js_1.extractIncrements)(map, ev);
|
|
120
|
+
for (const w of result.warnings) {
|
|
121
|
+
this.logger.debug("extraction warning", { ...w, counterId: map.counterId });
|
|
122
|
+
}
|
|
123
|
+
if (result.skipped || result.increments.length === 0)
|
|
124
|
+
continue;
|
|
125
|
+
const scope = result.increments[0].scopeId;
|
|
126
|
+
const idem = map.idempotency ?? this.cfg.defaultIdempotency;
|
|
127
|
+
const entry = {
|
|
128
|
+
counterId: map.counterId,
|
|
129
|
+
scopeId: scope,
|
|
130
|
+
eventId: result.eventId,
|
|
131
|
+
idemTtlSeconds: idem?.ttlSeconds,
|
|
132
|
+
increments: result.increments,
|
|
133
|
+
};
|
|
134
|
+
const idx = perMapRaws.length;
|
|
135
|
+
perMapRaws.push(entry);
|
|
136
|
+
if (idem?.enabled && result.eventId && this.cfg.cache?.markEventSeen) {
|
|
137
|
+
dedupeChecks.push({
|
|
138
|
+
index: idx,
|
|
139
|
+
scope,
|
|
140
|
+
// Namespace the dedupe key by counterId so different maps don't share it.
|
|
141
|
+
eventId: `${map.counterId}:${result.eventId}`,
|
|
142
|
+
ttl: idem.ttlSeconds ?? 86_400,
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
// Resolve all dedupe markers in parallel before anything enters the buffer.
|
|
148
|
+
if (dedupeChecks.length > 0) {
|
|
149
|
+
const results = await Promise.all(dedupeChecks.map((d) => this.cfg.cache.markEventSeen(d.scope, d.eventId, d.ttl)));
|
|
150
|
+
results.forEach((isNew, i) => {
|
|
151
|
+
if (!isNew) {
|
|
152
|
+
const d = dedupeChecks[i];
|
|
153
|
+
perMapRaws[d.index].increments = []; // drop just this map's increments for this event
|
|
154
|
+
this.emit("duplicate", { scopeId: d.scope, eventId: d.eventId });
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
const allRaws = perMapRaws.flatMap((p) => p.increments);
|
|
159
|
+
if (allRaws.length === 0)
|
|
160
|
+
return;
|
|
161
|
+
// Phase 2: cardinality protection — rewrite source-keys for overflow,
|
|
162
|
+
// drop where policy says drop.
|
|
163
|
+
const checked = [];
|
|
164
|
+
for (const r of allRaws) {
|
|
165
|
+
const subjResult = this.cardinality.check(r.scopeId, "subject", r.subjectType, r.subjectSourceKey, (w) => this.emit("cardinality_warning", w), (o) => this.emit("overflow", o));
|
|
166
|
+
if (subjResult === null) {
|
|
167
|
+
this.emit("dropped", { reason: "cardinality:subject" });
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
const metricResult = this.cardinality.check(r.scopeId, "metric", undefined, r.metricSourceKey, (w) => this.emit("cardinality_warning", w), (o) => this.emit("overflow", o));
|
|
171
|
+
if (metricResult === null) {
|
|
172
|
+
this.emit("dropped", { reason: "cardinality:metric" });
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
// Dimensions cardinality
|
|
176
|
+
let dropDueToDim = false;
|
|
177
|
+
const dimsCanonical = {};
|
|
178
|
+
for (const [k, v] of Object.entries(r.dimensions)) {
|
|
179
|
+
if (v === null || v === undefined) {
|
|
180
|
+
dimsCanonical[k] = null;
|
|
181
|
+
continue;
|
|
182
|
+
}
|
|
183
|
+
const sk = typeof v === "string" ? v : String(v);
|
|
184
|
+
const checkedVal = this.cardinality.check(r.scopeId, "dimensionValue", k, sk, (w) => this.emit("cardinality_warning", w), (o) => this.emit("overflow", o));
|
|
185
|
+
if (checkedVal === null) {
|
|
186
|
+
dropDueToDim = true;
|
|
187
|
+
break;
|
|
188
|
+
}
|
|
189
|
+
// Use the (possibly-overflowed) value; preserve numbers/booleans verbatim.
|
|
190
|
+
if (checkedVal !== sk) {
|
|
191
|
+
dimsCanonical[k] = checkedVal;
|
|
192
|
+
}
|
|
193
|
+
else {
|
|
194
|
+
dimsCanonical[k] = v;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
if (dropDueToDim) {
|
|
198
|
+
this.emit("dropped", { reason: "cardinality:dimension" });
|
|
199
|
+
continue;
|
|
200
|
+
}
|
|
201
|
+
r.dimensions = dimsCanonical;
|
|
202
|
+
// Stash mutated subject/metric source keys so the dictionary lookup uses overflow sentinels.
|
|
203
|
+
r.subjectSourceKey = subjResult;
|
|
204
|
+
r.metricSourceKey = metricResult;
|
|
205
|
+
checked.push(r);
|
|
206
|
+
}
|
|
207
|
+
if (checked.length === 0)
|
|
208
|
+
return;
|
|
209
|
+
// Phase 3: dictionary id resolution (batched).
|
|
210
|
+
const lookups = [];
|
|
211
|
+
const lookupKey = (scope, kind, q, sk) => `${scope}|${kind}|${q ?? ""}|${sk}`;
|
|
212
|
+
const seenLookups = new Set();
|
|
213
|
+
for (const r of checked) {
|
|
214
|
+
const sk = lookupKey(r.scopeId, "subject", r.subjectType, r.subjectSourceKey);
|
|
215
|
+
if (!seenLookups.has(sk)) {
|
|
216
|
+
seenLookups.add(sk);
|
|
217
|
+
lookups.push({ scopeId: r.scopeId, kind: "subject", qualifier: r.subjectType, sourceKey: r.subjectSourceKey });
|
|
218
|
+
}
|
|
219
|
+
const mk = lookupKey(r.scopeId, "metric", undefined, r.metricSourceKey);
|
|
220
|
+
if (!seenLookups.has(mk)) {
|
|
221
|
+
seenLookups.add(mk);
|
|
222
|
+
lookups.push({ scopeId: r.scopeId, kind: "metric", sourceKey: r.metricSourceKey });
|
|
223
|
+
}
|
|
224
|
+
// Dimension VALUES are stored as raw values in the cell (not via dict) — keeps cells self-describing
|
|
225
|
+
// and avoids dictionary churn for low-cardinality dimensions. Only subjects and metrics use the
|
|
226
|
+
// dictionary. (This is a deliberate simplification matching the spec's pragmatic intent.)
|
|
227
|
+
}
|
|
228
|
+
const ids = await this.cfg.durable.resolveIds(lookups);
|
|
229
|
+
// Phase 4: build cells and add to buffer. Multiple timeframes per increment.
|
|
230
|
+
for (const r of checked) {
|
|
231
|
+
const subjId = ids.get(lookupKey(r.scopeId, "subject", r.subjectType, r.subjectSourceKey));
|
|
232
|
+
const metricId = ids.get(lookupKey(r.scopeId, "metric", undefined, r.metricSourceKey));
|
|
233
|
+
if (subjId === undefined || metricId === undefined) {
|
|
234
|
+
this.logger.error("dictionary resolution returned no id", { r });
|
|
235
|
+
this.emit("dropped", { reason: "dict:missing" });
|
|
236
|
+
continue;
|
|
237
|
+
}
|
|
238
|
+
const dimsHash = (0, hash_js_1.dimensionsHash)(r.dimensions);
|
|
239
|
+
for (const tf of r.timeframes) {
|
|
240
|
+
const tfKey = (0, timeframe_js_1.bucketKey)(tf, r.eventTime);
|
|
241
|
+
this.buffer.add({
|
|
242
|
+
scopeId: r.scopeId,
|
|
243
|
+
counterId: r.counterId,
|
|
244
|
+
timeframeType: tf,
|
|
245
|
+
timeframeKey: tfKey,
|
|
246
|
+
subjectType: r.subjectType,
|
|
247
|
+
subjectId: subjId,
|
|
248
|
+
metricId,
|
|
249
|
+
dimensionsHash: dimsHash,
|
|
250
|
+
dimensions: r.dimensions,
|
|
251
|
+
}, r.delta);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
this.buffer.noteEvent();
|
|
255
|
+
// Phase 5: trigger or block on flush thresholds.
|
|
256
|
+
if (this.buffer.size() >= this.cfg.flush.maxBufferedCells ||
|
|
257
|
+
this.buffer.eventCount() >= this.cfg.flush.maxBufferedEvents) {
|
|
258
|
+
if (this.cfg.flush.onOverflow === "drop") {
|
|
259
|
+
// Signal but don't block.
|
|
260
|
+
this.flush().catch((e) => this.logger.error("flush failed", { err: String(e) }));
|
|
261
|
+
}
|
|
262
|
+
else {
|
|
263
|
+
await this.flush();
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
// ── Flush ─────────────────────────────────────────────────────────────────
|
|
268
|
+
/**
|
|
269
|
+
* Flush buffered deltas. At most one flush runs at a time; overlapping
|
|
270
|
+
* callers get the same in-flight promise.
|
|
271
|
+
*/
|
|
272
|
+
flush() {
|
|
273
|
+
if (this.flushing)
|
|
274
|
+
return this.flushing;
|
|
275
|
+
if (this.buffer.size() === 0)
|
|
276
|
+
return Promise.resolve();
|
|
277
|
+
const start = Date.now();
|
|
278
|
+
const snapshot = this.buffer.drain();
|
|
279
|
+
const cellCount = snapshot.length;
|
|
280
|
+
const p = (async () => {
|
|
281
|
+
try {
|
|
282
|
+
// Two-phase: durable first (source of truth), then cache.
|
|
283
|
+
await this.cfg.durable.applyDeltas(snapshot);
|
|
284
|
+
if (this.cfg.cache) {
|
|
285
|
+
try {
|
|
286
|
+
await this.cfg.cache.applyDeltas(snapshot);
|
|
287
|
+
}
|
|
288
|
+
catch (err) {
|
|
289
|
+
// Cache failure is non-fatal: durable has the truth, next reads will warm cache from durable.
|
|
290
|
+
this.logger.warn("cache flush failed (non-fatal)", { err: String(err) });
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
this.emit("flush", {
|
|
294
|
+
cells: cellCount,
|
|
295
|
+
events: 0, // we've already reset the counter via drain()
|
|
296
|
+
durationMs: Date.now() - start,
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
catch (err) {
|
|
300
|
+
// Durable failed — re-merge so we don't lose counts.
|
|
301
|
+
this.buffer.mergeBack(snapshot);
|
|
302
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
303
|
+
this.emit("flush_error", { error });
|
|
304
|
+
this.logger.error("durable flush failed; deltas re-merged", { err: error.message });
|
|
305
|
+
throw error;
|
|
306
|
+
}
|
|
307
|
+
finally {
|
|
308
|
+
this.flushing = null;
|
|
309
|
+
}
|
|
310
|
+
})();
|
|
311
|
+
this.flushing = p;
|
|
312
|
+
return p;
|
|
313
|
+
}
|
|
314
|
+
// ── Query ─────────────────────────────────────────────────────────────────
|
|
315
|
+
async query(spec) {
|
|
316
|
+
return (0, planner_js_1.executeQuery)(spec, this.cfg.durable, this.cfg.cache);
|
|
317
|
+
}
|
|
318
|
+
// ── Diagnostics ───────────────────────────────────────────────────────────
|
|
319
|
+
diag() {
|
|
320
|
+
return {
|
|
321
|
+
bufferCells: this.buffer.size(),
|
|
322
|
+
bufferEvents: this.buffer.eventCount(),
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
exports.Countex = Countex;
|
|
327
|
+
/** Factory matching the spec's public API. */
|
|
328
|
+
function createCountex(config) {
|
|
329
|
+
return new Countex(config);
|
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IACX,CAAC;IAED,6EAA6E;IAE7E,KAAK,CAAC,KAAK,CAAC,IAAe;QACzB,OAAO,IAAA,yBAAY,EAAC,IAAI,EAAE,IAAI,CAAC,GAAG,CAAC,OAAO,EAAE,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IAC9D,CAAC;IAED,6EAA6E;IAE7E,IAAI;QACF,OAAO;YACL,WAAW,EAAE,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE;YAC/B,YAAY,EAAE,IAAI,CAAC,MAAM,CAAC,UAAU,EAAE;SACvC,CAAC;IACJ,CAAC;CACF;AAxWD,0BAwWC;AAED,8CAA8C;AAC9C,SAAgB,aAAa,CAAC,MAAqB;IACjD,OAAO,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;AAC7B,CAAC"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Extraction.
|
|
3
|
+
*
|
|
4
|
+
* Given a CounterMap and a JSON event, produce a list of "raw increments" — each
|
|
5
|
+
* raw increment carries the source-keys (not yet resolved to internal ids), the
|
|
6
|
+
* dimensions, the delta, and the event time. The dictionary stage then resolves
|
|
7
|
+
* source-keys → internal numeric ids, and the buffer stage groups by cell.
|
|
8
|
+
*/
|
|
9
|
+
import type { CounterMap, Dimensions, TimeframeType } from "../types/index.js";
|
|
10
|
+
export interface RawIncrement {
|
|
11
|
+
scopeId: string;
|
|
12
|
+
counterId: string;
|
|
13
|
+
eventTime: Date;
|
|
14
|
+
/** Source-key for the subject; resolved into numeric id later. */
|
|
15
|
+
subjectType: string;
|
|
16
|
+
subjectSourceKey: string;
|
|
17
|
+
/** Source-key for the metric. */
|
|
18
|
+
metricSourceKey: string;
|
|
19
|
+
/** Already-normalized dimension values, ready for hashing. */
|
|
20
|
+
dimensions: Dimensions;
|
|
21
|
+
delta: number;
|
|
22
|
+
timeframes: TimeframeType[];
|
|
23
|
+
/** Optional event id for idempotency. */
|
|
24
|
+
eventId?: string;
|
|
25
|
+
}
|
|
26
|
+
export interface ExtractionWarning {
|
|
27
|
+
reason: string;
|
|
28
|
+
meta?: Record<string, unknown>;
|
|
29
|
+
}
|
|
30
|
+
export interface ExtractionResult {
|
|
31
|
+
increments: RawIncrement[];
|
|
32
|
+
warnings: ExtractionWarning[];
|
|
33
|
+
/** True when a required field was missing and the whole event must be skipped. */
|
|
34
|
+
skipped: boolean;
|
|
35
|
+
/** When the map declares idempotency, this is the resolved event id (or undefined when missing). */
|
|
36
|
+
eventId?: string;
|
|
37
|
+
}
|
|
38
|
+
export declare function extractIncrements(map: CounterMap, event: unknown): ExtractionResult;
|
|
39
|
+
//# sourceMappingURL=extraction.d.ts.map
|