Skip to content

Commit 4f36d2f

Browse files
Automate expectations creation
Add backend automation to create and lifecycle manage expectations for jobs and operations when due/start times are set, update, or backfilled. Include triggers, functions, and backfill logic to ensure expectations exist and evolve, tying into the new monitoring UI and navigation. X-Lovable-Edit-ID: edt-1e548683-890c-40da-a83e-75f850928ae5
2 parents 2b398be + 2b293a0 commit 4f36d2f

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
-- First drop and recreate the constraint
3+
ALTER TABLE expectations DROP CONSTRAINT IF EXISTS expectations_source_check;
4+
5+
ALTER TABLE expectations ADD CONSTRAINT expectations_source_check
6+
CHECK (source = ANY (ARRAY[
7+
'erp_sync'::text,
8+
'manual'::text,
9+
'scheduler'::text,
10+
'auto_replan'::text,
11+
'system'::text,
12+
'backfill'::text,
13+
'job_creation'::text,
14+
'job_update'::text,
15+
'operation_creation'::text,
16+
'operation_update'::text,
17+
'due_date_change'::text
18+
]));
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
-- Operation trigger using correct column name (operation_name)
3+
CREATE OR REPLACE FUNCTION public.auto_create_operation_expectation()
4+
RETURNS TRIGGER
5+
LANGUAGE plpgsql
6+
SECURITY DEFINER
7+
SET search_path = public
8+
AS $$
9+
DECLARE
10+
v_existing_id uuid;
11+
BEGIN
12+
IF NEW.planned_end IS NOT NULL AND NEW.status NOT IN ('completed') THEN
13+
SELECT id INTO v_existing_id
14+
FROM expectations
15+
WHERE entity_type = 'operation' AND entity_id = NEW.id
16+
AND expectation_type = 'completion_time' AND superseded_by IS NULL;
17+
18+
IF v_existing_id IS NULL THEN
19+
INSERT INTO expectations (tenant_id, entity_type, entity_id, expectation_type,
20+
belief_statement, expected_value, expected_at, source, context)
21+
VALUES (NEW.tenant_id, 'operation', NEW.id, 'completion_time',
22+
format('Operation %s should complete by %s', NEW.operation_name, to_char(NEW.planned_end, 'YYYY-MM-DD HH24:MI')),
23+
jsonb_build_object('status', 'completed', 'operation_name', NEW.operation_name),
24+
NEW.planned_end,
25+
CASE WHEN TG_OP = 'INSERT' THEN 'operation_creation' ELSE 'operation_update' END,
26+
jsonb_build_object('operation_id', NEW.id, 'operation_name', NEW.operation_name));
27+
END IF;
28+
END IF;
29+
RETURN NEW;
30+
END;
31+
$$;
32+
33+
DROP TRIGGER IF EXISTS auto_create_operation_expectation_trigger ON operations;
34+
CREATE TRIGGER auto_create_operation_expectation_trigger
35+
AFTER INSERT OR UPDATE OF planned_start, planned_end, status ON operations
36+
FOR EACH ROW EXECUTE FUNCTION auto_create_operation_expectation();
37+
38+
-- Backfill operation expectations
39+
INSERT INTO expectations (tenant_id, entity_type, entity_id, expectation_type,
40+
belief_statement, expected_value, expected_at, source, context)
41+
SELECT o.tenant_id, 'operation', o.id, 'completion_time',
42+
format('Operation %s should complete by %s', o.operation_name, to_char(o.planned_end, 'YYYY-MM-DD HH24:MI')),
43+
jsonb_build_object('status', 'completed', 'operation_name', o.operation_name),
44+
o.planned_end, 'backfill',
45+
jsonb_build_object('operation_id', o.id, 'operation_name', o.operation_name, 'backfilled_at', now())
46+
FROM operations o
47+
WHERE o.planned_end IS NOT NULL AND o.status NOT IN ('completed')
48+
AND NOT EXISTS (SELECT 1 FROM expectations e WHERE e.entity_id = o.id
49+
AND e.entity_type = 'operation' AND e.expectation_type = 'completion_time');
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
-- Backfill job expectations
3+
INSERT INTO expectations (tenant_id, entity_type, entity_id, expectation_type,
4+
belief_statement, expected_value, expected_at, source, context)
5+
SELECT j.tenant_id, 'job', j.id, 'completion_time',
6+
format('Job %s should be completed by %s', j.job_number, to_char(j.due_date, 'YYYY-MM-DD HH24:MI')),
7+
jsonb_build_object('status', 'completed', 'job_number', j.job_number, 'customer', j.customer),
8+
j.due_date, 'backfill',
9+
jsonb_build_object('job_id', j.id, 'job_number', j.job_number, 'customer', j.customer)
10+
FROM jobs j
11+
WHERE j.due_date IS NOT NULL AND j.deleted_at IS NULL AND j.status NOT IN ('completed')
12+
AND NOT EXISTS (SELECT 1 FROM expectations e WHERE e.entity_id = j.id AND e.entity_type = 'job');

0 commit comments

Comments
 (0)