summaryrefslogtreecommitdiff
path: root/populate_cache.sql
diff options
context:
space:
mode:
Diffstat (limited to 'populate_cache.sql')
-rw-r--r--populate_cache.sql13
1 files changed, 7 insertions, 6 deletions
diff --git a/populate_cache.sql b/populate_cache.sql
index 0af4b36..0c30a18 100644
--- a/populate_cache.sql
+++ b/populate_cache.sql
@@ -1,18 +1,19 @@
drop table value_cache cascade;
CREATE TABLE value_cache (
-value_int integer,
+value_real integer,
timestamp timestamp,
sensor_id integer
);
create or replace view vi_value_cache as
- SELECT timestamp, sensor_id, value_int, 'CACHE'
+ SELECT timestamp, sensor_id, value_real, 'CACHE'
FROM value_cache
UNION
- SELECT date_trunc('hour', timestamp), sensor_id, avg(value_int), 'LIVE'
+ SELECT date_trunc('hour', timestamp), sensor_id, COALESCE(avg(value_int),avg(value_real)), 'LIVE'
FROM value
- WHERE timestamp > coalesce( (select max(timestamp) from value_cache), now() - interval '2 days')
+ WHERE date_trunc('hour', timestamp) > coalesce((select max(timestamp) from value_cache),
+ now() - interval '2 days')
GROUP BY date_trunc('hour', timestamp), sensor_id;
;
@@ -34,9 +35,9 @@ BEGIN
delete from value_cache where timestamp >= last_update;
insert into value_cache
- select avg(value_int), date_trunc('hour', timestamp), sensor_id
+ select COALESCE(avg(value_real),avg(value_int)), date_trunc('hour', timestamp), sensor_id
from value
- where date_trunc('hour', timestamp) >= last_update
+ where date_trunc('hour', timestamp) > last_update
group by date_trunc('hour', timestamp), sensor_id;
RETURN last_update;