mirror of
https://github.com/farion1231/cc-switch.git
synced 2026-07-29 18:05:37 +08:00
feat(usage): lift provider/model filters to dashboard-wide scope
The provider/model filters only lived inside the request-log table, so there was no way to see "how much did app X spend on source Y" across the whole dashboard. Promote them to the top bar next to the app filter, applying globally to the hero summary, trend chart, request logs, and both stats tabs. Backend: the five stats queries (summary, summary-by-app, trends, provider stats, model stats) accept optional provider_name/model filters, applied to both the detail and daily-rollup branches (the rollup PK already carries provider_id/model/pricing_model). Sources match by exact display name via provider_name_coalesce, so session placeholder rows like "Claude (Session)" are selectable; models match by effective pricing model (pricing_model falling back to model), the same grouping key the model-stats tab uses. Request-log filtering switches from LIKE to these exact semantics. Frontend: two truncating dropdowns list only sources/models that have data in the current range, with the model list cascading from the selected source. Dynamic option values are prefix-encoded so a source literally named "all" cannot collide with the sentinel, query keys fall back to null instead of "all", and the option queries follow the dashboard refresh interval (otherwise their default 30s polling drags same-key stats queries along even with refresh disabled). The request-log filter bar keeps only the log-specific status-code select. Labels read "sources" rather than "providers" because direct-connect session buckets sit alongside real providers. i18n updated across zh/en/ja/zh-TW.
This commit is contained in:
+44
-5
@@ -52,39 +52,78 @@ export const usageApi = {
|
||||
startDate?: number,
|
||||
endDate?: number,
|
||||
appType?: string,
|
||||
providerName?: string,
|
||||
model?: string,
|
||||
): Promise<UsageSummary> => {
|
||||
return invoke("get_usage_summary", { startDate, endDate, appType });
|
||||
return invoke("get_usage_summary", {
|
||||
startDate,
|
||||
endDate,
|
||||
appType,
|
||||
providerName,
|
||||
model,
|
||||
});
|
||||
},
|
||||
|
||||
getUsageSummaryByApp: async (
|
||||
startDate?: number,
|
||||
endDate?: number,
|
||||
providerName?: string,
|
||||
model?: string,
|
||||
): Promise<UsageSummaryByApp[]> => {
|
||||
return invoke("get_usage_summary_by_app", { startDate, endDate });
|
||||
return invoke("get_usage_summary_by_app", {
|
||||
startDate,
|
||||
endDate,
|
||||
providerName,
|
||||
model,
|
||||
});
|
||||
},
|
||||
|
||||
getUsageTrends: async (
|
||||
startDate?: number,
|
||||
endDate?: number,
|
||||
appType?: string,
|
||||
providerName?: string,
|
||||
model?: string,
|
||||
): Promise<DailyStats[]> => {
|
||||
return invoke("get_usage_trends", { startDate, endDate, appType });
|
||||
return invoke("get_usage_trends", {
|
||||
startDate,
|
||||
endDate,
|
||||
appType,
|
||||
providerName,
|
||||
model,
|
||||
});
|
||||
},
|
||||
|
||||
getProviderStats: async (
|
||||
startDate?: number,
|
||||
endDate?: number,
|
||||
appType?: string,
|
||||
providerName?: string,
|
||||
model?: string,
|
||||
): Promise<ProviderStats[]> => {
|
||||
return invoke("get_provider_stats", { startDate, endDate, appType });
|
||||
return invoke("get_provider_stats", {
|
||||
startDate,
|
||||
endDate,
|
||||
appType,
|
||||
providerName,
|
||||
model,
|
||||
});
|
||||
},
|
||||
|
||||
getModelStats: async (
|
||||
startDate?: number,
|
||||
endDate?: number,
|
||||
appType?: string,
|
||||
providerName?: string,
|
||||
model?: string,
|
||||
): Promise<ModelStats[]> => {
|
||||
return invoke("get_model_stats", { startDate, endDate, appType });
|
||||
return invoke("get_model_stats", {
|
||||
startDate,
|
||||
endDate,
|
||||
appType,
|
||||
providerName,
|
||||
model,
|
||||
});
|
||||
},
|
||||
|
||||
getRequestLogs: async (
|
||||
|
||||
+81
-26
@@ -1,7 +1,11 @@
|
||||
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
|
||||
import { usageApi } from "@/lib/api/usage";
|
||||
import { resolveUsageRange } from "@/lib/usageRange";
|
||||
import type { LogFilters, UsageRangeSelection } from "@/types/usage";
|
||||
import type {
|
||||
LogFilters,
|
||||
UsageRangeSelection,
|
||||
UsageScopeFilters,
|
||||
} from "@/types/usage";
|
||||
|
||||
const DEFAULT_REFETCH_INTERVAL_MS = 30000;
|
||||
|
||||
@@ -35,7 +39,7 @@ export const usageKeys = {
|
||||
preset: UsageRangeSelection["preset"],
|
||||
customStartDate: number | undefined,
|
||||
customEndDate: number | undefined,
|
||||
appType?: string,
|
||||
filters?: UsageScopeFilters,
|
||||
) =>
|
||||
[
|
||||
...usageKeys.all,
|
||||
@@ -43,12 +47,15 @@ export const usageKeys = {
|
||||
preset,
|
||||
customStartDate ?? 0,
|
||||
customEndDate ?? 0,
|
||||
appType ?? "all",
|
||||
filters?.appType ?? null,
|
||||
filters?.providerName ?? null,
|
||||
filters?.model ?? null,
|
||||
] as const,
|
||||
summaryByApp: (
|
||||
preset: UsageRangeSelection["preset"],
|
||||
customStartDate: number | undefined,
|
||||
customEndDate: number | undefined,
|
||||
filters?: UsageScopeFilters,
|
||||
) =>
|
||||
[
|
||||
...usageKeys.all,
|
||||
@@ -56,12 +63,14 @@ export const usageKeys = {
|
||||
preset,
|
||||
customStartDate ?? 0,
|
||||
customEndDate ?? 0,
|
||||
filters?.providerName ?? null,
|
||||
filters?.model ?? null,
|
||||
] as const,
|
||||
trends: (
|
||||
preset: UsageRangeSelection["preset"],
|
||||
customStartDate: number | undefined,
|
||||
customEndDate: number | undefined,
|
||||
appType?: string,
|
||||
filters?: UsageScopeFilters,
|
||||
) =>
|
||||
[
|
||||
...usageKeys.all,
|
||||
@@ -69,13 +78,15 @@ export const usageKeys = {
|
||||
preset,
|
||||
customStartDate ?? 0,
|
||||
customEndDate ?? 0,
|
||||
appType ?? "all",
|
||||
filters?.appType ?? null,
|
||||
filters?.providerName ?? null,
|
||||
filters?.model ?? null,
|
||||
] as const,
|
||||
providerStats: (
|
||||
preset: UsageRangeSelection["preset"],
|
||||
customStartDate: number | undefined,
|
||||
customEndDate: number | undefined,
|
||||
appType?: string,
|
||||
filters?: UsageScopeFilters,
|
||||
) =>
|
||||
[
|
||||
...usageKeys.all,
|
||||
@@ -83,13 +94,15 @@ export const usageKeys = {
|
||||
preset,
|
||||
customStartDate ?? 0,
|
||||
customEndDate ?? 0,
|
||||
appType ?? "all",
|
||||
filters?.appType ?? null,
|
||||
filters?.providerName ?? null,
|
||||
filters?.model ?? null,
|
||||
] as const,
|
||||
modelStats: (
|
||||
preset: UsageRangeSelection["preset"],
|
||||
customStartDate: number | undefined,
|
||||
customEndDate: number | undefined,
|
||||
appType?: string,
|
||||
filters?: UsageScopeFilters,
|
||||
) =>
|
||||
[
|
||||
...usageKeys.all,
|
||||
@@ -97,7 +110,9 @@ export const usageKeys = {
|
||||
preset,
|
||||
customStartDate ?? 0,
|
||||
customEndDate ?? 0,
|
||||
appType ?? "all",
|
||||
filters?.appType ?? null,
|
||||
filters?.providerName ?? null,
|
||||
filters?.model ?? null,
|
||||
] as const,
|
||||
logs: (key: RequestLogsKey, page: number, pageSize: number) =>
|
||||
[
|
||||
@@ -122,23 +137,38 @@ export const usageKeys = {
|
||||
[...usageKeys.all, providerId, appType] as const,
|
||||
};
|
||||
|
||||
/** 把 UI 侧的 "all" 哨兵归一成 undefined(后端语义:不过滤)。 */
|
||||
function normalizeScopeFilters(filters?: UsageScopeFilters): UsageScopeFilters {
|
||||
return {
|
||||
appType: filters?.appType === "all" ? undefined : filters?.appType,
|
||||
providerName: filters?.providerName,
|
||||
model: filters?.model,
|
||||
};
|
||||
}
|
||||
|
||||
// Hooks
|
||||
export function useUsageSummary(
|
||||
range: UsageRangeSelection,
|
||||
appType?: string,
|
||||
filters?: UsageScopeFilters,
|
||||
options?: UsageQueryOptions,
|
||||
) {
|
||||
const effectiveAppType = appType === "all" ? undefined : appType;
|
||||
const effective = normalizeScopeFilters(filters);
|
||||
return useQuery({
|
||||
queryKey: usageKeys.summary(
|
||||
range.preset,
|
||||
range.customStartDate,
|
||||
range.customEndDate,
|
||||
appType,
|
||||
effective,
|
||||
),
|
||||
queryFn: () => {
|
||||
const { startDate, endDate } = resolveUsageRange(range);
|
||||
return usageApi.getUsageSummary(startDate, endDate, effectiveAppType);
|
||||
return usageApi.getUsageSummary(
|
||||
startDate,
|
||||
endDate,
|
||||
effective.appType,
|
||||
effective.providerName,
|
||||
effective.model,
|
||||
);
|
||||
},
|
||||
refetchInterval: options?.refetchInterval ?? DEFAULT_REFETCH_INTERVAL_MS,
|
||||
refetchIntervalInBackground: options?.refetchIntervalInBackground ?? false,
|
||||
@@ -147,6 +177,7 @@ export function useUsageSummary(
|
||||
|
||||
export function useUsageSummaryByApp(
|
||||
range: UsageRangeSelection,
|
||||
filters?: Pick<UsageScopeFilters, "providerName" | "model">,
|
||||
options?: UsageQueryOptions,
|
||||
) {
|
||||
return useQuery({
|
||||
@@ -154,10 +185,16 @@ export function useUsageSummaryByApp(
|
||||
range.preset,
|
||||
range.customStartDate,
|
||||
range.customEndDate,
|
||||
filters,
|
||||
),
|
||||
queryFn: () => {
|
||||
const { startDate, endDate } = resolveUsageRange(range);
|
||||
return usageApi.getUsageSummaryByApp(startDate, endDate);
|
||||
return usageApi.getUsageSummaryByApp(
|
||||
startDate,
|
||||
endDate,
|
||||
filters?.providerName,
|
||||
filters?.model,
|
||||
);
|
||||
},
|
||||
refetchInterval: options?.refetchInterval ?? DEFAULT_REFETCH_INTERVAL_MS,
|
||||
refetchIntervalInBackground: options?.refetchIntervalInBackground ?? false,
|
||||
@@ -166,20 +203,26 @@ export function useUsageSummaryByApp(
|
||||
|
||||
export function useUsageTrends(
|
||||
range: UsageRangeSelection,
|
||||
appType?: string,
|
||||
filters?: UsageScopeFilters,
|
||||
options?: UsageQueryOptions,
|
||||
) {
|
||||
const effectiveAppType = appType === "all" ? undefined : appType;
|
||||
const effective = normalizeScopeFilters(filters);
|
||||
return useQuery({
|
||||
queryKey: usageKeys.trends(
|
||||
range.preset,
|
||||
range.customStartDate,
|
||||
range.customEndDate,
|
||||
appType,
|
||||
effective,
|
||||
),
|
||||
queryFn: () => {
|
||||
const { startDate, endDate } = resolveUsageRange(range);
|
||||
return usageApi.getUsageTrends(startDate, endDate, effectiveAppType);
|
||||
return usageApi.getUsageTrends(
|
||||
startDate,
|
||||
endDate,
|
||||
effective.appType,
|
||||
effective.providerName,
|
||||
effective.model,
|
||||
);
|
||||
},
|
||||
refetchInterval: options?.refetchInterval ?? DEFAULT_REFETCH_INTERVAL_MS,
|
||||
refetchIntervalInBackground: options?.refetchIntervalInBackground ?? false,
|
||||
@@ -188,20 +231,26 @@ export function useUsageTrends(
|
||||
|
||||
export function useProviderStats(
|
||||
range: UsageRangeSelection,
|
||||
appType?: string,
|
||||
filters?: UsageScopeFilters,
|
||||
options?: UsageQueryOptions,
|
||||
) {
|
||||
const effectiveAppType = appType === "all" ? undefined : appType;
|
||||
const effective = normalizeScopeFilters(filters);
|
||||
return useQuery({
|
||||
queryKey: usageKeys.providerStats(
|
||||
range.preset,
|
||||
range.customStartDate,
|
||||
range.customEndDate,
|
||||
appType,
|
||||
effective,
|
||||
),
|
||||
queryFn: () => {
|
||||
const { startDate, endDate } = resolveUsageRange(range);
|
||||
return usageApi.getProviderStats(startDate, endDate, effectiveAppType);
|
||||
return usageApi.getProviderStats(
|
||||
startDate,
|
||||
endDate,
|
||||
effective.appType,
|
||||
effective.providerName,
|
||||
effective.model,
|
||||
);
|
||||
},
|
||||
refetchInterval: options?.refetchInterval ?? DEFAULT_REFETCH_INTERVAL_MS,
|
||||
refetchIntervalInBackground: options?.refetchIntervalInBackground ?? false,
|
||||
@@ -210,20 +259,26 @@ export function useProviderStats(
|
||||
|
||||
export function useModelStats(
|
||||
range: UsageRangeSelection,
|
||||
appType?: string,
|
||||
filters?: UsageScopeFilters,
|
||||
options?: UsageQueryOptions,
|
||||
) {
|
||||
const effectiveAppType = appType === "all" ? undefined : appType;
|
||||
const effective = normalizeScopeFilters(filters);
|
||||
return useQuery({
|
||||
queryKey: usageKeys.modelStats(
|
||||
range.preset,
|
||||
range.customStartDate,
|
||||
range.customEndDate,
|
||||
appType,
|
||||
effective,
|
||||
),
|
||||
queryFn: () => {
|
||||
const { startDate, endDate } = resolveUsageRange(range);
|
||||
return usageApi.getModelStats(startDate, endDate, effectiveAppType);
|
||||
return usageApi.getModelStats(
|
||||
startDate,
|
||||
endDate,
|
||||
effective.appType,
|
||||
effective.providerName,
|
||||
effective.model,
|
||||
);
|
||||
},
|
||||
refetchInterval: options?.refetchInterval ?? DEFAULT_REFETCH_INTERVAL_MS,
|
||||
refetchIntervalInBackground: options?.refetchIntervalInBackground ?? false,
|
||||
|
||||
Reference in New Issue
Block a user