소스 수정

This commit is contained in:
2025-12-28 17:55:55 +09:00
parent ce90c41f0c
commit fb43bf6d07
5 changed files with 389 additions and 103 deletions

View File

@@ -62,6 +62,9 @@ async function detectApiVersion(baseUrl: string, serverName: string): Promise<st
async function detectAnomalies(targetId: number, serverName: string) {
const now = timestamp()
// 절대값 변화 최소 임계값 (이 값 이하면 무시)
const MIN_ABSOLUTE_CHANGE = 5
try {
// === 단기 변화율 감지 ===
const SHORT_TERM_THRESHOLD = 30
@@ -87,8 +90,12 @@ async function detectAnomalies(targetId: number, serverName: string) {
const cpuChange = prevCpuAvg > 1 ? ((currCpuAvg - prevCpuAvg) / prevCpuAvg) * 100 : currCpuAvg - prevCpuAvg
const memChange = prevMemAvg > 1 ? ((currMemAvg - prevMemAvg) / prevMemAvg) * 100 : currMemAvg - prevMemAvg
// CPU 단기 변화율 체크 (증가만 감지)
if (cpuChange >= SHORT_TERM_THRESHOLD) {
// 절대값 변화량
const cpuAbsChange = currCpuAvg - prevCpuAvg
const memAbsChange = currMemAvg - prevMemAvg
// CPU 단기 변화율 체크 (증가만 감지, 절대값 5%p 이상일 때만)
if (cpuChange >= SHORT_TERM_THRESHOLD && cpuAbsChange >= MIN_ABSOLUTE_CHANGE) {
const recentExists = await queryOne(`
SELECT 1 FROM anomaly_logs
WHERE target_id = $1 AND detect_type = 'short-term' AND metric = 'CPU'
@@ -108,8 +115,8 @@ async function detectAnomalies(targetId: number, serverName: string) {
}
}
// Memory 단기 변화율 체크 (증가만 감지)
if (memChange >= SHORT_TERM_THRESHOLD) {
// Memory 단기 변화율 체크 (증가만 감지, 절대값 5%p 이상일 때만)
if (memChange >= SHORT_TERM_THRESHOLD && memAbsChange >= MIN_ABSOLUTE_CHANGE) {
const recentExists = await queryOne(`
SELECT 1 FROM anomaly_logs
WHERE target_id = $1 AND detect_type = 'short-term' AND metric = 'Memory'
@@ -161,8 +168,12 @@ async function detectAnomalies(targetId: number, serverName: string) {
const cpuZscore = cpuStd > 0.1 ? (currCpu - cpuAvg) / cpuStd : 0
const memZscore = memStd > 0.1 ? (currMem - memAvg) / memStd : 0
// CPU Z-Score 체크 (높은 경우만 감지)
if (cpuZscore >= WARNING_Z) {
// 절대값 변화량
const cpuAbsDiff = currCpu - cpuAvg
const memAbsDiff = currMem - memAvg
// CPU Z-Score 체크 (높은 경우만 감지, 절대값 5%p 이상일 때만)
if (cpuZscore >= WARNING_Z && cpuAbsDiff >= MIN_ABSOLUTE_CHANGE) {
const recentExists = await queryOne(`
SELECT 1 FROM anomaly_logs
WHERE target_id = $1 AND detect_type = 'zscore' AND metric = 'CPU'
@@ -182,8 +193,8 @@ async function detectAnomalies(targetId: number, serverName: string) {
}
}
// Memory Z-Score 체크 (높은 경우만 감지)
if (memZscore >= WARNING_Z) {
// Memory Z-Score 체크 (높은 경우만 감지, 절대값 5%p 이상일 때만)
if (memZscore >= WARNING_Z && memAbsDiff >= MIN_ABSOLUTE_CHANGE) {
const recentExists = await queryOne(`
SELECT 1 FROM anomaly_logs
WHERE target_id = $1 AND detect_type = 'zscore' AND metric = 'Memory'
@@ -249,8 +260,12 @@ async function detectAnomalies(targetId: number, serverName: string) {
const cpuDeviation = cpuStd > 0.1 ? (currCpu - cpuAvg) / cpuStd : 0
const memDeviation = memStd > 0.1 ? (currMem - memAvg) / memStd : 0
// CPU 베이스라인 체크 (높은 경우만 감지)
if (cpuDeviation >= DEVIATION_THRESHOLD) {
// 절대값 변화량
const cpuBaseAbsDiff = currCpu - cpuAvg
const memBaseAbsDiff = currMem - memAvg
// CPU 베이스라인 체크 (높은 경우만 감지, 절대값 5%p 이상일 때만)
if (cpuDeviation >= DEVIATION_THRESHOLD && cpuBaseAbsDiff >= MIN_ABSOLUTE_CHANGE) {
const recentExists = await queryOne(`
SELECT 1 FROM anomaly_logs
WHERE target_id = $1 AND detect_type = 'baseline' AND metric = 'CPU'
@@ -271,8 +286,8 @@ async function detectAnomalies(targetId: number, serverName: string) {
}
}
// Memory 베이스라인 체크 (높은 경우만 감지)
if (memDeviation >= DEVIATION_THRESHOLD) {
// Memory 베이스라인 체크 (높은 경우만 감지, 절대값 5%p 이상일 때만)
if (memDeviation >= DEVIATION_THRESHOLD && memBaseAbsDiff >= MIN_ABSOLUTE_CHANGE) {
const recentExists = await queryOne(`
SELECT 1 FROM anomaly_logs
WHERE target_id = $1 AND detect_type = 'baseline' AND metric = 'Memory'