@yarkivaev/scada 2.6.27 → 2.6.28

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.27",
3
+ "version": "2.6.28",
4
4
  "description": "SCADA domain objects, state persistence, and plant monitoring",
5
5
  "repository": {
6
6
  "type": "git",
@@ -10,20 +10,20 @@ import { replaceSplit } from './splitFamily.js';
10
10
  * @returns {Promise<void>}
11
11
  */
12
12
  async function pendingInClient(segmentSink, closer, record, transaction) {
13
- await closer.close(record.machine, record.start_time, record.kind || 'phase', transaction);
13
+ await closer.close(record.machine, record.start_time, record.kind || 'phase', transaction, record.name);
14
14
  await segmentSink.write([record], transaction);
15
15
  }
16
16
  async function persistPending(segmentSink, closer, pool, record) {
17
17
  const kind = record.kind || 'phase';
18
18
  if (!pool || typeof pool.connect !== 'function') {
19
- await closer.close(record.machine, record.start_time, kind);
19
+ await closer.close(record.machine, record.start_time, kind, null, record.name);
20
20
  await segmentSink.write([record]);
21
21
  return;
22
22
  }
23
23
  const client = await pool.connect();
24
24
  try {
25
25
  await client.query('BEGIN');
26
- await closer.close(record.machine, record.start_time, kind, client);
26
+ await closer.close(record.machine, record.start_time, kind, client, record.name);
27
27
  await segmentSink.write([record], client);
28
28
  await client.query('COMMIT');
29
29
  } catch (error) {
@@ -1,29 +1,37 @@
1
1
  import processingErrorLog from '../processingErrorLog.js';
2
2
 
3
3
  /**
4
- * Closes stale open segment rows for one machine before a new open segment lands.
4
+ * Closes a previous different-name open and drops nested later opens
5
+ * before a new pending heartbeat lands. A same-name covering open stays.
5
6
  *
6
7
  * @param {object} pool - pg Pool with query() method
7
- * @returns {object} closer with close(machine, startTime, kind, client?)
8
+ * @returns {object} closer with close(machine, startTime, kind, client?, name?)
8
9
  *
9
10
  * @example
10
11
  * const closer = closeOrphanOpen(pool);
11
- * await closer.close('m1', '2024-01-01T00:00:00.000Z', 'phase');
12
+ * await closer.close('m1', '2024-01-01T00:00:00.000Z', 'phase', null, 'off');
12
13
  */
13
14
  export default function closeOrphanOpen(pool) {
14
15
  if (!pool || typeof pool.query !== 'function') {
15
16
  throw new Error('Pool must have a query() method');
16
17
  }
17
18
  return {
18
- async close(machine, startTime, kind, client) {
19
+ async close(machine, startTime, kind, client, name) {
19
20
  const track = kind || 'phase';
20
21
  const runner = client && typeof client.query === 'function' ? client : pool;
21
22
  try {
22
23
  await runner.query(
23
- `UPDATE segments
24
- SET end_time = start_time + interval '1 second', duration = 1
25
- WHERE machine = $1 AND kind = $3 AND duration = 0 AND start_time <> $2`,
26
- [machine, startTime, track]
24
+ `WITH nested AS (
25
+ DELETE FROM segments
26
+ WHERE machine = $1 AND kind = $3 AND duration = 0 AND start_time > $2
27
+ RETURNING 1
28
+ )
29
+ UPDATE segments
30
+ SET end_time = $2::timestamptz,
31
+ duration = EXTRACT(EPOCH FROM ($2::timestamptz - start_time))
32
+ WHERE machine = $1 AND kind = $3 AND duration = 0 AND start_time < $2
33
+ AND name IS DISTINCT FROM $4`,
34
+ [machine, startTime, track, name ?? null]
27
35
  );
28
36
  } catch (error) {
29
37
  processingErrorLog('close_orphan_open', error, { machine, startTime, kind: track });