gx
chenyc
5 天以前 747a86bb94006aaca721cfc0c0ce7061643a9ea6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
const assert = require('assert');
const { MetricAggregator } = require('../metric-aggregator');
 
describe('MetricAggregator', () => {
  it('merges latest metrics by device and always includes n', async () => {
    const flushed = [];
    const aggregator = new MetricAggregator({
      mode: 'batch',
      onFlush: async (device, payload) => {
        flushed.push({ device, payload });
        return true;
      },
    });
 
    aggregator.ingest({ deviceId: 'JH-001', ip: '127.0.0.1' }, { F: 24.6 });
    aggregator.ingest({ deviceId: 'JH-001', ip: '127.0.0.1' }, { A: 0, F: 25.1 });
    await aggregator.flush({ reason: 'test' });
 
    assert.strictEqual(flushed.length, 1);
    assert.deepStrictEqual(flushed[0].payload, {
      n: 'JH-001',
      F: 25.1,
      A: 0,
    });
  });
 
  it('flushes only dirty devices', async () => {
    const flushed = [];
    const aggregator = new MetricAggregator({
      mode: 'batch',
      onFlush: async (device, payload) => {
        flushed.push({ deviceId: device.deviceId, payload });
        return true;
      },
    });
 
    aggregator.ingest({ deviceId: 'JH-001' }, { F: 24.6 });
    await aggregator.flush({ reason: 'test-1' });
    await aggregator.flush({ reason: 'test-2' });
 
    assert.strictEqual(flushed.length, 1);
    assert.strictEqual(flushed[0].deviceId, 'JH-001');
  });
 
  it('keeps device dirty when flush callback reports failure', async () => {
    const flushed = [];
    const aggregator = new MetricAggregator({
      mode: 'batch',
      onFlush: async (_device, payload) => {
        flushed.push(payload);
        return false;
      },
    });
 
    aggregator.ingest({ deviceId: 'JH-001' }, { F: 24.6 });
    await aggregator.flush({ reason: 'test' });
 
    const snapshot = aggregator.getSnapshot('JH-001');
    assert.strictEqual(flushed.length, 1);
    assert.strictEqual(snapshot.dirty, true);
  });
 
  it('supports immediate mode', async () => {
    const flushed = [];
    const aggregator = new MetricAggregator({
      mode: 'immediate',
      onFlush: async (_device, payload) => {
        flushed.push(payload);
        return true;
      },
    });
 
    await aggregator.ingest({ deviceId: 'JH-001' }, { F: 24.6 });
 
    assert.strictEqual(flushed.length, 1);
    assert.deepStrictEqual(flushed[0], {
      n: 'JH-001',
      F: 24.6,
    });
  });
 
  it('waits a full flush interval before timer flushes newly cached data', async () => {
    const originalNow = Date.now;
    let now = 1_000;
    Date.now = () => now;
 
    try {
      const flushed = [];
      const aggregator = new MetricAggregator({
        mode: 'batch',
        flushIntervalMs: 60_000,
        onFlush: async (_device, payload) => {
          flushed.push(payload);
          return true;
        },
      });
 
      aggregator.ingest({ deviceId: 'JH-001' }, { F: 24.6 });
 
      now = 30_000;
      await aggregator.flush({ reason: 'timer' });
      assert.strictEqual(flushed.length, 0);
 
      now = 61_000;
      await aggregator.flush({ reason: 'timer' });
      assert.strictEqual(flushed.length, 1);
      assert.deepStrictEqual(flushed[0], {
        n: 'JH-001',
        F: 24.6,
      });
    } finally {
      Date.now = originalNow;
    }
  });
 
  it('preserves immediate flush intent when a second flush is queued during an ongoing flush', async () => {
    const flushed = [];
    let releaseFirstFlush = null;
    const firstFlushDone = new Promise((resolve) => {
      releaseFirstFlush = resolve;
    });
 
    const aggregator = new MetricAggregator({
      mode: 'batch',
      onFlush: async (_device, payload) => {
        flushed.push(payload);
 
        if (flushed.length === 1) {
          await firstFlushDone;
        }
 
        return true;
      },
    });
 
    aggregator.ingest({ deviceId: 'JH-001' }, { F: 24.6 });
    const firstFlushPromise = aggregator.flush({ reason: 'manual' });
 
    await new Promise((resolve) => setTimeout(resolve, 0));
 
    aggregator.ingest({ deviceId: 'JH-001' }, { N: 120, O: 80, P: 89 });
    const queuedFlushResult = await aggregator.flush({ reason: 'blood-pressure', deviceId: 'JH-001' });
 
    assert.strictEqual(queuedFlushResult, false);
 
    releaseFirstFlush();
    await firstFlushPromise;
 
    assert.strictEqual(flushed.length, 2);
    assert.deepStrictEqual(flushed[0], {
      n: 'JH-001',
      F: 24.6,
    });
    assert.deepStrictEqual(flushed[1], {
      n: 'JH-001',
      F: 24.6,
      N: 120,
      O: 80,
      P: 89,
    });
  });
});