Fix: Prevent console message loop on maintenance updates

The console was generating excessive messages after schedule maintenance updates.
This commit is contained in:
Tola Leng
2025-05-26 16:42:05 +08:00
parent 23be1fca0f
commit e74bf35264
7 changed files with 175 additions and 94 deletions
@@ -13,14 +13,7 @@ const CACHE_EXPIRY_MS = 600000; // 10 minutes cache expiry (increased significan
*/
export const isCacheValid = (): boolean => {
const now = Date.now();
const isValid = !!(cachedMaintenanceRecords && now - lastFetchTimestamp < CACHE_EXPIRY_MS);
// Minimal logging to reduce console spam
if (isValid) {
console.log("Using cached maintenance data");
}
return isValid;
return !!(cachedMaintenanceRecords && now - lastFetchTimestamp < CACHE_EXPIRY_MS);
};
/**
@@ -28,7 +21,6 @@ export const isCacheValid = (): boolean => {
*/
export const getCachedRecords = () => {
if (cachedMaintenanceRecords) {
console.log("Returning cached maintenance records:", cachedMaintenanceRecords.length);
return [...cachedMaintenanceRecords]; // Return a copy to prevent mutation
}
return null;
@@ -40,7 +32,6 @@ export const getCachedRecords = () => {
export const updateCache = (data: MaintenanceItem[], timestamp?: number): void => {
cachedMaintenanceRecords = data;
lastFetchTimestamp = timestamp || Date.now();
console.log("Maintenance cache updated with", data.length, "records");
};
/**
@@ -49,7 +40,6 @@ export const updateCache = (data: MaintenanceItem[], timestamp?: number): void =
export const clearCache = (): void => {
cachedMaintenanceRecords = null;
lastFetchTimestamp = 0;
console.log("Maintenance cache cleared");
};
/**
@@ -19,7 +19,6 @@ export const fetchAllMaintenanceRecords = async (forceRefresh = false): Promise<
// If forced refresh, clear cache first
if (forceRefresh) {
clearCache();
console.log('Cache cleared for forced refresh');
}
// Return cached data if available and not forced refresh
@@ -29,14 +28,12 @@ export const fetchAllMaintenanceRecords = async (forceRefresh = false): Promise<
// Request deduplication: if a request is already in progress, wait for it
if (currentRequest) {
console.log('Request already in progress, waiting for completion');
return await currentRequest;
}
// Strict rate limiting - prevent requests too close together (unless forced)
const timeSinceLastRequest = now - lastRequestTime;
if (timeSinceLastRequest < MIN_REQUEST_INTERVAL && !forceRefresh) {
console.log(`Rate limiting: ${Math.round((MIN_REQUEST_INTERVAL - timeSinceLastRequest) / 1000)}s remaining`);
return getCachedRecords() || [];
}
@@ -66,8 +63,6 @@ export const fetchAllMaintenanceRecords = async (forceRefresh = false): Promise<
* Perform the actual API request
*/
const performRequest = async (timestamp: number): Promise<MaintenanceItem[]> => {
console.log('Making API request for maintenance records...');
// Create abort controller for request timeout
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 30000); // 30 second timeout
@@ -95,7 +90,6 @@ const performRequest = async (timestamp: number): Promise<MaintenanceItem[]> =>
// Update cache
updateCache(normalizedData, timestamp);
console.log(`Successfully fetched ${normalizedData.length} maintenance records`);
return normalizedData;
} catch (error) {
@@ -61,14 +61,12 @@ const getCompletedMaintenance = async (): Promise<MaintenanceItem[]> => {
const createMaintenance = async (data: CreateMaintenanceInput): Promise<void> => {
await createMaintenanceRecord(data);
clearMaintenanceCache(); // Force cache refresh after creation
console.log('Maintenance created and cache cleared');
};
// Update an existing maintenance record
const updateMaintenanceRecord = async (id: string, data: Partial<MaintenanceItem>): Promise<void> => {
await updateMaintenance(id, data);
clearMaintenanceCache(); // Force cache refresh after update
console.log('Maintenance updated and cache cleared');
};
// Refresh cache manually