ERP-node/backend-node/src/services/riskAlertCacheService.ts

120 lines
3.4 KiB
TypeScript
Raw Normal View History

/**
* /
* - 10
* -
*/
import { RiskAlertService, Alert } from './riskAlertService';
export class RiskAlertCacheService {
private static instance: RiskAlertCacheService;
private riskAlertService: RiskAlertService;
// 메모리 캐시
private cachedAlerts: Alert[] = [];
private lastUpdated: Date | null = null;
private updateInterval: NodeJS.Timeout | null = null;
private constructor() {
this.riskAlertService = new RiskAlertService();
}
/**
*
*/
public static getInstance(): RiskAlertCacheService {
if (!RiskAlertCacheService.instance) {
RiskAlertCacheService.instance = new RiskAlertCacheService();
}
return RiskAlertCacheService.instance;
}
/**
* (10 )
*/
public startAutoRefresh(): void {
console.log('🔄 리스크/알림 자동 갱신 시작 (10분 간격)');
2025-10-21 12:51:57 +09:00
console.log(' - 기상특보: 즉시 호출');
console.log(' - 교통사고/도로공사: 10분 후 첫 호출');
2025-10-21 12:51:57 +09:00
// 기상특보만 즉시 호출 (ITS API는 10분 후부터)
this.refreshWeatherOnly();
2025-10-21 12:51:57 +09:00
// 10분마다 전체 갱신 (600,000ms)
this.updateInterval = setInterval(() => {
this.refreshCache();
}, 10 * 60 * 1000);
}
2025-10-21 12:51:57 +09:00
/**
* ( )
*/
private async refreshWeatherOnly(): Promise<void> {
try {
console.log('🌤️ 기상특보만 즉시 갱신 중...');
const weatherAlerts = await this.riskAlertService.getWeatherAlerts();
this.cachedAlerts = weatherAlerts;
this.lastUpdated = new Date();
console.log(`✅ 기상특보 갱신 완료! (${weatherAlerts.length}건)`);
} catch (error: any) {
console.error('❌ 기상특보 갱신 실패:', error.message);
}
}
/**
*
*/
public stopAutoRefresh(): void {
if (this.updateInterval) {
clearInterval(this.updateInterval);
this.updateInterval = null;
console.log('⏸️ 리스크/알림 자동 갱신 중지');
}
}
/**
*
*/
private async refreshCache(): Promise<void> {
try {
console.log('🔄 리스크/알림 캐시 갱신 중...');
const startTime = Date.now();
const alerts = await this.riskAlertService.getAllAlerts();
this.cachedAlerts = alerts;
this.lastUpdated = new Date();
const duration = Date.now() - startTime;
console.log(`✅ 리스크/알림 캐시 갱신 완료! (${duration}ms)`);
console.log(` - 총 ${alerts.length}건의 알림`);
console.log(` - 기상특보: ${alerts.filter(a => a.type === 'weather').length}`);
console.log(` - 교통사고: ${alerts.filter(a => a.type === 'accident').length}`);
console.log(` - 도로공사: ${alerts.filter(a => a.type === 'construction').length}`);
} catch (error: any) {
console.error('❌ 리스크/알림 캐시 갱신 실패:', error.message);
}
}
/**
* (!)
*/
public getCachedAlerts(): { alerts: Alert[]; lastUpdated: Date | null } {
return {
alerts: this.cachedAlerts,
lastUpdated: this.lastUpdated,
};
}
/**
* ( )
*/
public async forceRefresh(): Promise<Alert[]> {
await this.refreshCache();
return this.cachedAlerts;
}
}