@yarkivaev/scada 2.6.8 → 2.6.10

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.8",
3
+ "version": "2.6.10",
4
4
  "description": "SCADA domain objects, state persistence, and plant monitoring",
5
5
  "repository": {
6
6
  "type": "git",
@@ -29,7 +29,8 @@ function familyRecord(parsed) {
29
29
  export function decodeSegment(raw) {
30
30
  try {
31
31
  const parsed = JSON.parse(raw.payload);
32
- return parsed.type === 'split-family' ? familyRecord(parsed) : segmentNormalize(parsed);
32
+ if (parsed.type === 'split-family') { return familyRecord(parsed); }
33
+ return segmentNormalize(parsed);
33
34
  } catch (error) {
34
35
  error.permanent = true;
35
36
  throw error;
@@ -6,9 +6,8 @@ import userDecisionSink from '../sinks/userDecisionSink.js';
6
6
  /**
7
7
  * Pipeline for logging operator tag decisions from RabbitMQ to PostgreSQL.
8
8
  *
9
- * Subscribes to the user_decisions STOMP exchange and inserts each received
10
- * decision into the user_decisions audit table. Decisions carry machine,
11
- * start timestamp, operator identity, and full JSON payload.
9
+ * Subscribes to the user_decisions queue, applies each decision to segments,
10
+ * and appends the same payload to the user_decisions audit table.
12
11
  *
13
12
  * @example
14
13
  * const pipeline = decisionPipeline(
@@ -0,0 +1,49 @@
1
+ import { checkSplit, familyCover, loadFamily, replaceSplit, splitRows } from '../../../domain/segment/splitFamily.js';
2
+
3
+ function piece(raw) {
4
+ return {
5
+ start: new Date(Number(raw.start) * 1000),
6
+ end: new Date(Number(raw.end) * 1000),
7
+ tag: raw.tag || '',
8
+ open: Boolean(raw.open)
9
+ };
10
+ }
11
+
12
+ async function applySplit(pool, machine, start, splits) {
13
+ const family = await loadFamily(pool, machine, start);
14
+ if (family.length === 0) {
15
+ return;
16
+ }
17
+ const ordered = checkSplit(familyCover(family), splits.map(piece));
18
+ const [first] = ordered;
19
+ await replaceSplit(pool, { machine, origin: first.start.getTime(), pieces: splitRows(machine, ordered) });
20
+ }
21
+
22
+ /**
23
+ * Applies one operator decision to the segment rows it names.
24
+ * A tag list replaces the stored tags, including an empty list.
25
+ * A split rewrites the off family and keeps each piece tag.
26
+ *
27
+ * @param {object} pool - pg pool
28
+ * @param {string} machine - machine id
29
+ * @param {string} startTime - segment start as an ISO timestamp
30
+ * @param {string} payload - raw user_decisions JSON
31
+ * @returns {Promise<void>}
32
+ *
33
+ * @example
34
+ * await applyDecision(pool, 'icht2', '2026-11-17T07:17:09.930Z', '{"tags":["to_ladle"]}');
35
+ */
36
+ export async function applyDecision(pool, machine, startTime, payload) {
37
+ const body = JSON.parse(payload);
38
+ if (Array.isArray(body.splits)) {
39
+ await applySplit(pool, machine, new Date(startTime), body.splits);
40
+ return;
41
+ }
42
+ if (!Array.isArray(body.tags)) {
43
+ return;
44
+ }
45
+ await pool.query(
46
+ 'UPDATE segments SET tags = $1, properties = $2 WHERE machine = $3 AND start_time = $4 AND kind = \'phase\'',
47
+ [JSON.stringify(body.tags), JSON.stringify(body.properties || {}), machine, startTime]
48
+ );
49
+ }
@@ -1,10 +1,11 @@
1
1
  import processingErrorLog from '../processingErrorLog.js';
2
+ import { applyDecision } from './decisionSegment.js';
2
3
 
3
4
  /**
4
- * Sink for inserting user tag decisions into the user_decisions audit table.
5
+ * Sink for operator decisions from the user_decisions queue.
5
6
  *
6
- * Each accepted record is persisted with machine, start_time, username,
7
- * operator_id, decided_at, and the full raw JSON payload.
7
+ * The decision is applied to segments first, then appended to the audit table.
8
+ * A tag list replaces stored tags. A split rewrites the matching off family.
8
9
  *
9
10
  * @example
10
11
  * const sink = userDecisionSink(pool);
@@ -22,12 +23,13 @@ export default function userDecisionSink(pool) {
22
23
  }
23
24
  return {
24
25
  /**
25
- * Inserts a user decision record into the user_decisions table.
26
+ * Applies the decision to segments, then inserts the audit row.
26
27
  *
27
28
  * @param {object} record - machine, startTime, username, operatorId, decidedAt, payload
28
29
  */
29
30
  async accept({ machine, startTime, username, operatorId, decidedAt, payload }) {
30
31
  try {
32
+ await applyDecision(pool, machine, startTime, payload);
31
33
  await pool.query(
32
34
  'INSERT INTO user_decisions (machine, start_time, username, operator_id, decided_at, payload) VALUES ($1, $2, $3, $4, $5, $6)',
33
35
  [machine, startTime, username, operatorId ?? null, decidedAt, payload]