v0.4.4
This commit is contained in:
@@ -86,6 +86,19 @@ CREATE INDEX IF NOT EXISTS zone_readings_zone_time_idx
|
||||
CREATE INDEX IF NOT EXISTS zone_readings_time_idx
|
||||
ON zone_readings(timestamp DESC);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS ha_readings (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
entity_id TEXT NOT NULL,
|
||||
zone_id TEXT,
|
||||
kind TEXT NOT NULL,
|
||||
timestamp TEXT NOT NULL,
|
||||
temperature REAL NOT NULL
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS ha_readings_entity_time_idx
|
||||
ON ha_readings(entity_id, timestamp DESC);
|
||||
CREATE INDEX IF NOT EXISTS ha_readings_zone_time_idx
|
||||
ON ha_readings(zone_id, timestamp DESC);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS event_log (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
timestamp TEXT NOT NULL,
|
||||
@@ -110,6 +123,8 @@ INSERT OR IGNORE INTO schema_migrations(version, applied_at)
|
||||
VALUES (2, strftime('%Y-%m-%dT%H:%M:%fZ','now'));
|
||||
INSERT OR IGNORE INTO schema_migrations(version, applied_at)
|
||||
VALUES (3, strftime('%Y-%m-%dT%H:%M:%fZ','now'));
|
||||
INSERT OR IGNORE INTO schema_migrations(version, applied_at)
|
||||
VALUES (4, strftime('%Y-%m-%dT%H:%M:%fZ','now'));
|
||||
"#;
|
||||
|
||||
pub const COUNT_DEVICES: &str = "SELECT COUNT(*) FROM devices";
|
||||
@@ -198,6 +213,26 @@ ORDER BY timestamp ASC
|
||||
LIMIT ?2
|
||||
"#;
|
||||
|
||||
pub const LIST_DEVICE_HISTORY_BY_DEVICE_BUCKETED: &str = r#"
|
||||
SELECT MIN(id),device_id,MIN(timestamp),AVG(indoor_temperature),AVG(outdoor_temperature),
|
||||
AVG(target_temperature),MAX(power),MAX(source)
|
||||
FROM readings
|
||||
WHERE device_id=?1 AND timestamp>=?2
|
||||
GROUP BY device_id, CAST(unixepoch(timestamp)/?3 AS INTEGER)
|
||||
ORDER BY MIN(timestamp) ASC
|
||||
LIMIT ?4
|
||||
"#;
|
||||
|
||||
pub const LIST_DEVICE_HISTORY_ALL_BUCKETED: &str = r#"
|
||||
SELECT MIN(id),device_id,MIN(timestamp),AVG(indoor_temperature),AVG(outdoor_temperature),
|
||||
AVG(target_temperature),MAX(power),MAX(source)
|
||||
FROM readings
|
||||
WHERE timestamp>=?1
|
||||
GROUP BY device_id, CAST(unixepoch(timestamp)/?2 AS INTEGER)
|
||||
ORDER BY MIN(timestamp) ASC
|
||||
LIMIT ?3
|
||||
"#;
|
||||
|
||||
pub const PRUNE_READINGS: &str = "DELETE FROM readings WHERE timestamp < ?1";
|
||||
|
||||
pub const INSERT_ZONE_READING_IF_DUE: &str = r#"
|
||||
@@ -243,6 +278,43 @@ pub const DELETE_ZONE_READINGS_BY_ZONE_ID: &str = "DELETE FROM zone_readings WHE
|
||||
pub const DELETE_ZONE_READINGS_BY_DEVICE_ID: &str = "DELETE FROM zone_readings WHERE device_id=?1";
|
||||
pub const PRUNE_ZONE_READINGS: &str = "DELETE FROM zone_readings WHERE timestamp < ?1";
|
||||
|
||||
pub const INSERT_HA_READING_IF_DUE: &str = r#"
|
||||
INSERT INTO ha_readings(entity_id,zone_id,kind,timestamp,temperature)
|
||||
SELECT ?1,?2,?3,?4,?5
|
||||
WHERE NOT EXISTS (
|
||||
SELECT 1 FROM ha_readings
|
||||
WHERE entity_id=?1 AND COALESCE(zone_id,'')=COALESCE(?2,'') AND kind=?3 AND timestamp>=?6
|
||||
LIMIT 1
|
||||
)
|
||||
"#;
|
||||
|
||||
pub const LIST_HA_HISTORY_BY_ENTITY_BUCKETED: &str = r#"
|
||||
SELECT MIN(id),entity_id,MAX(zone_id),MAX(kind),MIN(timestamp),AVG(temperature)
|
||||
FROM ha_readings
|
||||
WHERE entity_id=?1 AND timestamp>=?2
|
||||
GROUP BY entity_id,COALESCE(zone_id,''),kind,CAST(unixepoch(timestamp)/?3 AS INTEGER)
|
||||
ORDER BY MIN(timestamp) ASC
|
||||
LIMIT ?4
|
||||
"#;
|
||||
|
||||
pub const LIST_HA_HISTORY_ALL_BUCKETED: &str = r#"
|
||||
SELECT MIN(id),entity_id,MAX(zone_id),MAX(kind),MIN(timestamp),AVG(temperature)
|
||||
FROM ha_readings
|
||||
WHERE timestamp>=?1
|
||||
GROUP BY entity_id,COALESCE(zone_id,''),kind,CAST(unixepoch(timestamp)/?2 AS INTEGER)
|
||||
ORDER BY MIN(timestamp) ASC
|
||||
LIMIT ?3
|
||||
"#;
|
||||
|
||||
pub const PRUNE_HA_READINGS: &str = "DELETE FROM ha_readings WHERE timestamp < ?1";
|
||||
|
||||
pub const HISTORY_COUNTS: &str = r#"
|
||||
SELECT
|
||||
(SELECT COUNT(*) FROM readings),
|
||||
(SELECT COUNT(*) FROM zone_readings),
|
||||
(SELECT COUNT(*) FROM ha_readings)
|
||||
"#;
|
||||
|
||||
pub const INSERT_EVENT: &str =
|
||||
"INSERT INTO event_log(timestamp,level,kind,message,metadata) VALUES(?1,?2,?3,?4,?5)";
|
||||
pub const LIST_EVENTS: &str =
|
||||
|
||||
Reference in New Issue
Block a user