@yarkivaev/scada 2.6.28 → 2.6.39

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.
Files changed (33) hide show
  1. package/README.md +1 -1
  2. package/db/migrations/V0010__fold_phase_overlaps.sql +50 -0
  3. package/db/migrations/V0011__skip_covered_unsplit_phase.sql +32 -0
  4. package/db/migrations/V0012__drop_phase_geometry_triggers.sql +6 -0
  5. package/db/migrations/V0013__contiguous_phase_inbox.sql +6 -0
  6. package/db/migrations/V0014__contiguous_phase_inbox_grants.sql +1 -0
  7. package/package.json +1 -1
  8. package/src/application/gateTimelineBuses.js +79 -0
  9. package/src/application/plantApi.js +20 -2
  10. package/src/application/plantServer.js +2 -1
  11. package/src/application/runtime/deliveryOperations.js +24 -10
  12. package/src/application/runtime/deliveryPublications.js +16 -11
  13. package/src/application/supervisorSink.js +2 -0
  14. package/src/application/sync/siteSyncTargets.js +3 -2
  15. package/src/domain/segment/dispatch.js +97 -8
  16. package/src/domain/segment/historyFold.js +227 -0
  17. package/src/infrastructure/http/plant/routes/timelineRoute.js +2 -2
  18. package/src/infrastructure/http/plant/streams/timelineStream.js +32 -3
  19. package/src/infrastructure/ingest/codecs/segmentCodec.js +19 -0
  20. package/src/infrastructure/ingest/pipelines/segmentBatch.js +25 -7
  21. package/src/infrastructure/ingest/pipelines/segmentPipeline.js +31 -12
  22. package/src/infrastructure/ingest/sinks/abutSegments.js +8 -4
  23. package/src/infrastructure/ingest/sinks/closeSilentOpen.js +4 -3
  24. package/src/infrastructure/ingest/sinks/contiguousPhases.js +171 -0
  25. package/src/infrastructure/ingest/sinks/continuedSegments.js +51 -7
  26. package/src/infrastructure/ingest/sinks/foldCastingPhases.js +147 -0
  27. package/src/infrastructure/ingest/sinks/foldingSegments.js +86 -0
  28. package/src/infrastructure/ingest/sinks/segmentTransaction.js +11 -5
  29. package/src/infrastructure/ingest/sinks/userDecisionSink.js +10 -1
  30. package/src/infrastructure/messaging/stomp/stompTimelineSegments.js +6 -2
  31. package/src/infrastructure/persistence/pg/history.js +7 -3
  32. package/src/infrastructure/persistence/pg/historyAbsorb.js +111 -13
  33. package/src/infrastructure/persistence/pg/segments.js +7 -0
@@ -1,18 +1,116 @@
1
- function mark(item) {
2
- return typeof item.tags === 'string' ? item.tags : JSON.stringify(item.tags ?? null);
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
- /** Drop same-name same-tag starts strictly inside a segment this commit already covers. */
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
- const covered = segments.filter((item) => { return item.start_time && item.end_time && item.name; }).map((item) => {
8
- return { name: item.name, start_time: item.start_time, end_time: item.end_time, tags: mark(item) };
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 (!covered.length) { return 0; }
11
- const removed = await client.query(`DELETE FROM segments AS stored WHERE stored.machine=$1 AND stored.kind='phase' AND EXISTS (
12
- SELECT 1 FROM json_array_elements($2::json) AS item
13
- WHERE stored.name=item->>'name'
14
- AND stored.start_time > (item->>'start_time')::timestamptz
15
- AND stored.start_time < (item->>'end_time')::timestamptz
16
- AND coalesce(stored.tags,'') = coalesce(item->>'tags',''))`, [machine, JSON.stringify(covered)]);
17
- return removed.rowCount;
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
  }
@@ -34,6 +34,13 @@ function reads(pool) {
34
34
  if (!Array.isArray(kinds) || kinds.length === 0) {
35
35
  return [];
36
36
  }
37
+ if (kinds.length === 1 && kinds[0] === 'phase') {
38
+ const result = await pool.query(
39
+ `SELECT kind, name, start_time, end_time, duration, options, tags, properties
40
+ FROM segments WHERE machine = $1 AND kind = 'phase' ORDER BY start_time DESC LIMIT 1`,
41
+ [machineId]);
42
+ return result.rows;
43
+ }
37
44
  const result = await pool.query(
38
45
  `SELECT DISTINCT ON (kind) kind, name, start_time, end_time, duration, options, tags, properties
39
46
  FROM segments WHERE machine = $1 AND kind = ANY($2)