first commit
This commit is contained in:
63
backend/app/routes/analytics.py
Normal file
63
backend/app/routes/analytics.py
Normal file
@@ -0,0 +1,63 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
|
||||
from flask import Blueprint, jsonify, request
|
||||
|
||||
from app.services.analytics import AnalyticsService
|
||||
from app.utils.serialization import to_plain
|
||||
|
||||
analytics_blueprint = Blueprint("analytics", __name__)
|
||||
service = AnalyticsService()
|
||||
|
||||
|
||||
@analytics_blueprint.get("/analytics/production")
|
||||
def production_analytics():
|
||||
range_key = request.args.get("range", "30d")
|
||||
bucket = request.args.get("bucket", "day")
|
||||
compare = request.args.get("compare", "none")
|
||||
start = request.args.get("start")
|
||||
end = request.args.get("end")
|
||||
compare_ranges_raw = request.args.get("compare_ranges", "")
|
||||
compare_ranges = []
|
||||
if compare_ranges_raw:
|
||||
try:
|
||||
compare_ranges = json.loads(compare_ranges_raw)
|
||||
except json.JSONDecodeError as exc:
|
||||
return jsonify({"detail": f"Invalid compare_ranges payload: {exc}"}), 400
|
||||
try:
|
||||
return jsonify(
|
||||
to_plain(
|
||||
service.production(
|
||||
range_key=range_key,
|
||||
bucket=bucket,
|
||||
compare_mode=compare,
|
||||
start=start,
|
||||
end=end,
|
||||
compare_ranges=compare_ranges,
|
||||
)
|
||||
)
|
||||
)
|
||||
except ValueError as exc:
|
||||
return jsonify({"detail": str(exc)}), 400
|
||||
|
||||
|
||||
@analytics_blueprint.get("/analytics/distribution")
|
||||
def production_distribution():
|
||||
range_key = request.args.get("range", "30d")
|
||||
bucket = request.args.get("bucket", "day")
|
||||
start = request.args.get("start")
|
||||
end = request.args.get("end")
|
||||
try:
|
||||
return jsonify(
|
||||
to_plain(
|
||||
service.distribution(
|
||||
range_key=range_key,
|
||||
bucket=bucket,
|
||||
start=start,
|
||||
end=end,
|
||||
)
|
||||
)
|
||||
)
|
||||
except ValueError as exc:
|
||||
return jsonify({"detail": str(exc)}), 400
|
||||
Reference in New Issue
Block a user