-
Notifications
You must be signed in to change notification settings - Fork 1
/
machine_production_status_3.sql
103 lines (83 loc) · 2.98 KB
/
machine_production_status_3.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
CREATE FOREIGN TABLE stream (
machine_name varchar(255),
status varchar(255),
event_timestamp timestamptz
)
SERVER pipelinedb;
CREATE VIEW v1 WITH (action = materialize) AS
SELECT machine_name AS machine,
keyed_max(event_timestamp, status) AS status,
max(event_timestamp) as start
FROM stream
GROUP BY machine_name;
CREATE VIEW v2 WITH (action = materialize) AS
SELECT (old).machine,
(old).status as status,
(old).start as start,
(new).start as end
FROM output_of('v1') WHERE ((old)) IS NOT NULL;
CREATE VIEW v3 WITH (action = materialize) AS
SELECT (old).machine,
(old).status as status,
extract('epoch' from (new).start - (old).start::timestamp) as duration
FROM output_of('v1') WHERE ((old)) IS NOT NULL;
-- machine 1 -> PRODUCING 12:00
INSERT INTO stream
VALUES ('machine1', 'PRODUCING', '2019-01-01 12:00');
-- machine 1 -> MAINTENANCE_NEEDE 12:05
INSERT INTO stream
VALUES ('machine1', 'MAINTENANCE_NEEDED', '2019-01-01 12:05');
-- machine 2 -> MAINTENANCE_NEEDED 11:50
INSERT INTO stream
VALUES ('machine2', 'MAINTENANCE_NEEDED', '2019-01-01 11:50');
-- machine 2 -> PRODUCING 12:30
INSERT INTO stream
VALUES ('machine2', 'PRODUCING', '2019-01-01 12:30');
-- machine 2 -> MAINTENANCE_NEEDED 12:40
INSERT INTO stream
VALUES ('machine2', 'MAINTENANCE_NEEDED', '2019-01-01 12:40');
-- machine 2 -> PRODUCING 12:40
INSERT INTO stream
VALUES ('machine2', 'PRODUCING', '2019-01-01 12:45');
SELECT *
FROM v1;
-- machine1 MAINTENANCE_NEEDED 2019-01-01 12:05:00.000000
-- machine2 PRODUCING 2019-01-01 12:45:00.000000
SELECT *
FROM v2;
-- machine1 PRODUCING 2019-01-01 12:00:00.000000 2019-01-01 12:05:00.000000
-- machine2 MAINTENANCE_NEEDED 2019-01-01 11:50:00.000000 2019-01-01 12:30:00.000000
-- machine2 PRODUCING 2019-01-01 12:30:00.000000 2019-01-01 12:40:00.000000
-- machine2 MAINTENANCE_NEEDED 2019-01-01 12:40:00.000000 2019-01-01 12:45:00.000000
SELECT *
FROM v3;
-- machine1 PRODUCING 300
-- machine2 MAINTENANCE_NEEDED 2400
-- machine2 PRODUCING 600
-- machine2 MAINTENANCE_NEEDED 300
SELECT machine, status, extract('epoch' from current_timestamp - start::timestamp) as duration
FROM v1
UNION
SELECT *
FROM v3;
-- machine1 PRODUCING 300
-- machine2 MAINTENANCE_NEEDED 2400
-- machine2 PRODUCING 6137152.347554
-- machine2 MAINTENANCE_NEEDED 300
-- machine2 PRODUCING 600
-- machine1 MAINTENANCE_NEEDED 6139552.347554
SELECT *, null as end
FROM v1
UNION
SELECT *
FROM v2;
-- machine1 PRODUCING 2019-01-01 12:00:00.000000 2019-01-01 12:05:00.000000
-- machine2 MAINTENANCE_NEEDED 2019-01-01 12:40:00.000000 2019-01-01 12:45:00.000000
-- machine1 MAINTENANCE_NEEDED 2019-01-01 12:05:00.000000
-- machine2 MAINTENANCE_NEEDED 2019-01-01 11:50:00.000000 2019-01-01 12:30:00.000000
-- machine2 PRODUCING 2019-01-01 12:45:00.000000
-- machine2 PRODUCING 2019-01-01 12:30:00.000000 2019-01-01 12:40:00.000000
DROP VIEW v3;
DROP VIEW v2;
DROP VIEW v1;
DROP FOREIGN TABLE stream;