Files
Checkmate/server/src/service/infrastructure/statusService.ts
T

406 lines
13 KiB
TypeScript
Raw Normal View History

2026-02-12 22:37:40 +00:00
import { IChecksRepository, IMonitorsRepository, IMonitorStatsRepository } from "@/repositories/index.js";
2026-01-16 18:11:04 +00:00
import type {
Monitor,
2026-02-12 18:30:44 +00:00
MonitorStatus,
2026-01-16 18:11:04 +00:00
MonitorStatusResponse,
StatusChangeResult,
Check,
HardwareStatusPayload,
PageSpeedStatusPayload,
2026-01-27 18:26:31 +00:00
CheckSnapshot,
2026-02-12 22:37:40 +00:00
MonitorStats,
2026-01-16 18:11:04 +00:00
} from "@/types/index.js";
2026-02-12 22:37:40 +00:00
import { ILogger } from "@/utils/logger.js";
2025-04-20 11:29:53 -07:00
const SERVICE_NAME = "StatusService";
2026-01-18 23:38:40 +00:00
export interface IStatusService {
updateRunningStats({ monitor, networkResponse }: { monitor: Monitor; networkResponse: any }): Promise<boolean>;
handleIncidentForCheck(check: any, monitor: Monitor, action: any, errorContext?: string): Promise<void>;
updateMonitorStatus(
statusResponse: MonitorStatusResponse<PageSpeedStatusPayload | HardwareStatusPayload | undefined>,
check: Check
): Promise<StatusChangeResult>;
}
export class StatusService implements IStatusService {
2025-04-20 11:29:53 -07:00
static SERVICE_NAME = SERVICE_NAME;
2026-01-15 22:33:25 +00:00
private logger: any;
private buffer: any;
private monitorsRepository: IMonitorsRepository;
2026-02-12 22:37:40 +00:00
private monitorStatsRepository: IMonitorStatsRepository;
private checksRepository: IChecksRepository;
2026-01-15 22:33:25 +00:00
2026-02-12 22:37:40 +00:00
constructor(
logger: ILogger,
buffer: any,
monitorsRepository: IMonitorsRepository,
monitorStatsRepository: IMonitorStatsRepository,
checksRepository: IChecksRepository
) {
2025-04-20 11:29:53 -07:00
this.logger = logger;
this.buffer = buffer;
2026-01-13 20:20:00 +00:00
this.monitorsRepository = monitorsRepository;
2026-02-12 22:37:40 +00:00
this.monitorStatsRepository = monitorStatsRepository;
this.checksRepository = checksRepository;
2025-07-25 15:15:14 -07:00
}
get serviceName() {
return StatusService.SERVICE_NAME;
2025-04-20 11:29:53 -07:00
}
async updateRunningStats({ monitor, networkResponse }: { monitor: Monitor; networkResponse: MonitorStatusResponse }) {
2025-04-20 11:29:53 -07:00
try {
2026-01-13 20:20:00 +00:00
const monitorId = monitor.id;
2025-08-18 10:55:12 -07:00
const { responseTime, status } = networkResponse;
2026-02-18 00:06:02 +00:00
let existingStats: MonitorStats | null = null;
existingStats = await this.monitorStatsRepository
2026-02-13 19:04:19 +00:00
.findByMonitorId(monitorId)
.then((result) => result)
.catch(() => {
this.logger.debug({
service: SERVICE_NAME,
method: "updateRunningStats",
message: `No existing stats found for monitor ${monitorId}, initializing new stats.`,
});
return null;
});
2026-02-18 00:06:02 +00:00
let stats: Omit<MonitorStats, "id" | "monitorId" | "createdAt" | "updatedAt">;
if (!existingStats) {
// Initialize new stats
2026-02-12 22:37:40 +00:00
stats = {
2025-04-20 11:29:53 -07:00
avgResponseTime: 0,
2026-02-17 22:47:37 +00:00
maxResponseTime: 0,
2025-04-20 11:29:53 -07:00
totalChecks: 0,
totalUpChecks: 0,
totalDownChecks: 0,
uptimePercentage: 0,
2026-02-12 22:37:40 +00:00
lastResponseTime: 0,
2026-02-12 23:03:16 +00:00
lastCheckTimestamp: 0,
2026-02-12 22:37:40 +00:00
};
2026-02-18 00:06:02 +00:00
} else {
// Use existing stats (omit id, monitorId, createdAt, updatedAt)
stats = {
avgResponseTime: existingStats.avgResponseTime,
maxResponseTime: existingStats.maxResponseTime,
totalChecks: existingStats.totalChecks,
totalUpChecks: existingStats.totalUpChecks,
totalDownChecks: existingStats.totalDownChecks,
uptimePercentage: existingStats.uptimePercentage,
lastResponseTime: existingStats.lastResponseTime,
lastCheckTimestamp: existingStats.lastCheckTimestamp,
timeOfLastFailure: existingStats.timeOfLastFailure,
};
2025-04-20 11:29:53 -07:00
}
// Update stats
2026-02-17 23:40:14 +00:00
stats.totalChecks++;
2025-04-20 11:29:53 -07:00
// Last response time
stats.lastResponseTime = responseTime ?? 0;
2025-04-20 11:29:53 -07:00
2026-02-17 22:47:37 +00:00
// Max response time
if (responseTime && responseTime > stats.maxResponseTime) {
stats.maxResponseTime = responseTime;
}
2025-04-20 11:29:53 -07:00
// Avg response time:
let avgResponseTime = stats.avgResponseTime;
if (typeof responseTime !== "undefined" && responseTime !== null) {
if (avgResponseTime === 0) {
avgResponseTime = responseTime;
} else {
2025-07-21 10:34:16 -07:00
avgResponseTime = (avgResponseTime * (stats.totalChecks - 1) + responseTime) / stats.totalChecks;
2025-04-20 11:29:53 -07:00
}
}
stats.avgResponseTime = avgResponseTime;
// Total checks
if (status === true) {
stats.totalUpChecks++;
// Update the timeSinceLastFailure if needed
if (stats.timeOfLastFailure === 0) {
stats.timeOfLastFailure = new Date().getTime();
}
} else {
stats.totalDownChecks++;
stats.timeOfLastFailure = 0;
}
// Calculate uptime percentage
let uptimePercentage;
if (stats.totalChecks > 0) {
uptimePercentage = stats.totalUpChecks / stats.totalChecks;
} else {
uptimePercentage = status === true ? 100 : 0;
}
stats.uptimePercentage = uptimePercentage;
// latest check
stats.lastCheckTimestamp = new Date().getTime();
2026-02-18 00:06:02 +00:00
// Create or update
if (!existingStats) {
await this.monitorStatsRepository.create({ monitorId, ...stats });
} else {
await this.monitorStatsRepository.updateByMonitorId(monitorId, stats);
}
2025-04-20 11:29:53 -07:00
return true;
2026-01-15 22:33:25 +00:00
} catch (error: any) {
2025-04-20 11:29:53 -07:00
this.logger.error({
2026-01-15 22:33:25 +00:00
service: SERVICE_NAME,
2025-04-20 11:29:53 -07:00
message: error.message,
method: "updateRunningStats",
stack: error.stack,
});
return false;
}
}
2026-01-18 23:38:40 +00:00
handleIncidentForCheck = async (check: Check, monitor: Monitor, action: any, errorContext = "incident handling") => {
2025-11-18 16:01:47 -05:00
try {
2026-02-12 23:03:16 +00:00
let savedCheck: Check | null = null;
2026-01-18 23:38:40 +00:00
if (!check.id) {
2025-11-18 16:01:47 -05:00
try {
2026-02-12 23:03:16 +00:00
savedCheck = await this.checksRepository.create(check);
2025-11-18 16:01:47 -05:00
this.buffer.removeCheckFromBuffer(check);
2026-01-15 22:33:25 +00:00
} catch (checkError: any) {
2025-11-18 16:01:47 -05:00
this.logger.error({
2026-01-15 22:33:25 +00:00
service: SERVICE_NAME,
2025-11-18 16:01:47 -05:00
method: "handleIncidentForCheck",
message: `Failed to save check immediately for ${errorContext}: ${checkError.message}`,
2026-01-13 20:20:00 +00:00
monitorId: monitor.id,
2025-11-18 16:01:47 -05:00
stack: checkError.stack,
});
savedCheck = null;
}
}
2026-01-18 23:38:40 +00:00
if (savedCheck && savedCheck.id) {
2025-11-18 16:01:47 -05:00
try {
this.buffer.addIncidentToBuffer({ monitor, check: savedCheck, action });
2026-01-15 22:33:25 +00:00
} catch (incidentError: any) {
2025-11-18 16:01:47 -05:00
this.logger.error({
2026-01-15 22:33:25 +00:00
service: SERVICE_NAME,
2025-11-18 16:01:47 -05:00
method: "handleIncidentForCheck",
message: `Failed to add incident to buffer for ${errorContext}: ${incidentError.message}`,
2026-01-13 20:20:00 +00:00
monitorId: monitor.id,
2025-11-18 16:01:47 -05:00
action,
stack: incidentError.stack,
});
}
}
2026-01-15 22:33:25 +00:00
} catch (error: any) {
2025-11-18 16:01:47 -05:00
this.logger.error({
2026-01-15 22:33:25 +00:00
service: SERVICE_NAME,
2025-11-18 16:01:47 -05:00
method: "handleIncidentForCheck",
message: `Error in ${errorContext}: ${error.message}`,
2026-01-13 20:20:00 +00:00
monitorId: monitor?.id,
2025-11-18 16:01:47 -05:00
stack: error.stack,
});
}
};
2026-01-15 22:33:25 +00:00
2026-01-15 23:45:40 +00:00
updateMonitorStatus = async (
2026-01-16 18:11:04 +00:00
statusResponse: MonitorStatusResponse<PageSpeedStatusPayload | HardwareStatusPayload | undefined>,
check: Check
2026-01-15 23:45:40 +00:00
): Promise<StatusChangeResult> => {
2025-04-20 11:29:53 -07:00
try {
2026-01-15 23:45:40 +00:00
const { monitorId, teamId, status, code } = statusResponse;
2026-01-15 20:55:01 +00:00
const monitor = await this.monitorsRepository.findById(monitorId, teamId);
2025-05-19 12:28:18 -07:00
2025-04-20 11:29:53 -07:00
// Update running stats
2026-01-15 23:45:40 +00:00
this.updateRunningStats({ monitor, networkResponse: statusResponse });
2025-05-19 12:28:18 -07:00
2026-01-18 23:38:40 +00:00
monitor.statusWindow = monitor.statusWindow || [];
monitor.statusWindow.push(status);
2026-01-18 23:38:40 +00:00
while (monitor.statusWindow.length > monitor.statusWindowSize) {
monitor.statusWindow.shift();
}
2026-01-27 18:26:31 +00:00
const checkSnapshot: CheckSnapshot = {
id: check.id,
status: check.status,
responseTime: check.responseTime,
timings: check.timings,
statusCode: check.statusCode,
message: check.message,
cpu: check.cpu,
memory: check.memory,
disk: check.disk,
host: check.host,
errors: check.errors,
capture: check.capture,
net: check.net,
accessibility: check.accessibility,
bestPractices: check.bestPractices,
seo: check.seo,
performance: check.performance,
audits: check.audits,
createdAt: check.createdAt,
};
monitor.recentChecks = monitor.recentChecks || [];
monitor.recentChecks.push(checkSnapshot);
const maxRecentChecks = 25;
while (monitor.recentChecks.length > maxRecentChecks) {
monitor.recentChecks.shift();
}
2026-01-18 23:38:40 +00:00
const prevStatus = monitor.status;
2026-02-12 18:30:44 +00:00
let newStatus: MonitorStatus = status === true ? "up" : "down";
let statusChanged = false;
// Return early if not enough data points
if (monitor.statusWindow.length < monitor.statusWindowSize) {
2026-02-12 18:30:44 +00:00
monitor.status = newStatus;
2026-01-15 18:33:03 +00:00
const updated = await this.monitorsRepository.updateById(monitor.id, monitor.teamId, monitor);
2025-04-20 11:29:53 -07:00
return {
2026-01-13 20:20:00 +00:00
monitor: updated,
2025-04-20 11:29:53 -07:00
statusChanged: false,
prevStatus,
2025-04-20 11:29:53 -07:00
code,
timestamp: Date.now(),
2025-04-20 11:29:53 -07:00
};
}
2025-05-19 12:28:18 -07:00
// Check if threshold has been met
const failures = monitor.statusWindow.filter((s) => s === false).length;
const failureRate = (failures / monitor.statusWindow.length) * 100;
// If threshold has been met and the monitor is not already down, mark down:
if (failureRate >= monitor.statusWindowThreshold && monitor.status !== "down") {
newStatus = "down";
statusChanged = true;
}
// If the failure rate is below the threshold and the monitor is down, recover:
else if (failureRate < monitor.statusWindowThreshold && monitor.status === "down") {
newStatus = "up";
statusChanged = true;
}
2026-02-14 22:14:51 +00:00
// Evaluate hardware threshold breaches (only for hardware monitors)
let thresholdBreaches: { cpu: boolean; memory: boolean; disk: boolean; temp: boolean } | undefined;
if (monitor.type === "hardware" && statusResponse.payload) {
const payload = statusResponse.payload as HardwareStatusPayload;
const metrics = payload?.data;
if (metrics) {
// Evaluate threshold breaches
const cpuUsage = metrics.cpu?.usage_percent ?? -1;
const cpuBreach = cpuUsage !== -1 && cpuUsage > monitor.cpuAlertThreshold / 100;
const memoryUsage = metrics.memory?.usage_percent ?? -1;
const memoryBreach = memoryUsage !== -1 && memoryUsage > monitor.memoryAlertThreshold / 100;
const diskBreach =
metrics.disk?.some((d: any) => typeof d?.usage_percent === "number" && d.usage_percent > monitor.diskAlertThreshold / 100) ?? false;
const temps = metrics.cpu?.temperature ?? [];
const tempBreach = temps.some((temp: number) => temp > monitor.tempAlertThreshold);
thresholdBreaches = {
cpu: cpuBreach,
memory: memoryBreach,
disk: diskBreach,
temp: tempBreach,
};
// Update counters: decrement if breached, reset to 5 if not breached
if (cpuBreach) {
monitor.cpuAlertCounter = Math.max(0, monitor.cpuAlertCounter - 1);
} else {
monitor.cpuAlertCounter = 5;
}
if (memoryBreach) {
monitor.memoryAlertCounter = Math.max(0, monitor.memoryAlertCounter - 1);
} else {
monitor.memoryAlertCounter = 5;
}
if (diskBreach) {
monitor.diskAlertCounter = Math.max(0, monitor.diskAlertCounter - 1);
} else {
monitor.diskAlertCounter = 5;
}
if (tempBreach) {
monitor.tempAlertCounter = Math.max(0, monitor.tempAlertCounter - 1);
} else {
monitor.tempAlertCounter = 5;
}
2026-02-14 22:52:14 +00:00
// Check if any counter has reached zero (initial breach)
const anyCounterZero =
monitor.cpuAlertCounter === 0 || monitor.memoryAlertCounter === 0 || monitor.diskAlertCounter === 0 || monitor.tempAlertCounter === 0;
const anyThresholdBreached = cpuBreach || memoryBreach || diskBreach || tempBreach;
const allThresholdsNormal = !cpuBreach && !memoryBreach && !diskBreach && !tempBreach;
// Update monitor status based on threshold breach state
if (newStatus !== "down") {
// Don't override "down" status - service unreachable takes precedence
// Check current monitor status, not newStatus for comparison
if (anyCounterZero && anyThresholdBreached && monitor.status !== "breached") {
// Initial breach: counter hit zero, change status to breached
newStatus = "breached";
statusChanged = true;
} else if (anyCounterZero && anyThresholdBreached && monitor.status === "breached") {
// Already breached, keep status but don't mark as changed
newStatus = "breached";
// statusChanged remains false
} else if (allThresholdsNormal && monitor.status === "breached") {
// All thresholds returned to normal, recover from breached state
newStatus = "up";
statusChanged = true;
}
}
2026-02-14 22:14:51 +00:00
}
}
2026-02-14 22:52:14 +00:00
// Apply the final status
monitor.status = newStatus;
2026-01-15 18:33:03 +00:00
const updated = await this.monitorsRepository.updateById(monitor.id, monitor.teamId, monitor);
2025-05-19 12:28:18 -07:00
2025-04-20 11:29:53 -07:00
return {
2026-01-13 20:20:00 +00:00
monitor: updated,
statusChanged,
2025-04-20 11:29:53 -07:00
prevStatus,
code,
timestamp: new Date().getTime(),
2026-02-14 22:14:51 +00:00
thresholdBreaches,
2025-04-20 11:29:53 -07:00
};
2026-01-15 22:33:25 +00:00
} catch (error: any) {
error.service = SERVICE_NAME;
2025-05-19 12:28:18 -07:00
error.method = "updateStatus";
2025-04-20 11:29:53 -07:00
throw error;
}
};
2026-01-16 18:11:04 +00:00
insertCheck = async (check: Check) => {
2025-04-20 11:29:53 -07:00
try {
if (typeof check === "undefined") {
this.logger.warn({
message: "Failed to build check",
2026-01-15 22:33:25 +00:00
service: SERVICE_NAME,
2025-04-20 11:29:53 -07:00
method: "insertCheck",
});
2025-08-18 10:55:12 -07:00
return false;
2025-04-20 11:29:53 -07:00
}
2026-02-17 22:27:52 +00:00
this.buffer.addToBuffer(check);
2025-08-18 10:55:12 -07:00
return true;
2026-01-15 22:33:25 +00:00
} catch (error: any) {
2025-04-20 11:29:53 -07:00
this.logger.error({
message: error.message,
2026-01-15 22:33:25 +00:00
service: error.service || SERVICE_NAME,
2025-06-06 14:05:59 +08:00
method: error.method || "insertCheck",
2026-01-16 18:11:04 +00:00
details: error.details || `Error inserting check for monitor: ${check?.metadata.monitorId}`,
2025-04-20 11:29:53 -07:00
stack: error.stack,
});
}
};
}