queue and buffer refactor
This commit is contained in:
1 parent
0f764ca084
commit
95cd33ac06
10 files changed
+218
-287
No files matched your search
@@ -183,7 +183,17 @@ export const initializeServices = async ({
|
|||||||
stringService,
|
stringService,
|
||||||
});
|
});
|
||||||
|
|
||||||
const bufferService = new BufferService({ db, logger, envSettings, incidentService });
|
const checkService = new CheckService({
|
||||||
|
db,
|
||||||
|
settingsService,
|
||||||
|
stringService,
|
||||||
|
errorService,
|
||||||
|
monitorsRepository,
|
||||||
|
logger,
|
||||||
|
checksRepository,
|
||||||
|
});
|
||||||
|
|
||||||
|
const bufferService = new BufferService({ db, logger, envSettings, incidentService, checkService });
|
||||||
|
|
||||||
const statusService = new StatusService({ db, logger, buffer: bufferService, incidentService, monitorsRepository });
|
const statusService = new StatusService({ db, logger, buffer: bufferService, incidentService, monitorsRepository });
|
||||||
|
|
||||||
@@ -208,6 +218,8 @@ export const initializeServices = async ({
|
|||||||
networkService,
|
networkService,
|
||||||
statusService,
|
statusService,
|
||||||
notificationService,
|
notificationService,
|
||||||
|
checkService,
|
||||||
|
buffer: bufferService,
|
||||||
});
|
});
|
||||||
|
|
||||||
const superSimpleQueue = await SuperSimpleQueue.create({
|
const superSimpleQueue = await SuperSimpleQueue.create({
|
||||||
@@ -229,13 +241,7 @@ export const initializeServices = async ({
|
|||||||
jobQueue: superSimpleQueue,
|
jobQueue: superSimpleQueue,
|
||||||
monitorsRepository,
|
monitorsRepository,
|
||||||
});
|
});
|
||||||
const checkService = new CheckService({
|
|
||||||
db,
|
|
||||||
settingsService,
|
|
||||||
stringService,
|
|
||||||
errorService,
|
|
||||||
monitorsRepository,
|
|
||||||
});
|
|
||||||
const diagnosticService = new DiagnosticService();
|
const diagnosticService = new DiagnosticService();
|
||||||
const inviteService = new InviteService({
|
const inviteService = new InviteService({
|
||||||
db,
|
db,
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import type { Check, CheckAudits, MonitorStatusResponse, MonitorType } from "@/types/index.js";
|
import type { Check, MonitorType } from "@/types/index.js";
|
||||||
import type { LatestChecksMap } from "@/repositories/checks/MongoChecksRepistory.js";
|
import type { LatestChecksMap } from "@/repositories/checks/MongoChecksRepistory.js";
|
||||||
|
|
||||||
export interface PageSpeedChecksResult {
|
export interface PageSpeedChecksResult {
|
||||||
@@ -55,7 +55,8 @@ export interface UptimeChecksResult {
|
|||||||
|
|
||||||
export interface IChecksRepository {
|
export interface IChecksRepository {
|
||||||
// create
|
// create
|
||||||
buildCheck(status: MonitorStatusResponse, type: MonitorType): Check;
|
createChecks(checks: Check[]): Promise<Check[]>;
|
||||||
|
|
||||||
// single fetch
|
// single fetch
|
||||||
// collection fetch
|
// collection fetch
|
||||||
findLatestChecksByMonitorIds(monitorIds: string[], options?: { limitPerMonitor?: number }): Promise<LatestChecksMap>;
|
findLatestChecksByMonitorIds(monitorIds: string[], options?: { limitPerMonitor?: number }): Promise<LatestChecksMap>;
|
||||||
|
|||||||
@@ -11,10 +11,8 @@ import type {
|
|||||||
CheckMetadata,
|
CheckMetadata,
|
||||||
CheckNetworkInterfaceInfo,
|
CheckNetworkInterfaceInfo,
|
||||||
GotTimings,
|
GotTimings,
|
||||||
MonitorStatusResponse,
|
|
||||||
MonitorType,
|
MonitorType,
|
||||||
} from "@/types/index.js";
|
} from "@/types/index.js";
|
||||||
import type { HardwareStatusPayload, PageSpeedStatusPayload } from "@/types/network.js";
|
|
||||||
import { CheckModel, type CheckDocument } from "@/db/models/index.js";
|
import { CheckModel, type CheckDocument } from "@/db/models/index.js";
|
||||||
import mongoose from "mongoose";
|
import mongoose from "mongoose";
|
||||||
|
|
||||||
@@ -144,11 +142,11 @@ class MongoChecksRepository implements IChecksRepository {
|
|||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
cls: audits.cls ?? 0,
|
cls: audits.cls,
|
||||||
si: audits.si ?? 0,
|
si: audits.si,
|
||||||
fcp: audits.fcp ?? 0,
|
fcp: audits.fcp,
|
||||||
lcp: audits.lcp ?? 0,
|
lcp: audits.lcp,
|
||||||
tbt: audits.tbt ?? 0,
|
tbt: audits.tbt,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -187,100 +185,8 @@ class MongoChecksRepository implements IChecksRepository {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
buildCheck = (
|
createChecks = async (checks: Check[]) => {
|
||||||
statusResponse: MonitorStatusResponse<PageSpeedStatusPayload | HardwareStatusPayload | undefined>
|
return await CheckModel.insertMany(checks);
|
||||||
) => {
|
|
||||||
const {
|
|
||||||
monitorId,
|
|
||||||
teamId,
|
|
||||||
type,
|
|
||||||
status,
|
|
||||||
responseTime,
|
|
||||||
code,
|
|
||||||
message,
|
|
||||||
payload,
|
|
||||||
first_byte_took,
|
|
||||||
body_read_took,
|
|
||||||
dns_took,
|
|
||||||
conn_took,
|
|
||||||
connect_took,
|
|
||||||
tls_took,
|
|
||||||
timings,
|
|
||||||
} = statusResponse;
|
|
||||||
|
|
||||||
const check: any = {
|
|
||||||
metadata: {
|
|
||||||
monitorId,
|
|
||||||
teamId,
|
|
||||||
type,
|
|
||||||
},
|
|
||||||
status,
|
|
||||||
statusCode: code,
|
|
||||||
responseTime,
|
|
||||||
timings: timings || {},
|
|
||||||
message,
|
|
||||||
first_byte_took,
|
|
||||||
body_read_took,
|
|
||||||
dns_took,
|
|
||||||
conn_took,
|
|
||||||
connect_took,
|
|
||||||
tls_took,
|
|
||||||
};
|
|
||||||
|
|
||||||
if (type === "pagespeed") {
|
|
||||||
const pageSpeedPayload = payload as PageSpeedStatusPayload | undefined;
|
|
||||||
if (!pageSpeedPayload) {
|
|
||||||
this.logger.warn({
|
|
||||||
message: "Failed to build check",
|
|
||||||
service: SERVICE_NAME,
|
|
||||||
method: "buildCheck",
|
|
||||||
details: "empty payload",
|
|
||||||
});
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
const categories = pageSpeedPayload.lighthouseResult?.categories ?? {};
|
|
||||||
const audits = pageSpeedPayload.lighthouseResult?.audits ?? {};
|
|
||||||
const mapAudit = (audit: any) => {
|
|
||||||
if (!audit || typeof audit !== "object") {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
id: audit.id,
|
|
||||||
title: audit.title,
|
|
||||||
score: typeof audit.score === "number" ? audit.score : (audit.score ?? null),
|
|
||||||
displayValue: audit.displayValue,
|
|
||||||
numericValue: typeof audit.numericValue === "number" ? audit.numericValue : undefined,
|
|
||||||
numericUnit: audit.numericUnit,
|
|
||||||
};
|
|
||||||
};
|
|
||||||
check.accessibility = (categories?.accessibility?.score || 0) * 100;
|
|
||||||
check.bestPractices = (categories?.["best-practices"]?.score || 0) * 100;
|
|
||||||
check.seo = (categories?.seo?.score || 0) * 100;
|
|
||||||
check.performance = (categories?.performance?.score || 0) * 100;
|
|
||||||
check.audits = {
|
|
||||||
cls: mapAudit(audits?.["cumulative-layout-shift"]),
|
|
||||||
si: mapAudit(audits?.["speed-index"]),
|
|
||||||
fcp: mapAudit(audits?.["first-contentful-paint"]),
|
|
||||||
lcp: mapAudit(audits?.["largest-contentful-paint"]),
|
|
||||||
tbt: mapAudit(audits?.["total-blocking-time"]),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (type === "hardware") {
|
|
||||||
const hardwarePayload = payload as HardwareStatusPayload | undefined;
|
|
||||||
const { cpu, memory, disk, host, net } = hardwarePayload?.data ?? {};
|
|
||||||
const errorsSource = Array.isArray(hardwarePayload?.errors)
|
|
||||||
? hardwarePayload?.errors
|
|
||||||
: (hardwarePayload?.errors as { errors?: CheckErrorInfo[] } | undefined)?.errors;
|
|
||||||
check.cpu = cpu ?? {};
|
|
||||||
check.memory = memory ?? {};
|
|
||||||
check.disk = disk ?? {};
|
|
||||||
check.host = host ?? {};
|
|
||||||
check.errors = errorsSource ?? [];
|
|
||||||
check.capture = hardwarePayload?.capture ?? {};
|
|
||||||
check.net = net ?? {};
|
|
||||||
}
|
|
||||||
return check;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
findLatestChecksByMonitorIds = async (monitorIds: string[], options?: { limitPerMonitor?: number }): Promise<LatestChecksMap> => {
|
findLatestChecksByMonitorIds = async (monitorIds: string[], options?: { limitPerMonitor?: number }): Promise<LatestChecksMap> => {
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { IMonitorsRepository } from "@/repositories/index.js";
|
import { IChecksRepository, IMonitorsRepository } from "@/repositories/index.js";
|
||||||
import type { MonitorType } from "@/types/index.js";
|
import type { MonitorType, MonitorStatusResponse, CheckErrorInfo, Check } from "@/types/index.js";
|
||||||
|
import type { HardwareStatusPayload, PageSpeedStatusPayload } from "@/types/network.js";
|
||||||
|
|
||||||
const SERVICE_NAME = "checkService";
|
const SERVICE_NAME = "checkService";
|
||||||
|
|
||||||
@@ -11,32 +12,113 @@ class CheckService {
|
|||||||
private stringService: any;
|
private stringService: any;
|
||||||
private errorService: any;
|
private errorService: any;
|
||||||
private monitorsRepository: IMonitorsRepository;
|
private monitorsRepository: IMonitorsRepository;
|
||||||
|
private checksRepository: IChecksRepository;
|
||||||
|
private logger: any;
|
||||||
constructor({
|
constructor({
|
||||||
db,
|
db,
|
||||||
settingsService,
|
settingsService,
|
||||||
stringService,
|
stringService,
|
||||||
errorService,
|
errorService,
|
||||||
monitorsRepository,
|
monitorsRepository,
|
||||||
|
logger,
|
||||||
|
checksRepository,
|
||||||
}: {
|
}: {
|
||||||
db: any;
|
db: any;
|
||||||
settingsService: any;
|
settingsService: any;
|
||||||
stringService: any;
|
stringService: any;
|
||||||
errorService: any;
|
errorService: any;
|
||||||
monitorsRepository: IMonitorsRepository;
|
monitorsRepository: IMonitorsRepository;
|
||||||
|
logger: any;
|
||||||
|
checksRepository: IChecksRepository;
|
||||||
}) {
|
}) {
|
||||||
this.db = db;
|
this.db = db;
|
||||||
this.settingsService = settingsService;
|
this.settingsService = settingsService;
|
||||||
this.stringService = stringService;
|
this.stringService = stringService;
|
||||||
this.errorService = errorService;
|
this.errorService = errorService;
|
||||||
this.monitorsRepository = monitorsRepository;
|
this.monitorsRepository = monitorsRepository;
|
||||||
|
this.logger = logger;
|
||||||
|
this.checksRepository = checksRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
get serviceName() {
|
get serviceName() {
|
||||||
return CheckService.SERVICE_NAME;
|
return CheckService.SERVICE_NAME;
|
||||||
}
|
}
|
||||||
|
|
||||||
buildCheck = async (statusResponse: any, type: MonitorType) => {};
|
createChecks = async (checks: Check[]) => {
|
||||||
|
return this.checksRepository.createChecks(checks);
|
||||||
|
};
|
||||||
|
|
||||||
|
buildCheck = (statusResponse: MonitorStatusResponse<PageSpeedStatusPayload | HardwareStatusPayload | undefined>) => {
|
||||||
|
const { monitorId, teamId, type, status, responseTime, code, message, payload, timings } = statusResponse;
|
||||||
|
|
||||||
|
const check: Partial<Check> = {
|
||||||
|
metadata: {
|
||||||
|
monitorId,
|
||||||
|
teamId,
|
||||||
|
type,
|
||||||
|
},
|
||||||
|
status,
|
||||||
|
statusCode: code,
|
||||||
|
responseTime: responseTime || 0,
|
||||||
|
timings: timings,
|
||||||
|
message,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (type === "pagespeed") {
|
||||||
|
const pageSpeedPayload = payload as PageSpeedStatusPayload | undefined;
|
||||||
|
if (!pageSpeedPayload) {
|
||||||
|
this.logger.warn({
|
||||||
|
message: "Failed to build check",
|
||||||
|
service: SERVICE_NAME,
|
||||||
|
method: "buildCheck",
|
||||||
|
details: "empty payload",
|
||||||
|
});
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
const categories = pageSpeedPayload.lighthouseResult?.categories ?? {};
|
||||||
|
const audits = pageSpeedPayload.lighthouseResult?.audits ?? {};
|
||||||
|
const mapAudit = (audit: any) => {
|
||||||
|
if (!audit || typeof audit !== "object") {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
id: audit.id,
|
||||||
|
title: audit.title,
|
||||||
|
score: typeof audit.score === "number" ? audit.score : (audit.score ?? null),
|
||||||
|
displayValue: audit.displayValue,
|
||||||
|
numericValue: typeof audit.numericValue === "number" ? audit.numericValue : undefined,
|
||||||
|
numericUnit: audit.numericUnit,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
check.accessibility = (categories?.accessibility?.score || 0) * 100;
|
||||||
|
check.bestPractices = (categories?.["best-practices"]?.score || 0) * 100;
|
||||||
|
check.seo = (categories?.seo?.score || 0) * 100;
|
||||||
|
check.performance = (categories?.performance?.score || 0) * 100;
|
||||||
|
check.audits = {
|
||||||
|
cls: mapAudit(audits?.["cumulative-layout-shift"]),
|
||||||
|
si: mapAudit(audits?.["speed-index"]),
|
||||||
|
fcp: mapAudit(audits?.["first-contentful-paint"]),
|
||||||
|
lcp: mapAudit(audits?.["largest-contentful-paint"]),
|
||||||
|
tbt: mapAudit(audits?.["total-blocking-time"]),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (type === "hardware") {
|
||||||
|
const hardwarePayload = payload as HardwareStatusPayload | undefined;
|
||||||
|
const { cpu, memory, disk, host, net } = hardwarePayload?.data ?? {};
|
||||||
|
const errorsSource = Array.isArray(hardwarePayload?.errors)
|
||||||
|
? hardwarePayload?.errors
|
||||||
|
: (hardwarePayload?.errors as { errors?: CheckErrorInfo[] } | undefined)?.errors;
|
||||||
|
check.cpu = cpu;
|
||||||
|
check.memory = memory;
|
||||||
|
check.disk = disk;
|
||||||
|
check.host = host;
|
||||||
|
check.errors = errorsSource;
|
||||||
|
check.capture = hardwarePayload?.capture;
|
||||||
|
check.net = net;
|
||||||
|
}
|
||||||
|
return check;
|
||||||
|
};
|
||||||
|
|
||||||
getChecksByMonitor = async ({ monitorId, query, teamId }: { monitorId: string; query: any; teamId: string }) => {
|
getChecksByMonitor = async ({ monitorId, query, teamId }: { monitorId: string; query: any; teamId: string }) => {
|
||||||
if (!monitorId) {
|
if (!monitorId) {
|
||||||
|
|||||||
@@ -10,6 +10,8 @@ class SuperSimpleQueueHelper {
|
|||||||
private networkService: INetworkService;
|
private networkService: INetworkService;
|
||||||
private statusService: any;
|
private statusService: any;
|
||||||
private notificationService: any;
|
private notificationService: any;
|
||||||
|
private checkService: any;
|
||||||
|
private buffer: any;
|
||||||
|
|
||||||
constructor({
|
constructor({
|
||||||
db,
|
db,
|
||||||
@@ -17,18 +19,24 @@ class SuperSimpleQueueHelper {
|
|||||||
networkService,
|
networkService,
|
||||||
statusService,
|
statusService,
|
||||||
notificationService,
|
notificationService,
|
||||||
|
checkService,
|
||||||
|
buffer,
|
||||||
}: {
|
}: {
|
||||||
db: any;
|
db: any;
|
||||||
logger: any;
|
logger: any;
|
||||||
networkService: INetworkService;
|
networkService: INetworkService;
|
||||||
statusService: any;
|
statusService: any;
|
||||||
notificationService: any;
|
notificationService: any;
|
||||||
|
checkService: any;
|
||||||
|
buffer: any;
|
||||||
}) {
|
}) {
|
||||||
this.db = db;
|
this.db = db;
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
this.networkService = networkService;
|
this.networkService = networkService;
|
||||||
this.statusService = statusService;
|
this.statusService = statusService;
|
||||||
this.notificationService = notificationService;
|
this.notificationService = notificationService;
|
||||||
|
this.checkService = checkService;
|
||||||
|
this.buffer = buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
get serviceName() {
|
get serviceName() {
|
||||||
@@ -61,7 +69,15 @@ class SuperSimpleQueueHelper {
|
|||||||
throw new Error("No network response");
|
throw new Error("No network response");
|
||||||
}
|
}
|
||||||
|
|
||||||
const statusChangeResult = await this.statusService.updateMonitorStatus(status);
|
// Step 3. Build check
|
||||||
|
const check = await this.checkService.buildCheck(status);
|
||||||
|
|
||||||
|
// Step 4 Add check to buffer
|
||||||
|
this.buffer.addToBuffer({ check });
|
||||||
|
|
||||||
|
// Step 4. Update monitor status
|
||||||
|
const statusChangeResult = await this.statusService.updateMonitorStatus(status, check);
|
||||||
|
|
||||||
this.notificationService
|
this.notificationService
|
||||||
.handleNotifications({
|
.handleNotifications({
|
||||||
...status,
|
...status,
|
||||||
|
|||||||
+41
-18
@@ -1,15 +1,38 @@
|
|||||||
import { config } from "@/config/index.js";
|
import { config } from "@/config/index.js";
|
||||||
|
import type { Check } from "@/types/index.js";
|
||||||
|
|
||||||
const SERVICE_NAME = "BufferService";
|
const SERVICE_NAME = "BufferService";
|
||||||
|
|
||||||
class BufferService {
|
class BufferService {
|
||||||
static SERVICE_NAME = SERVICE_NAME;
|
static SERVICE_NAME = SERVICE_NAME;
|
||||||
constructor({ db, logger, envSettings, incidentService }) {
|
private BUFFER_TIMEOUT: number;
|
||||||
console.log(envSettings);
|
private db: any;
|
||||||
|
private logger: any;
|
||||||
|
private incidentService: any;
|
||||||
|
private SERVICE_NAME: string;
|
||||||
|
private buffer: any[];
|
||||||
|
private incidentBuffer: any[];
|
||||||
|
private bufferTimer: NodeJS.Timeout;
|
||||||
|
private checksService: any;
|
||||||
|
|
||||||
|
constructor({
|
||||||
|
db,
|
||||||
|
logger,
|
||||||
|
envSettings,
|
||||||
|
incidentService,
|
||||||
|
checkService,
|
||||||
|
}: {
|
||||||
|
db: any;
|
||||||
|
logger: any;
|
||||||
|
envSettings: any;
|
||||||
|
incidentService: any;
|
||||||
|
checkService: any;
|
||||||
|
}) {
|
||||||
this.BUFFER_TIMEOUT = config.NODE_ENV === "development" ? 10 : 1000 * 60 * 1; // 1 minute
|
this.BUFFER_TIMEOUT = config.NODE_ENV === "development" ? 10 : 1000 * 60 * 1; // 1 minute
|
||||||
this.db = db;
|
this.db = db;
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
this.incidentService = incidentService;
|
this.incidentService = incidentService;
|
||||||
|
this.checksService = checkService;
|
||||||
this.SERVICE_NAME = SERVICE_NAME;
|
this.SERVICE_NAME = SERVICE_NAME;
|
||||||
this.buffer = [];
|
this.buffer = [];
|
||||||
this.incidentBuffer = [];
|
this.incidentBuffer = [];
|
||||||
@@ -25,10 +48,10 @@ class BufferService {
|
|||||||
return BufferService.SERVICE_NAME;
|
return BufferService.SERVICE_NAME;
|
||||||
}
|
}
|
||||||
|
|
||||||
addToBuffer({ check }) {
|
addToBuffer({ check }: { check: Check }) {
|
||||||
try {
|
try {
|
||||||
this.buffer.push(check);
|
this.buffer.push(check);
|
||||||
} catch (error) {
|
} catch (error: any) {
|
||||||
this.logger.error({
|
this.logger.error({
|
||||||
message: error.message,
|
message: error.message,
|
||||||
service: this.SERVICE_NAME,
|
service: this.SERVICE_NAME,
|
||||||
@@ -38,7 +61,7 @@ class BufferService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
addIncidentToBuffer({ monitor, check, action = "create" }) {
|
addIncidentToBuffer({ monitor, check, action = "create" }: { monitor: any; check: Check; action?: string }) {
|
||||||
try {
|
try {
|
||||||
if (!monitor || !check) {
|
if (!monitor || !check) {
|
||||||
this.logger.warn({
|
this.logger.warn({
|
||||||
@@ -50,7 +73,7 @@ class BufferService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.incidentBuffer.push({ monitor, check, action });
|
this.incidentBuffer.push({ monitor, check, action });
|
||||||
} catch (error) {
|
} catch (error: any) {
|
||||||
this.logger.error({
|
this.logger.error({
|
||||||
message: error.message,
|
message: error.message,
|
||||||
service: this.SERVICE_NAME,
|
service: this.SERVICE_NAME,
|
||||||
@@ -60,20 +83,20 @@ class BufferService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
removeCheckFromBuffer(checkToRemove) {
|
removeCheckFromBuffer(checkToRemove: Check) {
|
||||||
try {
|
try {
|
||||||
if (!checkToRemove) {
|
if (!checkToRemove) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const index = this.buffer.findIndex((check) => {
|
const index = this.buffer.findIndex((check) => {
|
||||||
if (checkToRemove._id && check._id) {
|
if (checkToRemove.id && check.id) {
|
||||||
return check._id.toString() === checkToRemove._id.toString();
|
return check.id.toString() === checkToRemove.id.toString();
|
||||||
}
|
}
|
||||||
return (
|
return (
|
||||||
check.monitorId?.toString() === checkToRemove.monitorId?.toString() &&
|
check.monitorId?.toString() === checkToRemove.metadata.monitorId &&
|
||||||
check.teamId?.toString() === checkToRemove.teamId?.toString() &&
|
check.teamId?.toString() === checkToRemove.metadata.teamId &&
|
||||||
check.type === checkToRemove.type &&
|
check.type === checkToRemove.metadata.type &&
|
||||||
check.status === checkToRemove.status &&
|
check.status === checkToRemove.status &&
|
||||||
check.statusCode === checkToRemove.statusCode &&
|
check.statusCode === checkToRemove.statusCode &&
|
||||||
check.responseTime === checkToRemove.responseTime &&
|
check.responseTime === checkToRemove.responseTime &&
|
||||||
@@ -87,7 +110,7 @@ class BufferService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
} catch (error) {
|
} catch (error: any) {
|
||||||
this.logger.error({
|
this.logger.error({
|
||||||
message: error.message,
|
message: error.message,
|
||||||
service: this.SERVICE_NAME,
|
service: this.SERVICE_NAME,
|
||||||
@@ -102,7 +125,7 @@ class BufferService {
|
|||||||
this.bufferTimer = setTimeout(async () => {
|
this.bufferTimer = setTimeout(async () => {
|
||||||
try {
|
try {
|
||||||
await this.flushBuffer();
|
await this.flushBuffer();
|
||||||
} catch (error) {
|
} catch (error: any) {
|
||||||
this.logger.error({
|
this.logger.error({
|
||||||
message: `Error in flush cycle: ${error.message}`,
|
message: `Error in flush cycle: ${error.message}`,
|
||||||
service: this.SERVICE_NAME,
|
service: this.SERVICE_NAME,
|
||||||
@@ -118,9 +141,9 @@ class BufferService {
|
|||||||
async flushBuffer() {
|
async flushBuffer() {
|
||||||
try {
|
try {
|
||||||
if (this.buffer.length > 0) {
|
if (this.buffer.length > 0) {
|
||||||
await this.db.checkModule.createChecks(this.buffer);
|
await this.checksService.createChecks(this.buffer);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error: any) {
|
||||||
this.logger.error({
|
this.logger.error({
|
||||||
message: error.message,
|
message: error.message,
|
||||||
service: this.SERVICE_NAME,
|
service: this.SERVICE_NAME,
|
||||||
@@ -133,7 +156,7 @@ class BufferService {
|
|||||||
if (this.incidentBuffer.length > 0 && this.incidentService) {
|
if (this.incidentBuffer.length > 0 && this.incidentService) {
|
||||||
await this.flushIncidentBuffer();
|
await this.flushIncidentBuffer();
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error: any) {
|
||||||
this.logger.error({
|
this.logger.error({
|
||||||
message: error.message,
|
message: error.message,
|
||||||
service: this.SERVICE_NAME,
|
service: this.SERVICE_NAME,
|
||||||
@@ -154,7 +177,7 @@ class BufferService {
|
|||||||
try {
|
try {
|
||||||
const itemsToProcess = [...this.incidentBuffer];
|
const itemsToProcess = [...this.incidentBuffer];
|
||||||
await this.incidentService.processIncidentsFromBuffer(itemsToProcess);
|
await this.incidentService.processIncidentsFromBuffer(itemsToProcess);
|
||||||
} catch (error) {
|
} catch (error: any) {
|
||||||
this.logger.error({
|
this.logger.error({
|
||||||
message: `Error flushing incident buffer: ${error.message}`,
|
message: `Error flushing incident buffer: ${error.message}`,
|
||||||
service: this.SERVICE_NAME,
|
service: this.SERVICE_NAME,
|
||||||
@@ -1,8 +1,15 @@
|
|||||||
import { IMonitorsRepository } from "@/repositories/index.js";
|
import { IMonitorsRepository } from "@/repositories/index.js";
|
||||||
import MonitorStats from "../../db/models/MonitorStats.js";
|
import MonitorStats from "../../db/models/MonitorStats.js";
|
||||||
import { CheckModel } from "@/db/models/index.js";
|
import { CheckModel } from "@/db/models/index.js";
|
||||||
import type { CheckErrorInfo, Monitor, MonitorStatusResponse, StatusChangeResult } from "@/types/index.js";
|
import type {
|
||||||
import type { HardwareStatusPayload, PageSpeedStatusPayload } from "@/types/network.js";
|
CheckErrorInfo,
|
||||||
|
Monitor,
|
||||||
|
MonitorStatusResponse,
|
||||||
|
StatusChangeResult,
|
||||||
|
Check,
|
||||||
|
HardwareStatusPayload,
|
||||||
|
PageSpeedStatusPayload,
|
||||||
|
} from "@/types/index.js";
|
||||||
const SERVICE_NAME = "StatusService";
|
const SERVICE_NAME = "StatusService";
|
||||||
|
|
||||||
class StatusService {
|
class StatusService {
|
||||||
@@ -163,10 +170,9 @@ class StatusService {
|
|||||||
};
|
};
|
||||||
|
|
||||||
updateMonitorStatus = async (
|
updateMonitorStatus = async (
|
||||||
statusResponse: MonitorStatusResponse<PageSpeedStatusPayload | HardwareStatusPayload | undefined>
|
statusResponse: MonitorStatusResponse<PageSpeedStatusPayload | HardwareStatusPayload | undefined>,
|
||||||
|
check: Check
|
||||||
): Promise<StatusChangeResult> => {
|
): Promise<StatusChangeResult> => {
|
||||||
const check = this.buildCheck(statusResponse);
|
|
||||||
await this.insertCheck(check);
|
|
||||||
try {
|
try {
|
||||||
const { monitorId, teamId, status, code } = statusResponse;
|
const { monitorId, teamId, status, code } = statusResponse;
|
||||||
const monitor = await this.monitorsRepository.findById(monitorId, teamId);
|
const monitor = await this.monitorsRepository.findById(monitorId, teamId);
|
||||||
@@ -301,116 +307,7 @@ class StatusService {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
buildCheck = (
|
insertCheck = async (check: Check) => {
|
||||||
networkResponse: MonitorStatusResponse<PageSpeedStatusPayload | HardwareStatusPayload | undefined>
|
|
||||||
) => {
|
|
||||||
const {
|
|
||||||
monitorId,
|
|
||||||
teamId,
|
|
||||||
type,
|
|
||||||
status,
|
|
||||||
responseTime,
|
|
||||||
code,
|
|
||||||
message,
|
|
||||||
payload,
|
|
||||||
first_byte_took,
|
|
||||||
body_read_took,
|
|
||||||
dns_took,
|
|
||||||
conn_took,
|
|
||||||
connect_took,
|
|
||||||
tls_took,
|
|
||||||
timings,
|
|
||||||
} = networkResponse;
|
|
||||||
|
|
||||||
const check: any = {
|
|
||||||
metadata: {
|
|
||||||
monitorId,
|
|
||||||
teamId,
|
|
||||||
type,
|
|
||||||
},
|
|
||||||
status,
|
|
||||||
statusCode: code,
|
|
||||||
responseTime,
|
|
||||||
timings: timings || {},
|
|
||||||
message,
|
|
||||||
first_byte_took,
|
|
||||||
body_read_took,
|
|
||||||
dns_took,
|
|
||||||
conn_took,
|
|
||||||
connect_took,
|
|
||||||
tls_took,
|
|
||||||
};
|
|
||||||
|
|
||||||
if (type === "pagespeed") {
|
|
||||||
const pageSpeedPayload = payload as PageSpeedStatusPayload | undefined;
|
|
||||||
if (!pageSpeedPayload) {
|
|
||||||
this.logger.warn({
|
|
||||||
message: "Failed to build check",
|
|
||||||
service: SERVICE_NAME,
|
|
||||||
method: "buildCheck",
|
|
||||||
details: "empty payload",
|
|
||||||
});
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
const categories = pageSpeedPayload.lighthouseResult?.categories ?? {};
|
|
||||||
const audits = pageSpeedPayload.lighthouseResult?.audits ?? {};
|
|
||||||
const mapAudit = (audit: any) => {
|
|
||||||
if (!audit || typeof audit !== "object") {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
id: audit.id,
|
|
||||||
title: audit.title,
|
|
||||||
score: typeof audit.score === "number" ? audit.score : (audit.score ?? null),
|
|
||||||
displayValue: audit.displayValue,
|
|
||||||
numericValue: typeof audit.numericValue === "number" ? audit.numericValue : undefined,
|
|
||||||
numericUnit: audit.numericUnit,
|
|
||||||
};
|
|
||||||
};
|
|
||||||
check.accessibility = (categories?.accessibility?.score || 0) * 100;
|
|
||||||
check.bestPractices = (categories?.["best-practices"]?.score || 0) * 100;
|
|
||||||
check.seo = (categories?.seo?.score || 0) * 100;
|
|
||||||
check.performance = (categories?.performance?.score || 0) * 100;
|
|
||||||
check.audits = {
|
|
||||||
cls: mapAudit(audits?.["cumulative-layout-shift"]),
|
|
||||||
si: mapAudit(audits?.["speed-index"]),
|
|
||||||
fcp: mapAudit(audits?.["first-contentful-paint"]),
|
|
||||||
lcp: mapAudit(audits?.["largest-contentful-paint"]),
|
|
||||||
tbt: mapAudit(audits?.["total-blocking-time"]),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (type === "hardware") {
|
|
||||||
const hardwarePayload = payload as HardwareStatusPayload | undefined;
|
|
||||||
const { cpu, memory, disk, host, net } = hardwarePayload?.data ?? {};
|
|
||||||
const errorsSource = Array.isArray(hardwarePayload?.errors)
|
|
||||||
? hardwarePayload?.errors
|
|
||||||
: (hardwarePayload?.errors as { errors?: CheckErrorInfo[] } | undefined)?.errors;
|
|
||||||
check.cpu = cpu ?? {};
|
|
||||||
check.memory = memory ?? {};
|
|
||||||
check.disk = disk ?? {};
|
|
||||||
check.host = host ?? {};
|
|
||||||
check.errors = errorsSource ?? [];
|
|
||||||
check.capture = hardwarePayload?.capture ?? {};
|
|
||||||
check.net = net ?? {};
|
|
||||||
}
|
|
||||||
return check;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Inserts a check into the database based on the network response.
|
|
||||||
*
|
|
||||||
* @param {Object} networkResponse - The network response object.
|
|
||||||
* @param {string} networkResponse.monitorId - The monitor ID.
|
|
||||||
* @param {string} networkResponse.type - The type of the response.
|
|
||||||
* @param {string} networkResponse.status - The status of the response.
|
|
||||||
* @param {number} networkResponse.responseTime - The response time.
|
|
||||||
* @param {number} networkResponse.code - The status code.
|
|
||||||
* @param {string} networkResponse.message - The message.
|
|
||||||
* @param {Object} networkResponse.payload - The payload of the response.
|
|
||||||
* @returns {Promise<void>} A promise that resolves when the check is inserted.
|
|
||||||
*/
|
|
||||||
insertCheck = async (check: any) => {
|
|
||||||
try {
|
try {
|
||||||
if (typeof check === "undefined") {
|
if (typeof check === "undefined") {
|
||||||
this.logger.warn({
|
this.logger.warn({
|
||||||
@@ -427,7 +324,7 @@ class StatusService {
|
|||||||
message: error.message,
|
message: error.message,
|
||||||
service: error.service || SERVICE_NAME,
|
service: error.service || SERVICE_NAME,
|
||||||
method: error.method || "insertCheck",
|
method: error.method || "insertCheck",
|
||||||
details: error.details || `Error inserting check for monitor: ${check?.monitorId}`,
|
details: error.details || `Error inserting check for monitor: ${check?.metadata.monitorId}`,
|
||||||
stack: error.stack,
|
stack: error.stack,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
+35
-35
@@ -9,40 +9,40 @@ export interface CheckMetadata {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface CheckCpuInfo {
|
export interface CheckCpuInfo {
|
||||||
physical_core: number;
|
physical_core?: number;
|
||||||
logical_core: number;
|
logical_core?: number;
|
||||||
frequency: number;
|
frequency?: number;
|
||||||
temperature: number[];
|
temperature?: number[];
|
||||||
free_percent: number;
|
free_percent?: number;
|
||||||
usage_percent: number;
|
usage_percent?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface CheckMemoryInfo {
|
export interface CheckMemoryInfo {
|
||||||
total_bytes: number;
|
total_bytes?: number;
|
||||||
available_bytes: number;
|
available_bytes?: number;
|
||||||
used_bytes: number;
|
used_bytes?: number;
|
||||||
usage_percent: number;
|
usage_percent?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface CheckHostInfo {
|
export interface CheckHostInfo {
|
||||||
os: string;
|
os?: string;
|
||||||
platform: string;
|
platform?: string;
|
||||||
kernel_version: string;
|
kernel_version?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface CheckCaptureInfo {
|
export interface CheckCaptureInfo {
|
||||||
version: string;
|
version?: string;
|
||||||
mode: string;
|
mode?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface CheckDiskInfo {
|
export interface CheckDiskInfo {
|
||||||
device: string;
|
device?: string;
|
||||||
mountpoint: string;
|
mountpoint?: string;
|
||||||
read_speed_bytes: number;
|
read_speed_bytes?: number;
|
||||||
write_speed_bytes: number;
|
write_speed_bytes?: number;
|
||||||
total_bytes: number;
|
total_bytes?: number;
|
||||||
free_bytes: number;
|
free_bytes?: number;
|
||||||
usage_percent: number;
|
usage_percent?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface CheckErrorInfo {
|
export interface CheckErrorInfo {
|
||||||
@@ -65,11 +65,11 @@ export interface CheckNetworkInterfaceInfo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface CheckAudits {
|
export interface CheckAudits {
|
||||||
cls: ILighthouseAudit;
|
cls?: ILighthouseAudit;
|
||||||
si: ILighthouseAudit;
|
si?: ILighthouseAudit;
|
||||||
fcp: ILighthouseAudit;
|
fcp?: ILighthouseAudit;
|
||||||
lcp: ILighthouseAudit;
|
lcp?: ILighthouseAudit;
|
||||||
tbt: ILighthouseAudit;
|
tbt?: ILighthouseAudit;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ILighthouseAudit {
|
export interface ILighthouseAudit {
|
||||||
@@ -86,19 +86,19 @@ export interface Check {
|
|||||||
metadata: CheckMetadata;
|
metadata: CheckMetadata;
|
||||||
status: boolean;
|
status: boolean;
|
||||||
responseTime: number;
|
responseTime: number;
|
||||||
timings: GotTimings;
|
timings?: GotTimings;
|
||||||
statusCode: number;
|
statusCode: number;
|
||||||
message: string;
|
message: string;
|
||||||
ack: boolean;
|
ack: boolean;
|
||||||
ackAt?: string | null;
|
ackAt?: string | null;
|
||||||
expiry: string;
|
expiry: string;
|
||||||
cpu: CheckCpuInfo;
|
cpu?: CheckCpuInfo;
|
||||||
memory: CheckMemoryInfo;
|
memory?: CheckMemoryInfo;
|
||||||
disk: CheckDiskInfo[];
|
disk?: CheckDiskInfo[];
|
||||||
host: CheckHostInfo;
|
host?: CheckHostInfo;
|
||||||
errors: CheckErrorInfo[];
|
errors?: CheckErrorInfo[];
|
||||||
capture: CheckCaptureInfo;
|
capture?: CheckCaptureInfo;
|
||||||
net: CheckNetworkInterfaceInfo[];
|
net?: CheckNetworkInterfaceInfo[];
|
||||||
accessibility?: number;
|
accessibility?: number;
|
||||||
bestPractices?: number;
|
bestPractices?: number;
|
||||||
seo?: number;
|
seo?: number;
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
export const MonitorTypes = ["http", "ping", "pagespeed", "hardware", "docker", "port", "game"] as const;
|
export const MonitorTypes = ["http", "ping", "pagespeed", "hardware", "docker", "port", "game", "unknown"] as const;
|
||||||
export type MonitorType = (typeof MonitorTypes)[number];
|
export type MonitorType = (typeof MonitorTypes)[number];
|
||||||
|
|
||||||
export interface MonitorThresholds {
|
export interface MonitorThresholds {
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ import type {
|
|||||||
export interface MonitorStatusResponse<T = unknown> {
|
export interface MonitorStatusResponse<T = unknown> {
|
||||||
monitorId: string;
|
monitorId: string;
|
||||||
teamId: string;
|
teamId: string;
|
||||||
type: MonitorType | "unknown";
|
type: MonitorType;
|
||||||
status: boolean;
|
status: boolean;
|
||||||
code: number;
|
code: number;
|
||||||
message: string;
|
message: string;
|
||||||
|
|||||||
Reference in new issue
Block a user