feat: show node connection status and traffic stats in admin panel
Enable sing-box Clash/V2Ray APIs for per-user metrics, persist cumulative traffic in SQLite, and refresh the dashboard every five seconds. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+19
@@ -58,6 +58,15 @@ def init_db(env: dict[str, str]) -> None:
|
||||
enabled INTEGER NOT NULL DEFAULT 1,
|
||||
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS traffic_counters (
|
||||
node_id INTEGER PRIMARY KEY,
|
||||
upload_total INTEGER NOT NULL DEFAULT 0,
|
||||
download_total INTEGER NOT NULL DEFAULT 0,
|
||||
snapshot_upload INTEGER NOT NULL DEFAULT 0,
|
||||
snapshot_download INTEGER NOT NULL DEFAULT 0,
|
||||
updated_at TEXT,
|
||||
FOREIGN KEY (node_id) REFERENCES nodes(id) ON DELETE CASCADE
|
||||
);
|
||||
"""
|
||||
)
|
||||
|
||||
@@ -80,6 +89,12 @@ def init_db(env: dict[str, str]) -> None:
|
||||
("默认节点", uuid, hy2),
|
||||
)
|
||||
|
||||
for row in conn.execute("SELECT id FROM nodes").fetchall():
|
||||
conn.execute(
|
||||
"INSERT OR IGNORE INTO traffic_counters (node_id) VALUES (?)",
|
||||
(row["id"],),
|
||||
)
|
||||
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
@@ -115,6 +130,10 @@ def add_node(name: str) -> dict:
|
||||
)
|
||||
node_id = cur.lastrowid
|
||||
row = conn.execute("SELECT * FROM nodes WHERE id = ?", (node_id,)).fetchone()
|
||||
conn.execute(
|
||||
"INSERT OR IGNORE INTO traffic_counters (node_id) VALUES (?)",
|
||||
(node_id,),
|
||||
)
|
||||
conn.commit()
|
||||
conn.close()
|
||||
return dict(row)
|
||||
|
||||
Reference in New Issue
Block a user