소스 수정

This commit is contained in:
2025-12-28 16:18:02 +09:00
parent 1801f46c89
commit 56451c8070
4 changed files with 29 additions and 30 deletions

View File

@@ -6,10 +6,10 @@ export default defineEventHandler(async (event) => {
const WINDOW_MINUTES = 30 const WINDOW_MINUTES = 30
const servers = await query<any>(` const servers = await query<any>(`
SELECT target_id, name as server_name SELECT target_id, server_name
FROM server_targets FROM server_targets
WHERE is_active = 1 WHERE is_active = 1
ORDER BY name ORDER BY server_name
`) `)
const anomalies: any[] = [] const anomalies: any[] = []
@@ -17,12 +17,12 @@ export default defineEventHandler(async (event) => {
for (const server of servers) { for (const server of servers) {
const snapshots = await query<any>(` const snapshots = await query<any>(`
SELECT cpu_usage as cpu_percent, memory_usage as memory_percent, checked_at as collected_at, SELECT cpu_percent, memory_percent, collected_at,
EXTRACT(EPOCH FROM (NOW() - checked_at)) / 60 as minutes_ago EXTRACT(EPOCH FROM (NOW() - collected_at::timestamp)) / 60 as minutes_ago
FROM server_logs FROM server_snapshots
WHERE target_id = $1 AND is_success = 1 WHERE target_id = $1 AND is_online = 1
AND checked_at >= NOW() - INTERVAL '${WINDOW_MINUTES} minutes' AND collected_at::timestamp >= NOW() - INTERVAL '${WINDOW_MINUTES} minutes'
ORDER BY checked_at ASC ORDER BY collected_at ASC
`, [server.target_id]) `, [server.target_id])
if (snapshots.length < MIN_SAMPLES) { if (snapshots.length < MIN_SAMPLES) {

View File

@@ -5,10 +5,10 @@ export default defineEventHandler(async (event) => {
const DANGER_Z = 3.0 const DANGER_Z = 3.0
const servers = await query<any>(` const servers = await query<any>(`
SELECT target_id, name as server_name SELECT target_id, server_name
FROM server_targets FROM server_targets
WHERE is_active = 1 WHERE is_active = 1
ORDER BY name ORDER BY server_name
`) `)
const anomalies: any[] = [] const anomalies: any[] = []
@@ -16,11 +16,11 @@ export default defineEventHandler(async (event) => {
for (const server of servers) { for (const server of servers) {
const snapshots = await query<any>(` const snapshots = await query<any>(`
SELECT cpu_usage as cpu_percent, memory_usage as memory_percent, checked_at as collected_at SELECT cpu_percent, memory_percent, collected_at
FROM server_logs FROM server_snapshots
WHERE target_id = $1 WHERE target_id = $1
AND checked_at >= NOW() - INTERVAL '1 hour' AND collected_at::timestamp >= NOW() - INTERVAL '1 hour'
ORDER BY checked_at DESC ORDER BY collected_at DESC
`, [server.target_id]) `, [server.target_id])
if (snapshots.length < 10) { if (snapshots.length < 10) {

View File

@@ -13,13 +13,13 @@ export default defineEventHandler(async (event) => {
const snapshot = await queryOne(` const snapshot = await queryOne(`
SELECT SELECT
l.*, s.*,
t.name as server_name, t.server_name,
t.host as server_ip t.server_ip
FROM server_logs l FROM server_snapshots s
JOIN server_targets t ON l.target_id = t.target_id JOIN server_targets t ON s.target_id = t.target_id
WHERE l.target_id = $1 WHERE s.target_id = $1
ORDER BY l.checked_at DESC ORDER BY s.collected_at DESC
LIMIT 1 LIMIT 1
`, [targetId]) `, [targetId])

View File

@@ -30,16 +30,15 @@ export default defineEventHandler(async (event) => {
const snapshots = await query(` const snapshots = await query(`
SELECT SELECT
log_id, snapshot_id,
cpu_usage as cpu_percent, cpu_percent,
memory_usage as memory_percent, memory_percent,
disk_usage as disk_percent, is_online,
is_success as is_online, collected_at
checked_at as collected_at FROM server_snapshots
FROM server_logs
WHERE target_id = $1 WHERE target_id = $1
AND checked_at >= NOW() - INTERVAL '${interval}' AND collected_at::timestamp >= NOW() - INTERVAL '${interval}'
ORDER BY checked_at ASC ORDER BY collected_at ASC
`, [targetId]) `, [targetId])
return { return {