-
Notifications
You must be signed in to change notification settings - Fork 9
/
subscribe.js
52 lines (46 loc) · 1.19 KB
/
subscribe.js
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
const { Client } = require("pg");
const State = require("./state");
async function main() {
const client = new Client({
user: "MATERIALIZE_USERNAME",
password: "MATERIALIZE_PASSWORD",
host: "MATERIALIZE_HOST",
port: 6875,
database: 'materialize',
ssl: true
});
await client.connect();
await client.query("BEGIN");
await client.query("DECLARE c CURSOR FOR SUBSCRIBE counter_sum WITH (PROGRESS)");
let buffer = [];
const state = new State();
// Loop indefinitely
while (true) {
const { rows } = await client.query('FETCH ALL c');
rows.forEach(row => {
// Map row fields
const {
mz_timestamp: ts,
mz_progressed: progress,
mz_diff: diff,
sum,
} = row;
// When a progress is detected, get the last values
if (progress) {
if (buffer.length > 0) {
try {
state.update(buffer, ts);
} catch (err) {
console.error(err);
} finally {
console.log("State: ", state.getState());
buffer.splice(0, buffer.length);
}
}
} else {
buffer.push({ value: { sum }, diff });
}
});
}
}
main();