From: Eugene Crosser Date: Fri, 26 Jul 2024 20:44:31 +0000 (+0200) Subject: Add hourly aggregation X-Git-Url: http://average.org/gitweb/?a=commitdiff_plain;h=2d427020ba054e21eb9f7e222297e72d2cbcb896;p=tbcncollector.git Add hourly aggregation --- diff --git a/aggregate.sql b/aggregate.sql new file mode 100644 index 0000000..0efab0c --- /dev/null +++ b/aggregate.sql @@ -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")); diff --git a/debian/changelog b/debian/changelog index 2d27138..eeeb261 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,9 @@ +tbcncollector (1.00) experimental; urgency=low + + * Hourly aggregation + + -- Eugene Crosser Fri, 26 Jul 2024 22:47:09 +0200 + tbcncollector (0.01) experimental; urgency=low * Initial version diff --git a/debian/control b/debian/control index babdb48..65e837d 100644 --- a/debian/control +++ b/debian/control @@ -19,6 +19,7 @@ Architecture: all Section: python Depends: adduser, python3-bleak, + sqlite3, ${misc:Depends}, ${python3:Depends} Description: Daemon to collect data from ThermoBeacons diff --git a/debian/default b/debian/default index 5b7ef27..ced87e4 100644 --- a/debian/default +++ b/debian/default @@ -1,2 +1,3 @@ # Environment for tbcncollector -OPTIONS="" +RAW_DB="/var/lib/tbcncollector/raw.db" +HOURLY_DB="/var/lib/tbcncollector/hourly.db" diff --git a/debian/install b/debian/install index f53ee3b..e93f657 100644 --- a/debian/install +++ b/debian/install @@ -1 +1,2 @@ README.md var/lib/tbcncollector +aggregate.sql usr/share/tbcncollector/ diff --git a/debian/rules b/debian/rules index 98153f8..5e66d01 100755 --- a/debian/rules +++ b/debian/rules @@ -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 index 0000000..ea90eaf --- /dev/null +++ b/debian/tbcncollector-hourly.service @@ -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 index 0000000..4d4d749 --- /dev/null +++ b/debian/tbcncollector-hourly.timer @@ -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 diff --git a/debian/tbcncollector.service b/debian/tbcncollector.service index b09fc08..f0a4369 100644 --- a/debian/tbcncollector.service +++ b/debian/tbcncollector.service @@ -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