@yarkivaev/scada 2.6.8 → 2.6.11
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 +1 -1
- package/src/infrastructure/ingest/codecs/segmentCodec.js +2 -1
- package/src/infrastructure/ingest/pipelines/decisionPipeline.js +2 -3
- package/src/infrastructure/ingest/sinks/decisionSegment.js +49 -0
- package/src/infrastructure/ingest/sinks/userDecisionSink.js +6 -4
- package/src/infrastructure/persistence/pg/history.js +2 -1
- package/src/infrastructure/persistence/pg/historyAdmission.js +1 -1
- package/src/infrastructure/persistence/pg/historyCheckpoint.js +1 -1
package/package.json
CHANGED
|
@@ -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
|
-
|
|
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
|
|
10
|
-
*
|
|
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
|
|
5
|
+
* Sink for operator decisions from the user_decisions queue.
|
|
5
6
|
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
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
|
-
*
|
|
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]
|
|
@@ -171,7 +171,8 @@ export default function historyPg(pool, { retentionDays = 30, now = Date.now, ob
|
|
|
171
171
|
if (observationSource) { try { await assertHistoryReady(pool,machine); } catch (error) { if (error.statusCode===409) { return {items:[]}; } throw error; } }
|
|
172
172
|
const fence = await historyFence(pool, machine, timeScale);
|
|
173
173
|
const result = await pool.query(`SELECT id,token,machine AS "streamId",from_ms AS "from",until_ms AS "until",cursor_ms AS cursor,end_ms AS end
|
|
174
|
-
FROM history_work WHERE machine=$1 AND until_ms < $2
|
|
174
|
+
FROM history_work WHERE machine=$1 AND until_ms < $2 - COALESCE((SELECT bucket_ms FROM history_topics WHERE machine=$1 LIMIT 1), 30000 * $3)
|
|
175
|
+
ORDER BY until_ms ASC,created_at,id LIMIT 8`, [machine, fence, timeScale]);
|
|
175
176
|
return { items: result.rows.map((row) => { return { ...row, from: Number(row.from), until: Number(row.until),cursor:row.cursor === null ? null : Number(row.cursor),end:row.end === null ? null : Number(row.end) }; }) };
|
|
176
177
|
},
|
|
177
178
|
commit(machine, workId, segments, base) { return commit(pool, machine, workId, segments, {base, observationSource,timeScale}); }
|
|
@@ -44,7 +44,7 @@ async function stamp(client,machine,time) {
|
|
|
44
44
|
async function enqueue(client,mapping,row) {
|
|
45
45
|
const pending=await client.query('SELECT id FROM history_work WHERE machine=$1 AND cursor_ms IS NULL AND end_ms IS NULL AND floor(from_ms::numeric/$2)=floor($3::numeric/$2) ORDER BY created_at LIMIT 1',[mapping.machine,mapping.bucket_ms,row.time]);
|
|
46
46
|
if(pending.rowCount){
|
|
47
|
-
await client.query('UPDATE history_work SET
|
|
47
|
+
await client.query('UPDATE history_work SET from_ms=least(from_ms,$2),until_ms=greatest(until_ms,$2) WHERE id=$1',[pending.rows[0].id,row.time]);
|
|
48
48
|
}else{await client.query('INSERT INTO history_work(id,machine,from_ms,until_ms) VALUES(gen_random_uuid(),$1,$2,$2)',[mapping.machine,row.time]);}
|
|
49
49
|
}
|
|
50
50
|
/** Durable queue redelivery owns payload recovery. Barriers never expire on a timer. */
|
|
@@ -21,7 +21,7 @@ export async function classifierSeed(pool,machine,before,key,{claim,cursor}={})
|
|
|
21
21
|
return result.rowCount ? {at:Number(result.rows[0].time_ms),state:result.rows[0].state} : null;
|
|
22
22
|
}
|
|
23
23
|
export async function classifierPage(pool,machine,page,key) {
|
|
24
|
-
const target=await pool.query('SELECT time_ms FROM history_checkpoints WHERE machine=$1 AND classifier_key=$2 AND time_ms BETWEEN $3 AND $4 ORDER BY time_ms LIMIT 1',[machine,key,page.from,page.until]);
|
|
24
|
+
const target=await pool.query('SELECT time_ms FROM history_checkpoints WHERE machine=$1 AND classifier_key=$2 AND valid AND time_ms BETWEEN $3 AND $4 ORDER BY time_ms LIMIT 1',[machine,key,page.from,page.until]);
|
|
25
25
|
if (!target.rowCount || Number(target.rows[0].time_ms)===page.until) { return page; }
|
|
26
26
|
const until=Number(target.rows[0].time_ms);
|
|
27
27
|
return {...page,until,next:until+1};
|