@yarkivaev/scada 2.6.12 → 2.6.13

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.12",
3
+ "version": "2.6.13",
4
4
  "description": "SCADA domain objects, state persistence, and plant monitoring",
5
5
  "repository": {
6
6
  "type": "git",
@@ -1,7 +1,8 @@
1
1
  /**
2
2
  * Pulls a later fragment of the same segment back onto the previous row
3
3
  * when the uncovered gap is still inside the segmentation continuity budget.
4
- * A supervisor restart then does not open a hole where metric samples continued.
4
+ * An overlapping island that starts before the pending heartbeat end stays
5
+ * one segment instead of a second heating.
5
6
  *
6
7
  * @param {number} budgetSeconds - maximum uncovered gap that stays one segment
7
8
  * @returns {function} apply(record, prior) -> record
@@ -19,8 +20,12 @@ export default function segmentContinue(budgetSeconds) {
19
20
  if (priorStart === record.start_time) {
20
21
  return record;
21
22
  }
22
- const gap = Date.parse(record.start_time) - Date.parse(prior.end_time);
23
- if (gap < 0 || gap > budgetSeconds * 1000) {
23
+ const incomingMs = Date.parse(record.start_time);
24
+ if (incomingMs < Date.parse(priorStart)) {
25
+ return record;
26
+ }
27
+ const gap = incomingMs - Date.parse(prior.end_time);
28
+ if (gap > budgetSeconds * 1000) {
24
29
  return record;
25
30
  }
26
31
  const startMs = Date.parse(priorStart);
@@ -2,16 +2,18 @@
2
2
  * Maps a timeline segment row to REST JSON shape.
3
3
  *
4
4
  * @param {object} row - segment with start_time, end_time, duration
5
+ * @param {function} [clock] - paints duration-zero end as now when provided
5
6
  * @returns {object} JSON-serializable segment
6
7
  *
7
8
  * @example
8
9
  * segmentJson({ name: 'on', start_time: new Date(), end_time: new Date(), duration: 60 });
9
10
  */
10
- export default function segmentJson(row) {
11
+ export default function segmentJson(row, clock) {
12
+ const live = row.duration === 0 && typeof clock === 'function';
11
13
  const mapped = {
12
14
  name: row.name,
13
15
  start: row.start_time.toISOString(),
14
- end: row.duration === 0 ? new Date().toISOString() : row.end_time.toISOString(),
16
+ end: live ? clock().toISOString() : row.end_time.toISOString(),
15
17
  duration: row.duration
16
18
  };
17
19
  if (row.kind) {
@@ -28,3 +30,31 @@ export default function segmentJson(row) {
28
30
  }
29
31
  return mapped;
30
32
  }
33
+
34
+ /**
35
+ * Maps GET /segments rows so only the last duration-zero end is painted as now.
36
+ *
37
+ * @param {object[]} rows - timeline rows
38
+ * @param {function} [clock] - now source for the live pending row
39
+ * @returns {object[]} JSON-serializable segments
40
+ *
41
+ * @example
42
+ * segmentItems(rows);
43
+ */
44
+ export function segmentItems(rows, clock = () => {
45
+ return new Date();
46
+ }) {
47
+ const last = rows.reduce((index, row, i) => {
48
+ return row.duration === 0 ? i : index;
49
+ }, -1);
50
+ return rows.map((row, i) => {
51
+ if (i === last) {
52
+ return segmentJson(row, clock);
53
+ }
54
+ if (row.duration !== 0) {
55
+ return segmentJson(row);
56
+ }
57
+ const elapsed = Math.max(1, (Date.parse(row.end_time) - Date.parse(row.start_time)) / 1000);
58
+ return segmentJson({ ...row, duration: elapsed });
59
+ });
60
+ }
@@ -1,6 +1,6 @@
1
1
  import machineInPlant from '../../../../application/machineInPlant.js';
2
2
  import allowedSegmentTags from '../allowedSegmentTags.js';
3
- import segmentJson from '../json/segmentJson.js';
3
+ import { segmentItems } from '../json/segmentJson.js';
4
4
  import timelineOperator from '../timelineOperator.js';
5
5
  import { errorResponse, jsonResponse, readBody, route, sendRouteError } from '@yarkivaev/simple-server';
6
6
 
@@ -97,7 +97,7 @@ export default function timelineRoute(basePath, plant, operatorOptions, decorate
97
97
  });
98
98
  }
99
99
  const rows = await decorate(params.machineId, await result.machine.timeline.list(options));
100
- jsonResponse({ items: rows.map(segmentJson) }).send(res);
100
+ jsonResponse({ items: segmentItems(rows) }).send(res);
101
101
  }),
102
102
  route('PATCH', `${basePath}/machines/:machineId/segments`, async (req, res, params) => {
103
103
  const result = machineInPlant(plant, params.machineId);
@@ -98,10 +98,11 @@ export default function segmentPipeline(stomp, postgres, pool, config) {
98
98
  destination,
99
99
  async start() {
100
100
  if (settings.repairHistoryOnStart !== false) {
101
- const repair = abutSegments(pool, budget);
102
- void repair.catch((error) => {
101
+ try {
102
+ await abutSegments(pool, budget);
103
+ } catch (error) {
103
104
  processingErrorLog('abut_segments', error, { budgetSeconds: budget });
104
- });
105
+ }
105
106
  }
106
107
  await source.start();
107
108
  await silence.start();
@@ -1,18 +1,4 @@
1
- /**
2
- * Closes uncovered time between adjacent same-name segments that stayed
3
- * inside the continuity budget. Existing restart fragments then meet on
4
- * the timeline without deleting rows.
5
- *
6
- * @param {object} pool - pg pool
7
- * @param {number} budgetSeconds - maximum gap that still counts as continuous
8
- * @returns {Promise<void>}
9
- *
10
- * @example
11
- * await abutSegments(pool, 30);
12
- */
13
- export default async function abutSegments(pool, budgetSeconds) {
14
- await pool.query(
15
- `UPDATE segments AS left_row
1
+ const GAP_SQL = `UPDATE segments AS left_row
16
2
  SET end_time = next_row.next_start,
17
3
  duration = EXTRACT(EPOCH FROM (next_row.next_start - left_row.start_time))
18
4
  FROM (
@@ -27,7 +13,65 @@ export default async function abutSegments(pool, budgetSeconds) {
27
13
  AND left_row.duration > 0
28
14
  AND left_row.name = next_row.next_name
29
15
  AND next_row.next_start > left_row.end_time
30
- AND next_row.next_start - left_row.end_time <= make_interval(secs => $1)`,
31
- [budgetSeconds]
32
- );
16
+ AND next_row.next_start - left_row.end_time <= make_interval(secs => $1)`;
17
+
18
+ const ABSORB_SQL = `UPDATE segments AS left_row
19
+ SET end_time = CASE
20
+ WHEN next_row.next_duration = 0 THEN next_row.next_end
21
+ WHEN next_row.next_end > left_row.end_time THEN next_row.next_end
22
+ ELSE left_row.end_time
23
+ END,
24
+ duration = CASE
25
+ WHEN next_row.next_duration = 0 THEN 0
26
+ ELSE EXTRACT(EPOCH FROM (
27
+ CASE
28
+ WHEN next_row.next_end > left_row.end_time THEN next_row.next_end
29
+ ELSE left_row.end_time
30
+ END - left_row.start_time
31
+ ))
32
+ END
33
+ FROM (
34
+ SELECT machine, kind, start_time, name, end_time, duration,
35
+ lead(start_time) OVER (PARTITION BY machine, kind ORDER BY start_time) AS next_start,
36
+ lead(name) OVER (PARTITION BY machine, kind ORDER BY start_time) AS next_name,
37
+ lead(end_time) OVER (PARTITION BY machine, kind ORDER BY start_time) AS next_end,
38
+ lead(duration) OVER (PARTITION BY machine, kind ORDER BY start_time) AS next_duration
39
+ FROM segments
40
+ ) AS next_row
41
+ WHERE left_row.machine = next_row.machine
42
+ AND left_row.kind = next_row.kind
43
+ AND left_row.start_time = next_row.start_time
44
+ AND left_row.name = next_row.next_name
45
+ AND next_row.next_start > left_row.start_time
46
+ AND next_row.next_start <= left_row.end_time + make_interval(secs => $1)`;
47
+
48
+ const COLLAPSE_SQL = `DELETE FROM segments AS later
49
+ USING (
50
+ SELECT machine, kind, name, end_time,
51
+ lead(start_time) OVER (PARTITION BY machine, kind ORDER BY start_time) AS next_start,
52
+ lead(name) OVER (PARTITION BY machine, kind ORDER BY start_time) AS next_name
53
+ FROM segments
54
+ ) AS pair
55
+ WHERE later.machine = pair.machine
56
+ AND later.kind = pair.kind
57
+ AND later.start_time = pair.next_start
58
+ AND pair.name = pair.next_name
59
+ AND pair.next_start <= pair.end_time + make_interval(secs => $1)`;
60
+
61
+ /**
62
+ * Closes uncovered time between adjacent same-name segments that stayed
63
+ * inside the continuity budget, then collapses the later overlapping
64
+ * same-name fragment into the earlier start.
65
+ *
66
+ * @param {object} pool - pg pool
67
+ * @param {number} budgetSeconds - maximum gap that still counts as continuous
68
+ * @returns {Promise<void>}
69
+ *
70
+ * @example
71
+ * await abutSegments(pool, 30);
72
+ */
73
+ export default async function abutSegments(pool, budgetSeconds) {
74
+ await pool.query(GAP_SQL, [budgetSeconds]);
75
+ await pool.query(ABSORB_SQL, [budgetSeconds]);
76
+ await pool.query(COLLAPSE_SQL, [budgetSeconds]);
33
77
  }