@yarkivaev/scada 2.6.39 → 2.6.40

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yarkivaev/scada",
3
- "version": "2.6.39",
3
+ "version": "2.6.40",
4
4
  "description": "SCADA domain objects, state persistence, and plant monitoring",
5
5
  "repository": {
6
6
  "type": "git",
@@ -30,7 +30,7 @@ function heartbeatKey(parsed) {
30
30
  * @returns {boolean}
31
31
  */
32
32
  function isHeartbeat(parsed) {
33
- return parsed.type === 'segment' && parsed.duration === 0;
33
+ return parsed.type === 'segment' && parsed.duration === 0 && parsed.properties?.phase_revision === undefined;
34
34
  }
35
35
 
36
36
  /**
@@ -96,7 +96,7 @@ export default function timelineRoute(basePath, plant, operatorOptions, decorate
96
96
  return kind.length > 0;
97
97
  });
98
98
  }
99
- const rows = await decorate(params.machineId, await result.machine.timeline.list(options));
99
+ const rows = await decorate(params.machineId, await result.machine.timeline.list(options), options);
100
100
  jsonResponse({ items: segmentItems(currentPhase ? currentPhase(params.machineId, rows) : rows) }).send(res);
101
101
  }),
102
102
  route('PATCH', `${basePath}/machines/:machineId/segments`, async (req, res, params) => {
@@ -15,14 +15,24 @@ export function phaseAnchors(raw) {
15
15
  }));
16
16
  }
17
17
 
18
+ function revision(properties) {
19
+ // Older property strings need not be valid JSON. The wire revision is a decimal string.
20
+ return `COALESCE(substring((${properties}) from '"phase_revision"[[:space:]]*:[[:space:]]*"([0-9]{1,16})"')::bigint,0)`;
21
+ }
22
+
23
+ function advancesPending(incoming, stored, nondecreasingEnd) {
24
+ return `(${revision(incoming)} > ${revision(stored)}
25
+ OR (${revision(incoming)} = ${revision(stored)} AND ${nondecreasingEnd}))`;
26
+ }
27
+
18
28
  const stage = `
19
29
  INSERT INTO contiguous_phase_inbox (machine, start_time, phase)
20
30
  VALUES ($1,$2,$3)
21
31
  ON CONFLICT (machine, start_time) DO UPDATE SET phase=EXCLUDED.phase
22
32
  WHERE (contiguous_phase_inbox.phase->>'duration')::numeric = 0
23
33
  AND ((EXCLUDED.phase->>'duration')::numeric > 0
24
- OR (EXCLUDED.phase->>'end_time')::timestamptz >=
25
- (contiguous_phase_inbox.phase->>'end_time')::timestamptz)`;
34
+ OR ${advancesPending("EXCLUDED.phase->>'properties'", "contiguous_phase_inbox.phase->>'properties'",
35
+ "(EXCLUDED.phase->>'end_time')::timestamptz >= (contiguous_phase_inbox.phase->>'end_time')::timestamptz")})`;
26
36
 
27
37
  const visible = `
28
38
  INSERT INTO segments (machine,kind,name,start_time,end_time,duration,options,tags,properties,resolved,updated_at)
@@ -33,7 +43,7 @@ const visible = `
33
43
  WHERE segments.duration=0
34
44
  AND (EXCLUDED.duration>0 OR
35
45
  ((segments.name=EXCLUDED.name OR segments.start_time=segments.end_time)
36
- AND segments.end_time<=EXCLUDED.end_time))`;
46
+ AND ${advancesPending('EXCLUDED.properties', 'segments.properties', 'segments.end_time<=EXCLUDED.end_time')}))`;
37
47
 
38
48
  function invalid(message) {
39
49
  return Object.assign(new RangeError(message), { permanent: true });