Добавить db.py
This commit is contained in:
@@ -0,0 +1,144 @@
|
||||
import os
|
||||
import sqlite3
|
||||
from datetime import datetime
|
||||
|
||||
DB_PATH = os.path.join(os.path.dirname(__file__), "data", "smart.db")
|
||||
|
||||
SCHEMA = """
|
||||
CREATE TABLE IF NOT EXISTS runs (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
hostname TEXT NOT NULL,
|
||||
ssh_host TEXT,
|
||||
started_at TEXT NOT NULL,
|
||||
finished_at TEXT,
|
||||
status TEXT NOT NULL DEFAULT 'running',
|
||||
error TEXT,
|
||||
archive TEXT
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS drives (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
run_id INTEGER NOT NULL REFERENCES runs(id) ON DELETE CASCADE,
|
||||
uid INTEGER,
|
||||
serial TEXT,
|
||||
name TEXT,
|
||||
bdev TEXT,
|
||||
model TEXT,
|
||||
vendor TEXT,
|
||||
size INTEGER,
|
||||
slot INTEGER,
|
||||
raid TEXT,
|
||||
disk_type TEXT,
|
||||
smart_rc INTEGER,
|
||||
smart_file TEXT,
|
||||
smart_error TEXT,
|
||||
temperature INTEGER,
|
||||
power_on_hours INTEGER,
|
||||
health TEXT,
|
||||
non_medium_errors INTEGER
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_drives_run ON drives(run_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_drives_serial ON drives(serial);
|
||||
CREATE INDEX IF NOT EXISTS idx_runs_host ON runs(hostname);
|
||||
"""
|
||||
|
||||
|
||||
def get_conn():
|
||||
os.makedirs(os.path.dirname(DB_PATH), exist_ok=True)
|
||||
conn = sqlite3.connect(DB_PATH, timeout=10)
|
||||
conn.row_factory = sqlite3.Row
|
||||
conn.execute("PRAGMA foreign_keys = ON")
|
||||
conn.execute("PRAGMA journal_mode = WAL")
|
||||
conn.execute("PRAGMA synchronous = NORMAL")
|
||||
return conn
|
||||
|
||||
|
||||
def init_db():
|
||||
with get_conn() as c:
|
||||
c.executescript(SCHEMA)
|
||||
|
||||
|
||||
def create_run(hostname, ssh_host):
|
||||
with get_conn() as c:
|
||||
cur = c.execute(
|
||||
"INSERT INTO runs(hostname, ssh_host, started_at, status) VALUES (?,?,?,?)",
|
||||
(hostname, ssh_host, datetime.now().isoformat(timespec="seconds"), "running"),
|
||||
)
|
||||
return cur.lastrowid
|
||||
|
||||
|
||||
def finish_run(run_id, status, error=None, archive=None):
|
||||
with get_conn() as c:
|
||||
c.execute(
|
||||
"UPDATE runs SET finished_at=?, status=?, error=?, archive=? WHERE id=?",
|
||||
(datetime.now().isoformat(timespec="seconds"), status, error, archive, run_id),
|
||||
)
|
||||
|
||||
|
||||
def insert_drive(run_id, d, extra):
|
||||
# Принудительно приводим к int, если None, пишем 0 (чтобы графики не падали)
|
||||
errors = extra.get("non_medium_errors")
|
||||
errors = int(errors) if errors is not None else 0
|
||||
|
||||
poh = extra.get("power_on_hours")
|
||||
poh = int(poh) if poh is not None else 0
|
||||
|
||||
temp = extra.get("temperature")
|
||||
temp = int(temp) if temp is not None else 0
|
||||
|
||||
with get_conn() as c:
|
||||
c.execute(
|
||||
"""INSERT INTO drives(run_id, uid, serial, name, bdev, model, vendor,
|
||||
size, slot, raid, disk_type,
|
||||
smart_rc, smart_file, smart_error,
|
||||
temperature, power_on_hours, health, non_medium_errors)
|
||||
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)""",
|
||||
(
|
||||
run_id,
|
||||
d.get("uid"), d.get("serial"), d.get("name"), d.get("bdev"),
|
||||
d.get("model"), d.get("vendor"),
|
||||
_int(d.get("size")), _int(d.get("slot")), d.get("raid"), d.get("type"),
|
||||
extra.get("smart_rc"), extra.get("smart_file"), extra.get("smart_error"),
|
||||
temp, poh, extra.get("health"), errors
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
def list_runs(limit=200):
|
||||
with get_conn() as c:
|
||||
return c.execute(
|
||||
"""SELECT r.*,
|
||||
(SELECT COUNT(*) FROM drives d WHERE d.run_id=r.id) AS drives_count,
|
||||
(SELECT COUNT(*) FROM drives d WHERE d.run_id=r.id
|
||||
AND d.smart_rc NOT IN (0,4)) AS bad_count
|
||||
FROM runs r ORDER BY r.id DESC LIMIT ?""",
|
||||
(limit,),
|
||||
).fetchall()
|
||||
|
||||
|
||||
def get_run(run_id):
|
||||
with get_conn() as c:
|
||||
run = c.execute("SELECT * FROM runs WHERE id=?", (run_id,)).fetchone()
|
||||
drives = c.execute("SELECT * FROM drives WHERE run_id=? ORDER BY uid", (run_id,)).fetchall()
|
||||
return run, drives
|
||||
|
||||
|
||||
def runs_for_charts(limit=50):
|
||||
with get_conn() as c:
|
||||
return c.execute(
|
||||
"""SELECT r.id, r.hostname, r.started_at, r.status,
|
||||
COUNT(d.id) AS total,
|
||||
SUM(CASE WHEN d.smart_rc NOT IN (0,4) THEN 1 ELSE 0 END) AS bad,
|
||||
MAX(d.temperature) AS max_temp
|
||||
FROM runs r LEFT JOIN drives d ON d.run_id=r.id
|
||||
GROUP BY r.id ORDER BY r.id DESC LIMIT ?""",
|
||||
(limit,),
|
||||
).fetchall()
|
||||
|
||||
|
||||
def _int(v):
|
||||
try:
|
||||
return int(v)
|
||||
except (TypeError, ValueError):
|
||||
return None
|
||||
Reference in New Issue
Block a user