@golemio/flow 1.2.6-dev.960416138 → 1.2.6-dev.985218732

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,608 @@
1
+ -- drop dependent views
2
+ drop view analytic.v_pedestrians_detections_15min;
3
+ drop view analytic.v_pedestrians_detections_api;
4
+ drop view analytic.v_pedestrians_detections_daily;
5
+ drop view analytic.v_pedestrians_flow_quality;
6
+
7
+ -- flow_sinks
8
+ alter table flow_sinks alter column history_start_timestamp type bigint using extract(epoch from history_start_timestamp) * 1000;
9
+
10
+ -- flow_od_measurements
11
+ drop table if exists flow_od_measurements_old;
12
+ alter table flow_od_measurements rename to flow_od_measurements_old;
13
+ CREATE TABLE flow_od_measurements (
14
+ cube_id int4 NOT NULL,
15
+ analytic_id int4 NOT NULL,
16
+ sequence_number int4 NOT NULL,
17
+ sink_id int4 NOT NULL,
18
+ start_timestamp int8 NOT NULL,
19
+ end_timestamp int8 NOT NULL,
20
+ category varchar(150) NOT NULL,
21
+ origin varchar(150) NOT NULL,
22
+ destination varchar(150) NOT NULL,
23
+ value int4 NOT NULL,
24
+ data_validity varchar(50) NULL,
25
+ create_batch_id int8 NULL,
26
+ created_at timestamptz NULL,
27
+ created_by varchar(150) NULL,
28
+ update_batch_id int8 NULL,
29
+ updated_at timestamptz NULL,
30
+ updated_by varchar(150) NULL,
31
+ CONSTRAINT flow_od_measurementspkey PRIMARY KEY (cube_id, sink_id, start_timestamp, end_timestamp, category, origin, destination, sequence_number)
32
+ );
33
+ CREATE INDEX flow_od_measurements_created_at ON flow_od_measurements USING btree (created_at);
34
+
35
+ -- flow_measurements
36
+ drop table if exists flow_measurements_old;
37
+ alter table flow_measurements rename to flow_measurements_old;
38
+
39
+ DO $$
40
+ DECLARE
41
+ partition_name text;
42
+ BEGIN
43
+ FOR partition_name IN (SELECT relname FROM pg_class WHERE relnamespace = (SELECT current_schema() as value)::regnamespace AND relname LIKE 'flow_measurements_%' and relispartition = true and relpartbound is not null)
44
+ LOOP
45
+ EXECUTE format('ALTER TABLE %I.%I RENAME TO %I', (SELECT current_schema() as value), partition_name, partition_name || '_old');
46
+ END LOOP;
47
+ END $$;
48
+
49
+ CREATE TABLE flow_measurements (
50
+ sink_id int4 NOT NULL,
51
+ cube_id int4 NOT NULL,
52
+ sequence_number int4 NOT NULL,
53
+ analytic_id int4 NOT NULL,
54
+ start_timestamp int8 NOT NULL,
55
+ end_timestamp int8 NOT NULL,
56
+ category varchar(150) NOT NULL,
57
+ value int4 NOT NULL,
58
+ create_batch_id int8 NULL,
59
+ created_at timestamptz NULL,
60
+ created_by varchar(150) NULL,
61
+ update_batch_id int8 NULL,
62
+ updated_at timestamptz NULL,
63
+ updated_by varchar(150) NULL,
64
+ data_validity varchar(50) NULL,
65
+ CONSTRAINT flow_measurements_pkey PRIMARY KEY (cube_id, sink_id, start_timestamp, end_timestamp, category, sequence_number)
66
+ )
67
+ PARTITION BY RANGE (start_timestamp);
68
+ CREATE INDEX flow_measurements_created_at ON ONLY flow_measurements USING btree (created_at);
69
+ CREATE INDEX flow_measurements_start_cube_sink ON ONLY flow_measurements USING btree (cube_id, sink_id);
70
+ CREATE INDEX flow_measurements_start_timestamp ON ONLY flow_measurements USING btree (start_timestamp);
71
+
72
+ -- partitions
73
+ CREATE TABLE flow_measurements_min PARTITION OF flow_measurements FOR VALUES FROM (MINVALUE) TO (extract ('epoch' from '2020-09-01'::timestamp)*1000);
74
+ CREATE TABLE flow_measurements_y2020m09 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2020-09-01'::timestamp)*1000) TO (extract ('epoch' from '2020-10-01'::timestamp)*1000);
75
+ CREATE TABLE flow_measurements_y2020m10 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2020-10-01'::timestamp)*1000) TO (extract ('epoch' from '2020-11-01'::timestamp)*1000);
76
+ CREATE TABLE flow_measurements_y2020m11 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2020-11-01'::timestamp)*1000) TO (extract ('epoch' from '2020-12-01'::timestamp)*1000);
77
+ CREATE TABLE flow_measurements_y2020m12 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2020-12-01'::timestamp)*1000) TO (extract ('epoch' from '2021-01-01'::timestamp)*1000);
78
+ --2021
79
+ CREATE TABLE flow_measurements_y2021m01 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2021-01-01'::timestamp)*1000) TO (extract ('epoch' from '2021-02-01'::timestamp)*1000);
80
+ CREATE TABLE flow_measurements_y2021m02 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2021-02-01'::timestamp)*1000) TO (extract ('epoch' from '2021-03-01'::timestamp)*1000);
81
+ CREATE TABLE flow_measurements_y2021m03 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2021-03-01'::timestamp)*1000) TO (extract ('epoch' from '2021-04-01'::timestamp)*1000);
82
+ CREATE TABLE flow_measurements_y2021m04 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2021-04-01'::timestamp)*1000) TO (extract ('epoch' from '2021-05-01'::timestamp)*1000);
83
+ CREATE TABLE flow_measurements_y2021m05 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2021-05-01'::timestamp)*1000) TO (extract ('epoch' from '2021-06-01'::timestamp)*1000);
84
+ CREATE TABLE flow_measurements_y2021m06 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2021-06-01'::timestamp)*1000) TO (extract ('epoch' from '2021-07-01'::timestamp)*1000);
85
+ CREATE TABLE flow_measurements_y2021m07 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2021-07-01'::timestamp)*1000) TO (extract ('epoch' from '2021-08-01'::timestamp)*1000);
86
+ CREATE TABLE flow_measurements_y2021m08 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2021-08-01'::timestamp)*1000) TO (extract ('epoch' from '2021-09-01'::timestamp)*1000);
87
+ CREATE TABLE flow_measurements_y2021m09 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2021-09-01'::timestamp)*1000) TO (extract ('epoch' from '2021-10-01'::timestamp)*1000);
88
+ CREATE TABLE flow_measurements_y2021m10 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2021-10-01'::timestamp)*1000) TO (extract ('epoch' from '2021-11-01'::timestamp)*1000);
89
+ CREATE TABLE flow_measurements_y2021m11 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2021-11-01'::timestamp)*1000) TO (extract ('epoch' from '2021-12-01'::timestamp)*1000);
90
+ CREATE TABLE flow_measurements_y2021m12 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2021-12-01'::timestamp)*1000) TO (extract ('epoch' from '2022-01-01'::timestamp)*1000);
91
+ --2022
92
+ CREATE TABLE flow_measurements_y2022m01 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2022-01-01'::timestamp)*1000) TO (extract ('epoch' from '2022-02-01'::timestamp)*1000);
93
+ CREATE TABLE flow_measurements_y2022m02 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2022-02-01'::timestamp)*1000) TO (extract ('epoch' from '2022-03-01'::timestamp)*1000);
94
+ CREATE TABLE flow_measurements_y2022m03 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2022-03-01'::timestamp)*1000) TO (extract ('epoch' from '2022-04-01'::timestamp)*1000);
95
+ CREATE TABLE flow_measurements_y2022m04 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2022-04-01'::timestamp)*1000) TO (extract ('epoch' from '2022-05-01'::timestamp)*1000);
96
+ CREATE TABLE flow_measurements_y2022m05 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2022-05-01'::timestamp)*1000) TO (extract ('epoch' from '2022-06-01'::timestamp)*1000);
97
+ CREATE TABLE flow_measurements_y2022m06 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2022-06-01'::timestamp)*1000) TO (extract ('epoch' from '2022-07-01'::timestamp)*1000);
98
+ CREATE TABLE flow_measurements_y2022m07 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2022-07-01'::timestamp)*1000) TO (extract ('epoch' from '2022-08-01'::timestamp)*1000);
99
+ CREATE TABLE flow_measurements_y2022m08 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2022-08-01'::timestamp)*1000) TO (extract ('epoch' from '2022-09-01'::timestamp)*1000);
100
+ CREATE TABLE flow_measurements_y2022m09 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2022-09-01'::timestamp)*1000) TO (extract ('epoch' from '2022-10-01'::timestamp)*1000);
101
+ CREATE TABLE flow_measurements_y2022m10 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2022-10-01'::timestamp)*1000) TO (extract ('epoch' from '2022-11-01'::timestamp)*1000);
102
+ CREATE TABLE flow_measurements_y2022m11 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2022-11-01'::timestamp)*1000) TO (extract ('epoch' from '2022-12-01'::timestamp)*1000);
103
+ CREATE TABLE flow_measurements_y2022m12 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2022-12-01'::timestamp)*1000) TO (extract ('epoch' from '2023-01-01'::timestamp)*1000);
104
+ --2023
105
+ CREATE TABLE flow_measurements_y2023m01 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2023-01-01'::timestamp)*1000) TO (extract ('epoch' from '2023-02-01'::timestamp)*1000);
106
+ CREATE TABLE flow_measurements_y2023m02 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2023-02-01'::timestamp)*1000) TO (extract ('epoch' from '2023-03-01'::timestamp)*1000);
107
+ CREATE TABLE flow_measurements_y2023m03 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2023-03-01'::timestamp)*1000) TO (extract ('epoch' from '2023-04-01'::timestamp)*1000);
108
+ CREATE TABLE flow_measurements_y2023m04 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2023-04-01'::timestamp)*1000) TO (extract ('epoch' from '2023-05-01'::timestamp)*1000);
109
+ CREATE TABLE flow_measurements_y2023m05 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2023-05-01'::timestamp)*1000) TO (extract ('epoch' from '2023-06-01'::timestamp)*1000);
110
+ CREATE TABLE flow_measurements_y2023m06 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2023-06-01'::timestamp)*1000) TO (extract ('epoch' from '2023-07-01'::timestamp)*1000);
111
+ CREATE TABLE flow_measurements_y2023m07 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2023-07-01'::timestamp)*1000) TO (extract ('epoch' from '2023-08-01'::timestamp)*1000);
112
+ CREATE TABLE flow_measurements_y2023m08 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2023-08-01'::timestamp)*1000) TO (extract ('epoch' from '2023-09-01'::timestamp)*1000);
113
+ CREATE TABLE flow_measurements_y2023m09 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2023-09-01'::timestamp)*1000) TO (extract ('epoch' from '2023-10-01'::timestamp)*1000);
114
+ CREATE TABLE flow_measurements_y2023m10 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2023-10-01'::timestamp)*1000) TO (extract ('epoch' from '2023-11-01'::timestamp)*1000);
115
+ CREATE TABLE flow_measurements_y2023m11 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2023-11-01'::timestamp)*1000) TO (extract ('epoch' from '2023-12-01'::timestamp)*1000);
116
+ CREATE TABLE flow_measurements_y2023m12 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2023-12-01'::timestamp)*1000) TO (extract ('epoch' from '2024-01-01'::timestamp)*1000);
117
+ --2024
118
+ CREATE TABLE flow_measurements_y2024m01 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2024-01-01'::timestamp)*1000) TO (extract ('epoch' from '2024-02-01'::timestamp)*1000);
119
+ CREATE TABLE flow_measurements_y2024m02 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2024-02-01'::timestamp)*1000) TO (extract ('epoch' from '2024-03-01'::timestamp)*1000);
120
+ CREATE TABLE flow_measurements_y2024m03 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2024-03-01'::timestamp)*1000) TO (extract ('epoch' from '2024-04-01'::timestamp)*1000);
121
+ CREATE TABLE flow_measurements_y2024m04 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2024-04-01'::timestamp)*1000) TO (extract ('epoch' from '2024-05-01'::timestamp)*1000);
122
+ CREATE TABLE flow_measurements_y2024m05 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2024-05-01'::timestamp)*1000) TO (extract ('epoch' from '2024-06-01'::timestamp)*1000);
123
+ CREATE TABLE flow_measurements_y2024m06 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2024-06-01'::timestamp)*1000) TO (extract ('epoch' from '2024-07-01'::timestamp)*1000);
124
+ CREATE TABLE flow_measurements_y2024m07 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2024-07-01'::timestamp)*1000) TO (extract ('epoch' from '2024-08-01'::timestamp)*1000);
125
+ CREATE TABLE flow_measurements_y2024m08 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2024-08-01'::timestamp)*1000) TO (extract ('epoch' from '2024-09-01'::timestamp)*1000);
126
+ CREATE TABLE flow_measurements_y2024m09 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2024-09-01'::timestamp)*1000) TO (extract ('epoch' from '2024-10-01'::timestamp)*1000);
127
+ CREATE TABLE flow_measurements_y2024m10 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2024-10-01'::timestamp)*1000) TO (extract ('epoch' from '2024-11-01'::timestamp)*1000);
128
+ CREATE TABLE flow_measurements_y2024m11 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2024-11-01'::timestamp)*1000) TO (extract ('epoch' from '2024-12-01'::timestamp)*1000);
129
+ CREATE TABLE flow_measurements_y2024m12 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2024-12-01'::timestamp)*1000) TO (extract ('epoch' from '2025-01-01'::timestamp)*1000);
130
+ --2025
131
+ CREATE TABLE flow_measurements_y2025m01 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2025-01-01'::timestamp)*1000) TO (extract ('epoch' from '2025-02-01'::timestamp)*1000);
132
+ CREATE TABLE flow_measurements_y2025m02 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2025-02-01'::timestamp)*1000) TO (extract ('epoch' from '2025-03-01'::timestamp)*1000);
133
+ CREATE TABLE flow_measurements_y2025m03 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2025-03-01'::timestamp)*1000) TO (extract ('epoch' from '2025-04-01'::timestamp)*1000);
134
+ CREATE TABLE flow_measurements_y2025m04 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2025-04-01'::timestamp)*1000) TO (extract ('epoch' from '2025-05-01'::timestamp)*1000);
135
+ CREATE TABLE flow_measurements_y2025m05 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2025-05-01'::timestamp)*1000) TO (extract ('epoch' from '2025-06-01'::timestamp)*1000);
136
+ CREATE TABLE flow_measurements_y2025m06 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2025-06-01'::timestamp)*1000) TO (extract ('epoch' from '2025-07-01'::timestamp)*1000);
137
+ CREATE TABLE flow_measurements_y2025m07 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2025-07-01'::timestamp)*1000) TO (extract ('epoch' from '2025-08-01'::timestamp)*1000);
138
+ CREATE TABLE flow_measurements_y2025m08 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2025-08-01'::timestamp)*1000) TO (extract ('epoch' from '2025-09-01'::timestamp)*1000);
139
+ CREATE TABLE flow_measurements_y2025m09 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2025-09-01'::timestamp)*1000) TO (extract ('epoch' from '2025-10-01'::timestamp)*1000);
140
+ CREATE TABLE flow_measurements_y2025m10 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2025-10-01'::timestamp)*1000) TO (extract ('epoch' from '2025-11-01'::timestamp)*1000);
141
+ CREATE TABLE flow_measurements_y2025m11 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2025-11-01'::timestamp)*1000) TO (extract ('epoch' from '2025-12-01'::timestamp)*1000);
142
+ CREATE TABLE flow_measurements_y2025m12 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2025-12-01'::timestamp)*1000) TO (extract ('epoch' from '2026-01-01'::timestamp)*1000);
143
+ --2026
144
+ CREATE TABLE flow_measurements_y2026m01 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2026-01-01'::timestamp)*1000) TO (extract ('epoch' from '2026-02-01'::timestamp)*1000);
145
+ CREATE TABLE flow_measurements_y2026m02 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2026-02-01'::timestamp)*1000) TO (extract ('epoch' from '2026-03-01'::timestamp)*1000);
146
+ CREATE TABLE flow_measurements_y2026m03 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2026-03-01'::timestamp)*1000) TO (extract ('epoch' from '2026-04-01'::timestamp)*1000);
147
+ CREATE TABLE flow_measurements_y2026m04 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2026-04-01'::timestamp)*1000) TO (extract ('epoch' from '2026-05-01'::timestamp)*1000);
148
+ CREATE TABLE flow_measurements_y2026m05 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2026-05-01'::timestamp)*1000) TO (extract ('epoch' from '2026-06-01'::timestamp)*1000);
149
+ CREATE TABLE flow_measurements_y2026m06 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2026-06-01'::timestamp)*1000) TO (extract ('epoch' from '2026-07-01'::timestamp)*1000);
150
+ CREATE TABLE flow_measurements_y2026m07 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2026-07-01'::timestamp)*1000) TO (extract ('epoch' from '2026-08-01'::timestamp)*1000);
151
+ CREATE TABLE flow_measurements_y2026m08 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2026-08-01'::timestamp)*1000) TO (extract ('epoch' from '2026-09-01'::timestamp)*1000);
152
+ CREATE TABLE flow_measurements_y2026m09 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2026-09-01'::timestamp)*1000) TO (extract ('epoch' from '2026-10-01'::timestamp)*1000);
153
+ CREATE TABLE flow_measurements_y2026m10 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2026-10-01'::timestamp)*1000) TO (extract ('epoch' from '2026-11-01'::timestamp)*1000);
154
+ CREATE TABLE flow_measurements_y2026m11 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2026-11-01'::timestamp)*1000) TO (extract ('epoch' from '2026-12-01'::timestamp)*1000);
155
+ CREATE TABLE flow_measurements_y2026m12 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2026-12-01'::timestamp)*1000) TO (extract ('epoch' from '2027-01-01'::timestamp)*1000);
156
+ --202
157
+ CREATE TABLE flow_measurements_y2027up PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2027-01-01'::timestamp)*1000) TO (maxvalue);
158
+
159
+ -- counters_detections
160
+ alter table counters_detections alter column measured_to type bigint using extract(epoch from measured_to) * 1000;
161
+ alter table counters_detections alter column measured_from type bigint using extract(epoch from measured_from) * 1000;
162
+
163
+ -- prodecures
164
+ CREATE OR replace procedure update_detections_data()
165
+ LANGUAGE plpgsql
166
+ AS $$
167
+ declare
168
+ lastupdatetimestamp timestamptz;
169
+ lastupdateunix bigint;
170
+ begin
171
+ select
172
+ case
173
+ when flowmax.max_measured_from is not null
174
+ then flowmax.max_measured_from
175
+ else to_timestamp(0)
176
+ end as max_measured_from into lastupdatetimestamp
177
+ from (select max(measured_from) - interval '1 hours' as max_measured_from from flow.pedestrians_detections_api) flowMax;
178
+
179
+ lastupdateunix := extract ('epoch' from lastupdatetimestamp) * 1000;
180
+
181
+
182
+ insert into flow.pedestrians_detections_api
183
+ with wifi as (
184
+ select
185
+ pedestrians_wifi.location_id,
186
+ pedestrians_wifi.direction_id,
187
+ date_trunc('hour'::text, pedestrians_wifi.measured_from) + (date_part('minute'::text, pedestrians_wifi.measured_from)::integer / 15)::double precision * '00:15:00'::interval as measured_from,
188
+ date_trunc('hour'::text, pedestrians_wifi.measured_from) + (date_part('minute'::text, pedestrians_wifi.measured_from)::integer / 15)::double precision * '00:15:00'::interval + '00:15:00'::interval as measured_to,
189
+ sum(pedestrians_wifi.value) as value,
190
+ count(pedestrians_wifi.value) as count_n,
191
+ 3 as quantity
192
+ from
193
+ flow.pedestrians_wifi
194
+ group by
195
+ pedestrians_wifi.location_id,
196
+ pedestrians_wifi.direction_id,
197
+ (date_trunc('hour'::text, pedestrians_wifi.measured_from) + (date_part('minute'::text, pedestrians_wifi.measured_from)::integer / 15)::double precision * '00:15:00'::interval),
198
+ (date_trunc('hour'::text, pedestrians_wifi.measured_from) + (date_part('minute'::text, pedestrians_wifi.measured_from)::integer / 15)::double precision * '00:15:00'::interval + '00:15:00'::interval)
199
+ ) SELECT wifi.measured_from,
200
+ wifi.measured_to,
201
+ wifi.location_id,
202
+ wifi.direction_id,
203
+ wifi.value,
204
+ wifi.count_n::numeric / wifi.quantity::numeric AS quality
205
+ FROM wifi
206
+ where wifi.measured_from > lastupdatetimestamp
207
+ ON CONFLICT (location_id,direction_id,measured_from,measured_to)
208
+ DO update
209
+ SET value = EXCLUDED.value,
210
+ quality = EXCLUDED.quality;
211
+
212
+ insert into flow.pedestrians_detections_api
213
+ with pyro as (
214
+ select
215
+ cd.locations_id as location_id,
216
+ cd.directions_id as direction_id,
217
+ to_timestamp((cd.measured_from / 1000)::double precision) as measured_from,
218
+ to_timestamp((cd.measured_from / 1000)::double precision) + '00:15:00'::interval as measured_to,
219
+ sum(cd.value) as value,
220
+ 1 as count_n,
221
+ 1 as quantity
222
+ from flow.counters_detections cd
223
+ where
224
+ cd.category::text = 'pedestrian'::text
225
+ and (cd.directions_id::text in (select distinct pedestrians_directions_api.direction_id from flow.pedestrians_directions_api))
226
+ group by
227
+ cd.locations_id,
228
+ cd.directions_id,
229
+ (to_timestamp((cd.measured_from / 1000)::double precision)),
230
+ (to_timestamp((cd.measured_from / 1000)::double precision) + '00:15:00'::interval)
231
+ )
232
+ select
233
+ pyro.measured_from,
234
+ pyro.measured_to,
235
+ pyro.location_id,
236
+ pyro.direction_id,
237
+ pyro.value,
238
+ pyro.count_n::numeric / pyro.quantity::numeric as quality
239
+ from pyro
240
+ where pyro.measured_from > lastupdatetimestamp
241
+ ON CONFLICT (location_id,direction_id,measured_from,measured_to)
242
+ DO update
243
+ SET value = EXCLUDED.value,
244
+ quality = EXCLUDED.quality;
245
+
246
+
247
+ insert into flow.pedestrians_detections_api
248
+ with vyber as (
249
+ select * from flow.flow_measurements where flow_measurements.start_timestamp > lastupdateunix and category::text = 'pedestrian'::text
250
+ ), flow as (
251
+ select
252
+ vyber.cube_id::character varying(50) as location_id,
253
+ vyber.sink_id::character varying(50) as direction_id,
254
+ date_trunc('hour'::text, to_timestamp((vyber.start_timestamp / 1000)::double precision)) + (date_part('minute'::text, to_timestamp((vyber.start_timestamp / 1000)::double precision))::integer / 15)::double precision * '00:15:00'::interval as measured_from,
255
+ date_trunc('hour'::text, to_timestamp((vyber.start_timestamp / 1000)::double precision)) + (date_part('minute'::text, to_timestamp((vyber.start_timestamp / 1000)::double precision))::integer / 15)::double precision * '00:15:00'::interval + '00:15:00'::interval as measured_to,
256
+ sum(vyber.value) as value,
257
+ count(vyber.value) as count_n,
258
+ 3 as quantity
259
+ from
260
+ vyber
261
+ where
262
+ ((vyber.cube_id::character varying(50)::text,
263
+ vyber.sink_id::character varying(50)::text) in (
264
+ select distinct
265
+ pedestrians_directions_api.cube_id as location_id,
266
+ pedestrians_directions_api.direction_id
267
+ from
268
+ flow.pedestrians_directions_api))
269
+ group by 1,2,3,4
270
+ ) select
271
+ flow.measured_from,
272
+ flow.measured_to,
273
+ flow.location_id,
274
+ flow.direction_id,
275
+ flow.value,
276
+ flow.count_n::numeric / flow.quantity::numeric as quality
277
+ from flow
278
+ where flow.measured_from > lastupdatetimestamp
279
+ ON CONFLICT (location_id,direction_id,measured_from,measured_to)
280
+ DO update
281
+ SET value = EXCLUDED.value,
282
+ quality = EXCLUDED.quality;
283
+ end;
284
+ $$;
285
+
286
+ -- recreate views
287
+ CREATE OR REPLACE VIEW analytic.v_pedestrians_detections_15min
288
+ AS WITH wifi AS (
289
+ SELECT pedestrians_wifi.location_id AS locations_id,
290
+ pedestrians_wifi.direction_id AS directions_id,
291
+ date_trunc('hour'::text, pedestrians_wifi.measured_from) + (date_part('minute'::text, pedestrians_wifi.measured_from)::integer / 15)::double precision * '00:15:00'::interval AS measured_from,
292
+ sum(pedestrians_wifi.value) AS value,
293
+ count(pedestrians_wifi.value) AS count_n
294
+ FROM flow.pedestrians_wifi
295
+ GROUP BY pedestrians_wifi.location_id, pedestrians_wifi.direction_id, (date_trunc('hour'::text, pedestrians_wifi.measured_from) + (date_part('minute'::text, pedestrians_wifi.measured_from)::integer / 15)::double precision * '00:15:00'::interval)
296
+ ), pyro AS (
297
+ SELECT cd.locations_id,
298
+ lg.first_dir AS directions_id,
299
+ to_timestamp((cd.measured_from / 1000)::double precision) AS measured_from,
300
+ sum(cd.value) AS value,
301
+ count(cd.value) AS count_n
302
+ FROM flow.counters_detections cd
303
+ LEFT JOIN ( SELECT pedestrians_locations_gates.direction_id,
304
+ pedestrians_locations_gates.direction_type,
305
+ min(pedestrians_locations_gates.direction_id::text) OVER (PARTITION BY pedestrians_locations_gates.cube_id, pedestrians_locations_gates.direction_type) AS first_dir
306
+ FROM analytic.pedestrians_locations_gates) lg ON cd.directions_id::text = lg.direction_id::text
307
+ WHERE cd.category::text = 'pedestrian'::text AND (cd.directions_id::text IN ( SELECT DISTINCT pedestrians_locations_gates.direction_id
308
+ FROM analytic.pedestrians_locations_gates))
309
+ GROUP BY cd.locations_id, lg.first_dir, (to_timestamp((cd.measured_from / 1000)::double precision))
310
+ ), flow AS (
311
+ SELECT flow_measurements.cube_id::character varying(50) AS location_id,
312
+ flow_measurements.sink_id::character varying(50) AS direction_id,
313
+ date_trunc('hour'::text, to_timestamp((flow_measurements.start_timestamp / 1000)::double precision)) + (date_part('minute'::text, to_timestamp((flow_measurements.start_timestamp / 1000)::double precision))::integer / 15)::double precision * '00:15:00'::interval AS measured_from,
314
+ sum(flow_measurements.value) AS value,
315
+ count(flow_measurements.value) AS count_n
316
+ FROM flow.flow_measurements
317
+ WHERE ((flow_measurements.cube_id::character varying(50)::text, flow_measurements.sink_id::character varying(50)::text) IN ( SELECT DISTINCT pedestrians_locations_gates.cube_id AS location_id,
318
+ pedestrians_locations_gates.direction_id
319
+ FROM analytic.pedestrians_locations_gates)) AND flow_measurements.category::text = 'pedestrian'::text
320
+ GROUP BY (flow_measurements.cube_id::character varying(50)), (flow_measurements.sink_id::character varying(50)), (date_trunc('hour'::text, to_timestamp((flow_measurements.start_timestamp / 1000)::double precision)) + (date_part('minute'::text, to_timestamp((flow_measurements.start_timestamp / 1000)::double precision))::integer / 15)::double precision * '00:15:00'::interval)
321
+ ORDER BY (date_trunc('hour'::text, to_timestamp((flow_measurements.start_timestamp / 1000)::double precision)) + (date_part('minute'::text, to_timestamp((flow_measurements.start_timestamp / 1000)::double precision))::integer / 15)::double precision * '00:15:00'::interval), (flow_measurements.cube_id::character varying(50)), (flow_measurements.sink_id::character varying(50))
322
+ ), measurements AS (
323
+ SELECT wifi.locations_id,
324
+ wifi.directions_id,
325
+ wifi.measured_from,
326
+ wifi.value,
327
+ wifi.count_n
328
+ FROM wifi
329
+ UNION ALL
330
+ SELECT pyro.locations_id,
331
+ pyro.directions_id,
332
+ pyro.measured_from,
333
+ pyro.value,
334
+ pyro.count_n
335
+ FROM pyro
336
+ UNION ALL
337
+ SELECT flow.location_id,
338
+ flow.direction_id,
339
+ flow.measured_from,
340
+ flow.value,
341
+ flow.count_n
342
+ FROM flow
343
+ ), calendar AS (
344
+ SELECT generate_series(min(measurements.measured_from), max(measurements.measured_from), '00:15:00'::interval) AS calendar
345
+ FROM measurements
346
+ ), classes AS (
347
+ SELECT calendar.calendar,
348
+ m_1.location_id,
349
+ m_1.direction_id
350
+ FROM calendar
351
+ JOIN ( SELECT DISTINCT measurements.locations_id::character varying(50) AS location_id,
352
+ measurements.directions_id::character varying(50) AS direction_id
353
+ FROM measurements) m_1 ON true
354
+ )
355
+ SELECT c.calendar,
356
+ date_trunc('hour'::text, c.calendar) AS calendar_hour,
357
+ c.calendar::date AS calendar_date,
358
+ ll.measurement_start,
359
+ COALESCE(ll.measurement_end, now()::date::timestamp with time zone) AS measurement_end,
360
+ c.location_id,
361
+ c.direction_id,
362
+ concat(c.location_id, '-', c.direction_id) AS compound_id,
363
+ COALESCE(m.value, 0::bigint) AS value,
364
+ COALESCE(m.count_n, 0::bigint) AS count_n
365
+ FROM classes c
366
+ LEFT JOIN measurements m ON c.calendar = m.measured_from AND c.location_id::text = m.locations_id::text AND c.direction_id::text = m.directions_id::text
367
+ LEFT JOIN analytic.pedestrians_locations_list ll ON c.location_id::text = ll.cube_id::text
368
+ WHERE c.calendar >= ll.measurement_start AND c.calendar <= COALESCE(ll.measurement_end, now()::date::timestamp with time zone);
369
+
370
+
371
+ CREATE OR REPLACE VIEW analytic.v_pedestrians_detections_api
372
+ AS WITH wifi AS (
373
+ SELECT pedestrians_wifi.location_id,
374
+ pedestrians_wifi.direction_id,
375
+ date_trunc('hour'::text, pedestrians_wifi.measured_from) + (date_part('minute'::text, pedestrians_wifi.measured_from)::integer / 15)::double precision * '00:15:00'::interval AS measured_from,
376
+ date_trunc('hour'::text, pedestrians_wifi.measured_from) + (date_part('minute'::text, pedestrians_wifi.measured_from)::integer / 15)::double precision * '00:15:00'::interval + '00:15:00'::interval AS measured_to,
377
+ sum(pedestrians_wifi.value) AS value,
378
+ count(pedestrians_wifi.value) AS count_n,
379
+ 3 AS quantity
380
+ FROM flow.pedestrians_wifi
381
+ GROUP BY pedestrians_wifi.location_id, pedestrians_wifi.direction_id, (date_trunc('hour'::text, pedestrians_wifi.measured_from) + (date_part('minute'::text, pedestrians_wifi.measured_from)::integer / 15)::double precision * '00:15:00'::interval), (date_trunc('hour'::text, pedestrians_wifi.measured_from) + (date_part('minute'::text, pedestrians_wifi.measured_from)::integer / 15)::double precision * '00:15:00'::interval + '00:15:00'::interval)
382
+ ), pyro AS (
383
+ SELECT cd.locations_id AS location_id,
384
+ cd.directions_id AS direction_id,
385
+ to_timestamp((cd.measured_from / 1000)::double precision) AS measured_from,
386
+ to_timestamp((cd.measured_from / 1000)::double precision) + '00:15:00'::interval AS measured_to,
387
+ sum(cd.value) AS value,
388
+ count(cd.value) AS count_n,
389
+ 1 AS quantity
390
+ FROM flow.counters_detections cd
391
+ WHERE cd.category::text = 'pedestrian'::text AND (cd.directions_id::text IN ( SELECT DISTINCT pedestrians_directions_api.direction_id
392
+ FROM flow.pedestrians_directions_api))
393
+ GROUP BY cd.locations_id, cd.directions_id, (to_timestamp((cd.measured_from / 1000)::double precision)), (to_timestamp((cd.measured_from / 1000)::double precision) + '00:15:00'::interval)
394
+ ), flow AS (
395
+ SELECT flow_measurements.cube_id::character varying(50) AS location_id,
396
+ flow_measurements.sink_id::character varying(50) AS direction_id,
397
+ date_trunc('hour'::text, to_timestamp((flow_measurements.start_timestamp / 1000)::double precision)) + (date_part('minute'::text, to_timestamp((flow_measurements.start_timestamp / 1000)::double precision))::integer / 15)::double precision * '00:15:00'::interval AS measured_from,
398
+ date_trunc('hour'::text, to_timestamp((flow_measurements.start_timestamp / 1000)::double precision)) + (date_part('minute'::text, to_timestamp((flow_measurements.start_timestamp / 1000)::double precision))::integer / 15)::double precision * '00:15:00'::interval + '00:15:00'::interval AS measured_to,
399
+ sum(flow_measurements.value) AS value,
400
+ count(flow_measurements.value) AS count_n,
401
+ 3 AS quantity
402
+ FROM flow.flow_measurements
403
+ WHERE ((flow_measurements.cube_id::character varying(50)::text, flow_measurements.sink_id::character varying(50)::text) IN ( SELECT DISTINCT pedestrians_directions_api.cube_id AS location_id,
404
+ pedestrians_directions_api.direction_id
405
+ FROM flow.pedestrians_directions_api)) AND flow_measurements.category::text = 'pedestrian'::text
406
+ GROUP BY (flow_measurements.cube_id::character varying(50)), (flow_measurements.sink_id::character varying(50)), (date_trunc('hour'::text, to_timestamp((flow_measurements.start_timestamp / 1000)::double precision)) + (date_part('minute'::text, to_timestamp((flow_measurements.start_timestamp / 1000)::double precision))::integer / 15)::double precision * '00:15:00'::interval), (date_trunc('hour'::text, to_timestamp((flow_measurements.start_timestamp / 1000)::double precision)) + (date_part('minute'::text, to_timestamp((flow_measurements.start_timestamp / 1000)::double precision))::integer / 15)::double precision * '00:15:00'::interval + '00:15:00'::interval)
407
+ ORDER BY (date_trunc('hour'::text, to_timestamp((flow_measurements.start_timestamp / 1000)::double precision)) + (date_part('minute'::text, to_timestamp((flow_measurements.start_timestamp / 1000)::double precision))::integer / 15)::double precision * '00:15:00'::interval), (flow_measurements.cube_id::character varying(50)), (flow_measurements.sink_id::character varying(50))
408
+ ), measurements AS (
409
+ SELECT wifi.location_id,
410
+ wifi.direction_id,
411
+ wifi.measured_from,
412
+ wifi.measured_to,
413
+ wifi.value,
414
+ wifi.count_n,
415
+ wifi.quantity
416
+ FROM wifi
417
+ UNION ALL
418
+ SELECT pyro.location_id,
419
+ pyro.direction_id,
420
+ pyro.measured_from,
421
+ pyro.measured_to,
422
+ pyro.value,
423
+ pyro.count_n,
424
+ pyro.quantity
425
+ FROM pyro
426
+ UNION ALL
427
+ SELECT flow.location_id,
428
+ flow.direction_id,
429
+ flow.measured_from,
430
+ flow.measured_to,
431
+ flow.value,
432
+ flow.count_n,
433
+ flow.quantity
434
+ FROM flow
435
+ )
436
+ SELECT measurements.measured_from,
437
+ measurements.measured_to,
438
+ pda.location_id::character varying(50) AS location_id,
439
+ measurements.direction_id,
440
+ measurements.value,
441
+ CASE
442
+ WHEN (measurements.count_n::numeric / measurements.quantity::numeric) > 1::numeric THEN 1::numeric
443
+ ELSE measurements.count_n::numeric / measurements.quantity::numeric
444
+ END AS quality
445
+ FROM measurements
446
+ LEFT JOIN flow.pedestrians_directions_api pda ON measurements.direction_id::text = pda.direction_id::text and measurements.location_id = pda.cube_id;
447
+
448
+
449
+ CREATE OR REPLACE VIEW analytic.v_pedestrians_detections_daily
450
+ AS WITH wifi AS (
451
+ SELECT pedestrians_wifi.location_id AS locations_id,
452
+ pedestrians_wifi.direction_id AS directions_id,
453
+ pedestrians_wifi.measured_from::date AS measured_from,
454
+ sum(pedestrians_wifi.value) AS value,
455
+ count(pedestrians_wifi.value) AS count_n,
456
+ count(pedestrians_wifi.value)::numeric / 288::numeric AS quality_ratio
457
+ FROM flow.pedestrians_wifi
458
+ GROUP BY pedestrians_wifi.location_id, pedestrians_wifi.direction_id, (pedestrians_wifi.measured_from::date)
459
+ ), pyro AS (
460
+ SELECT cd.locations_id,
461
+ lg.first_dir AS directions_id,
462
+ to_timestamp((cd.measured_from / 1000)::double precision)::date AS measured_from,
463
+ sum(cd.value) AS value,
464
+ count(cd.value) / count(DISTINCT cd.directions_id) AS count_n,
465
+ count(cd.value)::numeric / 96::numeric / count(DISTINCT cd.directions_id)::numeric AS quality_ratio
466
+ FROM flow.counters_detections cd
467
+ LEFT JOIN ( SELECT pedestrians_locations_gates.direction_id,
468
+ pedestrians_locations_gates.direction_type,
469
+ min(pedestrians_locations_gates.direction_id::text) OVER (PARTITION BY pedestrians_locations_gates.cube_id, pedestrians_locations_gates.direction_type) AS first_dir
470
+ FROM analytic.pedestrians_locations_gates) lg ON cd.directions_id::text = lg.direction_id::text
471
+ WHERE cd.category::text = 'pedestrian'::text AND (cd.directions_id::text IN ( SELECT DISTINCT pedestrians_locations_gates.direction_id
472
+ FROM analytic.pedestrians_locations_gates))
473
+ GROUP BY cd.locations_id, lg.first_dir, (to_timestamp((cd.measured_from / 1000)::double precision)::date)
474
+ ), flow AS (
475
+ SELECT flow_measurements.cube_id::character varying(50) AS location_id,
476
+ flow_measurements.sink_id::character varying(50) AS direction_id,
477
+ to_timestamp((flow_measurements.start_timestamp / 1000)::double precision)::date AS measured_from,
478
+ sum(flow_measurements.value) AS value,
479
+ count(flow_measurements.value) AS count_n,
480
+ count(flow_measurements.value)::numeric / 288::numeric AS quality_ratio
481
+ FROM flow.flow_measurements
482
+ WHERE ((flow_measurements.cube_id::character varying(50)::text, flow_measurements.sink_id::character varying(50)::text) IN ( SELECT DISTINCT pedestrians_locations_gates.cube_id AS location_id,
483
+ pedestrians_locations_gates.direction_id
484
+ FROM analytic.pedestrians_locations_gates)) AND flow_measurements.category::text = 'pedestrian'::text
485
+ GROUP BY (flow_measurements.cube_id::character varying(50)), (flow_measurements.sink_id::character varying(50)), (to_timestamp((flow_measurements.start_timestamp / 1000)::double precision)::date)
486
+ ORDER BY (to_timestamp((flow_measurements.start_timestamp / 1000)::double precision)::date), (flow_measurements.cube_id::character varying(50)) DESC, (flow_measurements.sink_id::character varying(50))
487
+ ), measurements AS (
488
+ SELECT pyro.locations_id,
489
+ pyro.directions_id,
490
+ pyro.measured_from,
491
+ pyro.value,
492
+ pyro.count_n,
493
+ pyro.quality_ratio
494
+ FROM pyro
495
+ UNION ALL
496
+ SELECT flow.location_id,
497
+ flow.direction_id,
498
+ flow.measured_from,
499
+ flow.value,
500
+ flow.count_n,
501
+ flow.quality_ratio
502
+ FROM flow
503
+ UNION ALL
504
+ SELECT wifi.locations_id,
505
+ wifi.directions_id,
506
+ wifi.measured_from,
507
+ wifi.value,
508
+ wifi.count_n,
509
+ wifi.quality_ratio
510
+ FROM wifi
511
+ ), calendar AS (
512
+ SELECT generate_series(min(measurements.measured_from)::timestamp with time zone, max(measurements.measured_from)::timestamp with time zone, '1 day'::interval) AS calendar
513
+ FROM measurements
514
+ ), classes AS (
515
+ SELECT calendar.calendar,
516
+ m_1.location_id,
517
+ m_1.direction_id
518
+ FROM calendar
519
+ JOIN ( SELECT DISTINCT measurements.locations_id::character varying(50) AS location_id,
520
+ measurements.directions_id::character varying(50) AS direction_id
521
+ FROM measurements) m_1 ON true
522
+ )
523
+ SELECT c.calendar,
524
+ c.location_id,
525
+ c.direction_id,
526
+ concat(c.location_id, '-', c.direction_id) AS compound_id,
527
+ COALESCE(m.value, 0::bigint) AS value,
528
+ COALESCE(m.count_n, 0::bigint) AS count_n,
529
+ COALESCE(m.quality_ratio, 0::numeric) AS quality_ratio,
530
+ CASE
531
+ WHEN c.calendar < ll.measurement_start OR c.calendar > ll.measurement_end THEN 4
532
+ WHEN COALESCE(m.quality_ratio, 0::numeric) = 0::numeric THEN 3
533
+ WHEN COALESCE(m.quality_ratio, 0::numeric) < 0.9 THEN 2
534
+ WHEN COALESCE(m.quality_ratio, 0::numeric) > 0.9 AND COALESCE(m.quality_ratio, 0::numeric) < 1::numeric THEN 1
535
+ WHEN COALESCE(m.quality_ratio, 0::numeric) >= 1::numeric THEN 0
536
+ ELSE NULL::integer
537
+ END AS quality_code,
538
+ CASE
539
+ WHEN c.calendar < ll.measurement_start OR c.calendar > ll.measurement_end THEN 'Nenainstalovaná technologie'::text
540
+ WHEN COALESCE(m.quality_ratio, 0::numeric) = 0::numeric THEN 'Bez dat (0%)'::text
541
+ WHEN COALESCE(m.quality_ratio, 0::numeric) < 0.9 THEN 'Částečná data (<90%)'::text
542
+ WHEN COALESCE(m.quality_ratio, 0::numeric) > 0.9 AND COALESCE(m.quality_ratio, 0::numeric) < 1::numeric THEN 'Nekompletní data (>90%)'::text
543
+ WHEN COALESCE(m.quality_ratio, 0::numeric) >= 1::numeric THEN 'Kompletní data (100%)'::text
544
+ ELSE NULL::text
545
+ END AS quality_status
546
+ FROM classes c
547
+ LEFT JOIN measurements m ON c.calendar = m.measured_from AND c.location_id::text = m.locations_id::text AND c.direction_id::text = m.directions_id
548
+ LEFT JOIN analytic.pedestrians_locations_list ll ON c.location_id::text = ll.cube_id::text;
549
+
550
+
551
+ CREATE OR REPLACE VIEW analytic.v_pedestrians_flow_quality
552
+ AS WITH timeline_category AS (
553
+ SELECT DISTINCT fs.cube_id,
554
+ fs.id AS sink_id,
555
+ timeline."timestamp"
556
+ FROM flow_sinks fs
557
+ LEFT JOIN ( SELECT generate_series(date_trunc('month'::text, CURRENT_DATE - '180 days'::interval)::timestamp with time zone, date_trunc('hour'::text, CURRENT_DATE::timestamp with time zone), '01:00:00'::interval) AS "timestamp") timeline ON true
558
+ ), real_nrow AS (
559
+ SELECT date_trunc('hour'::text, to_timestamp((flow_measurements.start_timestamp / 1000)::double precision)) AS start_timestamp,
560
+ flow_measurements.cube_id,
561
+ flow_measurements.sink_id,
562
+ count(*) AS n_row,
563
+ count(*) FILTER (WHERE flow_measurements.category::text = 'pedestrian'::text) AS n_row_pedestrian
564
+ FROM flow_measurements
565
+ WHERE flow_measurements.start_timestamp::double precision >= (1000::double precision * date_part('epoch'::text, CURRENT_DATE - '210 days'::interval))
566
+ GROUP BY (date_trunc('hour'::text, to_timestamp((flow_measurements.start_timestamp / 1000)::double precision))), flow_measurements.cube_id, flow_measurements.sink_id
567
+ )
568
+ SELECT t."timestamp",
569
+ date_trunc('month'::text, t."timestamp")::date AS month,
570
+ t.cube_id,
571
+ t.sink_id,
572
+ concat(t.cube_id, ' - ', t.sink_id) AS cube_sink,
573
+ COALESCE(r.n_row::numeric, 0::bigint::numeric) AS n_row,
574
+ COALESCE(r.n_row_pedestrian::numeric, 0::bigint::numeric) AS n_row_pedestrian,
575
+ 108::bigint AS expected_nrow, -- 9 categories * 12 measurements/hr
576
+ 12 AS expected_nrow_pedestrian -- 1 category * 12 measurements/hr
577
+ FROM timeline_category t
578
+ LEFT JOIN real_nrow r ON t."timestamp" = r.start_timestamp AND t.cube_id = r.cube_id AND t.sink_id = r.sink_id;
579
+
580
+
581
+ CREATE OR REPLACE VIEW analytic.v_pedestrians_flow_quality
582
+ AS WITH timeline_category AS (
583
+ SELECT DISTINCT fs.cube_id,
584
+ fs.id AS sink_id,
585
+ timeline."timestamp"
586
+ FROM flow_sinks fs
587
+ LEFT JOIN ( SELECT generate_series(date_trunc('month'::text, CURRENT_DATE - '180 days'::interval)::timestamp with time zone, date_trunc('hour'::text, CURRENT_DATE::timestamp with time zone), '01:00:00'::interval) AS "timestamp") timeline ON true
588
+ ), real_nrow AS (
589
+ SELECT date_trunc('hour'::text, to_timestamp((flow_measurements.start_timestamp / 1000)::double precision)) AS start_timestamp,
590
+ flow_measurements.cube_id,
591
+ flow_measurements.sink_id,
592
+ count(*) AS n_row,
593
+ count(*) FILTER (WHERE flow_measurements.category::text = 'pedestrian'::text) AS n_row_pedestrian
594
+ FROM flow_measurements
595
+ WHERE flow_measurements.start_timestamp::double precision >= (1000::double precision * date_part('epoch'::text, CURRENT_DATE - '210 days'::interval))
596
+ GROUP BY (date_trunc('hour'::text, to_timestamp((flow_measurements.start_timestamp / 1000)::double precision))), flow_measurements.cube_id, flow_measurements.sink_id
597
+ )
598
+ SELECT t."timestamp",
599
+ date_trunc('month'::text, t."timestamp")::date AS month,
600
+ t.cube_id,
601
+ t.sink_id,
602
+ concat(t.cube_id, ' - ', t.sink_id) AS cube_sink,
603
+ COALESCE(r.n_row::numeric, 0::bigint::numeric) AS n_row,
604
+ COALESCE(r.n_row_pedestrian::numeric, 0::bigint::numeric) AS n_row_pedestrian,
605
+ 108::bigint AS expected_nrow, -- 9 categories * 12 measurements/hr
606
+ 12 AS expected_nrow_pedestrian -- 1 category * 12 measurements/hr
607
+ FROM timeline_category t
608
+ LEFT JOIN real_nrow r ON t."timestamp" = r.start_timestamp AND t.cube_id = r.cube_id AND t.sink_id = r.sink_id;