@yarkivaev/scada 2.6.7 → 2.6.8

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.7",
3
+ "version": "2.6.8",
4
4
  "description": "SCADA domain objects, state persistence, and plant monitoring",
5
5
  "repository": {
6
6
  "type": "git",
@@ -21,7 +21,7 @@ export const segmentColumns = ['machine', 'kind', 'name', 'start_time', 'end_tim
21
21
  export const segmentConflict = ['machine', 'kind', 'start_time'];
22
22
  export const segmentUpdateColumns = ['name', 'end_time', 'duration', 'options', 'resolved', 'updated_at'];
23
23
  export const splitUpdateColumns = ['name', 'end_time', 'duration', 'tags', 'options', 'properties', 'resolved'];
24
- export const segmentUpdateWhere = '(segments.duration = 0 OR EXCLUDED.duration > 0 OR EXCLUDED.end_time > segments.end_time) AND NOT (segments.duration > 0 AND EXCLUDED.duration = 0 AND COALESCE(segments.properties, \'\') LIKE \'%"split_of"%\')';
24
+ export const segmentUpdateWhere = '(segments.duration = 0 OR EXCLUDED.duration > 0 OR EXCLUDED.end_time > segments.end_time) AND NOT (segments.duration > 0 AND EXCLUDED.duration = 0 AND COALESCE(segments.properties, \'\') LIKE \'%"split_of"%\' AND EXISTS (SELECT 1 FROM segments AS kin WHERE kin.machine = segments.machine AND kin.kind = segments.kind AND kin.start_time > segments.start_time AND COALESCE(kin.properties, \'\') LIKE \'%"split_of"%\'))';
25
25
  export const segmentsIngestDestination = '/queue/scada.segments.ingest';
26
26
 
27
27
  export { default as segmentDispatch } from '../../../domain/segment/dispatch.js';
@@ -169,9 +169,9 @@ export default function historyPg(pool, { retentionDays = 30, now = Date.now, ob
169
169
  async work(machine) {
170
170
  machineName(machine);
171
171
  if (observationSource) { try { await assertHistoryReady(pool,machine); } catch (error) { if (error.statusCode===409) { return {items:[]}; } throw error; } }
172
- const fence = observationSource ? null : await historyFence(pool, machine);
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 ${fence === null ? '' : 'AND until_ms < $2'} ORDER BY until_ms DESC,created_at,id LIMIT 8`, fence === null ? [machine] : [machine, fence]);
174
+ FROM history_work WHERE machine=$1 AND until_ms < $2 ORDER BY until_ms DESC,created_at,id LIMIT 8`, [machine, fence]);
175
175
  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
176
  },
177
177
  commit(machine, workId, segments, base) { return commit(pool, machine, workId, segments, {base, observationSource,timeScale}); }
@@ -37,6 +37,10 @@ export async function assertHistoryReady(pool,machine) {
37
37
  const result=await pool.query('SELECT 1 FROM history_admissions a JOIN history_topics t ON t.topic=a.topic WHERE t.machine=$1 LIMIT 1',[machine]);
38
38
  if (result.rowCount) { const error=new Error('Unfinished external telemetry admission'); error.statusCode=409; throw error; }
39
39
  }
40
+ async function stamp(client,machine,time) {
41
+ await client.query('DELETE FROM history_observations WHERE machine=$1 AND channel=$2 AND time_ms<$3',[machine,'lead',time]);
42
+ await client.query('INSERT INTO history_observations(machine,channel,time_ms,event_id,value) VALUES($1,$2,$3,$4,0) ON CONFLICT DO NOTHING',[machine,'lead',time,`lead:${machine}:${time}`]);
43
+ }
40
44
  async function enqueue(client,mapping,row) {
41
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]);
42
46
  if(pending.rowCount){
@@ -68,6 +72,8 @@ export default function historyAdmission(pool,{now=Date.now,timeScale=1,timestam
68
72
  await client.query('UPDATE history_checkpoints SET valid=false WHERE machine=$1 AND time_ms>=$2',[mapping.machine,Math.min(...times)]);
69
73
  // eslint-disable-next-line no-await-in-loop
70
74
  await client.query('UPDATE history_work SET token=gen_random_uuid(),cursor_ms=NULL,end_ms=NULL,generation=gen_random_uuid(),classifier_checkpoint=NULL,correction_from=NULL,correction_until=NULL,started=false WHERE machine=$1 AND cursor_ms>$2',[mapping.machine,Math.min(...times)]);
75
+ // eslint-disable-next-line no-await-in-loop
76
+ await stamp(client,mapping.machine,Math.floor(Math.max(...times)/timeScale));
71
77
  }
72
78
  for (const row of eligible) {
73
79
  // eslint-disable-next-line no-await-in-loop
@@ -1,6 +1,7 @@
1
- /** Start of the furnace bucket that can still receive observations, in milliseconds. */
2
- export default async function historyFence(pool, machine) {
3
- const result = await pool.query(`SELECT floor(extract(epoch FROM clock_timestamp())*1000 / bucket) * bucket AS open_ms
4
- FROM (SELECT COALESCE((SELECT bucket_ms FROM history_topics WHERE machine=$1 LIMIT 1), 30000) AS bucket) sizes`, [machine]);
1
+ /** Start of the furnace bucket that can still receive observations, in the work time unit. */
2
+ export default async function historyFence(pool, machine, timeScale = 1) {
3
+ if (![1, 1000].includes(timeScale)) { throw new TypeError('External history supports millisecond or microsecond coordinates'); }
4
+ const result = await pool.query(`SELECT floor(extract(epoch FROM clock_timestamp()) * 1000 * $2 / bucket) * bucket AS open_ms
5
+ FROM (SELECT COALESCE((SELECT bucket_ms FROM history_topics WHERE machine=$1 LIMIT 1), 30000 * $2) AS bucket) sizes`, [machine, timeScale]);
5
6
  return Number(result.rows[0].open_ms);
6
7
  }