@yarkivaev/scada 2.6.17 → 2.6.19
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.
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
-- A later correction window must not close a live covering.
|
|
2
|
+
CREATE OR REPLACE FUNCTION history_apply_correction(p_work UUID,p_machine TEXT,p_scale INTEGER,p_converged BOOLEAN)
|
|
3
|
+
RETURNS INTEGER LANGUAGE plpgsql AS $$
|
|
4
|
+
DECLARE w history_work%ROWTYPE; item history_corrections%ROWTYPE; owner history_annotations%ROWTYPE;
|
|
5
|
+
tail segments%ROWTYPE; lo TIMESTAMPTZ; hi TIMESTAMPTZ; a TIMESTAMPTZ; b TIMESTAMPTZ;
|
|
6
|
+
incoming JSONB; inherited JSONB; endpoint_cut BOOLEAN; inserted INTEGER := 0; keys TEXT[] := ARRAY['casting','cadence_rule','cadence_threshold_ms','casting_detected_at','source_interrupted'];
|
|
7
|
+
BEGIN
|
|
8
|
+
SELECT * INTO STRICT w FROM history_work WHERE id=p_work AND machine=p_machine;
|
|
9
|
+
IF p_converged THEN
|
|
10
|
+
FOR item IN SELECT * FROM history_corrections WHERE work_id=w.id AND generation=w.generation AND duration=0 LOOP
|
|
11
|
+
SELECT * INTO tail FROM segments WHERE machine=p_machine AND kind=item.kind AND name=item.name
|
|
12
|
+
AND start_time<=item.end_time AND end_time>item.end_time ORDER BY end_time DESC LIMIT 1;
|
|
13
|
+
IF FOUND THEN
|
|
14
|
+
UPDATE history_corrections SET end_time=tail.end_time,
|
|
15
|
+
duration=CASE WHEN tail.duration=0 THEN 0 ELSE extract(epoch FROM tail.end_time-item.start_time) END
|
|
16
|
+
WHERE work_id=w.id AND generation=w.generation AND kind=item.kind AND start_time=item.start_time;
|
|
17
|
+
END IF;
|
|
18
|
+
END LOOP;
|
|
19
|
+
END IF;
|
|
20
|
+
lo := 'epoch'::timestamptz+(w.correction_from/p_scale)*interval '1 millisecond';
|
|
21
|
+
hi := 'epoch'::timestamptz+(w.correction_until/p_scale)*interval '1 millisecond';
|
|
22
|
+
SELECT least(lo,COALESCE(min(start_time),lo)),greatest(hi,COALESCE(max(end_time),hi)) INTO lo,hi
|
|
23
|
+
FROM history_corrections WHERE work_id=w.id AND generation=w.generation;
|
|
24
|
+
INSERT INTO history_annotations(machine,kind,start_time,name,end_time,duration,tags,properties,resolved,consumed)
|
|
25
|
+
SELECT machine,kind,start_time,name,end_time,duration,tags,properties,resolved,consumed FROM segments
|
|
26
|
+
WHERE machine=p_machine AND kind='phase' AND start_time<=hi AND end_time>=lo
|
|
27
|
+
ON CONFLICT(machine,kind,start_time,name) DO UPDATE SET end_time=greatest(history_annotations.end_time,EXCLUDED.end_time),
|
|
28
|
+
duration=EXCLUDED.duration,tags=EXCLUDED.tags,properties=EXCLUDED.properties,resolved=EXCLUDED.resolved,consumed=EXCLUDED.consumed;
|
|
29
|
+
UPDATE segments SET end_time=lo,duration=greatest(0,extract(epoch FROM lo-start_time)),updated_at=clock_timestamp()
|
|
30
|
+
WHERE machine=p_machine AND kind='phase' AND start_time<lo AND end_time>lo AND duration>0;
|
|
31
|
+
DELETE FROM segments WHERE machine=p_machine AND kind='phase' AND start_time>=lo
|
|
32
|
+
AND (start_time<hi OR (start_time=hi AND end_time=hi));
|
|
33
|
+
FOR item IN SELECT * FROM history_corrections WHERE work_id=w.id AND generation=w.generation ORDER BY start_time LOOP
|
|
34
|
+
SELECT EXISTS(SELECT 1 FROM history_annotations WHERE machine=p_machine AND kind=item.kind AND name=item.name
|
|
35
|
+
AND start_time=item.end_time AND end_time=item.end_time) INTO endpoint_cut;
|
|
36
|
+
a := item.start_time;
|
|
37
|
+
FOR b IN SELECT cut FROM (
|
|
38
|
+
SELECT start_time AS cut FROM history_annotations WHERE machine=p_machine AND kind=item.kind
|
|
39
|
+
AND start_time>item.start_time AND start_time<item.end_time
|
|
40
|
+
UNION SELECT item.end_time
|
|
41
|
+
) boundaries ORDER BY cut LOOP
|
|
42
|
+
SELECT * INTO owner FROM history_annotations WHERE machine=p_machine AND kind=item.kind AND name=item.name
|
|
43
|
+
AND start_time<=a AND start_time<=item.end_time AND end_time>=item.start_time ORDER BY start_time DESC LIMIT 1;
|
|
44
|
+
IF NOT FOUND THEN
|
|
45
|
+
SELECT * INTO owner FROM history_annotations WHERE machine=p_machine AND kind=item.kind AND name=item.name
|
|
46
|
+
AND start_time>a AND start_time<=item.end_time AND end_time>=item.start_time ORDER BY start_time LIMIT 1;
|
|
47
|
+
END IF;
|
|
48
|
+
incoming := COALESCE(NULLIF(NULLIF(item.properties,''),'null')::jsonb,'{}'::jsonb);
|
|
49
|
+
inherited := COALESCE(NULLIF(NULLIF(owner.properties,''),'null')::jsonb,'{}'::jsonb);
|
|
50
|
+
IF jsonb_typeof(incoming)<>'object' THEN incoming:='{}'::jsonb; END IF;
|
|
51
|
+
IF jsonb_typeof(inherited)<>'object' THEN inherited:='{}'::jsonb; END IF;
|
|
52
|
+
INSERT INTO segments(machine,kind,name,start_time,end_time,duration,options,tags,properties,resolved,consumed,updated_at)
|
|
53
|
+
VALUES(p_machine,item.kind,item.name,a,b,CASE WHEN item.duration=0 AND b=item.end_time AND NOT endpoint_cut THEN 0 ELSE extract(epoch FROM b-a) END,
|
|
54
|
+
item.options,COALESCE(owner.tags,item.tags),(incoming||(inherited-keys))::text,COALESCE(owner.resolved,item.resolved),COALESCE(owner.consumed,true),clock_timestamp())
|
|
55
|
+
ON CONFLICT(machine,kind,start_time) DO UPDATE SET name=EXCLUDED.name,end_time=EXCLUDED.end_time,duration=EXCLUDED.duration,
|
|
56
|
+
options=EXCLUDED.options,tags=EXCLUDED.tags,properties=EXCLUDED.properties,resolved=EXCLUDED.resolved,consumed=EXCLUDED.consumed,updated_at=EXCLUDED.updated_at;
|
|
57
|
+
inserted:=inserted+1; a:=b;
|
|
58
|
+
END LOOP;
|
|
59
|
+
IF item.start_time<item.end_time AND endpoint_cut THEN
|
|
60
|
+
SELECT * INTO owner FROM history_annotations WHERE machine=p_machine AND kind=item.kind AND name=item.name AND start_time=item.end_time;
|
|
61
|
+
incoming := COALESCE(NULLIF(NULLIF(item.properties,''),'null')::jsonb,'{}'::jsonb);
|
|
62
|
+
inherited := COALESCE(NULLIF(NULLIF(owner.properties,''),'null')::jsonb,'{}'::jsonb);
|
|
63
|
+
IF jsonb_typeof(incoming)<>'object' THEN incoming:='{}'::jsonb; END IF;
|
|
64
|
+
IF jsonb_typeof(inherited)<>'object' THEN inherited:='{}'::jsonb; END IF;
|
|
65
|
+
INSERT INTO segments(machine,kind,name,start_time,end_time,duration,options,tags,properties,resolved,consumed)
|
|
66
|
+
VALUES(p_machine,item.kind,item.name,item.end_time,item.end_time,0,item.options,owner.tags,(incoming||(inherited-keys))::text,owner.resolved,COALESCE(owner.consumed,true))
|
|
67
|
+
ON CONFLICT(machine,kind,start_time) DO NOTHING;
|
|
68
|
+
END IF;
|
|
69
|
+
END LOOP;
|
|
70
|
+
INSERT INTO history_checkpoints(machine,classifier_key,time_ms,state,valid)
|
|
71
|
+
SELECT machine,classifier_key,time_ms,state,true FROM history_staged_checkpoints WHERE work_id=w.id AND generation=w.generation
|
|
72
|
+
ON CONFLICT(machine,classifier_key,time_ms) DO UPDATE SET state=EXCLUDED.state,valid=true;
|
|
73
|
+
RETURN inserted;
|
|
74
|
+
END $$;
|
package/package.json
CHANGED
|
@@ -28,6 +28,30 @@ function freshRead(report, input, { now, freshnessMs, controlEpoch, clockSkewMs
|
|
|
28
28
|
&& Number.isFinite(row.lastRead) && row.lastRead <= now + clockSkewMs && now - row.lastRead <= freshnessMs;
|
|
29
29
|
});
|
|
30
30
|
}
|
|
31
|
+
function holderOf(slots, task) {
|
|
32
|
+
const holders = slots.filter((node) => { return node.assignments.some((row) => { return row.taskId === task.taskId; }); });
|
|
33
|
+
return holders.length === 1 ? holders[0] : null;
|
|
34
|
+
}
|
|
35
|
+
function destination(slots, task, holder, timing) {
|
|
36
|
+
const input = holder.assignments.find((row) => { return row.taskId === task.taskId; });
|
|
37
|
+
if (timing.now - (input.assignedAt ?? 0) < timing.replacementDelayMs) { return null; }
|
|
38
|
+
const candidates = slots.filter((node) => {
|
|
39
|
+
return node.live && wanted(task, node) && (node.id === holder.id || node.load + (task.cost ?? 1) <= node.capacity);
|
|
40
|
+
});
|
|
41
|
+
const home = [...candidates].sort(byPlace(task))[0];
|
|
42
|
+
return home && home.id !== holder.id ? home : null;
|
|
43
|
+
}
|
|
44
|
+
function reclaim(slots, tasks, timing) {
|
|
45
|
+
tasks.filter((task) => { return copies(task) === 1; }).forEach((task) => {
|
|
46
|
+
const holder = holderOf(slots, task);
|
|
47
|
+
const home = holder ? destination(slots, task, holder, timing) : null;
|
|
48
|
+
if (!home) { return; }
|
|
49
|
+
holder.assignments = holder.assignments.filter((row) => { return row.taskId !== task.taskId; });
|
|
50
|
+
holder.load -= task.cost ?? 1;
|
|
51
|
+
home.assignments.push({ taskId: task.taskId, specVersion: 1, generation: 0, assignedAt: timing.now, spec: task.spec });
|
|
52
|
+
home.load += task.cost ?? 1;
|
|
53
|
+
});
|
|
54
|
+
}
|
|
31
55
|
function retain(slots, tasks, owners, observed, timing) {
|
|
32
56
|
for (const task of tasks) {
|
|
33
57
|
const confirmed = owners(task).filter((node) => {
|
|
@@ -119,6 +143,7 @@ export default function schedule(catalog, reports, current, { now = Date.now(),
|
|
|
119
143
|
}
|
|
120
144
|
}
|
|
121
145
|
}
|
|
146
|
+
reclaim(slots, tasks, { now, replacementDelayMs });
|
|
122
147
|
retain(slots, tasks, owners, observed, { now, freshnessMs, controlEpoch, clockSkewMs });
|
|
123
148
|
return snapshots(slots, taskMap, catalog, observed, { now, freshnessMs, clockSkewMs });
|
|
124
149
|
}
|