Publish reviewed history-free open-source snapshot (2026-10-12)
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
"""Small, subscription-scoped chart payload; no browsing or physical NIC data."""
|
||||
from datetime import timedelta
|
||||
from django.conf import settings
|
||||
from django.db.models import Sum
|
||||
from django.utils import timezone
|
||||
from .models import HourlyUsageRecord, UsageRecord
|
||||
|
||||
|
||||
def client_usage_history(subscription, now=None):
|
||||
now = timezone.localtime(now or timezone.now())
|
||||
today = now.date()
|
||||
hour = now.replace(minute=0, second=0, microsecond=0)
|
||||
daily = {r["day"]: r for r in UsageRecord.objects.filter(
|
||||
subscription=subscription, day__gte=today-timedelta(days=29), day__lte=today
|
||||
).values("day").annotate(upload=Sum("upload_bytes"), download=Sum("download_bytes"))}
|
||||
hourly = {r["hour"]: r for r in HourlyUsageRecord.objects.filter(
|
||||
subscription=subscription, hour__gte=hour-timedelta(hours=23), hour__lte=hour
|
||||
).values("hour").annotate(upload=Sum("upload_bytes"), download=Sum("download_bytes"))}
|
||||
|
||||
def days(count):
|
||||
return [{"day": day.isoformat(), "upload_bytes": daily.get(day, {}).get("upload", 0),
|
||||
"download_bytes": daily.get(day, {}).get("download", 0)}
|
||||
for day in (today-timedelta(days=n) for n in range(count-1, -1, -1))]
|
||||
|
||||
def series(count, hourly_mode):
|
||||
if hourly_mode:
|
||||
points = [{"time": bucket.isoformat(),
|
||||
# No recorded report is unknown, not a made-up zero or daily interpolation.
|
||||
"upload_bytes": hourly.get(bucket, {}).get("upload"),
|
||||
"download_bytes": hourly.get(bucket, {}).get("download")}
|
||||
for bucket in (hour-timedelta(hours=n) for n in range(count-1, -1, -1))]
|
||||
else:
|
||||
points = [{"time": row["day"], "upload_bytes": row["upload_bytes"],
|
||||
"download_bytes": row["download_bytes"]} for row in days(count)]
|
||||
return {"scope": "subscription", "bucket": "hour" if hourly_mode else "day",
|
||||
"timezone": settings.TIME_ZONE, "generated_at": now.isoformat(), "points": points,
|
||||
"note": "按上报入账小时统计;含当前小时;缺口表示无小时明细" if hourly_mode
|
||||
else "按自然日统计;含今日;当天尚未结束"}
|
||||
|
||||
return days(7), {"12h": series(12, True), "1d": series(24, True),
|
||||
"7d": series(7, False), "30d": series(30, False)}
|
||||
Reference in New Issue
Block a user