feat: add buffer lookup and location results update to services
## Changes - Add findInBuffer() to BufferService for looking up checks still in the write buffer - Add updateLocationResults() to IChecksRepository interface and MongoChecksRepository - Update GlobalpingService to return GlobalpingCheckResult[] instead of MonitorStatusResponse[] ## Benefits Non-destructive service additions that prepare the backfill pipeline. Existing check-writing and Globalping flows are unchanged.
This commit is contained in:
1 parent
d25ce9e498
commit
3b858de2b5
4 files changed
+20
-14
No files matched your search
@@ -2,6 +2,7 @@ import type {
|
|||||||
Check,
|
Check,
|
||||||
ChecksQueryResult,
|
ChecksQueryResult,
|
||||||
ChecksSummary,
|
ChecksSummary,
|
||||||
|
LocationResult,
|
||||||
MonitorType,
|
MonitorType,
|
||||||
PageSpeedChecksResult,
|
PageSpeedChecksResult,
|
||||||
HardwareChecksResult,
|
HardwareChecksResult,
|
||||||
@@ -37,6 +38,7 @@ export interface IChecksRepository {
|
|||||||
findSummaryByTeamId(teamId: string, dateRange: string): Promise<ChecksSummary>;
|
findSummaryByTeamId(teamId: string, dateRange: string): Promise<ChecksSummary>;
|
||||||
findLocationChecksByMonitorId(monitorId: string, startDate: Date, endDate: Date, dateString: string): Promise<Record<string, UptimeChecksResult>>;
|
findLocationChecksByMonitorId(monitorId: string, startDate: Date, endDate: Date, dateString: string): Promise<Record<string, UptimeChecksResult>>;
|
||||||
// update
|
// update
|
||||||
|
updateLocationResults(checkId: string, locationResults: LocationResult[]): Promise<void>;
|
||||||
//delete
|
//delete
|
||||||
deleteByMonitorId(monitorId: string): Promise<number>;
|
deleteByMonitorId(monitorId: string): Promise<number>;
|
||||||
deleteByTeamId(teamId: string): Promise<number>;
|
deleteByTeamId(teamId: string): Promise<number>;
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import type {
|
|||||||
CheckMetadata,
|
CheckMetadata,
|
||||||
CheckNetworkInterfaceInfo,
|
CheckNetworkInterfaceInfo,
|
||||||
GotTimings,
|
GotTimings,
|
||||||
|
LocationResult,
|
||||||
MonitorType,
|
MonitorType,
|
||||||
} from "@/types/index.js";
|
} from "@/types/index.js";
|
||||||
import { CheckModel, MonitorModel, type CheckDocument } from "@/db/models/index.js";
|
import { CheckModel, MonitorModel, type CheckDocument } from "@/db/models/index.js";
|
||||||
@@ -406,6 +407,10 @@ class MongoChecksRepository implements IChecksRepository {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
updateLocationResults = async (checkId: string, locationResults: LocationResult[]): Promise<void> => {
|
||||||
|
await CheckModel.updateOne({ _id: new mongoose.Types.ObjectId(checkId) }, { $set: { locationResults } });
|
||||||
|
};
|
||||||
|
|
||||||
deleteByMonitorId = async (monitorId: string): Promise<number> => {
|
deleteByMonitorId = async (monitorId: string): Promise<number> => {
|
||||||
const result = await CheckModel.deleteMany({ "metadata.monitorId": new mongoose.Types.ObjectId(monitorId) });
|
const result = await CheckModel.deleteMany({ "metadata.monitorId": new mongoose.Types.ObjectId(monitorId) });
|
||||||
return result.deletedCount;
|
return result.deletedCount;
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ const SERVICE_NAME = "BufferService";
|
|||||||
export interface IBufferService {
|
export interface IBufferService {
|
||||||
addToBuffer(check: Check): void;
|
addToBuffer(check: Check): void;
|
||||||
removeCheckFromBuffer(check: Check): boolean;
|
removeCheckFromBuffer(check: Check): boolean;
|
||||||
|
findInBuffer(checkId: string): Check | undefined;
|
||||||
scheduleNextFlush(): void;
|
scheduleNextFlush(): void;
|
||||||
flushBuffer(): Promise<void>;
|
flushBuffer(): Promise<void>;
|
||||||
}
|
}
|
||||||
@@ -49,6 +50,10 @@ class BufferService implements IBufferService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
findInBuffer(checkId: string): Check | undefined {
|
||||||
|
return this.buffer.find((c: Check) => c.id?.toString() === checkId);
|
||||||
|
}
|
||||||
|
|
||||||
removeCheckFromBuffer(checkToRemove: Check) {
|
removeCheckFromBuffer(checkToRemove: Check) {
|
||||||
try {
|
try {
|
||||||
if (!checkToRemove) {
|
if (!checkToRemove) {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { Globalping } from "globalping";
|
import { Globalping } from "globalping";
|
||||||
import type { ISettingsService } from "@/service/system/settingsService.js";
|
import type { ISettingsService } from "@/service/system/settingsService.js";
|
||||||
import type { Monitor } from "@/types/monitor.js";
|
import type { Monitor } from "@/types/monitor.js";
|
||||||
import type { MonitorStatusResponse } from "@/types/network.js";
|
import type { GlobalpingCheckResult } from "@/types/network.js";
|
||||||
|
|
||||||
const SERVICE_NAME = "GlobalpingService";
|
const SERVICE_NAME = "GlobalpingService";
|
||||||
|
|
||||||
@@ -130,7 +130,7 @@ class GlobalpingService {
|
|||||||
return { client: this.client, locationIds };
|
return { client: this.client, locationIds };
|
||||||
}
|
}
|
||||||
|
|
||||||
async runChecks(monitor: Monitor): Promise<MonitorStatusResponse[]> {
|
async runChecks(monitor: Monitor): Promise<GlobalpingCheckResult[]> {
|
||||||
if (monitor.type !== "http" && monitor.type !== "ping") {
|
if (monitor.type !== "http" && monitor.type !== "ping") {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
@@ -148,7 +148,7 @@ class GlobalpingService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async executeChecks(monitor: Monitor): Promise<MonitorStatusResponse[]> {
|
private async executeChecks(monitor: Monitor): Promise<GlobalpingCheckResult[]> {
|
||||||
const { client, locationIds } = await this.getClientAndLocations();
|
const { client, locationIds } = await this.getClientAndLocations();
|
||||||
if (!client || locationIds.length === 0) {
|
if (!client || locationIds.length === 0) {
|
||||||
return [];
|
return [];
|
||||||
@@ -224,7 +224,7 @@ class GlobalpingService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const measurement = awaitResult.data as any;
|
const measurement = awaitResult.data as any;
|
||||||
const results: MonitorStatusResponse[] = [];
|
const results: GlobalpingCheckResult[] = [];
|
||||||
|
|
||||||
for (const item of measurement.results || []) {
|
for (const item of measurement.results || []) {
|
||||||
const probe = item.probe;
|
const probe = item.probe;
|
||||||
@@ -233,27 +233,21 @@ class GlobalpingService {
|
|||||||
|
|
||||||
if (measurementType === "http") {
|
if (measurementType === "http") {
|
||||||
results.push({
|
results.push({
|
||||||
monitorId: monitor.id,
|
location: locationId,
|
||||||
teamId: monitor.teamId,
|
|
||||||
type: monitor.type,
|
|
||||||
status: result.statusCode >= 200 && result.statusCode < 400,
|
status: result.statusCode >= 200 && result.statusCode < 400,
|
||||||
code: result.statusCode || 0,
|
statusCode: result.statusCode || 0,
|
||||||
message: result.statusCodeName || result.rawOutput || "",
|
message: result.statusCodeName || result.rawOutput || "",
|
||||||
responseTime: result.timings?.total ?? 0,
|
responseTime: result.timings?.total ?? 0,
|
||||||
location: locationId,
|
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
// Ping: loss < 100% is considered up (consistent with local ping checks)
|
// Ping: loss < 100% is considered up (consistent with local ping checks)
|
||||||
const stats = result.stats;
|
const stats = result.stats;
|
||||||
results.push({
|
results.push({
|
||||||
monitorId: monitor.id,
|
location: locationId,
|
||||||
teamId: monitor.teamId,
|
|
||||||
type: monitor.type,
|
|
||||||
status: stats?.loss !== undefined ? stats.loss < 100 : false,
|
status: stats?.loss !== undefined ? stats.loss < 100 : false,
|
||||||
code: stats?.loss === 0 ? 200 : 5000,
|
statusCode: stats?.loss === 0 ? 200 : 5000,
|
||||||
message: stats?.loss === 0 ? "Ping successful" : `Packet loss: ${stats?.loss}%`,
|
message: stats?.loss === 0 ? "Ping successful" : `Packet loss: ${stats?.loss}%`,
|
||||||
responseTime: stats?.avg ?? 0,
|
responseTime: stats?.avg ?? 0,
|
||||||
location: locationId,
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user