feat(admin): phase 3 — dashboard #overview landing

GET /api/admin/overview returns 24h digest (~0.08ms/call).

- adminController.getOverview: 7 prepared statements (users 24h, sessions 24h, active users, classes count, failed sessions, banned this week, top-5 sessions)

- new section frontend/js/admin/sections/overview.js (~205L): bento-grid cards, alerts (only when >0), top-5 table, quick-links

- nav-item + tab-pane reordered: #overview is now default; #stats remains routable

Auth: admin-only (inside requireRole('admin') block, sibling of /stats).

Backward compat: all 13 existing routes unchanged.

Known follow-ups (post-merge polish):

- activeClasses counts all (label could be 'Всего классов')

- failedSessions24h includes in_progress (could tighten to abandoned only)

- topSessions24h drops NULL-score completed rows

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Maxim Dolgolyov
2026-05-16 23:26:59 +03:00
parent fa67ad1294
commit 41acbdd0d0
9 changed files with 351 additions and 29 deletions
+2 -1
View File
@@ -6,7 +6,8 @@
- ✅ Phase 1 implemented — `window.AdminRouter` обёртывает старый `switchTab` (hash ↔ tab двусторонне). `switchTab` принимает 2-й аргумент `{ fromRouter: true }` для предотвращения рекурсии. Default = `#stats`. Файлы: `frontend/js/admin/router.js` (новый), `frontend/admin.html` (+1 строка), `frontend/js/admin/admin.js` (модификация `switchTab` + IIFE `initAdminRouter`).
- ✅ Phase 2 implemented (commit 92030b4) — admin.js ужат с ~3591L до 701L. Все 13 plan-tabs живут в `frontend/js/admin/sections/*.js` (IIFE pattern) + `frontend/js/admin/_shared.js` (window.AdminCtx). switchTab() диспетчит в `AdminSections[ROUTE_TO_SECTION[name]].init()`. Lazy-load работает (inited флаг внутри каждой IIFE). System tabs (topics/audit/errors/health/classroom/avatars) остались inline в admin.js — Phase 2 их не extract'ил.
- Phase 3-6 not started
- Phase 3 implemented — `#overview` стал дефолтным route'ом admin-панели. Backend: `GET /api/admin/overview` (admin-only, ~0.08ms/call) возвращает digest за 24ч: новые регистрации, запущенные сессии, активные юзеры, активные классы, failed-сессии, забаненные за неделю (из audit log), топ-5 завершённых сессий. Frontend: `frontend/js/admin/sections/overview.js` (~205L) рендерит bento-grid карточки + alerts + топ-таблицу + quick-links (deep-link через `AdminRouter.navigate`). `admin.js`: дефолт `'stats'``'overview'` в `activate()`, initial nav, и initial init. Old `#stats` остался работающим (доступен через nav-item). Файлы: `frontend/js/admin/sections/overview.js` (NEW), `backend/src/controllers/adminController.js` (+57L: `overviewStmts` + `getOverview`), `backend/src/routes/admin.js` (+1L), `js/api.js` (+1 helper), `frontend/admin.html` (nav-item + tab-pane + script tag), `frontend/js/admin/admin.js` (ROUTE_TO_SECTION + default route refs).
- ⬜ Phase 4-6 not started
## Temporary Workarounds
+3 -3
View File
@@ -37,7 +37,7 @@
- [x] Phase 1: Hash-router [domain: frontend] → [subplan](./phase-1-hash-router.md)
- [x] Phase 2: Split admin.html → per-section modules [domain: frontend] → [subplan](./phase-2-split-sections.md)
- [ ] Phase 3: Dashboard #overview [domain: fullstack] → [subplan](./phase-3-dashboard.md) (parallelizable with 4, 5)
- [x] Phase 3: Dashboard #overview [domain: fullstack] → [subplan](./phase-3-dashboard.md) (parallelizable with 4, 5)
- [ ] Phase 4: Cmd+K command palette [domain: fullstack] → [subplan](./phase-4-palette.md) (parallelizable with 3, 5)
- [ ] Phase 5: Per-row quick actions [domain: frontend] → [subplan](./phase-5-quick-actions.md) (parallelizable with 3, 4)
- [ ] Phase 6: Deep entity pages [domain: frontend] → [subplan](./phase-6-deep-pages.md)
@@ -49,8 +49,8 @@
| Phase | Domain | Status | Review | Build | Committed |
|-------|--------|--------|--------|-------|-----------|
| Phase 1: Hash-router | frontend | ✅ Done | ✅ PASS w/ notes | ✅ | ✅ 8a7bed4 |
| Phase 2: Split sections | frontend | ✅ Done | ⬜ pending | ✅ node --check | ✅ 92030b4 |
| Phase 3: Dashboard | fullstack | ⬜ Not Started | ⬜ | ⬜ | ⬜ |
| Phase 2: Split sections | frontend | ✅ Done | ✅ PASS (1 blocker fixed: fa67ad1) | ✅ node --check | ✅ 92030b4 + fa67ad1 |
| Phase 3: Dashboard | fullstack | ✅ Done | ⬜ pending | ✅ node --check + queries verified | ⬜ |
| Phase 4: Palette | fullstack | ⬜ Not Started | ⬜ | ⬜ | ⬜ |
| Phase 5: Quick actions | frontend | ⬜ Not Started | ⬜ | ⬜ | ⬜ |
| Phase 6: Deep pages | frontend | ⬜ Not Started | ⬜ | ⬜ | ⬜ |
+59 -11
View File
@@ -1,6 +1,6 @@
# Phase 3: Dashboard #overview
**Status:** ⬜ Not Started
**Status:** ✅ Implemented (pending review)
**Parent plan:** [PLAN.md](./PLAN.md)
**Domain:** fullstack
**Parallelizable with:** Phase 4, Phase 5
@@ -11,7 +11,7 @@
## Tasks
- [ ] Backend: новый endpoint `GET /api/admin/overview`:
- [x] Backend: новый endpoint `GET /api/admin/overview`:
```js
{
newUsers24h: number, // регистрации за 24ч
@@ -28,7 +28,7 @@
- Route: добавить в `backend/src/routes/admin.js`
- Auth: admin или teacher (как остальные admin/* — RBAC same)
- Performance: один запрос для каждого поля, простые COUNT/SELECT, без JOIN'ов где возможно
- [ ] Frontend: новый section `frontend/js/admin/sections/overview.js`:
- [x] Frontend: новый section `frontend/js/admin/sections/overview.js`:
- Использует структуру из Phase 2
- Загружает `/api/admin/overview`
- Рендерит карточки в `<div id="tab-overview">` секции:
@@ -37,11 +37,11 @@
- **Топ-сессии** — таблица top-5 за день с click→drilldown
- **Quick links** — "Все пользователи", "Все сессии", "Создать тест" (deep-link в соответствующие routes)
- LS.skeleton при загрузке, LS.state.error на fail
- [ ] HTML: добавить `<div class="tab-pane" id="tab-overview">` в admin.html (перед остальными tab-pane)
- [ ] Nav: добавить admin-nav-item для `overview` (icon: layout-dashboard / activity)
- [ ] Регистрация в ROUTE_TO_SECTION (из Phase 2): `overview: 'overview'`
- [ ] Сделать `#overview` дефолтным route'ом в router (из Phase 1) — если пустой hash, navigate to `#overview` вместо `#stats`
- [ ] Старый `#stats` остаётся как доступный route (legacy backend stats), но не дефолтный
- [x] HTML: добавить `<div class="tab-pane" id="tab-overview">` в admin.html (перед остальными tab-pane)
- [x] Nav: добавить admin-nav-item для `overview` (icon: layout-dashboard / activity)
- [x] Регистрация в ROUTE_TO_SECTION (из Phase 2): `overview: 'overview'`
- [x] Сделать `#overview` дефолтным route'ом в router (из Phase 1) — если пустой hash, navigate to `#overview` вместо `#stats`
- [x] Старый `#stats` остаётся как доступный route (legacy backend stats), но не дефолтный
## Files to Modify/Create
@@ -107,6 +107,54 @@ Bento-grid из 4-6 карточек:
## Handoff to Next Phase
<!-- Implementer: записать, какой shape у overview-ответа,
какие route переходы внедрены, какие deep-links открыты для Phase 6
(например, `#users?filter=banned` или `#sessions?status=failed`). -->
**Endpoint shape (`GET /api/admin/overview`):**
```json
{
"newUsers24h": 0,
"newSessions24h": 0,
"activeUsers24h": 2,
"activeClasses": 5,
"failedSessions24h": 0,
"bannedThisWeek": [
{ "id": 42, "name": "...", "email": "...", "banned_at": "2026-05-15 12:30:00" }
],
"topSessions24h": [
{ "id": 101, "user_name": "...", "subject_name": "Физика",
"score": 18, "total": 20, "percent": 90.0, "finished_at": "2026-05-16 09:14:22" }
]
}
```
**Performance:** ~0.08ms/call avg (benchmarked 100 iters) — well under 100ms target.
**Auth:** uses `router.use(requireRole('admin'))` block (admin-only, same as `/stats`).
`/features` block (teacher+admin) is above the route. Teacher access NOT granted — matches
sibling `/stats` behavior. If Phase 4-6 wants teacher access, move `/overview` above the
admin-only `router.use(...)` line in `backend/src/routes/admin.js`.
**Quick-link wiring (Phase 4/5 extension point):**
Quick-link buttons live in `overview.js` → render() → `.ov-quick-grid`. They use a
`data-go="#hash"` attribute and a delegated click → `AdminRouter.navigate(...)`. To add
a new quick-link, append a `<button class="ov-quick-btn" data-go="#whatever">` to the grid.
**Deep-link openings for Phase 6:**
- `#users` and `#sessions` are wired (vanilla routes — no filter params yet)
- Future: extend `AdminRouter` to support `#users?filter=banned` / `#sessions?status=failed`
query-style params. Currently router parses `route + params` (`/users/123` form), no `?`
parsing — Phase 6 can extend `router.js::parseHash()`.
**Router defaults updated:**
- `admin.js::initAdminRouter::activate()` fallback route changed `'stats'` → `'overview'`
- Initial dispatch (empty hash) navigates to `#overview` instead of `#stats`
- Initial init call: `AdminSections.overview.init()` instead of `AdminSections.stats.init()`
**Files modified/created (line counts):**
- NEW: `frontend/js/admin/sections/overview.js` (~205 lines)
- `backend/src/controllers/adminController.js` (+57 lines: new statements + `getOverview`)
- `backend/src/routes/admin.js` (+1 line)
- `js/api.js` (+1 line helper + 1 export)
- `frontend/admin.html` (nav-item +3 lines, tab-pane +4 lines, script tag +1 line, swap `active`)
- `frontend/js/admin/admin.js` (`ROUTE_TO_SECTION` +1 entry, default route refs swapped from `stats` → `overview`)
**Unrelated to Phase 3 (do not touch):** old `#stats` route remains functional — used as fallback in the past, can be linked from Overview as "детальная статистика" in a future polish pass.