@yarkivaev/scada 2.6.28 → 2.6.38
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 +1 -1
- package/db/migrations/V0010__fold_phase_overlaps.sql +50 -0
- package/db/migrations/V0011__skip_covered_unsplit_phase.sql +32 -0
- package/db/migrations/V0012__drop_phase_geometry_triggers.sql +6 -0
- package/package.json +1 -1
- package/src/application/gateTimelineBuses.js +79 -0
- package/src/application/plantServer.js +2 -1
- package/src/application/runtime/deliveryOperations.js +24 -10
- package/src/application/runtime/deliveryPublications.js +16 -11
- package/src/application/sync/siteSyncTargets.js +3 -2
- package/src/domain/segment/dispatch.js +97 -8
- package/src/domain/segment/historyFold.js +227 -0
- package/src/infrastructure/ingest/pipelines/segmentBatch.js +25 -7
- package/src/infrastructure/ingest/pipelines/segmentPipeline.js +25 -10
- package/src/infrastructure/ingest/sinks/continuedSegments.js +31 -6
- package/src/infrastructure/ingest/sinks/foldCastingPhases.js +145 -0
- package/src/infrastructure/ingest/sinks/foldingSegments.js +86 -0
- package/src/infrastructure/ingest/sinks/segmentTransaction.js +6 -2
- package/src/infrastructure/ingest/sinks/userDecisionSink.js +10 -1
- package/src/infrastructure/persistence/pg/history.js +5 -3
- package/src/infrastructure/persistence/pg/historyAbsorb.js +111 -13
|
@@ -28,14 +28,23 @@ export default function userDecisionSink(pool) {
|
|
|
28
28
|
* @param {object} record - machine, startTime, username, operatorId, decidedAt, payload
|
|
29
29
|
*/
|
|
30
30
|
async accept({ machine, startTime, username, operatorId, decidedAt, payload }) {
|
|
31
|
+
const context = { machine, startTime, username, operatorId, decidedAt, payload };
|
|
31
32
|
try {
|
|
32
33
|
await applyDecision(pool, machine, startTime, payload);
|
|
34
|
+
} catch (error) {
|
|
35
|
+
if (!(error instanceof RangeError)) {
|
|
36
|
+
processingErrorLog('decision_sink_insert', error, context);
|
|
37
|
+
throw error;
|
|
38
|
+
}
|
|
39
|
+
processingErrorLog('decision_sink_refused', error, context);
|
|
40
|
+
}
|
|
41
|
+
try {
|
|
33
42
|
await pool.query(
|
|
34
43
|
'INSERT INTO user_decisions (machine, start_time, username, operator_id, decided_at, payload) VALUES ($1, $2, $3, $4, $5, $6)',
|
|
35
44
|
[machine, startTime, username, operatorId ?? null, decidedAt, payload]
|
|
36
45
|
);
|
|
37
46
|
} catch (error) {
|
|
38
|
-
processingErrorLog('decision_sink_insert', error,
|
|
47
|
+
processingErrorLog('decision_sink_insert', error, context);
|
|
39
48
|
throw error;
|
|
40
49
|
}
|
|
41
50
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import nativeHistoryBounds, {nativeTailProtocol} from './historyNativeBounds.js';
|
|
2
|
-
import historyAbsorb from './historyAbsorb.js';
|
|
2
|
+
import historyAbsorb, {historyDedupe, span} from './historyAbsorb.js';
|
|
3
3
|
import historyFence from './historyFence.js';
|
|
4
4
|
import retainMetricReceipts from '../../ingest/db/retainMetricReceipts.js';
|
|
5
5
|
import historyContext from './historyContext.js';
|
|
@@ -111,6 +111,7 @@ function commit(pool, machine, workId, segments, {base, observationSource,timeSc
|
|
|
111
111
|
}
|
|
112
112
|
if(base?.replace){
|
|
113
113
|
const next=await historyCorrection(client,machine,work.rows[0],segments,{base,context:latest,timeScale});
|
|
114
|
+
if (segments.length) { await historyDedupe(client, machine, span(segments)); }
|
|
114
115
|
await advance(client,machine,workId,next);
|
|
115
116
|
return {applied:true};
|
|
116
117
|
}
|
|
@@ -118,8 +119,8 @@ function commit(pool, machine, workId, segments, {base, observationSource,timeSc
|
|
|
118
119
|
SELECT machine,kind,start_time,name,end_time,duration,tags,properties,resolved FROM segments
|
|
119
120
|
WHERE machine=$1 AND start_time<=$3 AND end_time>=$2
|
|
120
121
|
ON CONFLICT(machine,kind,start_time,name) DO UPDATE SET end_time=greatest(history_annotations.end_time,EXCLUDED.end_time),duration=EXCLUDED.duration,tags=EXCLUDED.tags,properties=EXCLUDED.properties,resolved=EXCLUDED.resolved`, [machine,new Date((latest?.context.page.from ?? base.from)/timeScale),new Date((latest?.context.page.until ?? base.until)/timeScale)]);
|
|
121
|
-
await historyAbsorb(client, machine, segments);
|
|
122
|
-
for (const item of
|
|
122
|
+
const folded = await historyAbsorb(client, machine, segments);
|
|
123
|
+
for (const item of folded) {
|
|
123
124
|
// Only computed fields change. User cuts/labels must be applied by caller before commit.
|
|
124
125
|
// eslint-disable-next-line no-await-in-loop
|
|
125
126
|
await client.query(`INSERT INTO segments(machine,kind,name,start_time,end_time,duration,options,tags,properties,resolved,consumed)
|
|
@@ -133,6 +134,7 @@ function commit(pool, machine, workId, segments, {base, observationSource,timeSc
|
|
|
133
134
|
typeof item.tags === 'string' ? item.tags : JSON.stringify(item.tags ?? null),
|
|
134
135
|
typeof item.properties === 'string' ? item.properties : JSON.stringify(item.properties ?? null), item.resolved ?? true, item.consumed ?? true]);
|
|
135
136
|
}
|
|
137
|
+
if (segments.length) { await historyDedupe(client, machine, span(segments)); }
|
|
136
138
|
const converged=await saveCheckpoint(client,machine,base?.checkpoint,latest);
|
|
137
139
|
await advance(client,machine,workId,converged ? {next:null} : latest?.context.page);
|
|
138
140
|
return { applied: true };
|
|
@@ -1,18 +1,116 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import historyFold from '../../../domain/segment/historyFold.js';
|
|
2
|
+
|
|
3
|
+
export function span(segments) {
|
|
4
|
+
return segments.reduce((range, item) => {
|
|
5
|
+
return {
|
|
6
|
+
start: Math.min(range.start, new Date(item.start_time).getTime()),
|
|
7
|
+
end: Math.max(range.end, new Date(item.end_time).getTime())
|
|
8
|
+
};
|
|
9
|
+
}, { start: Infinity, end: -Infinity });
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function startOf(row) {
|
|
13
|
+
return new Date(row.start_time).getTime();
|
|
3
14
|
}
|
|
4
15
|
|
|
5
|
-
|
|
16
|
+
function windows(segments) {
|
|
17
|
+
const ordered = segments.map((item) => {
|
|
18
|
+
return { start: new Date(item.start_time).getTime(), end: new Date(item.end_time).getTime() };
|
|
19
|
+
}).sort((left, right) => { return left.start - right.start; });
|
|
20
|
+
return ordered.reduce((merged, range) => {
|
|
21
|
+
const last = merged.at(-1);
|
|
22
|
+
if (!last || range.start >= last.end) {
|
|
23
|
+
merged.push({ ...range });
|
|
24
|
+
return merged;
|
|
25
|
+
}
|
|
26
|
+
last.end = Math.max(last.end, range.end);
|
|
27
|
+
return merged;
|
|
28
|
+
}, []);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Fold later stream phases into stored rows, then drop absorbed starts. */
|
|
6
32
|
export default async function historyAbsorb(client, machine, segments) {
|
|
7
|
-
|
|
8
|
-
return
|
|
33
|
+
if (!segments.length) {
|
|
34
|
+
return [];
|
|
35
|
+
}
|
|
36
|
+
const pieces = windows(segments);
|
|
37
|
+
const starts = pieces.map((piece) => { return new Date(piece.start); });
|
|
38
|
+
const ends = pieces.map((piece) => { return new Date(piece.end); });
|
|
39
|
+
const stored = await client.query(
|
|
40
|
+
`SELECT DISTINCT ON (start_time) machine,kind,name,start_time,end_time,duration,options,tags,properties,resolved,consumed
|
|
41
|
+
FROM (
|
|
42
|
+
SELECT s.machine,s.kind,s.name,s.start_time,s.end_time,s.duration,s.options,s.tags,s.properties,s.resolved,s.consumed
|
|
43
|
+
FROM unnest($2::timestamptz[], $3::timestamptz[]) AS piece(start_time, end_time)
|
|
44
|
+
JOIN LATERAL (
|
|
45
|
+
SELECT * FROM segments
|
|
46
|
+
WHERE machine=$1 AND kind='phase' AND start_time <= piece.start_time
|
|
47
|
+
ORDER BY start_time DESC
|
|
48
|
+
LIMIT 1
|
|
49
|
+
) s ON s.end_time > piece.start_time
|
|
50
|
+
UNION ALL
|
|
51
|
+
SELECT s.machine,s.kind,s.name,s.start_time,s.end_time,s.duration,s.options,s.tags,s.properties,s.resolved,s.consumed
|
|
52
|
+
FROM segments AS s
|
|
53
|
+
JOIN unnest($2::timestamptz[], $3::timestamptz[]) AS piece(start_time, end_time)
|
|
54
|
+
ON s.start_time > piece.start_time AND s.start_time < piece.end_time
|
|
55
|
+
WHERE s.machine=$1 AND s.kind='phase'
|
|
56
|
+
UNION ALL
|
|
57
|
+
SELECT s.machine,s.kind,s.name,s.start_time,s.end_time,s.duration,s.options,s.tags,s.properties,s.resolved,s.consumed
|
|
58
|
+
FROM segments AS s
|
|
59
|
+
JOIN unnest($2::timestamptz[], $3::timestamptz[]) AS piece(start_time, end_time)
|
|
60
|
+
ON s.start_time <= piece.start_time
|
|
61
|
+
WHERE s.machine=$1 AND s.kind='phase' AND s.duration = 0
|
|
62
|
+
) hit
|
|
63
|
+
ORDER BY start_time`,
|
|
64
|
+
[machine, starts, ends]
|
|
65
|
+
);
|
|
66
|
+
const folded = historyFold(stored.rows, segments);
|
|
67
|
+
const kept = new Set(folded.map(startOf));
|
|
68
|
+
const dropped = stored.rows.filter((row) => {
|
|
69
|
+
return !kept.has(startOf(row));
|
|
9
70
|
});
|
|
10
|
-
if (
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
71
|
+
if (dropped.length) {
|
|
72
|
+
await client.query(
|
|
73
|
+
'DELETE FROM segments WHERE machine=$1 AND kind=\'phase\' AND start_time = ANY($2::timestamptz[])',
|
|
74
|
+
[machine, dropped.map((row) => { return row.start_time; })]
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
return folded;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Drops later unsplit same-tag starts that landed inside an earlier host. */
|
|
81
|
+
export async function historyDedupe(client, machine, range) {
|
|
82
|
+
const bounds = [machine, new Date(range.start), new Date(range.end)];
|
|
83
|
+
await client.query(
|
|
84
|
+
`UPDATE segments AS host
|
|
85
|
+
SET end_time = extra.end_time,
|
|
86
|
+
duration = CASE WHEN host.duration = 0 THEN 0
|
|
87
|
+
ELSE extract(epoch FROM extra.end_time - host.start_time) END,
|
|
88
|
+
updated_at = clock_timestamp()
|
|
89
|
+
FROM segments AS extra
|
|
90
|
+
WHERE host.machine = $1 AND extra.machine = $1
|
|
91
|
+
AND host.kind = 'phase' AND extra.kind = 'phase'
|
|
92
|
+
AND host.name = extra.name
|
|
93
|
+
AND coalesce(host.tags, '') = coalesce(extra.tags, '')
|
|
94
|
+
AND coalesce(host.properties, '') NOT LIKE '%"split_of"%'
|
|
95
|
+
AND coalesce(extra.properties, '') NOT LIKE '%"split_of"%'
|
|
96
|
+
AND extra.start_time > host.start_time
|
|
97
|
+
AND extra.start_time < host.end_time
|
|
98
|
+
AND extra.end_time > host.end_time
|
|
99
|
+
AND extra.start_time < $3 AND extra.end_time > $2`,
|
|
100
|
+
bounds
|
|
101
|
+
);
|
|
102
|
+
await client.query(
|
|
103
|
+
`DELETE FROM segments AS extra
|
|
104
|
+
USING segments AS host
|
|
105
|
+
WHERE extra.machine = $1 AND host.machine = $1
|
|
106
|
+
AND extra.kind = 'phase' AND host.kind = 'phase'
|
|
107
|
+
AND extra.name = host.name
|
|
108
|
+
AND coalesce(extra.tags, '') = coalesce(host.tags, '')
|
|
109
|
+
AND coalesce(extra.properties, '') NOT LIKE '%"split_of"%'
|
|
110
|
+
AND coalesce(host.properties, '') NOT LIKE '%"split_of"%'
|
|
111
|
+
AND extra.start_time > host.start_time
|
|
112
|
+
AND extra.start_time < host.end_time
|
|
113
|
+
AND extra.start_time < $3 AND extra.end_time > $2`,
|
|
114
|
+
bounds
|
|
115
|
+
);
|
|
18
116
|
}
|