askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
danq9
signup: the person chooses the password and name on the confirm page
83c7aa6
2 weeks, 5 days ago
History
object_metrics_history.py · 4.5 KB · 152 lines
| 1 | """TSV-backed metrics history snapshots. |
| 2 | |
| 3 | The server's request metrics live in process memory and reset on restart. |
| 4 | This module persists small periodic snapshots so dashboards can show trends |
| 5 | across hours and deploys. Sampling is traffic-driven: the server appends at |
| 6 | most one row per interval while requests are flowing, which is exactly when |
| 7 | the numbers change. |
| 8 | """ |
| 9 | |
| 10 | from __future__ import annotations |
| 11 | |
| 12 | import csv |
| 13 | import threading |
| 14 | from datetime import datetime, timezone |
| 15 | from pathlib import Path |
| 16 | from typing import Any, Iterable, Mapping |
| 17 | |
| 18 | from object_versions import DEFAULT_DATA_DIR |
| 19 | |
| 20 | METRICS_DIR = "metrics" |
| 21 | HISTORY_FILE = "history.tsv" |
| 22 | DEFAULT_MAX_ROWS = 10080 # one week of minute samples |
| 23 | SNAPSHOT_FIELDS = ( |
| 24 | "timestamp", |
| 25 | "uptime_seconds", |
| 26 | "requests", |
| 27 | "errors", |
| 28 | "rps", |
| 29 | "error_rate", |
| 30 | "p50_ms", |
| 31 | "p95_ms", |
| 32 | "cpu_percent", |
| 33 | "memory_used_percent", |
| 34 | "disk_used_percent", |
| 35 | ) |
| 36 | |
| 37 | _FILE_LOCKS: dict[Path, threading.Lock] = {} |
| 38 | _FILE_LOCKS_LOCK = threading.Lock() |
| 39 | |
| 40 | |
| 41 | def history_path(base_dir: Path | str = DEFAULT_DATA_DIR) -> Path: |
| 42 | root = Path(base_dir) / METRICS_DIR |
| 43 | path = root / HISTORY_FILE |
| 44 | resolved_root = root.resolve(strict=False) |
| 45 | resolved_path = path.resolve(strict=False) |
| 46 | |
| 47 | try: |
| 48 | resolved_path.relative_to(resolved_root) |
| 49 | except ValueError as exc: |
| 50 | raise ValueError("Metrics history path escapes metrics directory") from exc |
| 51 | |
| 52 | return path |
| 53 | |
| 54 | |
| 55 | def append_snapshot( |
| 56 | snapshot: Mapping[str, Any], |
| 57 | *, |
| 58 | base_dir: Path | str = DEFAULT_DATA_DIR, |
| 59 | max_rows: int = DEFAULT_MAX_ROWS, |
| 60 | now: datetime | None = None, |
| 61 | ) -> dict[str, Any]: |
| 62 | """Append one snapshot row, pruning to max_rows when the file grows past it.""" |
| 63 | row = {"timestamp": _format_timestamp(now or _now())} |
| 64 | for field in SNAPSHOT_FIELDS[1:]: |
| 65 | row[field] = _number_text(snapshot.get(field)) |
| 66 | |
| 67 | path = history_path(base_dir) |
| 68 | with _file_lock(path): |
| 69 | exists = path.exists() |
| 70 | path.parent.mkdir(parents=True, exist_ok=True) |
| 71 | with path.open("a", newline="", encoding="utf-8") as handle: |
| 72 | writer = csv.DictWriter(handle, fieldnames=SNAPSHOT_FIELDS, delimiter="\t") |
| 73 | if not exists: |
| 74 | writer.writeheader() |
| 75 | writer.writerow(row) |
| 76 | |
| 77 | rows = _read_rows(path) |
| 78 | if len(rows) > max_rows: |
| 79 | _write_rows(path, rows[-max_rows:]) |
| 80 | |
| 81 | return row |
| 82 | |
| 83 | |
| 84 | def read_history( |
| 85 | *, |
| 86 | base_dir: Path | str = DEFAULT_DATA_DIR, |
| 87 | limit: int = 360, |
| 88 | ) -> list[dict[str, Any]]: |
| 89 | """Return the most recent snapshot rows, oldest first, numbers parsed.""" |
| 90 | path = history_path(base_dir) |
| 91 | with _file_lock(path): |
| 92 | rows = _read_rows(path) |
| 93 | |
| 94 | parsed = [] |
| 95 | for row in rows[-max(limit, 0):]: |
| 96 | entry: dict[str, Any] = {"timestamp": row.get("timestamp", "")} |
| 97 | for field in SNAPSHOT_FIELDS[1:]: |
| 98 | entry[field] = _parse_number(row.get(field)) |
| 99 | parsed.append(entry) |
| 100 | return parsed |
| 101 | |
| 102 | |
| 103 | def _read_rows(path: Path) -> list[dict[str, str]]: |
| 104 | if not path.exists(): |
| 105 | return [] |
| 106 | with path.open(newline="", encoding="utf-8") as handle: |
| 107 | return [row for row in csv.DictReader(handle, delimiter="\t") if row.get("timestamp")] |
| 108 | |
| 109 | |
| 110 | def _write_rows(path: Path, rows: Iterable[Mapping[str, str]]) -> None: |
| 111 | temp_path = path.with_name(f".{path.name}.tmp") |
| 112 | with temp_path.open("w", newline="", encoding="utf-8") as handle: |
| 113 | writer = csv.DictWriter(handle, fieldnames=SNAPSHOT_FIELDS, delimiter="\t") |
| 114 | writer.writeheader() |
| 115 | for row in rows: |
| 116 | writer.writerow({field: row.get(field, "") for field in SNAPSHOT_FIELDS}) |
| 117 | temp_path.replace(path) |
| 118 | |
| 119 | |
| 120 | def _number_text(value: Any) -> str: |
| 121 | if value is None or isinstance(value, bool): |
| 122 | return "" |
| 123 | if isinstance(value, (int, float)): |
| 124 | return f"{value:.4f}".rstrip("0").rstrip(".") if isinstance(value, float) else str(value) |
| 125 | return "" |
| 126 | |
| 127 | |
| 128 | def _parse_number(value: Any) -> float | None: |
| 129 | if value in (None, ""): |
| 130 | return None |
| 131 | try: |
| 132 | return float(value) |
| 133 | except (TypeError, ValueError): |
| 134 | return None |
| 135 | |
| 136 | |
| 137 | def _format_timestamp(value: datetime) -> str: |
| 138 | return value.astimezone(timezone.utc).isoformat(timespec="seconds").replace("+00:00", "Z") |
| 139 | |
| 140 | |
| 141 | def _now() -> datetime: |
| 142 | return datetime.now(timezone.utc) |
| 143 | |
| 144 | |
| 145 | def _file_lock(path: Path) -> threading.Lock: |
| 146 | resolved = path.resolve(strict=False) |
| 147 | with _FILE_LOCKS_LOCK: |
| 148 | lock = _FILE_LOCKS.get(resolved) |
| 149 | if lock is None: |
| 150 | lock = threading.Lock() |
| 151 | _FILE_LOCKS[resolved] = lock |
| 152 | return lock |