@camstack/server 1.2.751 → 1.2.753
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/CHANGELOG.md +16 -0
- package/dist/api/trpc/generated-cap-routers.js +10 -0
- package/dist/core/moleculer/moleculer.service.js +13 -1
- package/package.json +11 -8
- package/system/dist/{agent-https-Oc0Go3Gs.js → agent-https-B0bjQVfy.js} +1 -1
- package/system/dist/{agent-https-DoGV9jKg.mjs → agent-https-cxghRfum.mjs} +1 -1
- package/system/dist/builtins/addon-pages-aggregator/addon-pages-aggregator.addon.js +1 -1
- package/system/dist/builtins/addon-pages-aggregator/addon-pages-aggregator.addon.mjs +1 -1
- package/system/dist/builtins/addon-widgets-aggregator/addon-widgets-aggregator.addon.js +1 -1
- package/system/dist/builtins/addon-widgets-aggregator/addon-widgets-aggregator.addon.mjs +1 -1
- package/system/dist/builtins/alerts/alerts.addon.js +1 -1
- package/system/dist/builtins/alerts/alerts.addon.mjs +1 -1
- package/system/dist/builtins/autotrack/index.js +1 -1
- package/system/dist/builtins/autotrack/index.mjs +1 -1
- package/system/dist/builtins/backup-orchestrator/backup-orchestrator.addon.js +2 -2
- package/system/dist/builtins/backup-orchestrator/backup-orchestrator.addon.mjs +2 -2
- package/system/dist/builtins/calendars/calendars.addon.js +1 -1
- package/system/dist/builtins/calendars/calendars.addon.mjs +1 -1
- package/system/dist/builtins/camera-grid/index.js +1 -1
- package/system/dist/builtins/camera-grid/index.mjs +1 -1
- package/system/dist/builtins/composer/composer.addon.js +18 -22
- package/system/dist/builtins/composer/composer.addon.mjs +17 -21
- package/system/dist/builtins/console-logging/index.js +1 -1
- package/system/dist/builtins/console-logging/index.mjs +1 -1
- package/system/dist/builtins/core-blocks/composition-api.d.ts +4 -1
- package/system/dist/builtins/core-blocks/core-blocks.addon.js +232 -15
- package/system/dist/builtins/core-blocks/core-blocks.addon.mjs +232 -15
- package/system/dist/builtins/core-blocks/running-proposal.d.ts +32 -0
- package/system/dist/builtins/device-history/device-history.addon.js +24 -23
- package/system/dist/builtins/device-history/device-history.addon.mjs +3 -2
- package/system/dist/builtins/device-manager/device-manager.addon.js +1 -1
- package/system/dist/builtins/device-manager/device-manager.addon.mjs +1 -1
- package/system/dist/builtins/hub-forwarder/index.js +1 -1
- package/system/dist/builtins/hub-forwarder/index.mjs +1 -1
- package/system/dist/builtins/keychain/index.js +3 -3
- package/system/dist/builtins/keychain/index.mjs +3 -3
- package/system/dist/builtins/liveness-monitor/liveness-monitor.addon.js +1 -1
- package/system/dist/builtins/liveness-monitor/liveness-monitor.addon.mjs +1 -1
- package/system/dist/builtins/local-auth/local-auth.addon.js +2 -2
- package/system/dist/builtins/local-auth/local-auth.addon.mjs +2 -2
- package/system/dist/builtins/local-network/local-network.addon.js +2 -2
- package/system/dist/builtins/local-network/local-network.addon.mjs +2 -2
- package/system/dist/builtins/loki-logging/index.js +1 -1
- package/system/dist/builtins/loki-logging/index.mjs +1 -1
- package/system/dist/builtins/native-metrics/native-metrics.addon.js +1 -1
- package/system/dist/builtins/native-metrics/native-metrics.addon.mjs +1 -1
- package/system/dist/builtins/platform-probe/index.js +1 -1
- package/system/dist/builtins/platform-probe/index.mjs +1 -1
- package/system/dist/builtins/record-entries/index.js +1 -1
- package/system/dist/builtins/record-entries/index.mjs +1 -1
- package/system/dist/builtins/remote-access-orchestrator/remote-access-orchestrator.addon.js +1 -1
- package/system/dist/builtins/remote-access-orchestrator/remote-access-orchestrator.addon.mjs +1 -1
- package/system/dist/builtins/snapshot/index.js +5 -1
- package/system/dist/builtins/snapshot/index.mjs +5 -1
- package/system/dist/builtins/sqlite-storage/filesystem-storage.addon.js +1 -1
- package/system/dist/builtins/sqlite-storage/filesystem-storage.addon.mjs +1 -1
- package/system/dist/builtins/sqlite-storage/index.js +1 -1
- package/system/dist/builtins/sqlite-storage/index.mjs +1 -1
- package/system/dist/builtins/sqlite-storage/sqlite-settings.addon.js +2 -2
- package/system/dist/builtins/sqlite-storage/sqlite-settings.addon.mjs +2 -2
- package/system/dist/builtins/storage-orchestrator/storage-orchestrator.addon.js +1 -1
- package/system/dist/builtins/storage-orchestrator/storage-orchestrator.addon.mjs +1 -1
- package/system/dist/builtins/system-config/system-config.addon.js +3 -3
- package/system/dist/builtins/system-config/system-config.addon.mjs +284 -284
- package/system/dist/builtins/winston-logging/index.js +1 -1
- package/system/dist/builtins/winston-logging/index.mjs +1 -1
- package/system/dist/{retention-C-lijsmN.mjs → capture-engine-CfJdhMTH.mjs} +2 -246
- package/system/dist/{retention-WjI-Op7G.js → capture-engine-eRAebXP1.js} +1 -305
- package/system/dist/{composition-sources-Bxahhz4f.js → composition-sources-BvDYLpw_.js} +1 -1
- package/system/dist/{composition-sources-CzRiSzCV.mjs → composition-sources-ChtkMq1Q.mjs} +1 -1
- package/system/dist/{constants-uI6uXyjP.mjs → constants-CYNsr0lM.mjs} +1 -1
- package/system/dist/{constants-0rC5QY6i.js → constants-DkC9-VOa.js} +1 -1
- package/system/dist/{device-ca-CL_Arugd.js → device-ca-BxInMpK2.js} +1 -1
- package/system/dist/{device-ca-D7TyMlsS.mjs → device-ca-CQg3PTnE.mjs} +1 -1
- package/system/dist/{dist-Iq3vrBOE.mjs → dist-C5eIFTMo.mjs} +752 -554
- package/system/dist/{dist-CYnRCUzN.js → dist-DFWnAzAY.js} +775 -553
- package/system/dist/file-host-worker.js +1 -1
- package/system/dist/file-host-worker.mjs +1 -1
- package/system/dist/{hub-mdns-ciao-CNLYSVyJ.js → hub-mdns-ciao-BQDdPkPO.js} +2 -2
- package/system/dist/{hub-mdns-ciao-CvMKderb.mjs → hub-mdns-ciao-DW_g42El.mjs} +2 -2
- package/system/dist/index.js +9 -8
- package/system/dist/index.mjs +9 -8
- package/system/dist/kernel/transport/parent-unowned-call.d.ts +10 -0
- package/system/dist/{restore-archive-vzWYLf6p.mjs → restore-archive-CFGGJCfV.mjs} +82 -82
- package/system/dist/{restore-archive-Dw04PiG5.js → restore-archive-DRjYgh6D.js} +1 -1
- package/system/dist/retention-B9l9TnGE.mjs +245 -0
- package/system/dist/retention-BNONb5X_.js +304 -0
- package/system/dist/{wal-maintenance-DQXiTaph.js → wal-maintenance-D0zSmIwJ.js} +0 -0
- package/system/dist/{wal-maintenance-BzTHorT2.mjs → wal-maintenance-xYk-FpJW.mjs} +0 -0
- package/system/package.json +1 -1
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
//#region src/builtins/device-history/time/bucket-time.ts
|
|
2
|
+
/**
|
|
3
|
+
* Bucket arithmetic for the three rollup tiers (D850, plan §4/§5.5).
|
|
4
|
+
*
|
|
5
|
+
* 5m and 1h buckets are aligned to UTC; a 1d bucket starts at HUB-LOCAL
|
|
6
|
+
* midnight, so a day is 23, 24 or 25 hours long across a DST change (operator
|
|
7
|
+
* answer Q4). Pure: the zone is the process zone (`TZ`), nothing is read from
|
|
8
|
+
* the environment here.
|
|
9
|
+
*/
|
|
10
|
+
var FIVE_MINUTES_MS = 5 * 6e4;
|
|
11
|
+
var HOUR_MS = 60 * 6e4;
|
|
12
|
+
var DAY_MS = 24 * HOUR_MS;
|
|
13
|
+
function floorTo(ts, widthMs) {
|
|
14
|
+
return Math.floor(ts / widthMs) * widthMs;
|
|
15
|
+
}
|
|
16
|
+
/** Hub-local midnight at or before `ts`. */
|
|
17
|
+
function startOfLocalDay(ts) {
|
|
18
|
+
const d = new Date(ts);
|
|
19
|
+
d.setHours(0, 0, 0, 0);
|
|
20
|
+
return d.getTime();
|
|
21
|
+
}
|
|
22
|
+
/** The local midnight following the local midnight `dayStart`. */
|
|
23
|
+
function nextLocalDayStart(dayStart) {
|
|
24
|
+
const d = new Date(dayStart);
|
|
25
|
+
d.setDate(d.getDate() + 1);
|
|
26
|
+
d.setHours(0, 0, 0, 0);
|
|
27
|
+
return d.getTime();
|
|
28
|
+
}
|
|
29
|
+
/** Start of the `tier` bucket containing `ts`. */
|
|
30
|
+
function bucketStartOf(tier, ts) {
|
|
31
|
+
if (tier === "5m") return floorTo(ts, FIVE_MINUTES_MS);
|
|
32
|
+
if (tier === "1h") return floorTo(ts, HOUR_MS);
|
|
33
|
+
return startOfLocalDay(ts);
|
|
34
|
+
}
|
|
35
|
+
/** Exclusive end of the `tier` bucket that starts at `start`. */
|
|
36
|
+
function bucketEndOf(tier, start) {
|
|
37
|
+
if (tier === "5m") return start + FIVE_MINUTES_MS;
|
|
38
|
+
if (tier === "1h") return start + HOUR_MS;
|
|
39
|
+
return nextLocalDayStart(start);
|
|
40
|
+
}
|
|
41
|
+
/** Quiet time after a bucket closes before it is rolled up (late writers). */
|
|
42
|
+
var ROLLUP_GRACE_MS = {
|
|
43
|
+
"5m": 3e4,
|
|
44
|
+
"1h": 5 * 6e4,
|
|
45
|
+
"1d": 15 * 6e4
|
|
46
|
+
};
|
|
47
|
+
//#endregion
|
|
48
|
+
//#region src/builtins/device-history/retention/departure-tracker.ts
|
|
49
|
+
var DEPARTED_ABSENCE_MS = 7 * DAY_MS;
|
|
50
|
+
var DepartureTracker = class {
|
|
51
|
+
store;
|
|
52
|
+
registry;
|
|
53
|
+
logger;
|
|
54
|
+
missing = /* @__PURE__ */ new Map();
|
|
55
|
+
constructor(store, registry, logger) {
|
|
56
|
+
this.store = store;
|
|
57
|
+
this.registry = registry;
|
|
58
|
+
this.logger = logger;
|
|
59
|
+
}
|
|
60
|
+
async load() {
|
|
61
|
+
this.missing = await this.store.loadDepartures();
|
|
62
|
+
}
|
|
63
|
+
missingSince(deviceId) {
|
|
64
|
+
return this.missing.get(deviceId);
|
|
65
|
+
}
|
|
66
|
+
/** Forget a device's clock — its series were removed because it was DELETED (D865). */
|
|
67
|
+
async forget(deviceId) {
|
|
68
|
+
await this.store.clearMissing([deviceId]);
|
|
69
|
+
const next = new Map(this.missing);
|
|
70
|
+
next.delete(deviceId);
|
|
71
|
+
this.missing = next;
|
|
72
|
+
}
|
|
73
|
+
/** Feed one SUCCESSFUL listing. A failed read never reaches here. */
|
|
74
|
+
async observe(listed, now) {
|
|
75
|
+
const withSeries = new Set(this.registry.all().flatMap((s) => s.deviceId === null ? [] : [s.deviceId]));
|
|
76
|
+
const back = [...this.missing.keys()].filter((id) => listed.has(id));
|
|
77
|
+
const gone = [...withSeries].filter((id) => !listed.has(id) && !this.missing.has(id));
|
|
78
|
+
try {
|
|
79
|
+
await this.store.clearMissing(back);
|
|
80
|
+
await this.store.markMissing(gone, now);
|
|
81
|
+
} catch (err) {
|
|
82
|
+
this.logger.warn("device-history: departure clock not written", { meta: { error: err instanceof Error ? err.message : String(err) } });
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
const next = new Map(this.missing);
|
|
86
|
+
for (const id of back) next.delete(id);
|
|
87
|
+
for (const id of gone) next.set(id, now);
|
|
88
|
+
this.missing = next;
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
//#endregion
|
|
92
|
+
//#region src/builtins/device-history/retention/retention.ts
|
|
93
|
+
var DEFAULT_RETENTION = {
|
|
94
|
+
rawDays: 7,
|
|
95
|
+
fiveMinuteDays: 30,
|
|
96
|
+
hourDays: 365,
|
|
97
|
+
dayDays: null
|
|
98
|
+
};
|
|
99
|
+
/** Cutoff per tier: rows strictly older are deleted. `null` = keep forever. */
|
|
100
|
+
function retentionCutoffs(policy, now) {
|
|
101
|
+
return {
|
|
102
|
+
raw: now - policy.rawDays * DAY_MS,
|
|
103
|
+
"5m": now - policy.fiveMinuteDays * DAY_MS,
|
|
104
|
+
"1h": now - policy.hourDays * DAY_MS,
|
|
105
|
+
"1d": policy.dayDays === null || policy.dayDays <= 0 ? null : now - policy.dayDays * DAY_MS
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
var TIERS = [
|
|
109
|
+
"raw",
|
|
110
|
+
"5m",
|
|
111
|
+
"1h",
|
|
112
|
+
"1d"
|
|
113
|
+
];
|
|
114
|
+
var RETENTION_STEP_MS = {
|
|
115
|
+
raw: HOUR_MS,
|
|
116
|
+
"5m": HOUR_MS,
|
|
117
|
+
"1h": DAY_MS,
|
|
118
|
+
"1d": DAY_MS
|
|
119
|
+
};
|
|
120
|
+
var MAX_PURGES_PER_RUN = 20;
|
|
121
|
+
/** Width of one delete statement's time range, per tier. */
|
|
122
|
+
var SLICE_MS = {
|
|
123
|
+
raw: DAY_MS,
|
|
124
|
+
"5m": DAY_MS,
|
|
125
|
+
"1h": 30 * DAY_MS,
|
|
126
|
+
"1d": 30 * DAY_MS
|
|
127
|
+
};
|
|
128
|
+
/** A bucket's start may precede the series' first point by up to a day. */
|
|
129
|
+
var BUCKET_LEAD_MS = 2 * DAY_MS;
|
|
130
|
+
/** The short tiers hold nothing older than this unless the operator configured more. */
|
|
131
|
+
var SHORT_TIER_LOOKBACK_MS = 400 * DAY_MS;
|
|
132
|
+
var yieldToEventLoop = () => new Promise((resolve) => setImmediate(resolve));
|
|
133
|
+
var RetentionJob = class {
|
|
134
|
+
deps;
|
|
135
|
+
appliedUpTo = /* @__PURE__ */ new Map();
|
|
136
|
+
running = null;
|
|
137
|
+
constructor(deps) {
|
|
138
|
+
this.deps = deps;
|
|
139
|
+
}
|
|
140
|
+
run(policy, now) {
|
|
141
|
+
if (this.running !== null) return this.running;
|
|
142
|
+
const run = this.doRun(policy, now).finally(() => {
|
|
143
|
+
this.running = null;
|
|
144
|
+
});
|
|
145
|
+
this.running = run;
|
|
146
|
+
return run;
|
|
147
|
+
}
|
|
148
|
+
async doRun(policy, now) {
|
|
149
|
+
const { registry, counters, logger } = this.deps;
|
|
150
|
+
const cutoffs = retentionCutoffs(policy, now);
|
|
151
|
+
try {
|
|
152
|
+
for (const tier of TIERS) {
|
|
153
|
+
const cutoff = cutoffs[tier];
|
|
154
|
+
if (cutoff === null) continue;
|
|
155
|
+
for (const series of registry.all()) {
|
|
156
|
+
if (series.firstTs - 864e5 >= cutoff) continue;
|
|
157
|
+
if (tier === "raw" && registry.metric(series.key)?.ingest === "buckets") continue;
|
|
158
|
+
const marker = `${tier}:${series.id}`;
|
|
159
|
+
const applied = this.appliedUpTo.get(marker);
|
|
160
|
+
if (applied !== void 0 && cutoff - applied < RETENTION_STEP_MS[tier]) continue;
|
|
161
|
+
const lo = Math.max(0, applied === void 0 ? series.firstTs - BUCKET_LEAD_MS : applied - RETENTION_STEP_MS[tier]);
|
|
162
|
+
counters.addDeleted(tier, await this.deleteSliced(tier, series.id, lo, cutoff - 1));
|
|
163
|
+
this.appliedUpTo.set(marker, cutoff);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
await this.purgeDeparted(now);
|
|
167
|
+
counters.retentionLastRunAt = now;
|
|
168
|
+
} catch (err) {
|
|
169
|
+
logger.warn("device-history: retention pass failed — retried at the next run", { meta: { error: err instanceof Error ? err.message : String(err) } });
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
/** Delete `[lo, hiInclusive]` of one tier in time slices, yielding between them. */
|
|
173
|
+
/** Drop the visit markers of a purged series. */
|
|
174
|
+
forgetSeries(seriesId) {
|
|
175
|
+
for (const tier of TIERS) this.appliedUpTo.delete(`${tier}:${seriesId}`);
|
|
176
|
+
}
|
|
177
|
+
async deleteSliced(tier, seriesId, lo, hiInclusive) {
|
|
178
|
+
const yieldNow = this.deps.yieldNow ?? yieldToEventLoop;
|
|
179
|
+
let deleted = 0;
|
|
180
|
+
for (let from = lo; from <= hiInclusive; from += SLICE_MS[tier]) {
|
|
181
|
+
const to = Math.min(from + SLICE_MS[tier] - 1, hiInclusive);
|
|
182
|
+
deleted += await this.deps.store.deleteRange(tier, seriesId, from, to);
|
|
183
|
+
await yieldNow();
|
|
184
|
+
}
|
|
185
|
+
return deleted;
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Tier by tier in slices, then the series row (and whatever the slices missed).
|
|
189
|
+
* Answers the rows the slices deleted per tier. The caller removes the series
|
|
190
|
+
* from the registry, which releases its per-series state.
|
|
191
|
+
*/
|
|
192
|
+
async purgeSeries(series, now) {
|
|
193
|
+
const deleted = {
|
|
194
|
+
raw: 0,
|
|
195
|
+
"5m": 0,
|
|
196
|
+
"1h": 0,
|
|
197
|
+
"1d": 0
|
|
198
|
+
};
|
|
199
|
+
for (const tier of TIERS) {
|
|
200
|
+
const lo = tier === "raw" || tier === "5m" ? Math.max(series.firstTs - BUCKET_LEAD_MS, now - SHORT_TIER_LOOKBACK_MS) : series.firstTs - BUCKET_LEAD_MS;
|
|
201
|
+
deleted[tier] = await this.deleteSliced(tier, series.id, Math.max(0, lo), now + DAY_MS);
|
|
202
|
+
this.deps.counters.addDeleted(tier, deleted[tier]);
|
|
203
|
+
}
|
|
204
|
+
await this.deps.store.deleteSeries(series.id);
|
|
205
|
+
return deleted;
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Delete every series of a DELETED device now (D865). Driven by a tombstone
|
|
209
|
+
* through the `device-removal-hook`, never by a listing: the week-long
|
|
210
|
+
* absence clock above remains the backstop for a device removed before
|
|
211
|
+
* tombstones existed.
|
|
212
|
+
*/
|
|
213
|
+
async purgeDevice(deviceId, now) {
|
|
214
|
+
const { registry, counters } = this.deps;
|
|
215
|
+
let purged = 0;
|
|
216
|
+
for (const series of registry.all()) {
|
|
217
|
+
if (series.deviceId !== deviceId) continue;
|
|
218
|
+
await this.purgeSeries(series, now);
|
|
219
|
+
registry.remove(series.id);
|
|
220
|
+
counters.purgedSeries += 1;
|
|
221
|
+
purged += 1;
|
|
222
|
+
}
|
|
223
|
+
return purged;
|
|
224
|
+
}
|
|
225
|
+
async purgeDeparted(now) {
|
|
226
|
+
const { registry, departures, counters, logger } = this.deps;
|
|
227
|
+
let purged = 0;
|
|
228
|
+
for (const series of registry.all()) {
|
|
229
|
+
if (purged >= MAX_PURGES_PER_RUN) break;
|
|
230
|
+
if (series.deviceId === null) continue;
|
|
231
|
+
const since = departures.missingSince(series.deviceId);
|
|
232
|
+
if (since === void 0 || now - since < DEPARTED_ABSENCE_MS) continue;
|
|
233
|
+
await this.purgeSeries(series, now);
|
|
234
|
+
registry.remove(series.id);
|
|
235
|
+
counters.purgedSeries += 1;
|
|
236
|
+
purged += 1;
|
|
237
|
+
logger.info("device-history: purged the series of an unregistered device", {
|
|
238
|
+
tags: { deviceId: series.deviceId },
|
|
239
|
+
meta: { key: series.key }
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
};
|
|
244
|
+
//#endregion
|
|
245
|
+
export { FIVE_MINUTES_MS as a, bucketEndOf as c, DAY_MS as i, bucketStartOf as l, RetentionJob as n, HOUR_MS as o, DepartureTracker as r, ROLLUP_GRACE_MS as s, DEFAULT_RETENTION as t, floorTo as u };
|
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
//#region src/builtins/device-history/time/bucket-time.ts
|
|
2
|
+
/**
|
|
3
|
+
* Bucket arithmetic for the three rollup tiers (D850, plan §4/§5.5).
|
|
4
|
+
*
|
|
5
|
+
* 5m and 1h buckets are aligned to UTC; a 1d bucket starts at HUB-LOCAL
|
|
6
|
+
* midnight, so a day is 23, 24 or 25 hours long across a DST change (operator
|
|
7
|
+
* answer Q4). Pure: the zone is the process zone (`TZ`), nothing is read from
|
|
8
|
+
* the environment here.
|
|
9
|
+
*/
|
|
10
|
+
var FIVE_MINUTES_MS = 5 * 6e4;
|
|
11
|
+
var HOUR_MS = 60 * 6e4;
|
|
12
|
+
var DAY_MS = 24 * HOUR_MS;
|
|
13
|
+
function floorTo(ts, widthMs) {
|
|
14
|
+
return Math.floor(ts / widthMs) * widthMs;
|
|
15
|
+
}
|
|
16
|
+
/** Hub-local midnight at or before `ts`. */
|
|
17
|
+
function startOfLocalDay(ts) {
|
|
18
|
+
const d = new Date(ts);
|
|
19
|
+
d.setHours(0, 0, 0, 0);
|
|
20
|
+
return d.getTime();
|
|
21
|
+
}
|
|
22
|
+
/** The local midnight following the local midnight `dayStart`. */
|
|
23
|
+
function nextLocalDayStart(dayStart) {
|
|
24
|
+
const d = new Date(dayStart);
|
|
25
|
+
d.setDate(d.getDate() + 1);
|
|
26
|
+
d.setHours(0, 0, 0, 0);
|
|
27
|
+
return d.getTime();
|
|
28
|
+
}
|
|
29
|
+
/** Start of the `tier` bucket containing `ts`. */
|
|
30
|
+
function bucketStartOf(tier, ts) {
|
|
31
|
+
if (tier === "5m") return floorTo(ts, FIVE_MINUTES_MS);
|
|
32
|
+
if (tier === "1h") return floorTo(ts, HOUR_MS);
|
|
33
|
+
return startOfLocalDay(ts);
|
|
34
|
+
}
|
|
35
|
+
/** Exclusive end of the `tier` bucket that starts at `start`. */
|
|
36
|
+
function bucketEndOf(tier, start) {
|
|
37
|
+
if (tier === "5m") return start + FIVE_MINUTES_MS;
|
|
38
|
+
if (tier === "1h") return start + HOUR_MS;
|
|
39
|
+
return nextLocalDayStart(start);
|
|
40
|
+
}
|
|
41
|
+
/** Quiet time after a bucket closes before it is rolled up (late writers). */
|
|
42
|
+
var ROLLUP_GRACE_MS = {
|
|
43
|
+
"5m": 3e4,
|
|
44
|
+
"1h": 5 * 6e4,
|
|
45
|
+
"1d": 15 * 6e4
|
|
46
|
+
};
|
|
47
|
+
//#endregion
|
|
48
|
+
//#region src/builtins/device-history/retention/departure-tracker.ts
|
|
49
|
+
var DEPARTED_ABSENCE_MS = 7 * DAY_MS;
|
|
50
|
+
var DepartureTracker = class {
|
|
51
|
+
store;
|
|
52
|
+
registry;
|
|
53
|
+
logger;
|
|
54
|
+
missing = /* @__PURE__ */ new Map();
|
|
55
|
+
constructor(store, registry, logger) {
|
|
56
|
+
this.store = store;
|
|
57
|
+
this.registry = registry;
|
|
58
|
+
this.logger = logger;
|
|
59
|
+
}
|
|
60
|
+
async load() {
|
|
61
|
+
this.missing = await this.store.loadDepartures();
|
|
62
|
+
}
|
|
63
|
+
missingSince(deviceId) {
|
|
64
|
+
return this.missing.get(deviceId);
|
|
65
|
+
}
|
|
66
|
+
/** Forget a device's clock — its series were removed because it was DELETED (D865). */
|
|
67
|
+
async forget(deviceId) {
|
|
68
|
+
await this.store.clearMissing([deviceId]);
|
|
69
|
+
const next = new Map(this.missing);
|
|
70
|
+
next.delete(deviceId);
|
|
71
|
+
this.missing = next;
|
|
72
|
+
}
|
|
73
|
+
/** Feed one SUCCESSFUL listing. A failed read never reaches here. */
|
|
74
|
+
async observe(listed, now) {
|
|
75
|
+
const withSeries = new Set(this.registry.all().flatMap((s) => s.deviceId === null ? [] : [s.deviceId]));
|
|
76
|
+
const back = [...this.missing.keys()].filter((id) => listed.has(id));
|
|
77
|
+
const gone = [...withSeries].filter((id) => !listed.has(id) && !this.missing.has(id));
|
|
78
|
+
try {
|
|
79
|
+
await this.store.clearMissing(back);
|
|
80
|
+
await this.store.markMissing(gone, now);
|
|
81
|
+
} catch (err) {
|
|
82
|
+
this.logger.warn("device-history: departure clock not written", { meta: { error: err instanceof Error ? err.message : String(err) } });
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
const next = new Map(this.missing);
|
|
86
|
+
for (const id of back) next.delete(id);
|
|
87
|
+
for (const id of gone) next.set(id, now);
|
|
88
|
+
this.missing = next;
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
//#endregion
|
|
92
|
+
//#region src/builtins/device-history/retention/retention.ts
|
|
93
|
+
var DEFAULT_RETENTION = {
|
|
94
|
+
rawDays: 7,
|
|
95
|
+
fiveMinuteDays: 30,
|
|
96
|
+
hourDays: 365,
|
|
97
|
+
dayDays: null
|
|
98
|
+
};
|
|
99
|
+
/** Cutoff per tier: rows strictly older are deleted. `null` = keep forever. */
|
|
100
|
+
function retentionCutoffs(policy, now) {
|
|
101
|
+
return {
|
|
102
|
+
raw: now - policy.rawDays * DAY_MS,
|
|
103
|
+
"5m": now - policy.fiveMinuteDays * DAY_MS,
|
|
104
|
+
"1h": now - policy.hourDays * DAY_MS,
|
|
105
|
+
"1d": policy.dayDays === null || policy.dayDays <= 0 ? null : now - policy.dayDays * DAY_MS
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
var TIERS = [
|
|
109
|
+
"raw",
|
|
110
|
+
"5m",
|
|
111
|
+
"1h",
|
|
112
|
+
"1d"
|
|
113
|
+
];
|
|
114
|
+
var RETENTION_STEP_MS = {
|
|
115
|
+
raw: HOUR_MS,
|
|
116
|
+
"5m": HOUR_MS,
|
|
117
|
+
"1h": DAY_MS,
|
|
118
|
+
"1d": DAY_MS
|
|
119
|
+
};
|
|
120
|
+
var MAX_PURGES_PER_RUN = 20;
|
|
121
|
+
/** Width of one delete statement's time range, per tier. */
|
|
122
|
+
var SLICE_MS = {
|
|
123
|
+
raw: DAY_MS,
|
|
124
|
+
"5m": DAY_MS,
|
|
125
|
+
"1h": 30 * DAY_MS,
|
|
126
|
+
"1d": 30 * DAY_MS
|
|
127
|
+
};
|
|
128
|
+
/** A bucket's start may precede the series' first point by up to a day. */
|
|
129
|
+
var BUCKET_LEAD_MS = 2 * DAY_MS;
|
|
130
|
+
/** The short tiers hold nothing older than this unless the operator configured more. */
|
|
131
|
+
var SHORT_TIER_LOOKBACK_MS = 400 * DAY_MS;
|
|
132
|
+
var yieldToEventLoop = () => new Promise((resolve) => setImmediate(resolve));
|
|
133
|
+
var RetentionJob = class {
|
|
134
|
+
deps;
|
|
135
|
+
appliedUpTo = /* @__PURE__ */ new Map();
|
|
136
|
+
running = null;
|
|
137
|
+
constructor(deps) {
|
|
138
|
+
this.deps = deps;
|
|
139
|
+
}
|
|
140
|
+
run(policy, now) {
|
|
141
|
+
if (this.running !== null) return this.running;
|
|
142
|
+
const run = this.doRun(policy, now).finally(() => {
|
|
143
|
+
this.running = null;
|
|
144
|
+
});
|
|
145
|
+
this.running = run;
|
|
146
|
+
return run;
|
|
147
|
+
}
|
|
148
|
+
async doRun(policy, now) {
|
|
149
|
+
const { registry, counters, logger } = this.deps;
|
|
150
|
+
const cutoffs = retentionCutoffs(policy, now);
|
|
151
|
+
try {
|
|
152
|
+
for (const tier of TIERS) {
|
|
153
|
+
const cutoff = cutoffs[tier];
|
|
154
|
+
if (cutoff === null) continue;
|
|
155
|
+
for (const series of registry.all()) {
|
|
156
|
+
if (series.firstTs - 864e5 >= cutoff) continue;
|
|
157
|
+
if (tier === "raw" && registry.metric(series.key)?.ingest === "buckets") continue;
|
|
158
|
+
const marker = `${tier}:${series.id}`;
|
|
159
|
+
const applied = this.appliedUpTo.get(marker);
|
|
160
|
+
if (applied !== void 0 && cutoff - applied < RETENTION_STEP_MS[tier]) continue;
|
|
161
|
+
const lo = Math.max(0, applied === void 0 ? series.firstTs - BUCKET_LEAD_MS : applied - RETENTION_STEP_MS[tier]);
|
|
162
|
+
counters.addDeleted(tier, await this.deleteSliced(tier, series.id, lo, cutoff - 1));
|
|
163
|
+
this.appliedUpTo.set(marker, cutoff);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
await this.purgeDeparted(now);
|
|
167
|
+
counters.retentionLastRunAt = now;
|
|
168
|
+
} catch (err) {
|
|
169
|
+
logger.warn("device-history: retention pass failed — retried at the next run", { meta: { error: err instanceof Error ? err.message : String(err) } });
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
/** Delete `[lo, hiInclusive]` of one tier in time slices, yielding between them. */
|
|
173
|
+
/** Drop the visit markers of a purged series. */
|
|
174
|
+
forgetSeries(seriesId) {
|
|
175
|
+
for (const tier of TIERS) this.appliedUpTo.delete(`${tier}:${seriesId}`);
|
|
176
|
+
}
|
|
177
|
+
async deleteSliced(tier, seriesId, lo, hiInclusive) {
|
|
178
|
+
const yieldNow = this.deps.yieldNow ?? yieldToEventLoop;
|
|
179
|
+
let deleted = 0;
|
|
180
|
+
for (let from = lo; from <= hiInclusive; from += SLICE_MS[tier]) {
|
|
181
|
+
const to = Math.min(from + SLICE_MS[tier] - 1, hiInclusive);
|
|
182
|
+
deleted += await this.deps.store.deleteRange(tier, seriesId, from, to);
|
|
183
|
+
await yieldNow();
|
|
184
|
+
}
|
|
185
|
+
return deleted;
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Tier by tier in slices, then the series row (and whatever the slices missed).
|
|
189
|
+
* Answers the rows the slices deleted per tier. The caller removes the series
|
|
190
|
+
* from the registry, which releases its per-series state.
|
|
191
|
+
*/
|
|
192
|
+
async purgeSeries(series, now) {
|
|
193
|
+
const deleted = {
|
|
194
|
+
raw: 0,
|
|
195
|
+
"5m": 0,
|
|
196
|
+
"1h": 0,
|
|
197
|
+
"1d": 0
|
|
198
|
+
};
|
|
199
|
+
for (const tier of TIERS) {
|
|
200
|
+
const lo = tier === "raw" || tier === "5m" ? Math.max(series.firstTs - BUCKET_LEAD_MS, now - SHORT_TIER_LOOKBACK_MS) : series.firstTs - BUCKET_LEAD_MS;
|
|
201
|
+
deleted[tier] = await this.deleteSliced(tier, series.id, Math.max(0, lo), now + DAY_MS);
|
|
202
|
+
this.deps.counters.addDeleted(tier, deleted[tier]);
|
|
203
|
+
}
|
|
204
|
+
await this.deps.store.deleteSeries(series.id);
|
|
205
|
+
return deleted;
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Delete every series of a DELETED device now (D865). Driven by a tombstone
|
|
209
|
+
* through the `device-removal-hook`, never by a listing: the week-long
|
|
210
|
+
* absence clock above remains the backstop for a device removed before
|
|
211
|
+
* tombstones existed.
|
|
212
|
+
*/
|
|
213
|
+
async purgeDevice(deviceId, now) {
|
|
214
|
+
const { registry, counters } = this.deps;
|
|
215
|
+
let purged = 0;
|
|
216
|
+
for (const series of registry.all()) {
|
|
217
|
+
if (series.deviceId !== deviceId) continue;
|
|
218
|
+
await this.purgeSeries(series, now);
|
|
219
|
+
registry.remove(series.id);
|
|
220
|
+
counters.purgedSeries += 1;
|
|
221
|
+
purged += 1;
|
|
222
|
+
}
|
|
223
|
+
return purged;
|
|
224
|
+
}
|
|
225
|
+
async purgeDeparted(now) {
|
|
226
|
+
const { registry, departures, counters, logger } = this.deps;
|
|
227
|
+
let purged = 0;
|
|
228
|
+
for (const series of registry.all()) {
|
|
229
|
+
if (purged >= MAX_PURGES_PER_RUN) break;
|
|
230
|
+
if (series.deviceId === null) continue;
|
|
231
|
+
const since = departures.missingSince(series.deviceId);
|
|
232
|
+
if (since === void 0 || now - since < DEPARTED_ABSENCE_MS) continue;
|
|
233
|
+
await this.purgeSeries(series, now);
|
|
234
|
+
registry.remove(series.id);
|
|
235
|
+
counters.purgedSeries += 1;
|
|
236
|
+
purged += 1;
|
|
237
|
+
logger.info("device-history: purged the series of an unregistered device", {
|
|
238
|
+
tags: { deviceId: series.deviceId },
|
|
239
|
+
meta: { key: series.key }
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
};
|
|
244
|
+
//#endregion
|
|
245
|
+
Object.defineProperty(exports, "DAY_MS", {
|
|
246
|
+
enumerable: true,
|
|
247
|
+
get: function() {
|
|
248
|
+
return DAY_MS;
|
|
249
|
+
}
|
|
250
|
+
});
|
|
251
|
+
Object.defineProperty(exports, "DEFAULT_RETENTION", {
|
|
252
|
+
enumerable: true,
|
|
253
|
+
get: function() {
|
|
254
|
+
return DEFAULT_RETENTION;
|
|
255
|
+
}
|
|
256
|
+
});
|
|
257
|
+
Object.defineProperty(exports, "DepartureTracker", {
|
|
258
|
+
enumerable: true,
|
|
259
|
+
get: function() {
|
|
260
|
+
return DepartureTracker;
|
|
261
|
+
}
|
|
262
|
+
});
|
|
263
|
+
Object.defineProperty(exports, "FIVE_MINUTES_MS", {
|
|
264
|
+
enumerable: true,
|
|
265
|
+
get: function() {
|
|
266
|
+
return FIVE_MINUTES_MS;
|
|
267
|
+
}
|
|
268
|
+
});
|
|
269
|
+
Object.defineProperty(exports, "HOUR_MS", {
|
|
270
|
+
enumerable: true,
|
|
271
|
+
get: function() {
|
|
272
|
+
return HOUR_MS;
|
|
273
|
+
}
|
|
274
|
+
});
|
|
275
|
+
Object.defineProperty(exports, "ROLLUP_GRACE_MS", {
|
|
276
|
+
enumerable: true,
|
|
277
|
+
get: function() {
|
|
278
|
+
return ROLLUP_GRACE_MS;
|
|
279
|
+
}
|
|
280
|
+
});
|
|
281
|
+
Object.defineProperty(exports, "RetentionJob", {
|
|
282
|
+
enumerable: true,
|
|
283
|
+
get: function() {
|
|
284
|
+
return RetentionJob;
|
|
285
|
+
}
|
|
286
|
+
});
|
|
287
|
+
Object.defineProperty(exports, "bucketEndOf", {
|
|
288
|
+
enumerable: true,
|
|
289
|
+
get: function() {
|
|
290
|
+
return bucketEndOf;
|
|
291
|
+
}
|
|
292
|
+
});
|
|
293
|
+
Object.defineProperty(exports, "bucketStartOf", {
|
|
294
|
+
enumerable: true,
|
|
295
|
+
get: function() {
|
|
296
|
+
return bucketStartOf;
|
|
297
|
+
}
|
|
298
|
+
});
|
|
299
|
+
Object.defineProperty(exports, "floorTo", {
|
|
300
|
+
enumerable: true,
|
|
301
|
+
get: function() {
|
|
302
|
+
return floorTo;
|
|
303
|
+
}
|
|
304
|
+
});
|
|
Binary file
|
|
Binary file
|