@@ -35,7 +35,7 @@ WHERE units IN ('µg/m��','��g/m³', 'ug/m3');
35
35
36
36
37
37
38
- -- match the locations to the nodes using the source_name/id combo
38
+ -- match the locations to existing nodes using the source_name/id combo
39
39
UPDATE staging_sensornodes
40
40
SET sensor_nodes_id = s .sensor_nodes_id
41
41
, timezones_id = s .timezones_id
@@ -109,7 +109,9 @@ SELECT site_name
109
109
, metadata
110
110
, source_id
111
111
, timezones_id
112
- , get_providers_id(source_name)
112
+ -- default to the unknown provider
113
+ -- just to make sure we have one set
114
+ , COALESCE(get_providers_id(source_name), 1 )
113
115
, countries_id
114
116
FROM staging_sensornodes
115
117
WHERE sensor_nodes_id IS NULL
@@ -156,17 +158,20 @@ FROM r;
156
158
-- Sensor Systems --
157
159
-- ------------------
158
160
161
+
159
162
-- make sure that we have a system entry for every ingest_id
160
163
-- this is to deal with fetchers that do not add these data
161
164
INSERT INTO staging_sensorsystems (sensor_nodes_id, ingest_id, fetchlogs_id, metadata)
162
165
SELECT sensor_nodes_id
163
- , source_id -- the ingest_id has the source_name in it and we dont need/want that
166
+ -- , source_id -- the ingest_id has the source_name in it and we dont need/want that
167
+ , ingest_id
164
168
, fetchlogs_id
165
169
, ' {"note":"automatically added for sensor node"}'
166
170
FROM staging_sensornodes
167
- WHERE is_new
171
+ WHERE is_new AND ingest_id NOT IN ( SELECT ingest_sensor_nodes_id FROM staging_sensorsystems)
168
172
ON CONFLICT (ingest_id) DO UPDATE
169
- SET sensor_nodes_id = EXCLUDED .sensor_nodes_id ;
173
+ SET sensor_nodes_id = EXCLUDED .sensor_nodes_id
174
+ ;
170
175
171
176
-- Now match the sensor nodes to the system
172
177
UPDATE staging_sensorsystems
@@ -197,15 +202,18 @@ SELECT COUNT(1) INTO __rejected_systems
197
202
FROM r;
198
203
199
204
-- And finally we add/update the sensor systems
200
- INSERT INTO sensor_systems (sensor_nodes_id, source_id, metadata)
205
+ INSERT INTO sensor_systems (sensor_nodes_id, source_id, instruments_id, metadata)
201
206
SELECT sensor_nodes_id
202
- , ingest_id
207
+ , s .ingest_id
208
+ , i .instruments_id
203
209
, metadata
204
- FROM staging_sensorsystems
210
+ FROM staging_sensorsystems s
211
+ LEFT JOIN instruments i ON (s .instrument_ingest_id = i .ingest_id )
205
212
WHERE sensor_nodes_id IS NOT NULL
206
- GROUP BY sensor_nodes_id, ingest_id, metadata
213
+ GROUP BY sensor_nodes_id, s . ingest_id , instruments_id , metadata
207
214
ON CONFLICT (sensor_nodes_id, source_id) DO UPDATE SET
208
215
metadata= COALESCE(sensor_systems .metadata , ' {}' ) || COALESCE(EXCLUDED .metadata , ' {}' )
216
+ , instruments_id = EXCLUDED .instruments_id
209
217
, modified_on = now();
210
218
211
219
-- --------------------------
@@ -266,10 +274,11 @@ AND sensors.source_id = staging_sensors.ingest_id;
266
274
267
275
268
276
UPDATE staging_sensors
269
- SET measurands_id = measurands .measurands_id
270
- from measurands
271
- WHERE staging_sensors .measurand = measurands .measurand
272
- and staging_sensors .units = measurands .units ;
277
+ SET measurands_id = m .measurands_id
278
+ FROM (SELECT measurand, MIN (measurands_id) AS measurands_id FROM measurands GROUP BY measurand) as m
279
+ WHERE staging_sensors .measurand = m .measurand
280
+ -- AND staging_sensors.units=measurands.units
281
+ ;
273
282
274
283
275
284
WITH r AS (
@@ -290,20 +299,34 @@ INSERT INTO sensors (
290
299
source_id
291
300
, sensor_systems_id
292
301
, measurands_id
302
+ , data_logging_period_seconds
303
+ , data_averaging_period_seconds
304
+ , sensor_statuses_id
293
305
, metadata)
294
306
SELECT ingest_id
295
307
, sensor_systems_id
296
308
, measurands_id
309
+ , logging_interval_seconds
310
+ , averaging_interval_seconds
311
+ , COALESCE(ss .sensor_statuses_id , 1 )
297
312
, metadata
298
- FROM staging_sensors
313
+ FROM staging_sensors s
314
+ LEFT JOIN sensor_statuses ss ON (ss .short_code = s .status )
299
315
WHERE measurands_id is not null
300
316
AND sensor_systems_id is not null
301
317
GROUP BY ingest_id
302
318
, sensor_systems_id
303
319
, measurands_id
320
+ , logging_interval_seconds
321
+ , averaging_interval_seconds
322
+ , ss .sensor_statuses_id
304
323
, metadata
305
324
ON CONFLICT (sensor_systems_id, measurands_id, source_id) DO UPDATE
306
325
SET metadata = COALESCE(sensors .metadata , ' {}' ) || COALESCE(EXCLUDED .metadata , ' {}' )
326
+ , data_logging_period_seconds = EXCLUDED .data_logging_period_seconds
327
+ , data_averaging_period_seconds = EXCLUDED .data_averaging_period_seconds
328
+ , sensor_statuses_id = EXCLUDED .sensor_statuses_id
329
+ , modified_on = now()
307
330
RETURNING 1 )
308
331
SELECT COUNT (1 ) INTO __inserted_sensors
309
332
FROM inserts;
@@ -327,6 +350,61 @@ RETURNING 1)
327
350
SELECT COUNT (1 ) INTO __rejected_sensors
328
351
FROM r;
329
352
353
+
354
+ -- update the period so that we dont have to keep doing it later
355
+ -- we could do this on import as well if we feel this is slowing us down
356
+ UPDATE staging_flags
357
+ SET period = tstzrange(COALESCE(datetime_from, ' -infinity' ::timestamptz ),COALESCE(datetime_to, ' infinity' ::timestamptz ), ' []' );
358
+
359
+ -- Now we have to match things
360
+ -- get the right node id and sensors id for the flags
361
+ UPDATE staging_flags
362
+ SET sensors_id = s .sensors_id
363
+ , sensor_nodes_id = sy .sensor_nodes_id
364
+ FROM sensors s
365
+ JOIN sensor_systems sy ON (s .sensor_systems_id = sy .sensor_systems_id )
366
+ WHERE staging_flags .sensor_ingest_id = s .source_id ;
367
+
368
+ -- and then get the right flags_id
369
+ UPDATE staging_flags
370
+ SET flag_types_id = ft .flag_types_id
371
+ FROM flag_types ft
372
+ WHERE split_part(staging_flags .ingest_id , ' ::' , 1 ) = ft .ingest_id ;
373
+
374
+ -- now we should look to see if we should be just extending a flag
375
+ UPDATE staging_flags sf
376
+ SET flags_id = fm .flags_id
377
+ FROM flags fm
378
+ -- where the core information is the same (exactly)
379
+ WHERE sf .sensor_nodes_id = fm .sensor_nodes_id
380
+ AND sf .flag_types_id = fm .flag_types_id
381
+ AND ((sf .note = fm .note ) OR (sf .note IS NULL AND fm .note IS NULL ))
382
+ -- the periods touch or overlap
383
+ AND fm .period && sf .period
384
+ -- and the flagged record sensors contains the current sensors
385
+ AND fm .sensors_ids @> ARRAY[sf .sensors_id ];
386
+
387
+ -- and finally we will insert the new flags
388
+ INSERT INTO flags (flag_types_id, sensor_nodes_id, sensors_ids, period, note)
389
+ SELECT flag_types_id
390
+ , sensor_nodes_id
391
+ , CASE WHEN sensors_id IS NOT NULL THEN ARRAY[sensors_id] ELSE NULL END
392
+ , period
393
+ , note
394
+ FROM staging_flags
395
+ WHERE flag_types_id IS NOT NULL
396
+ AND sensor_nodes_id IS NOT NULL
397
+ AND flags_id IS NULL ;
398
+
399
+ -- And then update any that need to be updated
400
+ UPDATE flags fm
401
+ SET period = sf .period + fm .period
402
+ , note = sf .note
403
+ , modified_on = now()
404
+ FROM staging_flags sf
405
+ WHERE sf .flags_id = fm .flags_id ;
406
+
407
+
330
408
-- ----------------
331
409
-- Return stats --
332
410
-- ----------------
0 commit comments