@yarkivaev/scada 2.6.19 → 2.6.20

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.19",
3
+ "version": "2.6.20",
4
4
  "description": "SCADA domain objects, state persistence, and plant monitoring",
5
5
  "repository": {
6
6
  "type": "git",
@@ -7,7 +7,11 @@ export async function classifierClaim(pool,machine,{workId,workToken,from,until,
7
7
  if(!workId){return null;}
8
8
  const result=await pool.query('SELECT classifier_checkpoint,started,from_ms,until_ms,cursor_ms,end_ms FROM history_work WHERE id=$1 AND machine=$2 AND token=$3',[workId,machine,workToken]);
9
9
  const row=result.rows[0];
10
- if(!row || Number(row.from_ms)!==from || Number(row.until_ms)!==until || (row.cursor_ms===null?null:Number(row.cursor_ms))!==cursor || (row.end_ms===null?null:Number(row.end_ms))!==end){const error=new Error('History work claim changed');error.statusCode=409;throw error;}
10
+ const stored=row && (row.cursor_ms===null?null:Number(row.cursor_ms));
11
+ const bound=row && (row.end_ms===null?null:Number(row.end_ms));
12
+ if(!row || Number(row.from_ms)!==from || Number(row.until_ms)!==until || stored!==cursor || (end!==null && bound!==end)){
13
+ const error=new Error('History work claim changed');error.statusCode=409;throw error;
14
+ }
11
15
  return row;
12
16
  }
13
17
  export async function classifierSeed(pool,machine,before,key,{claim,cursor}={}) {
@@ -1,4 +1,4 @@
1
- import { createHash } from 'node:crypto';
1
+ import historyRevision from './historyRevision.js';
2
2
  import historySeed from './historySeed.js';
3
3
  import historyObservationsPg from './historyObservations.js';
4
4
  import {classifierClaim,classifierPage,classifierSeed} from './historyCheckpoint.js';
@@ -24,6 +24,8 @@ export default async function historyContext(pool,machine,from,until,{padding,cu
24
24
  const range = { from,until,paddingMs:padding,cursor,end:envelope,...(classifierKey && {classifierKey}),...(timeScale!==1 && {timeScale}),...(workId && {workId,workToken}) };
25
25
  if ((cursor !== null && !Number.isSafeInteger(cursor)) || (envelope !== null && !Number.isSafeInteger(envelope))) { throw new TypeError('Invalid history cursor'); }
26
26
  const claim=await classifierClaim(pool,machine,{workId,workToken,from,until,cursor,end:envelope});
27
+ const held=claim && envelope===null && claim.end_ms!==null ? Number(claim.end_ms) : envelope;
28
+ if (held!==null && envelope===null) { range.end=held; }
27
29
  if (from<cutoff && !claim) {
28
30
  const pending = cursor === null || envelope === null ? {rowCount:0} : await pool.query(`SELECT 1 FROM history_work WHERE machine=$1 AND from_ms=$2 AND until_ms=$3 AND cursor_ms=$4 AND end_ms=$5 LIMIT 1`,[machine,from,until,cursor,envelope]);
29
31
  if (!pending.rowCount) { return {available:false,items:[],segments:[],annotations:[],context:range}; }
@@ -31,13 +33,14 @@ export default async function historyContext(pool,machine,from,until,{padding,cu
31
33
  const classifier=classifierKey ? await classifierSeed(pool,machine,cursor ?? from,classifierKey,{claim,cursor}) : null;
32
34
  let left = cursor ?? from-padding;
33
35
  if (classifierKey && cursor===null) { left=classifier ? classifier.at+1 : await source.start(machine); }
34
- const right = classifierKey && envelope!==null ? envelope : Math.max(envelope ?? until,await source.boundary(machine,from,until,padding));
35
- const previous = await pool.query(`SELECT kind,name,start_time,end_time FROM segments WHERE machine=$1 AND kind='phase' AND start_time<=$2 ORDER BY start_time DESC LIMIT 1`,[machine,new Date(right/timeScale)]);
36
- const tail=classifierKey ? await pool.query("SELECT max(end_time) AS end_time FROM segments WHERE machine=$1 AND kind='phase'",[machine]) : null;
37
- const extended = classifierKey && envelope!==null ? envelope : Math.max(envelope ?? right,await segmentBoundary(pool,machine,right,previous.rows,timeScale),tail?.rows[0]?.end_time ? new Date(tail.rows[0].end_time).getTime()*timeScale : -Infinity);
36
+ const right = classifierKey && held!==null ? held : Math.max(held ?? until,await source.boundary(machine,from,until,padding));
37
+ const previous = await pool.query(`SELECT kind,name,start_time,end_time,duration FROM segments WHERE machine=$1 AND kind='phase' AND start_time<=$2 ORDER BY start_time DESC LIMIT 1`,[machine,new Date(right/timeScale)]);
38
+ const prior=previous.rows.at(-1);
39
+ const edge=classifierKey && prior && Number(prior.duration)===0 ? right : await segmentBoundary(pool,machine,right,previous.rows,timeScale);
40
+ const extended = classifierKey && held!==null ? held : Math.max(held ?? right,edge);
38
41
  const fence = !classifierKey && timeScale===1 ? await historyFence(pool, machine) : null;
39
42
  const end = fence!==null && until<fence ? Math.min(extended, fence-1) : extended;
40
- if(classifierKey && claim && envelope===null){
43
+ if(classifierKey && claim && held===null){
41
44
  await pool.query('UPDATE history_work SET end_ms=$3 WHERE id=$1 AND machine=$2',[workId,machine,end]);
42
45
  range.end=end;
43
46
  }
@@ -64,5 +67,5 @@ export default async function historyContext(pool,machine,from,until,{padding,cu
64
67
  const last = observations.items.at(-1)?.time;
65
68
  const rightClosed = next === null && last !== undefined && segments.rows.some((row) => { return row.duration>0 && new Date(row.end_time).getTime()*timeScale>=last; });
66
69
  const snapshot = { page,initial,...(classifierKey && {classifierSeed:classifier}),annotations:annotations.rows,segments:segments.rows,...observations,rightClosed };
67
- return { ...snapshot,available:true,revision:createHash('sha256').update(JSON.stringify(snapshot)).digest('hex'),context:range };
70
+ return { ...snapshot,available:true,revision:historyRevision(snapshot, until, timeScale),context:range };
68
71
  }
@@ -0,0 +1,34 @@
1
+ import { createHash } from 'node:crypto';
2
+
3
+ function closedContinuity(rows, until) {
4
+ return (rows ?? []).flatMap((row) => {
5
+ const from = Number(row.from);
6
+ if (from > until) { return []; }
7
+ return [{ from, until: Math.min(Number(row.until), until) }];
8
+ });
9
+ }
10
+ function frozen(until, timeScale, row) {
11
+ if (!row?.end_time) { return row; }
12
+ const edge = new Date(row.end_time).getTime() * timeScale;
13
+ if (edge <= until) { return row; }
14
+ return { ...row, end_time: new Date(until / timeScale) };
15
+ }
16
+
17
+ /**
18
+ * Closed-page identity. Observations after until cannot change the claim.
19
+ *
20
+ * {{{
21
+ * historyRevision({ items, segments, annotations, continuity }, until, timeScale)
22
+ * }}}
23
+ */
24
+ export default function historyRevision(snapshot, until, timeScale = 1) {
25
+ if (!Number.isSafeInteger(until)) { throw new TypeError('Closed history until required'); }
26
+ const items = (snapshot.items ?? []).filter((row) => { return Number(row.time) <= until; });
27
+ return createHash('sha256').update(JSON.stringify({
28
+ initial: snapshot.initial,
29
+ annotations: (snapshot.annotations ?? []).map((row) => { return frozen(until, timeScale, row); }),
30
+ segments: (snapshot.segments ?? []).map((row) => { return frozen(until, timeScale, row); }),
31
+ items,
32
+ continuity: closedContinuity(snapshot.continuity, until)
33
+ })).digest('hex');
34
+ }