edge-impulse-linux 1.26.0 → 1.26.2
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/README.md +14 -5
- package/build/cli/linux/agent-capture.d.ts +29 -0
- package/build/cli/linux/agent-capture.js +94 -0
- package/build/cli/linux/agent-capture.js.map +1 -0
- package/build/cli/linux/agent-runner-metadata.d.ts +12 -3
- package/build/cli/linux/agent-runner-metadata.js +15 -3
- package/build/cli/linux/agent-runner-metadata.js.map +1 -1
- package/build/cli/linux/audio-utils.d.ts +1 -10
- package/build/cli/linux/audio-utils.js +2 -25
- package/build/cli/linux/audio-utils.js.map +1 -1
- package/build/cli/linux/runner-downloader.js +27 -1
- package/build/cli/linux/runner-downloader.js.map +1 -1
- package/build/cli/linux/runner-utils.d.ts +5 -0
- package/build/cli/linux/runner-utils.js +7 -2
- package/build/cli/linux/runner-utils.js.map +1 -1
- package/build/cli/linux/runner.js +332 -117
- package/build/cli/linux/runner.js.map +1 -1
- package/build/cli-common/model-monitor.d.ts +97 -10
- package/build/cli-common/model-monitor.js +380 -373
- package/build/cli-common/model-monitor.js.map +1 -1
- package/build/cli-common/remote-mgmt-service.d.ts +1 -0
- package/build/cli-common/remote-mgmt-service.js +29 -11
- package/build/cli-common/remote-mgmt-service.js.map +1 -1
- package/build/library/classifier/audio-classifier.d.ts +28 -3
- package/build/library/classifier/audio-classifier.js +126 -40
- package/build/library/classifier/audio-classifier.js.map +1 -1
- package/build/library/classifier/image-classifier.d.ts +14 -0
- package/build/library/classifier/image-classifier.js +79 -3
- package/build/library/classifier/image-classifier.js.map +1 -1
- package/build/library/classifier/libcwrapper.d.ts +7 -1
- package/build/library/classifier/libcwrapper.js +10 -2
- package/build/library/classifier/libcwrapper.js.map +1 -1
- package/build/library/classifier/linux-impulse-runner.d.ts +36 -1
- package/build/library/classifier/linux-impulse-runner.js +266 -55
- package/build/library/classifier/linux-impulse-runner.js.map +1 -1
- package/build/library/sensors/audio-utils.d.ts +4 -0
- package/build/library/sensors/audio-utils.js +23 -0
- package/build/library/sensors/audio-utils.js.map +1 -0
- package/build/library/sensors/gstreamer.d.ts +1 -1
- package/build/library/sensors/gstreamer.js +45 -9
- package/build/library/sensors/gstreamer.js.map +1 -1
- package/build/library/sensors/recorder.js +61 -23
- package/build/library/sensors/recorder.js.map +1 -1
- package/build/sdk/studio/sdk/api/devicesApi.d.ts +2 -2
- package/build/sdk/studio/sdk/api/devicesApi.js +2 -2
- package/build/sdk/studio/sdk/model/additionalMetricOptionalFullPrecisionValue.d.ts +7 -0
- package/build/sdk/studio/sdk/model/additionalMetricOptionalFullPrecisionValue.js +4 -0
- package/build/sdk/studio/sdk/model/additionalMetricOptionalFullPrecisionValue.js.map +1 -0
- package/build/sdk/studio/sdk/model/dSPGroupItem.d.ts +9 -1
- package/build/sdk/studio/sdk/model/dSPGroupItem.js +3 -1
- package/build/sdk/studio/sdk/model/dSPGroupItem.js.map +1 -1
- package/build/sdk/studio/sdk/model/device.d.ts +9 -0
- package/build/sdk/studio/sdk/model/device.js.map +1 -1
- package/build/sdk/studio/sdk/model/deviceApplication.d.ts +12 -0
- package/build/sdk/studio/sdk/model/deviceApplication.js +4 -0
- package/build/sdk/studio/sdk/model/deviceApplication.js.map +1 -0
- package/build/sdk/studio/sdk/model/deviceApplicationModel.d.ts +13 -0
- package/build/sdk/studio/sdk/model/deviceApplicationModel.js +4 -0
- package/build/sdk/studio/sdk/model/deviceApplicationModel.js.map +1 -0
- package/build/sdk/studio/sdk/model/deviceDeploymentEvent.d.ts +9 -0
- package/build/sdk/studio/sdk/model/deviceDeploymentEvent.js.map +1 -1
- package/build/sdk/studio/sdk/model/deviceModelDeploymentRequest.d.ts +5 -0
- package/build/sdk/studio/sdk/model/foldModelMetadata.d.ts +5 -0
- package/build/sdk/studio/sdk/model/getMonitoringEventDetailsResponse.d.ts +4 -0
- package/build/sdk/studio/sdk/model/getMonitoringEventDetailsResponse.js.map +1 -1
- package/build/sdk/studio/sdk/model/inferenceHistoryAggregate.d.ts +8 -0
- package/build/sdk/studio/sdk/model/kerasModelMetadata.d.ts +5 -0
- package/build/sdk/studio/sdk/model/kerasModelMetadataBase.d.ts +5 -0
- package/build/sdk/studio/sdk/model/kerasModelMetadataResponse.d.ts +5 -0
- package/build/sdk/studio/sdk/model/models.d.ts +3 -0
- package/build/sdk/studio/sdk/model/models.js +3 -0
- package/build/sdk/studio/sdk/model/models.js.map +1 -1
- package/build/sdk/studio/sdk/model/monitoringEventContext.d.ts +12 -0
- package/build/sdk/studio/sdk/model/monitoringInferenceSummary.d.ts +4 -0
- package/build/sdk/studio/sdk/model/userOrganization.d.ts +2 -0
- package/build/shared/viewmodels/init.d.ts +0 -1
- package/build/shared/viewmodels/init.js +0 -1
- package/build/shared/viewmodels/init.js.map +1 -1
- package/package.json +5 -2
- package/test/agent-bundle-lifecycle.test.ts +445 -0
- package/test/agent-bundle.test.ts +104 -0
- package/test/agent-capture.test.ts +528 -0
- package/test/agent-client.test.ts +263 -138
- package/test/agent-command-processor.test.ts +194 -0
- package/test/agent-preview-replacement.test.ts +114 -0
- package/test/agent-runner-configuration.test.ts +119 -0
- package/test/agent-runner-integration.test.ts +372 -0
- package/test/gstreamer.test.ts +1 -1
- package/test/helpers/mock-agent.ts +106 -0
- package/test/model-monitor.test.ts +250 -55
- package/test/monitoring-fixture.ts +168 -0
- package/test/monitoring-policy-protocol.test.ts +193 -1
- package/test/runner-preview.test.ts +59 -0
- package/test/summary-rules-fixtures.test.ts +194 -0
- package/test/utils/agent-runner-fixtures.ts +147 -0
- package/test/agent-runner-metadata.test.ts +0 -31
- package/test/sensor-selection.test.ts +0 -26
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cover monitoring cadence and complete result streams using production calculators and storage.
|
|
3
|
+
* Stream tests control time and delay selected record writes to check window boundaries, delivery,
|
|
4
|
+
* and deployment replacement. Registry arithmetic cases live in summary-rules-fixtures.test.ts.
|
|
5
|
+
*/
|
|
1
6
|
import assert from 'assert';
|
|
2
7
|
import fs from 'fs/promises';
|
|
3
8
|
import os from 'os';
|
|
@@ -5,6 +10,7 @@ import Path from 'path';
|
|
|
5
10
|
import { Config } from '../cli-common/config';
|
|
6
11
|
import { ImpulseRecord, ModelMonitor } from '../cli-common/model-monitor';
|
|
7
12
|
import { ModelInformation } from '../library/classifier/linux-impulse-runner-types';
|
|
13
|
+
import { deferred, MonitoringClock, monitoringFixture, monitoringModel, monitoringResult } from './monitoring-fixture';
|
|
8
14
|
|
|
9
15
|
describe('model monitoring summary cadence', () => {
|
|
10
16
|
for (const interval of [ undefined, 5_000, 10_000, 60_000, 120_000 ]) {
|
|
@@ -26,7 +32,9 @@ describe('model monitoring summary cadence', () => {
|
|
|
26
32
|
thresholds: [{ type: 'classification', min_score: 0.5 }],
|
|
27
33
|
},
|
|
28
34
|
} as unknown as ModelInformation;
|
|
29
|
-
const monitor = await ModelMonitor.getModelMonitor(config, model
|
|
35
|
+
const monitor = await ModelMonitor.getModelMonitor(config, model, {
|
|
36
|
+
reportedThresholds: model.modelParameters.thresholds ?? [],
|
|
37
|
+
});
|
|
30
38
|
const calculator = (monitor as unknown as {
|
|
31
39
|
_metricsCalculator: { updateMetrics(record: ImpulseRecord): Promise<boolean> };
|
|
32
40
|
})._metricsCalculator;
|
|
@@ -52,69 +60,256 @@ describe('model monitoring summary cadence', () => {
|
|
|
52
60
|
}
|
|
53
61
|
});
|
|
54
62
|
|
|
63
|
+
describe('monitoring result streams', () => {
|
|
64
|
+
let clock: MonitoringClock;
|
|
65
|
+
|
|
66
|
+
beforeEach(() => { clock = new MonitoringClock(); });
|
|
67
|
+
afterEach(() => { clock.restore(); });
|
|
68
|
+
|
|
69
|
+
const input = { type: 'wav' as const, buffer: Buffer.from([ 0, 1 ]) };
|
|
70
|
+
const policy = { intervalMs: 5_000, minimumIntervalMs: 5_000,
|
|
71
|
+
preparationWindowMs: 0, requirementsMet: true };
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Feed a summary window, trigger its timer, then submit a boundary result while closure is queued.
|
|
75
|
+
* Exercise result- and timer-triggered closure of the next windows. Boundary results start new
|
|
76
|
+
* windows, while a late result stays retrievable without changing summaries. Idle time, repeated
|
|
77
|
+
* flushes and stop add no deliveries.
|
|
78
|
+
*/
|
|
79
|
+
it('closes an idle window once while a boundary result waits behind timer closure', async () => {
|
|
80
|
+
const fixture = await monitoringFixture();
|
|
81
|
+
const initialTime = clock.now;
|
|
82
|
+
try {
|
|
83
|
+
fixture.monitor.applySummaryPolicy(policy);
|
|
84
|
+
await fixture.monitor.processResult(monitoringResult({ cat: 0.75, dog: 0.25 }), input);
|
|
85
|
+
clock.now = initialTime + 4_999;
|
|
86
|
+
await fixture.monitor.processResult(monitoringResult({ cat: 0.25, dog: 0.75 }), input);
|
|
87
|
+
|
|
88
|
+
clock.fireDue(initialTime + 5_000);
|
|
89
|
+
const boundary = fixture.monitor.processResult(monitoringResult({ cat: 0.5, dog: 0.5 }), input);
|
|
90
|
+
await boundary;
|
|
91
|
+
// Do not fire the next timer: this boundary result must itself close the previous window.
|
|
92
|
+
clock.now = initialTime + 10_000;
|
|
93
|
+
await fixture.monitor.processResult(monitoringResult({ cat: 0.75, dog: 0.25 }), input);
|
|
94
|
+
clock.fireDue(initialTime + 15_000);
|
|
95
|
+
await fixture.monitor.flushMetrics();
|
|
96
|
+
clock.now = initialTime + 14_999;
|
|
97
|
+
await fixture.monitor.processResult(monitoringResult({ obsolete: 1 }), input);
|
|
98
|
+
clock.fireDue(initialTime + 30_000);
|
|
99
|
+
await fixture.monitor.flushMetrics();
|
|
100
|
+
await fixture.monitor.stop();
|
|
101
|
+
|
|
102
|
+
assert.deepStrictEqual(fixture.summaries.map(summary => [ summary.start, summary.end ]), [
|
|
103
|
+
[{ index: 1, timestamp: initialTime }, { index: 2, timestamp: initialTime + 4_999 }],
|
|
104
|
+
[{ index: 3, timestamp: initialTime + 5_000 },
|
|
105
|
+
{ index: 3, timestamp: initialTime + 5_000 }],
|
|
106
|
+
[{ index: 4, timestamp: initialTime + 10_000 },
|
|
107
|
+
{ index: 4, timestamp: initialTime + 10_000 }],
|
|
108
|
+
], 'boundary results start the next summary and idle periods add no empty summaries');
|
|
109
|
+
assert.deepStrictEqual(fixture.summaries.map(summary => summary.classificationCounter), [
|
|
110
|
+
[{ label: 'cat', value: 1 }, { label: 'dog', value: 1 }],
|
|
111
|
+
[{ label: 'cat', value: 1 }],
|
|
112
|
+
[{ label: 'cat', value: 1 }],
|
|
113
|
+
]);
|
|
114
|
+
const saved = await fixture.saved();
|
|
115
|
+
assert.deepStrictEqual(saved.records.map(record => [ record.index, record.timestamp ]),
|
|
116
|
+
[ [ 1, initialTime ], [ 2, initialTime + 4_999 ], [ 3, initialTime + 5_000 ],
|
|
117
|
+
[ 4, initialTime + 10_000 ], [ 5, initialTime + 14_999 ] ]);
|
|
118
|
+
assert.deepStrictEqual(saved.records.map(record => record.impulseRecord.result.classification),
|
|
119
|
+
[{ cat: 0.75, dog: 0.25 }, { cat: 0.25, dog: 0.75 }, { cat: 0.5, dog: 0.5 },
|
|
120
|
+
{ cat: 0.75, dog: 0.25 }, { obsolete: 1 }],
|
|
121
|
+
'late results remain retrievable but cannot modify completed summaries');
|
|
122
|
+
}
|
|
123
|
+
finally {
|
|
124
|
+
await fixture.dispose();
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Stream one JPEG record live, then read the same record through retained-record delivery.
|
|
130
|
+
* The live event must keep the original base64 image, while the JSON file stores a path and
|
|
131
|
+
* the retained-record response loads that file back into base64.
|
|
132
|
+
*/
|
|
133
|
+
it('keeps live JPEG data intact while storing and reloading the image file', async () => {
|
|
134
|
+
const fixture = await monitoringFixture();
|
|
135
|
+
const jpeg = Buffer.from([ 0xff, 0xd8, 0xff, 0xd9 ]);
|
|
136
|
+
const live: ImpulseRecord[] = [];
|
|
137
|
+
try {
|
|
138
|
+
fixture.monitor.on('impulse-record', record => live.push(record));
|
|
139
|
+
fixture.monitor.impulseDebug = true;
|
|
140
|
+
await fixture.monitor.processResult(monitoringResult({ cat: 1 }), { type: 'jpg', buffer: jpeg });
|
|
141
|
+
assert.strictEqual(live.length, 1);
|
|
142
|
+
assert.strictEqual(live[0].rawData.bufferBase64, jpeg.toString('base64'));
|
|
143
|
+
|
|
144
|
+
const retained = new Promise<ImpulseRecord>(resolve => {
|
|
145
|
+
fixture.monitor.once('impulse-records-response', record => resolve(record as ImpulseRecord));
|
|
146
|
+
});
|
|
147
|
+
fixture.monitor.getImpulseRecords({ index: 1 });
|
|
148
|
+
assert.strictEqual((await retained).rawData.bufferBase64, jpeg.toString('base64'));
|
|
149
|
+
|
|
150
|
+
const saved = await fixture.saved();
|
|
151
|
+
assert.strictEqual(saved.records.length, 1);
|
|
152
|
+
assert.notStrictEqual(saved.records[0].rawData.bufferBase64, jpeg.toString('base64'));
|
|
153
|
+
assert.strictEqual(Path.extname(saved.records[0].rawData.bufferBase64), '.jpg');
|
|
154
|
+
}
|
|
155
|
+
finally {
|
|
156
|
+
await fixture.dispose();
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Admit one result, reject a timestamp just within the backward-clock tolerance, then move farther
|
|
162
|
+
* backwards. The old window must close once, the corrected timeline must accept a new result, and
|
|
163
|
+
* its timer must close normally rather than repeatedly scheduling against the abandoned future grid.
|
|
164
|
+
*/
|
|
165
|
+
it('starts a new window grid after a large backward wall-clock correction', async () => {
|
|
166
|
+
const fixture = await monitoringFixture();
|
|
167
|
+
const initialTime = clock.now;
|
|
168
|
+
try {
|
|
169
|
+
fixture.monitor.applySummaryPolicy(policy);
|
|
170
|
+
await fixture.monitor.processResult(monitoringResult({ cat: 1 }), input);
|
|
171
|
+
|
|
172
|
+
clock.now = initialTime - 10_000;
|
|
173
|
+
await fixture.monitor.processResult(monitoringResult({ dog: 1 }), input);
|
|
174
|
+
assert.strictEqual(fixture.summaries.length, 0, 'a slightly late result does not reset the grid');
|
|
175
|
+
|
|
176
|
+
clock.now = initialTime - 10_001;
|
|
177
|
+
await fixture.monitor.processResult(monitoringResult({ cat: 1 }), input);
|
|
178
|
+
assert.deepStrictEqual(fixture.summaries.map(summary => [ summary.start.index, summary.end.index ]),
|
|
179
|
+
[ [ 1, 1 ] ], 'the clock correction closes the future window without admitting late data');
|
|
180
|
+
|
|
181
|
+
clock.fireDue(initialTime - 5_001);
|
|
182
|
+
await fixture.monitor.flushMetrics();
|
|
183
|
+
assert.deepStrictEqual(fixture.summaries.map(summary => [ summary.start.index, summary.end.index ]),
|
|
184
|
+
[ [ 1, 1 ], [ 3, 3 ] ], 'the corrected grid closes once at its new deadline');
|
|
185
|
+
}
|
|
186
|
+
finally {
|
|
187
|
+
await fixture.dispose();
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Hold the first record's save, queue a second result and flush the deployment before replacing it.
|
|
193
|
+
* Release the save and check that both accepted records contribute to the final old-model summary.
|
|
194
|
+
* Replace the deployment with different original/local cutoffs and verify its own files and original cutoff,
|
|
195
|
+
* retaining the reporting policy and leaving old files unchanged. A terminal stop remains terminal even if
|
|
196
|
+
* deployment state is subsequently replaced, and returning to old storage does not replay prior summaries.
|
|
197
|
+
*/
|
|
198
|
+
it('separates replacement deployment history and keeps terminal stop irreversible', async () => {
|
|
199
|
+
const initialModel = monitoringModel();
|
|
200
|
+
const initialThresholds = initialModel.modelParameters.thresholds ?? [];
|
|
201
|
+
const fixture = await monitoringFixture(initialModel, { reportedThresholds: initialThresholds });
|
|
202
|
+
const initialTime = clock.now;
|
|
203
|
+
const saving = deferred<void>();
|
|
204
|
+
const release = deferred<void>();
|
|
205
|
+
const storeIndex = fixture.config.storeStorageIndex.bind(fixture.config);
|
|
206
|
+
try {
|
|
207
|
+
fixture.monitor.applySummaryPolicy(policy);
|
|
208
|
+
// Pause the first record's index write so the second admission and flush queue behind it.
|
|
209
|
+
fixture.config.storeStorageIndex = async index => {
|
|
210
|
+
saving.resolve();
|
|
211
|
+
await release.promise;
|
|
212
|
+
await storeIndex(index);
|
|
213
|
+
};
|
|
214
|
+
const first = fixture.monitor.processResult(monitoringResult({ cat: 0.75, dog: 0.25 }), input);
|
|
215
|
+
await saving.promise;
|
|
216
|
+
clock.now++;
|
|
217
|
+
const second = fixture.monitor.processResult(monitoringResult({ cat: 0.25, dog: 0.75 }), input);
|
|
218
|
+
const flushed = fixture.monitor.flushMetrics();
|
|
219
|
+
release.resolve();
|
|
220
|
+
await Promise.all([ first, second, flushed ]);
|
|
221
|
+
assert.strictEqual(fixture.summaries.length, 1);
|
|
222
|
+
const old = await fixture.saved(1);
|
|
223
|
+
assert.deepStrictEqual(old.records.map(record => record.index), [ 1, 2 ]);
|
|
224
|
+
|
|
225
|
+
const replacement = monitoringModel(2, 0.8);
|
|
226
|
+
const reportedThresholds = replacement.modelParameters.thresholds!.map(block => ({ ...block }));
|
|
227
|
+
replacement.modelParameters.thresholds![0].min_score = 0.1;
|
|
228
|
+
await fixture.config.setDeploymentVersion(2);
|
|
229
|
+
await fixture.monitor.replaceModel(fixture.config, replacement, { reportedThresholds });
|
|
230
|
+
assert.strictEqual(fixture.monitor.effectiveSummaryIntervalMs, 5_000, 'replacement retains the policy');
|
|
231
|
+
clock.now = initialTime + 10_000;
|
|
232
|
+
await fixture.monitor.processResult(monitoringResult({ cat: 0.75, dog: 0.25 }), input);
|
|
233
|
+
await fixture.monitor.flushMetrics();
|
|
234
|
+
assert.strictEqual(fixture.summaries.length, 2);
|
|
235
|
+
assert.deepStrictEqual(fixture.summaries[1].classificationCounter, [{ label: 'no prediction', value: 1 }]);
|
|
236
|
+
const current = await fixture.saved(2);
|
|
237
|
+
assert.strictEqual(current.records.length, 1);
|
|
238
|
+
assert.deepStrictEqual(await fixture.saved(1), old, 'replacement does not rewrite old deployment files');
|
|
239
|
+
|
|
240
|
+
// Returning to a prior deployment must not replay summaries delivered earlier in this session.
|
|
241
|
+
await fixture.config.setDeploymentVersion(1);
|
|
242
|
+
const replacing = fixture.monitor.replaceModel(fixture.config, initialModel, {
|
|
243
|
+
reportedThresholds: initialThresholds,
|
|
244
|
+
});
|
|
245
|
+
const stopped = fixture.monitor.stop();
|
|
246
|
+
await Promise.all([ replacing, stopped ]);
|
|
247
|
+
await fixture.monitor.processResult(monitoringResult({ discarded: 1 }), input);
|
|
248
|
+
assert.strictEqual(fixture.summaries.length, 2);
|
|
249
|
+
assert.deepStrictEqual(await fixture.saved(1), old);
|
|
250
|
+
}
|
|
251
|
+
finally {
|
|
252
|
+
// Failure cleanup must also unblock the queue before stopping the monitor.
|
|
253
|
+
release.resolve();
|
|
254
|
+
fixture.config.storeStorageIndex = storeIndex;
|
|
255
|
+
await fixture.dispose();
|
|
256
|
+
}
|
|
257
|
+
});
|
|
258
|
+
});
|
|
259
|
+
|
|
55
260
|
|
|
56
261
|
describe('adaptive model monitoring cadence', () => {
|
|
57
262
|
for (const configured of [ undefined, 5_000, 10_000, 60_000 ]) {
|
|
58
|
-
it(`adapts an in-flight aggregate and preserves preference ${configured}`, async () => {
|
|
59
|
-
const
|
|
263
|
+
it(`adapts an in-flight aggregate and preserves preference ${configured ?? 'automatic'}`, async () => {
|
|
264
|
+
const clock = new MonitoringClock();
|
|
265
|
+
const fixture = await monitoringFixture(monitoringModel(), {
|
|
266
|
+
configuredSummaryIntervalMs: configured,
|
|
267
|
+
});
|
|
268
|
+
const initialTime = clock.now;
|
|
269
|
+
const input = { type: 'wav' as const, buffer: Buffer.from([ 0, 1 ]) };
|
|
270
|
+
const policy = { intervalMs: 60_000, minimumIntervalMs: 5_000,
|
|
271
|
+
preparationWindowMs: 0, requirementsMet: true };
|
|
60
272
|
try {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
modelParameters: {
|
|
72
|
-
model_type: 'classification', has_anomaly: 0,
|
|
73
|
-
thresholds: [{ type: 'classification', min_score: 0.5 }],
|
|
74
|
-
},
|
|
75
|
-
} as unknown as ModelInformation;
|
|
76
|
-
const monitor = await ModelMonitor.getModelMonitor(config, model);
|
|
77
|
-
const calculator = (monitor as unknown as {
|
|
78
|
-
_metricsCalculator: {
|
|
79
|
-
updateMetrics(record: ImpulseRecord): Promise<boolean>;
|
|
80
|
-
getMetrics(): { start: { index: number }; end: { index: number } };
|
|
81
|
-
};
|
|
82
|
-
})._metricsCalculator;
|
|
83
|
-
const policy = { intervalMs: 60_000, minimumIntervalMs: 5_000,
|
|
84
|
-
preparationWindowMs: 0, requirementsMet: true };
|
|
85
|
-
monitor.applySummaryPolicy(policy);
|
|
86
|
-
assert.strictEqual(monitor.effectiveSummaryIntervalMs, configured ?? 60_000);
|
|
87
|
-
const update = (index: number, elapsed: number) => calculator.updateMetrics({
|
|
88
|
-
timestamp: 1_000_000 + elapsed, index,
|
|
89
|
-
rawData: { type: 'wav', bufferBase64: '' },
|
|
90
|
-
impulseRecord: { result: { classification: { keyword: 0.9 } },
|
|
91
|
-
timing: { dsp: 1, classification: 1, anomaly: 0 }, info: undefined },
|
|
273
|
+
fixture.monitor.applySummaryPolicy(policy);
|
|
274
|
+
assert.strictEqual(fixture.monitor.effectiveSummaryIntervalMs, configured ?? 60_000);
|
|
275
|
+
await fixture.monitor.processResult(monitoringResult({ cat: 0.9 }), input);
|
|
276
|
+
clock.now = initialTime + 4_000;
|
|
277
|
+
await fixture.monitor.processResult(monitoringResult({ cat: 0.9 }), input);
|
|
278
|
+
|
|
279
|
+
fixture.monitor.applySummaryPolicy({
|
|
280
|
+
...policy,
|
|
281
|
+
intervalMs: 5_000,
|
|
282
|
+
preparationWindowMs: 120_000,
|
|
92
283
|
});
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
[ 0, 2 ]);
|
|
99
|
-
assert.strictEqual(await update(3, 5_450), false);
|
|
100
|
-
monitor.applySummaryPolicy(policy);
|
|
284
|
+
clock.now = initialTime + 5_000;
|
|
285
|
+
await fixture.monitor.processResult(monitoringResult({ cat: 0.9 }), input);
|
|
286
|
+
assert.deepStrictEqual(fixture.summaries.map(summary => [ summary.start.index, summary.end.index ]),
|
|
287
|
+
[ [ 1, 2 ] ], 'the boundary result starts the accelerated window');
|
|
288
|
+
|
|
101
289
|
const slower = configured ?? 60_000;
|
|
102
|
-
|
|
103
|
-
assert.strictEqual(
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
monitor.
|
|
108
|
-
assert.
|
|
109
|
-
|
|
290
|
+
fixture.monitor.applySummaryPolicy(policy);
|
|
291
|
+
assert.strictEqual(fixture.monitor.effectiveSummaryIntervalMs, slower);
|
|
292
|
+
clock.now = initialTime + 5_000 + slower - 1;
|
|
293
|
+
await fixture.monitor.processResult(monitoringResult({ cat: 0.9 }), input);
|
|
294
|
+
clock.now = initialTime + 5_000 + slower;
|
|
295
|
+
await fixture.monitor.processResult(monitoringResult({ cat: 0.9 }), input);
|
|
296
|
+
assert.deepStrictEqual(fixture.summaries.map(summary => [ summary.start.index, summary.end.index ]),
|
|
297
|
+
[ [ 1, 2 ], [ 3, 4 ] ], 'the relaxed cadence keeps the established window grid');
|
|
298
|
+
|
|
299
|
+
assert.strictEqual(fixture.monitor.configuredSummaryIntervalMs, configured);
|
|
300
|
+
fixture.monitor.applySummaryPolicy({ ...policy, intervalMs: NaN });
|
|
301
|
+
assert.strictEqual(fixture.monitor.effectiveSummaryIntervalMs, slower,
|
|
302
|
+
'invalid policy preserves last cadence');
|
|
303
|
+
fixture.monitor.applySummaryPolicy({ ...policy, intervalMs: 60_000, minimumIntervalMs: 60_000,
|
|
110
304
|
preparationWindowMs: 120_000, requirementsMet: false });
|
|
111
|
-
assert.strictEqual(monitor.effectiveSummaryIntervalMs, 60_000);
|
|
112
|
-
monitor.applySummaryPolicy(undefined);
|
|
113
|
-
assert.strictEqual(monitor.effectiveSummaryIntervalMs, 60_000,
|
|
114
|
-
'a server without policy support restores the
|
|
305
|
+
assert.strictEqual(fixture.monitor.effectiveSummaryIntervalMs, 60_000);
|
|
306
|
+
fixture.monitor.applySummaryPolicy(undefined);
|
|
307
|
+
assert.strictEqual(fixture.monitor.effectiveSummaryIntervalMs, 60_000,
|
|
308
|
+
'a server without policy support restores the conservative cadence');
|
|
115
309
|
}
|
|
116
310
|
finally {
|
|
117
|
-
await
|
|
311
|
+
await fixture.dispose();
|
|
312
|
+
clock.restore();
|
|
118
313
|
}
|
|
119
314
|
});
|
|
120
315
|
}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared setup for monitoring stream and protocol tests. ModelMonitor and its file storage
|
|
3
|
+
* are real; model inputs, CLI configuration and time are controlled by the test. Expected summaries
|
|
4
|
+
* belong in the tests, not here. Always dispose the fixture and restore the clock in cleanup.
|
|
5
|
+
*/
|
|
6
|
+
import fs from 'fs/promises';
|
|
7
|
+
import os from 'os';
|
|
8
|
+
import Path from 'path';
|
|
9
|
+
import { Config } from '../cli-common/config';
|
|
10
|
+
import { ImpulseRecord, InferenceMetrics, ModelMonitor } from '../cli-common/model-monitor';
|
|
11
|
+
import { ModelInformation, RunnerBlockThreshold,
|
|
12
|
+
RunnerClassifyResponseSuccess } from '../library/classifier/linux-impulse-runner-types';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Controls wall time and timeout delivery independently, so tests can choose whether a timer or
|
|
16
|
+
* prediction arrives first without sleeping. Replaces globals: use only in sequential tests.
|
|
17
|
+
* This is not a full Node clock; intervals and asynchronous callback completion are not controlled.
|
|
18
|
+
*/
|
|
19
|
+
export class MonitoringClock {
|
|
20
|
+
now = 1_000_000;
|
|
21
|
+
private readonly _now = Date.now;
|
|
22
|
+
private readonly _setTimeout = global.setTimeout;
|
|
23
|
+
private readonly _clearTimeout = global.clearTimeout;
|
|
24
|
+
private readonly _timers = new Map<object, { at: number, callback: () => void }>();
|
|
25
|
+
|
|
26
|
+
/** Install the clock, retaining the original globals for restore(). */
|
|
27
|
+
constructor() {
|
|
28
|
+
Date.now = () => this.now;
|
|
29
|
+
global.setTimeout = ((callback: () => void, delay = 0) => {
|
|
30
|
+
// Production timers call unref(); this fake handle has no process-lifetime effect.
|
|
31
|
+
const timer = { unref: () => undefined };
|
|
32
|
+
this._timers.set(timer, { at: this.now + delay, callback });
|
|
33
|
+
return timer;
|
|
34
|
+
}) as unknown as typeof setTimeout;
|
|
35
|
+
global.clearTimeout = ((timer: object) => this._timers.delete(timer)) as unknown as typeof clearTimeout;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Set wall time and invoke currently due callbacks in deadline order, without awaiting their
|
|
40
|
+
* asynchronous work. Callers must separately await the monitoring operation they trigger.
|
|
41
|
+
*/
|
|
42
|
+
fireDue(now: number): void {
|
|
43
|
+
this.now = now;
|
|
44
|
+
// Iterate a snapshot: timers created by these callbacks wait for the next fireDue() call.
|
|
45
|
+
for (const [ timer, task ] of [ ...this._timers ].sort((a, b) => a[1].at - b[1].at)) {
|
|
46
|
+
if (task.at <= now && this._timers.delete(timer)) task.callback();
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Restore all replaced globals and discard outstanding fake timers, including after failures. */
|
|
51
|
+
restore(): void {
|
|
52
|
+
Date.now = this._now;
|
|
53
|
+
global.setTimeout = this._setTimeout;
|
|
54
|
+
global.clearTimeout = this._clearTimeout;
|
|
55
|
+
this._timers.clear();
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Hold a prediction or storage operation pending until the test explicitly resolves it. */
|
|
60
|
+
export function deferred<T>() {
|
|
61
|
+
let resolve: (value: T | PromiseLike<T>) => void = () => undefined;
|
|
62
|
+
const promise = new Promise<T>(accept => resolve = accept);
|
|
63
|
+
return { promise, resolve };
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Build a prediction with fixed timings, so summary timing averages have known inputs. */
|
|
67
|
+
export function monitoringResult(classification: Record<string, number>): RunnerClassifyResponseSuccess {
|
|
68
|
+
return { result: { classification }, timing: { dsp: 2, classification: 4, anomaly: 0 }, info: undefined };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Build the minimal project-1 classification metadata used by monitoring tests.
|
|
73
|
+
* null means that the model reported no threshold, not a zero cutoff. Tests applying local settings
|
|
74
|
+
* before creating a monitor must copy the original thresholds and pass them explicitly to the fixture.
|
|
75
|
+
* This is not a complete trained model.
|
|
76
|
+
*/
|
|
77
|
+
export function monitoringModel(version = 1, cutoff: number | null = 0.5): ModelInformation {
|
|
78
|
+
const model = {
|
|
79
|
+
project: { id: 1, owner: 'test', name: 'monitoring', deploy_version: version },
|
|
80
|
+
modelParameters: {
|
|
81
|
+
model_type: 'classification', has_anomaly: 0, labels: [ 'cat', 'dog' ],
|
|
82
|
+
thresholds: cutoff === null ? [] : [{ id: 42, type: 'classification', min_score: cutoff }],
|
|
83
|
+
},
|
|
84
|
+
} as unknown as ModelInformation;
|
|
85
|
+
return model;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Create a real monitor with an isolated temporary storage root and in-memory CLI configuration.
|
|
90
|
+
* Models must use project 1, matching the storage paths below. Records are read back from
|
|
91
|
+
* production-written files; delivered summaries accumulate from the real monitor event.
|
|
92
|
+
* Supply reportedThresholds explicitly if the model already contains local threshold overrides.
|
|
93
|
+
* Otherwise its current thresholds stand in for the Hello snapshot, copied before monitor creation.
|
|
94
|
+
* The caller must dispose() in finally, and restore any MonitoringClock separately afterwards.
|
|
95
|
+
*/
|
|
96
|
+
export async function monitoringFixture(model = monitoringModel(),
|
|
97
|
+
{
|
|
98
|
+
reportedThresholds = model.modelParameters.thresholds ?? [],
|
|
99
|
+
configuredSummaryIntervalMs,
|
|
100
|
+
}: {
|
|
101
|
+
reportedThresholds?: readonly RunnerBlockThreshold[];
|
|
102
|
+
configuredSummaryIntervalMs?: number;
|
|
103
|
+
} = {}) {
|
|
104
|
+
const thresholds = reportedThresholds.map(block => ({ ...block }));
|
|
105
|
+
const root = await fs.mkdtemp(Path.join(os.tmpdir(), 'ei-monitoring-'));
|
|
106
|
+
let deploymentVersion = model.project.deploy_version;
|
|
107
|
+
let storagePath = Path.join(root, '1', deploymentVersion.toString());
|
|
108
|
+
let nextIndex = 0;
|
|
109
|
+
const config = {
|
|
110
|
+
/** Read the selected deployment, which tests can change before replacing the model. */
|
|
111
|
+
getDeploymentVersion: async () => deploymentVersion,
|
|
112
|
+
/** Select a deployment without touching the user's CLI configuration. */
|
|
113
|
+
setDeploymentVersion: async (version: number) => { deploymentVersion = version; },
|
|
114
|
+
/** Read the storage directory currently selected by the production storage manager. */
|
|
115
|
+
getStoragePath: async () => storagePath,
|
|
116
|
+
/** Retain directory changes made during storage initialization or model replacement. */
|
|
117
|
+
storeStoragePath: async (path: string) => { storagePath = path; },
|
|
118
|
+
/** Keep all deployment directories inside this fixture's temporary root. */
|
|
119
|
+
getDefaultStoragePath: () => root,
|
|
120
|
+
/** Supply no explicit storage size limit. */
|
|
121
|
+
getStorageMaxSizeMb: async () => undefined,
|
|
122
|
+
/** Read the record index retained between monitor instances. */
|
|
123
|
+
getStorageIndex: async () => nextIndex,
|
|
124
|
+
/** Retain the production index update; tests may delay this write to exercise queue ordering. */
|
|
125
|
+
storeStorageIndex: async (index: number) => { nextIndex = index; },
|
|
126
|
+
/** Supply the saved cadence preference selected by this test, or automatic cadence by default. */
|
|
127
|
+
getMonitorSummaryIntervalMs: async () => configuredSummaryIntervalMs,
|
|
128
|
+
} as unknown as Config;
|
|
129
|
+
const monitor = await ModelMonitor.getModelMonitor(config, model, { reportedThresholds: thresholds })
|
|
130
|
+
.catch(async error => {
|
|
131
|
+
await fs.rm(root, { recursive: true, force: true });
|
|
132
|
+
throw error;
|
|
133
|
+
});
|
|
134
|
+
const summaries: InferenceMetrics[] = [];
|
|
135
|
+
monitor.on('inference-summary', summary => summaries.push(summary));
|
|
136
|
+
|
|
137
|
+
return {
|
|
138
|
+
root, config, summaries, monitor,
|
|
139
|
+
/**
|
|
140
|
+
* Read a deployment's persisted records in index order, without calculating expected values.
|
|
141
|
+
* Raw JPEG bytes live in separate files; record JSON contains their paths.
|
|
142
|
+
*/
|
|
143
|
+
async saved(version = deploymentVersion) {
|
|
144
|
+
const path = Path.join(root, '1', version.toString());
|
|
145
|
+
const records: ImpulseRecord[] = [];
|
|
146
|
+
for (const segment of await fs.readdir(path, { withFileTypes: true })) {
|
|
147
|
+
if (!segment.isDirectory() || !/^\d+$/.test(segment.name)) continue;
|
|
148
|
+
for (const name of await fs.readdir(Path.join(path, segment.name))) {
|
|
149
|
+
// Segment directories also contain raw image files, which are not record JSON.
|
|
150
|
+
if (!name.endsWith('.json')) continue;
|
|
151
|
+
records.push(JSON.parse(await fs.readFile(Path.join(path, segment.name, name), 'utf8')));
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
records.sort((left, right) => left.index - right.index);
|
|
155
|
+
return { records };
|
|
156
|
+
},
|
|
157
|
+
/** Drain/stop the monitor, remove listeners and delete only this fixture's temporary root. */
|
|
158
|
+
async dispose() {
|
|
159
|
+
try {
|
|
160
|
+
await monitor.stop();
|
|
161
|
+
}
|
|
162
|
+
finally {
|
|
163
|
+
monitor.removeAllListeners();
|
|
164
|
+
await fs.rm(root, { recursive: true, force: true });
|
|
165
|
+
}
|
|
166
|
+
},
|
|
167
|
+
};
|
|
168
|
+
}
|