Files
flyer-crawler.projectium.com/src/utils/processingTimer.ts
Torben Sorensen 158778c2ec
Some checks failed
Deploy to Web Server flyer-crawler.projectium.com / deploy (push) Failing after 5m16s
DB refactor for easier testsing
App.ts refactor into hooks
unit tests
2025-12-08 20:46:12 -08:00

50 lines
1.7 KiB
TypeScript

// src/utils/processingTimer.ts
import { logger } from '../services/logger.client';
const PROCESSING_TIMES_KEY = 'flyerProcessingTimes';
const MAX_SAMPLES = 5;
/**
* Records the duration of a flyer processing job in localStorage.
* @param durationInSeconds - The processing time in seconds.
*/
export const recordProcessingTime = (durationInSeconds: number): void => {
try {
const storedTimes = localStorage.getItem(PROCESSING_TIMES_KEY);
const times: number[] = storedTimes ? JSON.parse(storedTimes) : [];
// Add the new time and keep only the last MAX_SAMPLES
times.push(durationInSeconds);
const recentTimes = times.slice(-MAX_SAMPLES);
localStorage.setItem(PROCESSING_TIMES_KEY, JSON.stringify(recentTimes));
} catch (error) {
// Use the centralized logger for consistency.
logger.error("Could not record processing time in localStorage.", { error });
}
};
/**
* Calculates the average processing time from stored durations.
* @returns The average time in seconds, or a default of 45 seconds if no data exists.
*/
export const getAverageProcessingTime = (): number => {
try {
const storedTimes = localStorage.getItem(PROCESSING_TIMES_KEY);
if (!storedTimes) return 45; // Default estimate if no history
const times: number[] = JSON.parse(storedTimes);
if (times.length === 0) return 45;
const sum = times.reduce((acc, time) => acc + time, 0);
const average = sum / times.length;
// Return a rounded, reasonable number
return Math.round(average);
} catch (error) {
// Use the centralized logger for consistency.
logger.error("Could not get average processing time from localStorage.", { error });
return 45; // Default on error
}
};