fix: use .find() for lastUsed instead of filter+sort

The call-logs endpoint already returns entries sorted by timestamp DESC,
so the first match for a given apiKeyName is guaranteed to be the most
recent one. Replace O(K·M·log M) filter+sort with O(M) find.
This commit is contained in:
ivan_yakimkin
2026-04-16 11:41:17 +03:00
parent 9bfbbd65f5
commit 33e7167090

View File

@@ -187,17 +187,11 @@ export default function ApiManagerPageClient() {
(entry: any) => entry.apiKeyName === key.name
);
// Find the most recent call-log for this key to determine lastUsed
const keyLogs = (logs || []).filter(
// The call-logs endpoint returns entries sorted by timestamp DESC,
// so the first match is the most recent one.
const lastUsed = (logs || []).find(
(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;
)?.timestamp || null;
stats[key.id] = {
totalRequests: analyticsMatch?.requests ?? 0,