Files
system-monitor/backend/api/settings/thresholds.put.ts
2025-12-28 12:03:48 +09:00

55 lines
1.6 KiB
TypeScript

import { getDb } from '../../utils/db'
export default defineEventHandler(async (event) => {
const body = await readBody(event)
if (!body || typeof body !== 'object') {
throw createError({
statusCode: 400,
message: 'Invalid request body'
})
}
const db = getDb()
const now = new Date().toLocaleString('sv-SE', { timeZone: 'Asia/Seoul' }).replace('T', ' ')
const stmt = db.prepare(`
UPDATE thresholds
SET warning = ?, critical = ?, danger = ?, updated_at = ?
WHERE category = ? AND metric = ?
`)
let updated = 0
for (const [category, metrics] of Object.entries(body)) {
if (typeof metrics !== 'object') continue
for (const [metric, values] of Object.entries(metrics as Record<string, any>)) {
if (!values || typeof values !== 'object') continue
const { warning, critical, danger } = values
// 유효성 검사
if (typeof warning !== 'number' || typeof critical !== 'number' || typeof danger !== 'number') {
continue
}
if (warning < 0 || warning > 100 || critical < 0 || critical > 100 || danger < 0 || danger > 100) {
continue
}
if (warning >= critical || critical >= danger) {
throw createError({
statusCode: 400,
message: `Invalid thresholds for ${category}.${metric}: warning < critical < danger 순서여야 합니다.`
})
}
const result = stmt.run(warning, critical, danger, now, category, metric)
if (result.changes > 0) updated++
}
}
return { success: true, updated }
})