feat(stats): per-user API quota attribution + admin usage page
Track who burned how much YouTube API quota. A QuotaEvent audit log (migration 0009) records every spend with the triggering user (NULL = background/system) and an action label, set via a request/job-scoped contextvar (quota.attribute) so no call signatures change. User-initiated work (sync subscriptions, unsubscribe, opt-in recent backfill, manual enrich) attributes to the user; scheduler work to System, split by action. - backend: QuotaEvent model + migration 0009; quota.attribute() contextvar; record_usage logs events; entry points wrapped (routes/sync, routes/channels, scheduler); GET /api/quota/my-usage + GET /api/quota/admin - frontend: admin-only Stats page (header nav, page=stats) with daily bars + per-user breakdown by action and range picker; 'Your API usage' in Settings -> Sync for every user Verified: attribution + endpoints compute correctly; events are per-user vs System.
This commit is contained in:
parent
bcc4371ac7
commit
f255728f75
15 changed files with 451 additions and 26 deletions
|
|
@ -165,6 +165,27 @@ export interface MyStatus {
|
|||
paused: boolean;
|
||||
}
|
||||
|
||||
export interface MyUsage {
|
||||
today: number;
|
||||
last_7d: number;
|
||||
last_30d: number;
|
||||
all_time: number;
|
||||
by_action: Record<string, number>;
|
||||
}
|
||||
|
||||
export interface AdminQuotaRow {
|
||||
user_id: number | null;
|
||||
email: string;
|
||||
total: number;
|
||||
by_action: Record<string, number>;
|
||||
}
|
||||
|
||||
export interface AdminQuota {
|
||||
range_days: number;
|
||||
rows: AdminQuotaRow[];
|
||||
daily: { day: string; total: number }[];
|
||||
}
|
||||
|
||||
export interface ManagedChannel {
|
||||
id: string;
|
||||
title: string | null;
|
||||
|
|
@ -215,6 +236,10 @@ export const api = {
|
|||
req(`/api/channels/${id}/subscription`, { method: "DELETE" }),
|
||||
syncSubscriptions: () => req("/api/sync/subscriptions", { method: "POST" }),
|
||||
|
||||
// --- quota usage ---
|
||||
myUsage: (): Promise<MyUsage> => req("/api/quota/my-usage"),
|
||||
adminQuota: (days = 30): Promise<AdminQuota> => req(`/api/quota/admin?days=${days}`),
|
||||
|
||||
// --- onboarding / admin ---
|
||||
requestAccess: (email: string): Promise<{ status: string }> =>
|
||||
req("/auth/request-access", { method: "POST", body: JSON.stringify({ email }) }),
|
||||
|
|
|
|||
|
|
@ -34,6 +34,20 @@ export function formatDuration(sec: number | null): string {
|
|||
return h > 0 ? `${h}:${pad(m)}:${pad(s)}` : `${m}:${pad(s)}`;
|
||||
}
|
||||
|
||||
const QUOTA_ACTION_LABELS: Record<string, string> = {
|
||||
sync_subscriptions: "Sync subscriptions",
|
||||
backfill_recent: "Recent backfill",
|
||||
backfill_deep: "Full-history backfill",
|
||||
enrich: "Enrichment",
|
||||
unsubscribe: "Unsubscribe",
|
||||
subscription_resync: "Auto subscription resync",
|
||||
api: "Other",
|
||||
};
|
||||
|
||||
export function quotaActionLabel(action: string): string {
|
||||
return QUOTA_ACTION_LABELS[action] ?? action;
|
||||
}
|
||||
|
||||
/** Coarse, human ETA for a remaining duration in seconds ("~3 hours", "~2 days"). */
|
||||
export function formatEta(seconds: number): string {
|
||||
if (seconds <= 0) return "done";
|
||||
|
|
|
|||
|
|
@ -74,12 +74,11 @@ export function hasFilterParams(params: URLSearchParams): boolean {
|
|||
return KEYS.some((k) => params.has(k));
|
||||
}
|
||||
|
||||
export type Page = "feed" | "channels";
|
||||
export type Page = "feed" | "channels" | "stats";
|
||||
|
||||
export function readPage(): Page {
|
||||
return new URLSearchParams(window.location.search).get("page") === "channels"
|
||||
? "channels"
|
||||
: "feed";
|
||||
const p = new URLSearchParams(window.location.search).get("page");
|
||||
return p === "channels" || p === "stats" ? p : "feed";
|
||||
}
|
||||
|
||||
/** Reflect the current filters + page into the address bar without adding history entries. */
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue