mirror of
https://github.com/System-End/nephthys.git
synced 2026-04-19 16:28:16 +00:00
Add /api/stats/range for custom stat ranges (#161)
* Add /api/stats/range for custom stat ranges * Fix error messages
This commit is contained in:
parent
2d029e3435
commit
bb4673a398
2 changed files with 40 additions and 0 deletions
38
nephthys/api/stats_range.py
Normal file
38
nephthys/api/stats_range.py
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
from datetime import datetime
|
||||
from datetime import UTC
|
||||
|
||||
from starlette.requests import Request
|
||||
from starlette.responses import JSONResponse
|
||||
|
||||
from nephthys.utils.stats import calculate_daily_stats
|
||||
|
||||
|
||||
async def stats_range(req: Request):
|
||||
since = req.query_params.get("since") or req.query_params.get("after")
|
||||
if since:
|
||||
try:
|
||||
since = datetime.fromisoformat(since).astimezone(UTC)
|
||||
except ValueError:
|
||||
msg = f"not a valid ISO datetime: {since}"
|
||||
return JSONResponse({"error": msg}, status_code=400)
|
||||
else:
|
||||
since = datetime.fromtimestamp(0, UTC)
|
||||
|
||||
until = req.query_params.get("until") or req.query_params.get("before")
|
||||
if until:
|
||||
try:
|
||||
until = datetime.fromisoformat(until).astimezone(UTC)
|
||||
except ValueError:
|
||||
msg = f"not a valid ISO datetime: {until}"
|
||||
return JSONResponse({"error": msg}, status_code=400)
|
||||
else:
|
||||
until = datetime.now(UTC)
|
||||
|
||||
stats = await calculate_daily_stats(since, until)
|
||||
return JSONResponse(
|
||||
{
|
||||
"stats": stats.as_dict(),
|
||||
"since": since.isoformat(),
|
||||
"until": until.isoformat(),
|
||||
}
|
||||
)
|
||||
|
|
@ -15,6 +15,7 @@ from starlette_exporter import PrometheusMiddleware
|
|||
|
||||
from nephthys.__main__ import main
|
||||
from nephthys.api.stats import stats
|
||||
from nephthys.api.stats_range import stats_range
|
||||
from nephthys.api.stats_v2 import stats_v2
|
||||
from nephthys.api.ticket import ticket_info
|
||||
from nephthys.api.tickets import tickets_list
|
||||
|
|
@ -67,6 +68,7 @@ app = Starlette(
|
|||
Route(path="/", endpoint=root, methods=["GET"]),
|
||||
Route(path="/slack/events", endpoint=endpoint, methods=["POST"]),
|
||||
Route(path="/api/stats", endpoint=stats, methods=["GET"]),
|
||||
Route(path="/api/stats/range", endpoint=stats_range, methods=["GET"]),
|
||||
Route(path="/api/stats_v2", endpoint=stats_v2, methods=["GET"]),
|
||||
Route(path="/api/user", endpoint=user_stats, methods=["GET"]),
|
||||
Route(path="/api/tickets", endpoint=tickets_list, methods=["GET"]),
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue