feat: admin dashboard — статистика сообщений/сессий/пользователей
/api/admin/stats — totals + daily breakdown (14 дней) /admin — карточки с метриками + таблица по дням + сессии Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -559,6 +559,30 @@ async def admin_sessions(request: Request):
|
|||||||
return [dict(r) for r in rows]
|
return [dict(r) for r in rows]
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/api/admin/stats")
|
||||||
|
async def admin_stats(request: Request):
|
||||||
|
"""Dashboard stats: messages per day, total sessions, etc."""
|
||||||
|
_get_user(request)
|
||||||
|
from db import _conn
|
||||||
|
with _conn() as conn:
|
||||||
|
# Messages per day (last 14 days)
|
||||||
|
daily = conn.execute(
|
||||||
|
"SELECT date(timestamp, 'unixepoch', '+10 hours') as day, "
|
||||||
|
"COUNT(*) as total, "
|
||||||
|
"SUM(CASE WHEN role='user' THEN 1 ELSE 0 END) as questions, "
|
||||||
|
"SUM(CASE WHEN role='assistant' THEN 1 ELSE 0 END) as answers "
|
||||||
|
"FROM messages WHERE timestamp > unixepoch() - 14*86400 "
|
||||||
|
"GROUP BY day ORDER BY day DESC",
|
||||||
|
).fetchall()
|
||||||
|
# Totals
|
||||||
|
totals = conn.execute(
|
||||||
|
"SELECT COUNT(*) as messages, COUNT(DISTINCT tab_id) as sessions, "
|
||||||
|
"COUNT(DISTINCT username) as users "
|
||||||
|
"FROM messages",
|
||||||
|
).fetchone()
|
||||||
|
return {"daily": [dict(r) for r in daily], "totals": dict(totals)}
|
||||||
|
|
||||||
|
|
||||||
@app.get("/admin", response_class=HTMLResponse)
|
@app.get("/admin", response_class=HTMLResponse)
|
||||||
async def admin_page():
|
async def admin_page():
|
||||||
return Path("static/admin.html").read_text(encoding="utf-8")
|
return Path("static/admin.html").read_text(encoding="utf-8")
|
||||||
|
|||||||
+23
-1
@@ -20,7 +20,10 @@ a:hover { text-decoration: underline; }
|
|||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<h1>Сессии пользователей</h1>
|
<h1>Dashboard</h1>
|
||||||
|
<div id="stats" style="display:flex;gap:1rem;margin-bottom:1.5rem;flex-wrap:wrap"></div>
|
||||||
|
<div id="daily" style="margin-bottom:1.5rem"></div>
|
||||||
|
<h2 style="font-size:1rem;margin-bottom:.5rem">Сессии</h2>
|
||||||
<table id="sessions">
|
<table id="sessions">
|
||||||
<tr><th>Время</th><th>Пользователь</th><th>Сообщений</th><th>Последний вопрос</th><th></th></tr>
|
<tr><th>Время</th><th>Пользователь</th><th>Сообщений</th><th>Последний вопрос</th><th></th></tr>
|
||||||
</table>
|
</table>
|
||||||
@@ -29,6 +32,25 @@ a:hover { text-decoration: underline; }
|
|||||||
const token = localStorage.getItem('token');
|
const token = localStorage.getItem('token');
|
||||||
if (!token) window.location.href = '/';
|
if (!token) window.location.href = '/';
|
||||||
|
|
||||||
|
// Stats
|
||||||
|
fetch('/api/admin/stats', {headers: {'Authorization': 'Bearer ' + token}})
|
||||||
|
.then(r => r.json())
|
||||||
|
.then(data => {
|
||||||
|
const t = data.totals;
|
||||||
|
const box = (label, val) => `<div style="background:var(--card);padding:1rem;border-radius:8px;box-shadow:0 1px 4px rgba(0,0,0,.06);min-width:120px"><div style="font-size:1.5rem;font-weight:700">${val}</div><div style="font-size:.75rem;color:var(--muted)">${label}</div></div>`;
|
||||||
|
document.getElementById('stats').innerHTML = box('Сообщений', t.messages) + box('Сессий', t.sessions) + box('Пользователей', t.users);
|
||||||
|
// Daily chart
|
||||||
|
if (data.daily && data.daily.length > 0) {
|
||||||
|
let html = '<table style="width:100%;border-collapse:collapse;background:var(--card);border-radius:8px;overflow:hidden;box-shadow:0 1px 4px rgba(0,0,0,.06)"><tr><th style="background:#ede5db;padding:6px;font-size:.75rem">Дата</th><th style="background:#ede5db;padding:6px;font-size:.75rem">Вопросов</th><th style="background:#ede5db;padding:6px;font-size:.75rem">Ответов</th></tr>';
|
||||||
|
for (const d of data.daily) {
|
||||||
|
html += `<tr><td style="padding:4px 8px;border-top:1px solid #eee;font-size:.8rem">${d.day}</td><td style="padding:4px 8px;border-top:1px solid #eee;font-size:.8rem">${d.questions}</td><td style="padding:4px 8px;border-top:1px solid #eee;font-size:.8rem">${d.answers}</td></tr>`;
|
||||||
|
}
|
||||||
|
html += '</table>';
|
||||||
|
document.getElementById('daily').innerHTML = html;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Sessions
|
||||||
fetch('/api/admin/sessions', {headers: {'Authorization': 'Bearer ' + token}})
|
fetch('/api/admin/sessions', {headers: {'Authorization': 'Bearer ' + token}})
|
||||||
.then(r => r.json())
|
.then(r => r.json())
|
||||||
.then(sessions => {
|
.then(sessions => {
|
||||||
|
|||||||
Reference in New Issue
Block a user