feat(админка): очередь внимания
This commit is contained in:
+45
-1
@@ -4,8 +4,9 @@ from __future__ import annotations
|
||||
import asyncio
|
||||
import time
|
||||
|
||||
from fastapi import APIRouter, Request
|
||||
from fastapi import APIRouter, HTTPException, Request
|
||||
|
||||
import attention
|
||||
import db
|
||||
import metrics
|
||||
import money
|
||||
@@ -50,6 +51,49 @@ async def visit(request: Request):
|
||||
return {"ok": True, **result}
|
||||
|
||||
|
||||
def _attention_snapshot() -> dict:
|
||||
work = metrics.collect(1)["work"]
|
||||
cash = money.summary()
|
||||
return attention.collect(work, cash)
|
||||
|
||||
|
||||
@router.get("/attention")
|
||||
async def attention_queue(request: Request):
|
||||
require_admin(request)
|
||||
return await asyncio.to_thread(_attention_snapshot)
|
||||
|
||||
|
||||
@router.post("/attention/{key}/done")
|
||||
async def attention_done(key: str, request: Request):
|
||||
require_admin(request)
|
||||
snap = await asyncio.to_thread(_attention_snapshot)
|
||||
keys = {i["key"] for i in snap["items"]}
|
||||
if key not in keys and not attention.state_of(key):
|
||||
raise HTTPException(404, "Такого сигнала нет")
|
||||
ok = await asyncio.to_thread(attention.mark_done, key)
|
||||
return {"ok": ok, "key": key}
|
||||
|
||||
|
||||
@router.post("/attention/{key}/snooze")
|
||||
async def attention_snooze(key: str, request: Request):
|
||||
require_admin(request)
|
||||
try:
|
||||
body = await request.json()
|
||||
if not isinstance(body, dict):
|
||||
raise ValueError
|
||||
minutes = int(body.get("minutes") or 0)
|
||||
except (ValueError, TypeError):
|
||||
raise HTTPException(400, "minutes 1..10080") from None
|
||||
snap = await asyncio.to_thread(_attention_snapshot)
|
||||
keys = {i["key"] for i in snap["items"]}
|
||||
if key not in keys and not attention.state_of(key):
|
||||
raise HTTPException(404, "Такого сигнала нет")
|
||||
ok = await asyncio.to_thread(attention.snooze, key, minutes)
|
||||
if not ok:
|
||||
raise HTTPException(400, "minutes 1..10080")
|
||||
return {"ok": True, "key": key, "minutes": minutes}
|
||||
|
||||
|
||||
@router.get("/dashboard")
|
||||
async def dashboard(request: Request):
|
||||
user = require_admin(request)
|
||||
|
||||
Reference in New Issue
Block a user