chore: code cleanup - remove duplicates and add caching
- Delete duplicate getCacheStats() function in cache.service.js - Fix date calculation bug in lotw.service.js (was Date.now()-Date.now()) - Extract duplicate helper functions (yieldToEventLoop, getQSOKey) to sync-helpers.js - Cache award definitions in memory to avoid repeated file I/O - Delete unused parseDCLJSONResponse() function - Remove unused imports (getPerformanceSummary, resetPerformanceMetrics) - Auto-discover award JSON files instead of hardcoded list Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { db, logger } from '../config.js';
|
||||
import { qsos } from '../db/schema/index.js';
|
||||
import { eq, and, or, desc, sql } from 'drizzle-orm';
|
||||
import { readFileSync } from 'fs';
|
||||
import { readFileSync, readdirSync } from 'fs';
|
||||
import { join } from 'path';
|
||||
import { getCachedAwardProgress, setCachedAwardProgress } from './cache.service.js';
|
||||
|
||||
@@ -13,23 +13,25 @@ import { getCachedAwardProgress, setCachedAwardProgress } from './cache.service.
|
||||
// Load award definitions from files
|
||||
const AWARD_DEFINITIONS_DIR = join(process.cwd(), 'award-definitions');
|
||||
|
||||
// In-memory cache for award definitions (static, never changes at runtime)
|
||||
let cachedAwardDefinitions = null;
|
||||
|
||||
/**
|
||||
* Load all award definitions
|
||||
* Load all award definitions (cached in memory)
|
||||
*/
|
||||
function loadAwardDefinitions() {
|
||||
// Return cached definitions if available
|
||||
if (cachedAwardDefinitions) {
|
||||
return cachedAwardDefinitions;
|
||||
}
|
||||
|
||||
const definitions = [];
|
||||
|
||||
try {
|
||||
const files = [
|
||||
'dxcc.json',
|
||||
'dxcc-sat.json',
|
||||
'was.json',
|
||||
'vucc-sat.json',
|
||||
'sat-rs44.json',
|
||||
'special-stations.json',
|
||||
'dld.json',
|
||||
'73-on-73.json',
|
||||
];
|
||||
// Auto-discover all JSON files in the award-definitions directory
|
||||
const files = readdirSync(AWARD_DEFINITIONS_DIR)
|
||||
.filter(f => f.endsWith('.json'))
|
||||
.sort();
|
||||
|
||||
for (const file of files) {
|
||||
try {
|
||||
@@ -45,6 +47,9 @@ function loadAwardDefinitions() {
|
||||
logger.error('Error loading award definitions', { error: error.message });
|
||||
}
|
||||
|
||||
// Cache the definitions for future calls
|
||||
cachedAwardDefinitions = definitions;
|
||||
|
||||
return definitions;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user