2 Commits
Author SHA1 Message Date
Iñigo Etxaniz eff8ea4dfe bug 2026-11-07 08:34:53 +00:00
Iñigo Etxaniz 4de1d3e008 upgrade script 2026-11-07 08:30:59 +00:00
2 changed files with 34 additions and 13 deletions
+4 -4
View File
@@ -1,7 +1,7 @@
# host-session
# host_session
Stores one row per finished session of the host in the custom table `host_session` (schema 0.0.1): start and end time, duration, mean and max of CPU, RAM and disk percentage, and coverage (share of the session's minutes with a CPU sample). The hub adds the gateway as `spoke_id`.
One row per session of the host in the custom table `host_session` (schema 0.0.1). The hub adds the gateway as `spoke_id`.
Runs every minute. The current boot comes from `/proc/uptime`. The previous session is found in the history of the uptime signal (63) before that boot: its last sample is the end, end minus that sample's uptime is the start. A session already stored (start within two minutes) is skipped, so repeated runs write nothing twice. The first row appears after the first reboot that follows the uptime signal's creation (2026-11-06 18:38).
The first run after a boot opens a row for the current session: its start time, the same value as end time (the mark of an open session), everything else 0. In the same run it closes the previous session: it finds that session in the history of the uptime signal (63) before the boot (last sample is the end, end minus that sample's uptime is the start), reads CPU, RAM and disk at one minute, and fills that session's open row with the end time, duration, means, maxima and coverage (share of minutes with a CPU sample). A previous session with no row, such as the one running when the job was first installed, gets a new full row. Every later run in the same boot sees the current row and stops after one query.
Signal ids are deb13-kde's in the home core. Needs only `requests` in the image.
Runs every minute. The current boot comes from `/proc/uptime`. Signal ids are deb13-kde's in the home core. Needs only `requests` in the image.
+30 -9
View File
@@ -60,9 +60,19 @@ def db_call(method, path, payload):
return body.get("data")
def already_stored(start_ms):
rows = db_call("POST", f"/api/db-query/{TABLE}/", {"filters": [], "orderBy": ["start_time"], "limit": 1000}) or []
return any(abs(row["start_time"] - start_ms) < MATCH_MS for row in rows)
def stored_rows():
return db_call("POST", f"/api/db-query/{TABLE}/", {"filters": [], "orderBy": ["start_time"], "limit": 1000}) or []
def find_row(rows, start_ms):
return next((row for row in rows if abs(row["start_time"] - start_ms) < MATCH_MS), None)
def open_row(start_ms):
row = {"start_time": start_ms, "end_time": start_ms, "duration_s": 0, "coverage": 0}
for prefix in ("cpu", "ram", "disk"):
row[f"{prefix}_mean"], row[f"{prefix}_max"] = 0, 0
return row
def mean_max(series, start_ms, end_ms):
@@ -87,16 +97,27 @@ def build_row(start_ms, end_ms):
return row
def main():
session = previous_session(current_boot_ms())
def close_previous(rows, boot_ms):
session = previous_session(boot_ms)
if session is None:
print("no previous session in the uptime history", flush=True)
return
if already_stored(session[0]):
print("previous session already stored", session[0], flush=True)
return
row = build_row(*session)
print("stored", db_call("POST", f"/api/db/{TABLE}/", row), row, flush=True)
stored = find_row(rows, session[0])
if stored is None:
print("previous session stored", db_call("POST", f"/api/db/{TABLE}/", row), row, flush=True)
elif stored["end_time"] == stored["start_time"]:
db_call("PUT", f"/api/db/{TABLE}/{stored['id']}/", row)
print("previous session closed", stored["id"], row, flush=True)
def main():
boot_ms = current_boot_ms()
rows = stored_rows()
if find_row(rows, boot_ms) is not None:
return
close_previous(rows, boot_ms)
print("current session opened", db_call("POST", f"/api/db/{TABLE}/", open_row(boot_ms)), flush=True)
if __name__ == "__main__":