소스 수정
This commit is contained in:
28
backend/api/server/location-stats.get.ts
Normal file
28
backend/api/server/location-stats.get.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { query } from '../../utils/db'
|
||||
|
||||
export default defineEventHandler(async () => {
|
||||
const stats = await query(`
|
||||
SELECT
|
||||
t.physical_location,
|
||||
COUNT(DISTINCT t.target_id) as server_count,
|
||||
ROUND(AVG(s.cpu_temp)::numeric, 1) as avg_temp,
|
||||
ROUND(SUM(n.speed_recv)::numeric, 0) as total_rx,
|
||||
ROUND(SUM(n.speed_sent)::numeric, 0) as total_tx
|
||||
FROM server_targets t
|
||||
LEFT JOIN server_snapshots s ON t.target_id = s.target_id
|
||||
AND s.collected_at::timestamp >= NOW() - INTERVAL '10 minutes'
|
||||
LEFT JOIN server_networks n ON t.target_id = n.target_id
|
||||
AND n.collected_at::timestamp >= NOW() - INTERVAL '10 minutes'
|
||||
WHERE t.is_active = 1
|
||||
GROUP BY t.physical_location
|
||||
ORDER BY t.physical_location
|
||||
`)
|
||||
|
||||
return stats.map((row: any) => ({
|
||||
location: row.physical_location || '미지정',
|
||||
serverCount: Number(row.server_count) || 0,
|
||||
avgTemp: row.avg_temp ? parseFloat(row.avg_temp) : null,
|
||||
totalRx: Number(row.total_rx) || 0,
|
||||
totalTx: Number(row.total_tx) || 0
|
||||
}))
|
||||
})
|
||||
@@ -183,6 +183,17 @@ async function getServerDashboard() {
|
||||
LIMIT 1
|
||||
`, [server.target_id])
|
||||
|
||||
// 최신 디스크 사용률 (최대값)
|
||||
const diskData = await queryOne(`
|
||||
SELECT MAX(disk_percent) as disk_percent
|
||||
FROM server_disks
|
||||
WHERE target_id = $1
|
||||
AND collected_at = (SELECT MAX(collected_at) FROM server_disks WHERE target_id = $1)
|
||||
AND device_name NOT LIKE '%loop%'
|
||||
AND mount_point NOT LIKE '%/snap%'
|
||||
AND fs_type NOT IN ('tmpfs', 'squashfs')
|
||||
`, [server.target_id])
|
||||
|
||||
// 오프라인 체크
|
||||
let isOffline = true
|
||||
let lastCollected = null
|
||||
@@ -199,7 +210,7 @@ async function getServerDashboard() {
|
||||
if (!isOffline && snapshot) {
|
||||
cpuLevel = getLevel(Number(snapshot.cpu_percent), thresholds.server?.cpu || { warning: 70, critical: 85, danger: 95 })
|
||||
memLevel = getLevel(Number(snapshot.memory_percent), thresholds.server?.memory || { warning: 80, critical: 90, danger: 95 })
|
||||
diskLevel = getLevel(Number(snapshot.disk_percent), thresholds.server?.disk || { warning: 80, critical: 90, danger: 95 })
|
||||
diskLevel = getLevel(Number(diskData?.disk_percent), thresholds.server?.disk || { warning: 80, critical: 90, danger: 95 })
|
||||
serverLevel = getHighestLevel([cpuLevel, memLevel, diskLevel])
|
||||
}
|
||||
|
||||
@@ -280,7 +291,7 @@ async function getServerDashboard() {
|
||||
cpu_level: cpuLevel,
|
||||
memory_percent: snapshot?.memory_percent ?? null,
|
||||
memory_level: memLevel,
|
||||
disk_percent: snapshot?.disk_percent ?? null,
|
||||
disk_percent: diskData?.disk_percent ?? null,
|
||||
disk_level: diskLevel,
|
||||
last_collected: lastCollected,
|
||||
containers: containers,
|
||||
|
||||
160
frontend/components/LocationStatsPortlet.vue
Normal file
160
frontend/components/LocationStatsPortlet.vue
Normal file
@@ -0,0 +1,160 @@
|
||||
<template>
|
||||
<div class="location-stats-card">
|
||||
<div class="card-header">
|
||||
<span class="card-icon">🏢</span>
|
||||
<span class="card-title">물리공간</span>
|
||||
</div>
|
||||
<div class="location-list">
|
||||
<div
|
||||
v-for="loc in locations"
|
||||
:key="loc.location"
|
||||
class="location-item"
|
||||
:class="getTempClass(loc.avgTemp)"
|
||||
>
|
||||
<div class="loc-name">{{ loc.location }}</div>
|
||||
<div class="loc-stats">
|
||||
<div class="stat-row temp">
|
||||
<span class="stat-icon">🌡</span>
|
||||
<span class="stat-value">{{ loc.avgTemp ? loc.avgTemp + '°C' : '-' }}</span>
|
||||
</div>
|
||||
<div class="stat-row traffic">
|
||||
<span class="stat-icon">↓</span>
|
||||
<span class="stat-value rx">{{ formatBytes(loc.totalRx) }}</span>
|
||||
</div>
|
||||
<div class="stat-row traffic">
|
||||
<span class="stat-icon">↑</span>
|
||||
<span class="stat-value tx">{{ formatBytes(loc.totalTx) }}</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="loc-servers">{{ loc.serverCount }}대</div>
|
||||
</div>
|
||||
<div v-if="!locations || locations.length === 0" class="no-data">
|
||||
데이터 없음
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
interface LocationStat {
|
||||
location: string
|
||||
serverCount: number
|
||||
avgTemp: number | null
|
||||
totalRx: number
|
||||
totalTx: number
|
||||
}
|
||||
|
||||
defineProps<{
|
||||
locations: LocationStat[]
|
||||
}>()
|
||||
|
||||
function getTempClass(temp: number | null): string {
|
||||
if (temp === null) return ''
|
||||
if (temp >= 70) return 'temp-critical'
|
||||
if (temp >= 60) return 'temp-warning'
|
||||
return 'temp-normal'
|
||||
}
|
||||
|
||||
function formatBytes(bytes: number): string {
|
||||
if (!bytes || bytes === 0) return '0 B/s'
|
||||
if (bytes >= 1024 * 1024 * 1024) return (bytes / (1024 * 1024 * 1024)).toFixed(1) + ' GB/s'
|
||||
if (bytes >= 1024 * 1024) return (bytes / (1024 * 1024)).toFixed(1) + ' MB/s'
|
||||
if (bytes >= 1024) return (bytes / 1024).toFixed(1) + ' KB/s'
|
||||
return bytes.toFixed(0) + ' B/s'
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.location-stats-card {
|
||||
background: var(--bg-secondary);
|
||||
border: 1px solid var(--border-color);
|
||||
border-radius: 12px;
|
||||
padding: 12px;
|
||||
flex: 1;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
min-height: 0;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.card-header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 6px;
|
||||
margin-bottom: 10px;
|
||||
padding-bottom: 8px;
|
||||
border-bottom: 1px solid var(--border-color);
|
||||
}
|
||||
|
||||
.card-icon { font-size: 18px; }
|
||||
.card-title { font-size: 13px; font-weight: 600; color: var(--text-primary); }
|
||||
|
||||
.location-list {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 8px;
|
||||
overflow-y: auto;
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.location-item {
|
||||
background: var(--bg-tertiary, #f8fafc);
|
||||
border-radius: 8px;
|
||||
padding: 10px;
|
||||
border-left: 3px solid #22c55e;
|
||||
}
|
||||
|
||||
.location-item.temp-warning { border-left-color: #f59e0b; }
|
||||
.location-item.temp-critical { border-left-color: #ef4444; }
|
||||
|
||||
.loc-name {
|
||||
font-size: 12px;
|
||||
font-weight: 600;
|
||||
color: var(--text-primary);
|
||||
margin-bottom: 6px;
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
|
||||
.loc-stats {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 3px;
|
||||
}
|
||||
|
||||
.stat-row {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 4px;
|
||||
font-size: 11px;
|
||||
}
|
||||
|
||||
.stat-icon {
|
||||
width: 14px;
|
||||
text-align: center;
|
||||
color: var(--text-muted);
|
||||
}
|
||||
|
||||
.stat-value {
|
||||
color: var(--text-secondary);
|
||||
font-family: monospace;
|
||||
}
|
||||
|
||||
.stat-value.rx { color: #06b6d4; }
|
||||
.stat-value.tx { color: #f59e0b; }
|
||||
|
||||
.loc-servers {
|
||||
font-size: 10px;
|
||||
color: var(--text-muted);
|
||||
text-align: right;
|
||||
margin-top: 4px;
|
||||
}
|
||||
|
||||
.no-data {
|
||||
font-size: 12px;
|
||||
color: var(--text-muted);
|
||||
text-align: center;
|
||||
padding: 20px 0;
|
||||
}
|
||||
</style>
|
||||
@@ -56,10 +56,10 @@ function formatTimeAgo(datetime: string | null): string {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
padding: 16px 12px;
|
||||
padding: 10px 8px;
|
||||
background: var(--bg-secondary);
|
||||
border: 1px solid var(--border-color);
|
||||
border-radius: 12px;
|
||||
border-radius: 10px;
|
||||
cursor: pointer;
|
||||
transition: all 0.2s;
|
||||
text-align: center;
|
||||
@@ -73,12 +73,12 @@ function formatTimeAgo(datetime: string | null): string {
|
||||
.network-card.unhealthy { border-top: 4px solid #ef4444; }
|
||||
.network-card.pending { border-top: 4px solid #9ca3af; }
|
||||
|
||||
.card-icon { font-size: 28px; margin-bottom: 8px; }
|
||||
.card-title { font-size: 14px; font-weight: 600; color: var(--text-primary); margin-bottom: 10px; }
|
||||
.card-icon { font-size: 22px; margin-bottom: 4px; }
|
||||
.card-title { font-size: 12px; font-weight: 600; color: var(--text-primary); margin-bottom: 6px; }
|
||||
|
||||
.card-status { display: flex; align-items: center; justify-content: center; gap: 6px; margin-bottom: 4px; }
|
||||
.status-icon { font-size: 16px; }
|
||||
.status-text { font-size: 15px; font-weight: 500; color: var(--text-secondary); }
|
||||
.card-status { display: flex; align-items: center; justify-content: center; gap: 4px; margin-bottom: 2px; }
|
||||
.status-icon { font-size: 14px; }
|
||||
.status-text { font-size: 13px; font-weight: 500; color: var(--text-secondary); }
|
||||
|
||||
.card-time { font-size: 12px; color: var(--text-muted); }
|
||||
.card-time { font-size: 10px; color: var(--text-muted); }
|
||||
</style>
|
||||
|
||||
@@ -102,7 +102,7 @@
|
||||
<div class="mini-bar">
|
||||
<div :class="['mini-fill', getContainerMemLevel(container)]" :style="{ width: getMemPercent(container) + '%' }"></div>
|
||||
</div>
|
||||
<span class="value">{{ formatMemoryShort(container.memory_usage) }}</span>
|
||||
<span :class="['value', { 'mem-highlight': isMemoryOver1GB(container.memory_usage) }]">{{ formatMemoryShort(container.memory_usage) }}</span>
|
||||
</div>
|
||||
<div class="card-metric">
|
||||
<span class="label">↓RX</span>
|
||||
@@ -211,9 +211,15 @@ function getMemPercent(c: ContainerStatus): number {
|
||||
|
||||
function formatMemoryShort(bytes: number | null): string {
|
||||
if (bytes === null || bytes === undefined) return '-'
|
||||
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(0)}K`
|
||||
if (bytes < 1024 * 1024 * 1024) return `${(bytes / 1024 / 1024).toFixed(0)}M`
|
||||
return `${(bytes / 1024 / 1024 / 1024).toFixed(1)}G`
|
||||
const numBytes = Number(bytes)
|
||||
if (numBytes < 1024 * 1024) return `${(numBytes / 1024).toFixed(0)}K`
|
||||
if (numBytes < 1024 * 1024 * 1024) return `${(numBytes / 1024 / 1024).toFixed(0)}M`
|
||||
return `${(numBytes / 1024 / 1024 / 1024).toFixed(1)}G`
|
||||
}
|
||||
|
||||
function isMemoryOver1GB(bytes: number | null): boolean {
|
||||
if (bytes === null || bytes === undefined) return false
|
||||
return Number(bytes) >= 1024 * 1024 * 1024
|
||||
}
|
||||
|
||||
function formatNetworkShort(bytes: number | null): string {
|
||||
@@ -312,6 +318,7 @@ function formatTimeAgo(datetime: string | null): string {
|
||||
.mini-fill.critical { background: #f97316; }
|
||||
.mini-fill.danger { background: #ef4444; }
|
||||
.card-metric .value { font-size: 11px; font-weight: 500; color: var(--text-secondary); width: 36px; text-align: right; flex-shrink: 0; overflow: hidden; text-overflow: ellipsis; }
|
||||
.card-metric .value.mem-highlight { color: #dc2626; font-weight: 700; }
|
||||
|
||||
.card-stopped { font-size: 12px; color: var(--text-muted); font-style: italic; text-align: center; padding: 6px 0; }
|
||||
|
||||
|
||||
@@ -48,6 +48,9 @@
|
||||
icon="🔒"
|
||||
:status="privnetStatus"
|
||||
/>
|
||||
<LocationStatsPortlet
|
||||
:locations="locationStats"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</main>
|
||||
@@ -83,11 +86,21 @@ let loadingStartTime = 0
|
||||
const pubnetStatus = ref<any>(null)
|
||||
const privnetStatus = ref<any>(null)
|
||||
const serverDashboard = ref<any>(null)
|
||||
const locationStats = ref<any[]>([])
|
||||
|
||||
// WebSocket
|
||||
let ws: WebSocket | null = null
|
||||
let timeInterval: ReturnType<typeof window.setInterval> | null = null
|
||||
|
||||
async function fetchLocationStats() {
|
||||
try {
|
||||
const data = await $fetch('/api/server/location-stats')
|
||||
locationStats.value = data as any[]
|
||||
} catch (err) {
|
||||
console.error('Failed to fetch location stats:', err)
|
||||
}
|
||||
}
|
||||
|
||||
function formatTime(date: Date): string {
|
||||
const y = date.getFullYear()
|
||||
const m = String(date.getMonth() + 1).padStart(2, '0')
|
||||
@@ -162,6 +175,7 @@ function connectWebSocket() {
|
||||
|
||||
if (msg.type === 'server') {
|
||||
serverDashboard.value = msg.data
|
||||
fetchLocationStats()
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('[WS] Parse error:', err)
|
||||
@@ -214,6 +228,7 @@ function navigateTo(path: string) {
|
||||
onMounted(() => {
|
||||
connectWebSocket()
|
||||
updateCurrentTime()
|
||||
fetchLocationStats()
|
||||
timeInterval = window.setInterval(updateCurrentTime, 1000)
|
||||
})
|
||||
|
||||
@@ -239,7 +254,7 @@ onUnmounted(() => {
|
||||
|
||||
.dashboard-layout { display: flex; gap: 16px; height: 100%; }
|
||||
.server-section { flex: 9; min-width: 0; }
|
||||
.network-section { flex: 1; min-width: 130px; max-width: 160px; display: flex; flex-direction: column; gap: 12px; }
|
||||
.network-section { flex: 1; min-width: 140px; max-width: 180px; display: flex; flex-direction: column; gap: 10px; }
|
||||
|
||||
.connection-status { position: fixed; bottom: 16px; right: 16px; padding: 8px 12px; background: var(--bg-secondary); border: 1px solid var(--border-color); border-radius: 8px; font-size: 12px; color: var(--text-muted); }
|
||||
.connection-status.connected { color: #16a34a; }
|
||||
|
||||
Reference in New Issue
Block a user