Files
9router/src/app/api/usage/stats/route.js
T
Dũng A Tô Ri A d1a8dd4987 feat(usage): add Today period option to Usage & Analytics (#1063)
Bổ sung lựa chọn Today vào bộ lọc thời gian của trang Usage & Analytics
(trước đây chỉ có 24h, 7D, 30D, 60D).

Khác biệt với 24h:
- 24h: cuộn 24 giờ trước → hiện tại
- Today: cố định từ 00:00 hôm nay (giờ local) → hiện tại

Thay đổi:
- page.js, UsageStats.js: thêm option Today vào danh sách PERIODS,
  đổi grid mobile từ 4 cột sang 5 cột để fit option mới.
- api/usage/stats, api/usage/chart: cho phép giá trị period today.
- usageRepo.js:
  + getUsageStats: dùng nhánh live history khi period = today,
    cutoff lấy từ 00:00 hôm nay theo local time.
  + getChartData: thêm 24 bucket theo giờ từ 00:00 → 23:59 hôm nay.
2026-05-14 10:03:05 +07:00

24 lines
746 B
JavaScript

import { NextResponse } from "next/server";
import { getUsageStats } from "@/lib/usageDb";
const VALID_PERIODS = new Set(["today", "24h", "7d", "30d", "60d", "all"]);
export const dynamic = "force-dynamic";
export async function GET(request) {
try {
const { searchParams } = new URL(request.url);
const period = searchParams.get("period") || "7d";
if (!VALID_PERIODS.has(period)) {
return NextResponse.json({ error: "Invalid period" }, { status: 400 });
}
const stats = await getUsageStats(period);
return NextResponse.json(stats);
} catch (error) {
console.error("[API] Failed to get usage stats:", error);
return NextResponse.json({ error: "Failed to fetch usage stats" }, { status: 500 });
}
}