From 33e7167090a7b9f97f824ca57718a43066defedc Mon Sep 17 00:00:00 2001 From: ivan_yakimkin Date: Thu, 16 Apr 2026 11:41:17 +0300 Subject: [PATCH] fix: use .find() for lastUsed instead of filter+sort MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- .../dashboard/api-manager/ApiManagerPageClient.tsx | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/src/app/(dashboard)/dashboard/api-manager/ApiManagerPageClient.tsx b/src/app/(dashboard)/dashboard/api-manager/ApiManagerPageClient.tsx index 1635025b97..a71b38a258 100644 --- a/src/app/(dashboard)/dashboard/api-manager/ApiManagerPageClient.tsx +++ b/src/app/(dashboard)/dashboard/api-manager/ApiManagerPageClient.tsx @@ -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,