Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion lib/acquia-api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
import axios from 'axios';

// --- Caching Infrastructure ---
interface CacheEntry<T> {
data: T;
timestamp: number; // The time the data was cached
}

const cache: Record<string, CacheEntry<any>> = {};
Copy link

Copilot AI Sep 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The global cache object will persist data across all instances and requests. In a server environment, this could lead to memory leaks and unexpected data sharing between users. Consider implementing per-instance caching or using a proper cache library with memory management.

Copilot uses AI. Check for mistakes.
const CACHE_DURATION_MS = 6 * 60 * 60 * 1000; // 6 hours in milliseconds

/**
* Generates a unique string key for caching based on the request parameters.
*/
const generateCacheKey = (parts: (string | undefined | null)[]): string => {
return parts.filter(Boolean).map(part => encodeURIComponent(part as string)).join(':');
};
// --- End Caching Infrastructure ---

export interface VisitsData {
applicationUuid: string;
applicationName?: string;
Expand Down Expand Up @@ -258,8 +275,16 @@ class AcquiaApiServiceFixed {
}

async getApplications(): Promise<Application[]> {
const cacheKey = 'applications';
const cachedEntry = cache[cacheKey];

if (cachedEntry && (Date.now() - cachedEntry.timestamp < CACHE_DURATION_MS)) {
// console.log('✅ Returning cached applications data.');
return cachedEntry.data;
}

try {
// console.log(`🔍 Fetching all applications`);
// console.log(`🔍 Fetching all applications (cache miss or stale)`);

const response = await this.makeAuthenticatedRequest('/applications');

Expand All @@ -286,6 +311,9 @@ class AcquiaApiServiceFixed {
console.warn('⚠️ No applications found in response');
}

// Store the fresh data in the cache
cache[cacheKey] = { data: applications, timestamp: Date.now() };

return applications;
} catch (error) {
console.error('❌ Error fetching applications:', error);
Expand Down Expand Up @@ -535,6 +563,18 @@ class AcquiaApiServiceFixed {
from?: string,
to?: string
): Promise<T[]> {
const cacheKey = generateCacheKey([baseEndpoint, subscriptionUuid, from, to]);
const cachedEntry = cache[cacheKey];

if (cachedEntry && (Date.now() - cachedEntry.timestamp < CACHE_DURATION_MS)) {
// console.log(`✅ Returning cached ${dataType} data for key: ${cacheKey}`);
this.reportProgress({
step: `Using cached ${dataType} data.`,
itemsCollected: cachedEntry.data.length
});
return cachedEntry.data;
}

let allData: T[] = [];
let currentPage = 1;
let totalPages = 1;
Expand Down Expand Up @@ -654,6 +694,9 @@ class AcquiaApiServiceFixed {
itemsCollected: allData.length
});

// Store the fresh data in the cache
cache[cacheKey] = { data: allData, timestamp: Date.now() };

// console.log(`🎉 Successfully fetched ${allData.length} ${dataType} records from ${currentPage - 1} pages`);

// Final summary of date range
Expand Down
Loading