]> average.org Git - tbcncollector.git/commitdiff
Add hourly aggregation
authorEugene Crosser <crosser@average.org>
Fri, 26 Jul 2024 20:44:31 +0000 (22:44 +0200)
committerEugene Crosser <crosser@average.org>
Fri, 26 Jul 2024 21:20:03 +0000 (23:20 +0200)
aggregate.sql [new file with mode: 0644]
debian/changelog
debian/control
debian/default
debian/install
debian/rules
debian/tbcncollector-hourly.service [new file with mode: 0644]
debian/tbcncollector-hourly.timer [new file with mode: 0644]
debian/tbcncollector.service

diff --git a/aggregate.sql b/aggregate.sql
new file mode 100644 (file)
index 0000000..0efab0c
--- /dev/null
@@ -0,0 +1,31 @@
+attach :hrldb as hrl;
+create table if not exists hrl.data (
+       dt int,
+       addr text,
+       count int,
+       batt float,
+       mint float,
+       temp float,
+       maxt float,
+       minh float,
+       humid float,
+       maxh float,
+       primary key(dt, addr)
+) without rowid;
+
+attach :rawdb as raw;
+insert or ignore into hrl.data (dt, addr, count, batt, mint, temp, maxt, minh, humid, maxh)
+select strftime("%s", strftime("%Y-%m-%d %H:00:00", dt, "unixepoch")) hour,
+       addr,
+       count(*) count,
+       avg(batt) batt,
+       min(temp) mint,
+       avg(temp) temp,
+       max(temp) maxt,
+       min(humid) minh,
+       avg(humid) humid,
+       max(humid) maxh
+from raw.data where dt < strftime("%s", strftime("%Y-%m-%d %H:00:00", unixepoch(), "unixepoch"))
+group by strftime("%Y-%m-%d %H", dt, "unixepoch"), addr;
+
+delete from raw.data where dt < strftime("%s", datetime(strftime("%Y-%m-%d %H:00:00"), "-1 hour"));
index 2d271380cb64abb4a3b25aece86e6841cc40e784..eeeb26144cd42025d75747e43835413970f88219 100644 (file)
@@ -1,3 +1,9 @@
+tbcncollector (1.00) experimental; urgency=low
+
+  * Hourly aggregation
+
+ -- Eugene Crosser <crosser@average.org>  Fri, 26 Jul 2024 22:47:09 +0200
+
 tbcncollector (0.01) experimental; urgency=low
 
   * Initial version
index babdb48855719a338308ed775d1d2069eae0109e..65e837df4090f2ddb517846f52cc28e8221ca872 100644 (file)
@@ -19,6 +19,7 @@ Architecture: all
 Section: python
 Depends: adduser,
          python3-bleak,
+         sqlite3,
          ${misc:Depends},
          ${python3:Depends}
 Description: Daemon to collect data from ThermoBeacons
index 5b7ef2718f1cbaf48cfc3c6d6075c50b9d7a4c0e..ced87e4b39e2dab3c57dbba794acf749eae827b3 100644 (file)
@@ -1,2 +1,3 @@
 # Environment for tbcncollector
-OPTIONS=""
+RAW_DB="/var/lib/tbcncollector/raw.db"
+HOURLY_DB="/var/lib/tbcncollector/hourly.db"
index f53ee3b904c861f985f849bc700d7209ca6f8171..e93f657a35542cf419eba86d8792e9c02fd5d0fc 100644 (file)
@@ -1 +1,2 @@
 README.md var/lib/tbcncollector
+aggregate.sql usr/share/tbcncollector/
index 98153f859e719b88b621737628b95a5dc7297201..5e66d01e87ec41f16fe95e65c73bffec5011d33c 100755 (executable)
@@ -6,3 +6,8 @@ export PYBUILD_NAME=tbcncollector
 
 %:
        dh $@ --with python3 --buildsystem pybuild
+
+override_dh_installsystemd:
+       dh_installsystemd --name=tbcncollector
+       dh_installsystemd --name=tbcncollector-hourly
+
diff --git a/debian/tbcncollector-hourly.service b/debian/tbcncollector-hourly.service
new file mode 100644 (file)
index 0000000..ea90eaf
--- /dev/null
@@ -0,0 +1,17 @@
+[Unit]
+Description=Aggregate tbncollector data on hourly basis
+Wants=tbcncollector-hourly.timer
+
+[Service]
+Type=oneshot
+EnvironmentFile=-/etc/default/tbcncollector
+ExecStart=/usr/bin/sqlite3 "" ".param set :rawdb "${RAW_DB} \
+                             ".param set :hrldb "${HOURLY_DB} \
+                             ".read /usr/share/tbcncollector/aggregate.sql"
+StandardOutput=journal
+StandardError=inherit
+User=tbcncollector
+Group=tbcncollector
+
+[Install]
+WantedBy=multi-user.target
diff --git a/debian/tbcncollector-hourly.timer b/debian/tbcncollector-hourly.timer
new file mode 100644 (file)
index 0000000..4d4d749
--- /dev/null
@@ -0,0 +1,10 @@
+[Unit]
+Description=Aggregate tbncollector data on hourly basis
+Requires=tbcncollector-hourly.service
+
+[Timer]
+Unit=tbcncollector-hourly.service
+OnCalendar=*-*-* *:05:00
+
+[Install]
+WantedBy=timers.target
index b09fc08040f2b1b0b6711d5cd90e672f3d9345ae..f0a4369a3dfb592062e69ac67374e79c83693761 100644 (file)
@@ -4,7 +4,7 @@ Description=ThermoBeacon Collector Service
 [Service]
 Type=simple
 EnvironmentFile=-/etc/default/tbcncollector
-ExecStart=python3 -m tbcncollector $OPTIONS
+ExecStart=python3 -m tbcncollector $RAW_DB
 KillSignal=INT
 Restart=on-failure
 StandardOutput=journal