upgrade script
This commit is contained in:
@@ -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, no end time, 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.
|
||||||
|
|||||||
@@ -60,9 +60,19 @@ def db_call(method, path, payload):
|
|||||||
return body.get("data")
|
return body.get("data")
|
||||||
|
|
||||||
|
|
||||||
def already_stored(start_ms):
|
def stored_rows():
|
||||||
rows = db_call("POST", f"/api/db-query/{TABLE}/", {"filters": [], "orderBy": ["start_time"], "limit": 1000}) or []
|
return 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 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": None, "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):
|
def mean_max(series, start_ms, end_ms):
|
||||||
@@ -87,16 +97,27 @@ def build_row(start_ms, end_ms):
|
|||||||
return row
|
return row
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def close_previous(rows, boot_ms):
|
||||||
session = previous_session(current_boot_ms())
|
session = previous_session(boot_ms)
|
||||||
if session is None:
|
if session is None:
|
||||||
print("no previous session in the uptime history", flush=True)
|
print("no previous session in the uptime history", flush=True)
|
||||||
return
|
return
|
||||||
if already_stored(session[0]):
|
|
||||||
print("previous session already stored", session[0], flush=True)
|
|
||||||
return
|
|
||||||
row = build_row(*session)
|
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.get("end_time") is None:
|
||||||
|
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__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
Reference in New Issue
Block a user