chore: Update MonitorTypeHelper function names to be more descriptive

This commit updates the function names in the MonitorTypeHelper class to be more descriptive and accurately reflect their purpose. The function isProbableMonitors has been renamed to isProbableMonitor, and doesMonitorTypeHaveInterval has been renamed to doesMonitorTypeRequireInterval. This change improves the clarity and maintainability of the codebase.
This commit is contained in:
Simon Larsen
2024-07-02 18:58:06 +01:00
parent 03cb9a67b4
commit 847f7fa5c9
9 changed files with 216 additions and 11 deletions

View File

@@ -134,7 +134,7 @@ export class MonitorTypeHelper {
return monitorTypeProps[0].title;
}
public static isProbableMonitors(monitorType: MonitorType): boolean {
public static isProbableMonitor(monitorType: MonitorType): boolean {
const isProbeableMonitor: boolean =
monitorType === MonitorType.API ||
monitorType === MonitorType.Website ||
@@ -172,7 +172,7 @@ export class MonitorTypeHelper {
}
public static doesMonitorTypeHaveInterval(monitorType: MonitorType): boolean {
return this.isProbableMonitors(monitorType);
return this.isProbableMonitor(monitorType);
}
public static doesMonitorTypeHaveCriteria(monitorType: MonitorType): boolean {

View File

@@ -1,10 +1,14 @@
import ObjectID from "Common/Types/ObjectID";
import PostgresDatabase from "../Infrastructure/PostgresDatabase";
import CreateBy from "../Types/Database/CreateBy";
import { OnCreate } from "../Types/Database/Hooks";
import { OnCreate, OnUpdate } from "../Types/Database/Hooks";
import DatabaseService from "./DatabaseService";
import OneUptimeDate from "Common/Types/Date";
import BadDataException from "Common/Types/Exception/BadDataException";
import MonitorProbe from "Model/Models/MonitorProbe";
import QueryHelper from "../Types/Database/QueryHelper";
import { LIMIT_PER_PROJECT } from "Common/Types/Database/LimitMax";
import MonitorService from "./MonitorService";
export class Service extends DatabaseService<MonitorProbe> {
public constructor(postgresDatabase?: PostgresDatabase) {
@@ -46,6 +50,48 @@ export class Service extends DatabaseService<MonitorProbe> {
return { createBy, carryForward: null };
}
protected override async onCreateSuccess(
_onCreate: OnCreate<MonitorProbe>,
createdItem: MonitorProbe,
): Promise<MonitorProbe> {
if (createdItem.probeId) {
await MonitorService.refreshProbeStatus(createdItem.probeId);
}
return Promise.resolve(createdItem);
}
protected override async onUpdateSuccess(
onUpdate: OnUpdate<MonitorProbe>,
updatedItemIds: ObjectID[],
): Promise<OnUpdate<MonitorProbe>> {
const monitorProbes: Array<MonitorProbe> = await this.findBy({
query: {
_id: QueryHelper.any(updatedItemIds),
},
select: {
monitorId: true,
probeId: true,
nextPingAt: true,
},
limit: LIMIT_PER_PROJECT,
skip: 0,
props: {
isRoot: true,
},
});
for (const monitorProbe of monitorProbes) {
if (!monitorProbe.probeId) {
continue;
}
await MonitorService.refreshProbeStatus(monitorProbe.probeId);
}
return onUpdate;
}
}
export default new Service();

View File

@@ -37,7 +37,7 @@ import MonitorOwnerUser from "Model/Models/MonitorOwnerUser";
import MonitorProbe from "Model/Models/MonitorProbe";
import MonitorStatus from "Model/Models/MonitorStatus";
import MonitorStatusTimeline from "Model/Models/MonitorStatusTimeline";
import Probe from "Model/Models/Probe";
import Probe, { ProbeConnectionStatus } from "Model/Models/Probe";
import User from "Model/Models/User";
import Select from "../Types/Database/Select";
@@ -232,7 +232,7 @@ export class Service extends DatabaseService<Model> {
if (
createdItem.monitorType &&
MonitorTypeHelper.isProbableMonitors(createdItem.monitorType)
MonitorTypeHelper.isProbableMonitor(createdItem.monitorType)
) {
await this.addDefaultProbesToMonitor(
createdItem.projectId,
@@ -265,6 +265,9 @@ export class Service extends DatabaseService<Model> {
);
}
// refresh probe status.
await this.refreshMonitorProbeStatus(createdItem.id);
return createdItem;
}
@@ -447,6 +450,159 @@ export class Service extends DatabaseService<Model> {
}
}
public async refreshMonitorProbeStatus(monitorId: ObjectID): Promise<void> {
const monitor: Model | null = await this.findOneById({
id: monitorId,
select: {
_id: true,
monitorType: true,
},
props: {
isRoot: true,
},
});
if (!monitor) {
return;
}
if (!monitor.id) {
return;
}
const monitorType: MonitorType | undefined = monitor?.monitorType;
if (!monitorType) {
return;
}
const isProbeableMonitor: boolean =
MonitorTypeHelper.isProbableMonitor(monitorType);
if (!isProbeableMonitor) {
return;
}
// get all the probes for this monitor.
const probesForMonitor: Array<MonitorProbe> =
await MonitorProbeService.findBy({
query: {
monitorId: monitorId,
},
select: {
_id: true,
isEnabled: true,
projectId: true,
monitorId: true,
probeId: true,
probe: {
connectionStatus: true,
},
},
skip: 0,
limit: LIMIT_PER_PROJECT,
props: {
isRoot: true,
},
});
const enabledProbes: Array<MonitorProbe> = probesForMonitor.filter(
(probe: MonitorProbe) => {
return probe.isEnabled;
},
);
if (probesForMonitor.length === 0 || enabledProbes.length === 0) {
// no probes for this monitor.
await this.updateOneById({
id: monitorId,
data: {
isNoProbeEnabledOnThisMonitor: true,
},
props: {
isRoot: true,
},
});
} else {
await this.updateOneById({
id: monitorId,
data: {
isNoProbeEnabledOnThisMonitor: false,
},
props: {
isRoot: true,
},
});
}
const disconnectedProbes: Array<MonitorProbe> = probesForMonitor.filter(
(monitorProbe: MonitorProbe) => {
return (
monitorProbe.probe?.connectionStatus ===
ProbeConnectionStatus.Disconnected
);
},
);
if (disconnectedProbes.length === probesForMonitor.length) {
// all probes are disconnected.
await this.updateOneById({
id: monitorId,
data: {
isAllProbesDisconnectedFromThisMonitor: true,
},
props: {
isRoot: true,
},
});
} else {
await this.updateOneById({
id: monitorId,
data: {
isAllProbesDisconnectedFromThisMonitor: false,
},
props: {
isRoot: true,
},
});
}
}
public async refreshProbeStatus(probeId: ObjectID): Promise<void> {
// get all the monitors for this probe.
const monitorProbes: Array<MonitorProbe> = await MonitorProbeService.findBy(
{
query: {
probeId: probeId,
},
select: {
_id: true,
isEnabled: true,
projectId: true,
monitorId: true,
monitor: {
monitorType: true,
},
},
skip: 0,
limit: LIMIT_PER_PROJECT,
props: {
isRoot: true,
},
},
);
if (monitorProbes.length === 0) {
return;
}
for (const monitorProbe of monitorProbes) {
await this.refreshMonitorProbeStatus(monitorProbe.monitorId!);
}
}
public async changeMonitorStatus(
projectId: ObjectID,
monitorIds: Array<ObjectID>,

View File

@@ -26,6 +26,7 @@ import EmailTemplateType from "Common/Types/Email/EmailTemplateType";
import DatabaseConfig from "../DatabaseConfig";
import URL from "Common/Types/API/URL";
import UpdateBy from "../Types/Database/UpdateBy";
import MonitorService from "./MonitorService";
export class Service extends DatabaseService<Model> {
public constructor(postgresDatabase?: PostgresDatabase) {
@@ -164,6 +165,8 @@ export class Service extends DatabaseService<Model> {
onUpdate.carryForward.probesToNotifyOwners.length > 0
) {
for (const probe of onUpdate.carryForward.probesToNotifyOwners) {
await MonitorService.refreshProbeStatus(probe.id!);
await this.notifyOwnersOnStatusChange({
probeId: probe.id!,
});

View File

@@ -126,7 +126,7 @@ export default class ProbeMonitorResponseService {
if (
monitor.monitorType &&
MonitorTypeHelper.isProbableMonitors(monitor.monitorType)
MonitorTypeHelper.isProbableMonitor(monitor.monitorType)
) {
dataToProcess = dataToProcess as ProbeMonitorResponse;
if ((dataToProcess as ProbeMonitorResponse).probeId) {

View File

@@ -59,7 +59,7 @@ const Summary: FunctionComponent<ComponentProps> = (
title="Monitor Summary"
description="Here is how your monitor is performing at this moment."
rightElement={
MonitorTypeHelper.isProbableMonitors(props.monitorType) &&
MonitorTypeHelper.isProbableMonitor(props.monitorType) &&
props.probes &&
props.probes.length > 0 &&
selectedProbe ? (

View File

@@ -89,7 +89,7 @@ const SummaryInfo: FunctionComponent<ComponentProps> = (
};
if (
MonitorTypeHelper.isProbableMonitors(props.monitorType) &&
MonitorTypeHelper.isProbableMonitor(props.monitorType) &&
(!props.probeMonitorResponses || props.probeMonitorResponses.length === 0)
) {
return (

View File

@@ -264,7 +264,7 @@ const MonitorView: FunctionComponent<PageComponentProps> = (): ReactElement => {
setMonitorMetricsByMinute(monitorMetricsByMinute.data.reverse());
const isMonitoredByProbe: boolean = item.monitorType
? MonitorTypeHelper.isProbableMonitors(item.monitorType)
? MonitorTypeHelper.isProbableMonitor(item.monitorType)
: false;
if (isMonitoredByProbe) {

View File

@@ -19,7 +19,7 @@ export interface ComponentProps {
const DashboardSideMenu: FunctionComponent<ComponentProps> = (
props: ComponentProps,
): ReactElement => {
const isProbeableMonitor: boolean = MonitorTypeHelper.isProbableMonitors(
const isProbeableMonitor: boolean = MonitorTypeHelper.isProbableMonitor(
props.monitorType,
);
@@ -60,7 +60,7 @@ const DashboardSideMenu: FunctionComponent<ComponentProps> = (
) : (
<></>
)}
{MonitorTypeHelper.isProbableMonitors(props.monitorType) ? (
{MonitorTypeHelper.isProbableMonitor(props.monitorType) ? (
<SideMenuItem
link={{
title: "Interval",