initial commit
This commit is contained in:
1 parent
f5bc8e78e4
commit
f337ca5ff0
7 files changed
+36
-29
No files matched your search
@@ -30,7 +30,7 @@ export const initializeControllers = (services: InitializedServices): Initialize
|
|||||||
return {
|
return {
|
||||||
authController: new AuthController(services.userService),
|
authController: new AuthController(services.userService),
|
||||||
monitorController: new MonitorController(services.monitorService),
|
monitorController: new MonitorController(services.monitorService),
|
||||||
settingsController: new SettingsController(services.settingsService, services.emailService, services.db),
|
settingsController: new SettingsController(services.settingsService, services.emailService),
|
||||||
checkController: new CheckController(services.checkService),
|
checkController: new CheckController(services.checkService),
|
||||||
inviteController: new InviteController(services.inviteService),
|
inviteController: new InviteController(services.inviteService),
|
||||||
maintenanceWindowController: new MaintenanceWindowController(services.maintenanceWindowService),
|
maintenanceWindowController: new MaintenanceWindowController(services.maintenanceWindowService),
|
||||||
|
|||||||
@@ -41,18 +41,6 @@ import crypto from "crypto";
|
|||||||
import { games, GameDig } from "gamedig";
|
import { games, GameDig } from "gamedig";
|
||||||
import jmespath from "jmespath";
|
import jmespath from "jmespath";
|
||||||
|
|
||||||
// DB Modules
|
|
||||||
import { GenerateAvatarImage } from "../utils/imageProcessing.js";
|
|
||||||
import { ParseBoolean } from "../utils/utils.js";
|
|
||||||
|
|
||||||
// Models
|
|
||||||
import InviteToken from "../db/models/Invite.js";
|
|
||||||
import Team from "../db/models/Team.js";
|
|
||||||
import MaintenanceWindow from "../db/models/MaintenanceWindow.js";
|
|
||||||
import MonitorStats from "../db/models/MonitorStats.js";
|
|
||||||
import NotificationModel from "../db/models/Notification.js";
|
|
||||||
import RecoveryToken from "../db/models/RecoveryToken.js";
|
|
||||||
|
|
||||||
// repositories
|
// repositories
|
||||||
import {
|
import {
|
||||||
MongoMonitorsRepository,
|
MongoMonitorsRepository,
|
||||||
@@ -176,7 +164,7 @@ export const initializeServices = async ({
|
|||||||
|
|
||||||
const bufferService = new BufferService({ logger, checkService, settingsService });
|
const bufferService = new BufferService({ logger, checkService, settingsService });
|
||||||
|
|
||||||
const statusService = new StatusService({ logger, buffer: bufferService, monitorsRepository });
|
const statusService = new StatusService(logger, bufferService, monitorsRepository, monitorStatsRepository, checksRepository);
|
||||||
|
|
||||||
const webhookProvider = new WebhookProvider(logger);
|
const webhookProvider = new WebhookProvider(logger);
|
||||||
const slackProvider = new SlackProvider(logger);
|
const slackProvider = new SlackProvider(logger);
|
||||||
|
|||||||
@@ -8,11 +8,9 @@ class SettingsController {
|
|||||||
static SERVICE_NAME = SERVICE_NAME;
|
static SERVICE_NAME = SERVICE_NAME;
|
||||||
private settingsService: any;
|
private settingsService: any;
|
||||||
private emailService: any;
|
private emailService: any;
|
||||||
private db: any;
|
constructor(settingsService: any, emailService: any) {
|
||||||
constructor(settingsService: any, emailService: any, db: any) {
|
|
||||||
this.settingsService = settingsService;
|
this.settingsService = settingsService;
|
||||||
this.emailService = emailService;
|
this.emailService = emailService;
|
||||||
this.db = db;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get serviceName() {
|
get serviceName() {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import type { MonitorStats } from "@/types/index.js";
|
import type { MonitorStats } from "@/types/index.js";
|
||||||
export interface IMonitorStatsRepository {
|
export interface IMonitorStatsRepository {
|
||||||
// create
|
// create
|
||||||
|
create(data: Omit<MonitorStats, "id" | "createdAt" | "updatedAt">): Promise<MonitorStats>;
|
||||||
// single fetch
|
// single fetch
|
||||||
findByMonitorId(monitorId: string): Promise<MonitorStats>;
|
findByMonitorId(monitorId: string): Promise<MonitorStats>;
|
||||||
// update
|
// update
|
||||||
|
|||||||
@@ -32,6 +32,11 @@ class MongoMonitorStatsRepository implements IMonitorStatsRepository {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
create = async (data: Omit<MonitorStats, "id" | "createdAt" | "updatedAt">): Promise<MonitorStats> => {
|
||||||
|
const created = await MonitorStatsModel.create(data);
|
||||||
|
return this.toEntity(created);
|
||||||
|
};
|
||||||
|
|
||||||
findByMonitorId = async (monitorId: string): Promise<MonitorStats> => {
|
findByMonitorId = async (monitorId: string): Promise<MonitorStats> => {
|
||||||
const monitorStats = await MonitorStatsModel.findOne({ monitorId: new mongoose.Types.ObjectId(monitorId) });
|
const monitorStats = await MonitorStatsModel.findOne({ monitorId: new mongoose.Types.ObjectId(monitorId) });
|
||||||
if (!monitorStats) {
|
if (!monitorStats) {
|
||||||
|
|||||||
@@ -2,7 +2,14 @@ import type { Check } from "@/types/index.js";
|
|||||||
|
|
||||||
const SERVICE_NAME = "BufferService";
|
const SERVICE_NAME = "BufferService";
|
||||||
|
|
||||||
class BufferService {
|
export interface IBufferService {
|
||||||
|
addToBuffer(check: Check): void;
|
||||||
|
removeCheckFromBuffer(check: Check): boolean;
|
||||||
|
scheduleNextFlush(): void;
|
||||||
|
flushBuffer(): Promise<void>;
|
||||||
|
}
|
||||||
|
|
||||||
|
class BufferService implements IBufferService {
|
||||||
static SERVICE_NAME = SERVICE_NAME;
|
static SERVICE_NAME = SERVICE_NAME;
|
||||||
private BUFFER_TIMEOUT: number;
|
private BUFFER_TIMEOUT: number;
|
||||||
private logger: any;
|
private logger: any;
|
||||||
@@ -13,7 +20,6 @@ class BufferService {
|
|||||||
|
|
||||||
constructor({ logger, checkService, settingsService }: { logger: any; checkService: any; settingsService: any }) {
|
constructor({ logger, checkService, settingsService }: { logger: any; checkService: any; settingsService: any }) {
|
||||||
this.BUFFER_TIMEOUT = settingsService.getSettings().nodeEnv === "development" ? 10 : 1000 * 60 * 1; // 1 minute
|
this.BUFFER_TIMEOUT = settingsService.getSettings().nodeEnv === "development" ? 10 : 1000 * 60 * 1; // 1 minute
|
||||||
console.log(this.BUFFER_TIMEOUT);
|
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
this.checksService = checkService;
|
this.checksService = checkService;
|
||||||
this.SERVICE_NAME = SERVICE_NAME;
|
this.SERVICE_NAME = SERVICE_NAME;
|
||||||
@@ -30,7 +36,7 @@ class BufferService {
|
|||||||
return BufferService.SERVICE_NAME;
|
return BufferService.SERVICE_NAME;
|
||||||
}
|
}
|
||||||
|
|
||||||
addToBuffer({ check }: { check: Check }) {
|
addToBuffer(check: Check) {
|
||||||
try {
|
try {
|
||||||
this.buffer.push(check);
|
this.buffer.push(check);
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
|
|||||||
@@ -1,6 +1,4 @@
|
|||||||
import { IMonitorsRepository } from "@/repositories/index.js";
|
import { IChecksRepository, IMonitorsRepository, IMonitorStatsRepository } from "@/repositories/index.js";
|
||||||
import MonitorStats from "../../db/models/MonitorStats.js";
|
|
||||||
import { CheckModel } from "@/db/models/index.js";
|
|
||||||
import type {
|
import type {
|
||||||
Monitor,
|
Monitor,
|
||||||
MonitorStatus,
|
MonitorStatus,
|
||||||
@@ -10,7 +8,9 @@ import type {
|
|||||||
HardwareStatusPayload,
|
HardwareStatusPayload,
|
||||||
PageSpeedStatusPayload,
|
PageSpeedStatusPayload,
|
||||||
CheckSnapshot,
|
CheckSnapshot,
|
||||||
|
MonitorStats,
|
||||||
} from "@/types/index.js";
|
} from "@/types/index.js";
|
||||||
|
import { ILogger } from "@/utils/logger.js";
|
||||||
const SERVICE_NAME = "StatusService";
|
const SERVICE_NAME = "StatusService";
|
||||||
|
|
||||||
export interface IStatusService {
|
export interface IStatusService {
|
||||||
@@ -27,11 +27,21 @@ export class StatusService implements IStatusService {
|
|||||||
private logger: any;
|
private logger: any;
|
||||||
private buffer: any;
|
private buffer: any;
|
||||||
private monitorsRepository: IMonitorsRepository;
|
private monitorsRepository: IMonitorsRepository;
|
||||||
|
private monitorStatsRepository: IMonitorStatsRepository;
|
||||||
|
private checksRepository: IChecksRepository;
|
||||||
|
|
||||||
constructor({ logger, buffer, monitorsRepository }: { logger: any; buffer: any; monitorsRepository: IMonitorsRepository }) {
|
constructor(
|
||||||
|
logger: ILogger,
|
||||||
|
buffer: any,
|
||||||
|
monitorsRepository: IMonitorsRepository,
|
||||||
|
monitorStatsRepository: IMonitorStatsRepository,
|
||||||
|
checksRepository: IChecksRepository
|
||||||
|
) {
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
this.buffer = buffer;
|
this.buffer = buffer;
|
||||||
this.monitorsRepository = monitorsRepository;
|
this.monitorsRepository = monitorsRepository;
|
||||||
|
this.monitorStatsRepository = monitorStatsRepository;
|
||||||
|
this.checksRepository = checksRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
get serviceName() {
|
get serviceName() {
|
||||||
@@ -42,18 +52,17 @@ export class StatusService implements IStatusService {
|
|||||||
try {
|
try {
|
||||||
const monitorId = monitor.id;
|
const monitorId = monitor.id;
|
||||||
const { responseTime, status } = networkResponse;
|
const { responseTime, status } = networkResponse;
|
||||||
// Get stats
|
let stats = await this.monitorStatsRepository.findByMonitorId(monitorId);
|
||||||
let stats = await MonitorStats.findOne({ monitorId });
|
|
||||||
if (!stats) {
|
if (!stats) {
|
||||||
stats = new MonitorStats({
|
stats = {
|
||||||
monitorId,
|
monitorId,
|
||||||
avgResponseTime: 0,
|
avgResponseTime: 0,
|
||||||
totalChecks: 0,
|
totalChecks: 0,
|
||||||
totalUpChecks: 0,
|
totalUpChecks: 0,
|
||||||
totalDownChecks: 0,
|
totalDownChecks: 0,
|
||||||
uptimePercentage: 0,
|
uptimePercentage: 0,
|
||||||
lastCheck: null,
|
lastResponseTime: 0,
|
||||||
});
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update stats
|
// Update stats
|
||||||
|
|||||||
Reference in new issue
Block a user