From 687dccb52246231c80b49eaa236dd6b6ee6d4525 Mon Sep 17 00:00:00 2001 From: leeheejin Date: Tue, 21 Oct 2025 12:51:57 +0900 Subject: [PATCH] =?UTF-8?q?=EC=95=8C=EB=A6=BC=20api=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/services/riskAlertCacheService.ts | 25 ++++++++-- .../dashboard/widgets/RiskAlertWidget.tsx | 48 +++++++++++++++++-- 2 files changed, 67 insertions(+), 6 deletions(-) diff --git a/backend-node/src/services/riskAlertCacheService.ts b/backend-node/src/services/riskAlertCacheService.ts index cc4de181..ce8b6089 100644 --- a/backend-node/src/services/riskAlertCacheService.ts +++ b/backend-node/src/services/riskAlertCacheService.ts @@ -34,16 +34,35 @@ export class RiskAlertCacheService { */ public startAutoRefresh(): void { console.log('๐Ÿ”„ ๋ฆฌ์Šคํฌ/์•Œ๋ฆผ ์ž๋™ ๊ฐฑ์‹  ์‹œ์ž‘ (10๋ถ„ ๊ฐ„๊ฒฉ)'); + console.log(' - ๊ธฐ์ƒํŠน๋ณด: ์ฆ‰์‹œ ํ˜ธ์ถœ'); + console.log(' - ๊ตํ†ต์‚ฌ๊ณ /๋„๋กœ๊ณต์‚ฌ: 10๋ถ„ ํ›„ ์ฒซ ํ˜ธ์ถœ'); - // ์ฆ‰์‹œ ์ฒซ ๊ฐฑ์‹  - this.refreshCache(); + // ๊ธฐ์ƒํŠน๋ณด๋งŒ ์ฆ‰์‹œ ํ˜ธ์ถœ (ITS API๋Š” 10๋ถ„ ํ›„๋ถ€ํ„ฐ) + this.refreshWeatherOnly(); - // 10๋ถ„๋งˆ๋‹ค ๊ฐฑ์‹  (600,000ms) + // 10๋ถ„๋งˆ๋‹ค ์ „์ฒด ๊ฐฑ์‹  (600,000ms) this.updateInterval = setInterval(() => { this.refreshCache(); }, 10 * 60 * 1000); } + /** + * ๊ธฐ์ƒํŠน๋ณด๋งŒ ๊ฐฑ์‹  (์žฌ์‹œ์ž‘ ์‹œ ์‚ฌ์šฉ) + */ + private async refreshWeatherOnly(): Promise { + 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); + } + } + /** * ์ž๋™ ๊ฐฑ์‹  ์ค‘์ง€ */ diff --git a/frontend/components/dashboard/widgets/RiskAlertWidget.tsx b/frontend/components/dashboard/widgets/RiskAlertWidget.tsx index 98278fdd..de6b2af8 100644 --- a/frontend/components/dashboard/widgets/RiskAlertWidget.tsx +++ b/frontend/components/dashboard/widgets/RiskAlertWidget.tsx @@ -33,11 +33,11 @@ export default function RiskAlertWidget({ element }: RiskAlertWidgetProps) { const [lastUpdated, setLastUpdated] = useState(null); const [newAlertIds, setNewAlertIds] = useState>(new Set()); - // ๋ฐ์ดํ„ฐ ๋กœ๋“œ (๋ฐฑ์—”๋“œ ํ†ตํ•ฉ ํ˜ธ์ถœ) + // ๋ฐ์ดํ„ฐ ๋กœ๋“œ (๋ฐฑ์—”๋“œ ์บ์‹œ ์กฐํšŒ) const loadData = async () => { setIsRefreshing(true); try { - // ๋ฐฑ์—”๋“œ API ํ˜ธ์ถœ (๊ตํ†ต์‚ฌ๊ณ , ๊ธฐ์ƒํŠน๋ณด, ๋„๋กœ๊ณต์‚ฌ ํ†ตํ•ฉ) + // ๋ฐฑ์—”๋“œ API ํ˜ธ์ถœ (์บ์‹œ๋œ ๋ฐ์ดํ„ฐ) const response = await apiClient.get<{ success: boolean; data: Alert[]; @@ -79,6 +79,48 @@ export default function RiskAlertWidget({ element }: RiskAlertWidgetProps) { } }; + // ๊ฐ•์ œ ์ƒˆ๋กœ๊ณ ์นจ (์‹ค์‹œ๊ฐ„ API ํ˜ธ์ถœ) + const forceRefresh = async () => { + setIsRefreshing(true); + try { + // ๊ฐ•์ œ ๊ฐฑ์‹  API ํ˜ธ์ถœ (์‹ค์‹œ๊ฐ„ ๋ฐ์ดํ„ฐ) + const response = await apiClient.post<{ + success: boolean; + data: Alert[]; + count: number; + message?: string; + }>("/risk-alerts/refresh", {}); + + if (response.data.success && response.data.data) { + const newData = response.data.data; + + // ์ƒˆ๋กœ์šด ์•Œ๋ฆผ ๊ฐ์ง€ + const oldIds = new Set(alerts.map(a => a.id)); + const newIds = new Set(); + newData.forEach(alert => { + if (!oldIds.has(alert.id)) { + newIds.add(alert.id); + } + }); + + setAlerts(newData); + setNewAlertIds(newIds); + setLastUpdated(new Date()); + + // 3์ดˆ ํ›„ ์ƒˆ ์•Œ๋ฆผ ์• ๋‹ˆ๋ฉ”์ด์…˜ ์ œ๊ฑฐ + if (newIds.size > 0) { + setTimeout(() => setNewAlertIds(new Set()), 3000); + } + } else { + console.error("โŒ ๋ฆฌ์Šคํฌ ์•Œ๋ฆผ ๊ฐ•์ œ ๊ฐฑ์‹  ์‹คํŒจ"); + } + } catch (error: any) { + console.error("โŒ ๋ฆฌ์Šคํฌ ์•Œ๋ฆผ ๊ฐ•์ œ ๊ฐฑ์‹  ์˜ค๋ฅ˜:", error.message); + } finally { + setIsRefreshing(false); + } + }; + useEffect(() => { loadData(); // 1๋ถ„๋งˆ๋‹ค ์ž๋™ ์ƒˆ๋กœ๊ณ ์นจ (60000ms) @@ -156,7 +198,7 @@ export default function RiskAlertWidget({ element }: RiskAlertWidgetProps) { {lastUpdated.toLocaleTimeString('ko-KR', { hour: '2-digit', minute: '2-digit' })} )} -