mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-04 14:22:09 +03:00
fix: API Manager usage stats showing 0 for all registered keys
The fetchUsageStats function fetched call-logs and filtered them by key.id === log.apiKeyId. However, the API key table uses UUIDs while the call-log pipeline stores a different identifier in the apiKeyId field — so the comparison never matched, yielding 0 requests for every key. Fix by sourcing request counts from the /api/usage/analytics endpoint (same data that already powers the 'API Key Breakdown' table on the Analytics tab), matched by apiKeyName. The lastUsed timestamp is still derived from call-logs, also matched by name. Both requests are issued in parallel via Promise.all to avoid increasing page load time.
This commit is contained in:
@@ -166,24 +166,42 @@ export default function ApiManagerPageClient() {
|
||||
const fetchUsageStats = async (apiKeys: ApiKey[]) => {
|
||||
if (apiKeys.length === 0) return;
|
||||
try {
|
||||
const res = await fetch("/api/usage/call-logs?limit=1000");
|
||||
if (!res.ok) return;
|
||||
const logs = await res.json();
|
||||
// Fetch analytics (accurate aggregated counts) and recent call-logs
|
||||
// (for lastUsed timestamps) in parallel.
|
||||
// The previous approach matched call-logs by key.id === log.apiKeyId,
|
||||
// but these use different ID schemes and never matched, yielding 0.
|
||||
const [analyticsRes, logsRes] = await Promise.all([
|
||||
fetch("/api/usage/analytics?range=all"),
|
||||
fetch("/api/usage/call-logs?limit=1000"),
|
||||
]);
|
||||
|
||||
const analytics = analyticsRes.ok ? await analyticsRes.json() : null;
|
||||
const byApiKey: any[] = analytics?.byApiKey || [];
|
||||
const logs = logsRes.ok ? await logsRes.json() : [];
|
||||
|
||||
const stats: Record<string, KeyUsageStats> = {};
|
||||
|
||||
for (const key of apiKeys) {
|
||||
const keyLogs = (logs || []).filter(
|
||||
(log: any) => log.apiKeyId === key.id || log.apiKeyName === key.name
|
||||
// Match analytics entry by key name (reliable across both systems)
|
||||
const analyticsMatch = byApiKey.find(
|
||||
(entry: any) => entry.apiKeyName === key.name
|
||||
);
|
||||
|
||||
// Find the most recent call-log for this key to determine lastUsed
|
||||
const keyLogs = (logs || []).filter(
|
||||
(log: any) => log.apiKeyName === key.name
|
||||
);
|
||||
const lastUsed =
|
||||
keyLogs.length > 0
|
||||
? keyLogs.sort(
|
||||
(a: any, b: any) =>
|
||||
new Date(b.timestamp).getTime() - new Date(a.timestamp).getTime()
|
||||
)[0]?.timestamp
|
||||
: null;
|
||||
|
||||
stats[key.id] = {
|
||||
totalRequests: keyLogs.length,
|
||||
lastUsed:
|
||||
keyLogs.length > 0
|
||||
? keyLogs.sort(
|
||||
(a: any, b: any) =>
|
||||
new Date(b.timestamp).getTime() - new Date(a.timestamp).getTime()
|
||||
)[0]?.timestamp
|
||||
: null,
|
||||
totalRequests: analyticsMatch?.requests ?? 0,
|
||||
lastUsed,
|
||||
};
|
||||
}
|
||||
setUsageStats(stats);
|
||||
|
||||
Reference in New Issue
Block a user