geo bufer

This commit is contained in:
Alex Holliday committed 2026-02-24 18:44:47 +00:00
1 parent 705ecf41c2
commit 5a74411ea2
3 files changed
+44 -15

No files matched your search

-2
View File
@@ -106,9 +106,7 @@ const GeoCheckSchema = new Schema<GeoCheckDocument>(
GeoCheckSchema.index({ "metadata.monitorId": 1, createdAt: -1 }); GeoCheckSchema.index({ "metadata.monitorId": 1, createdAt: -1 });
GeoCheckSchema.index({ "metadata.monitorId": 1, createdAt: 1 }); GeoCheckSchema.index({ "metadata.monitorId": 1, createdAt: 1 });
GeoCheckSchema.index({ "metadata.monitorId": 1, "results.location.continent": 1, createdAt: -1 });
GeoCheckSchema.index({ "metadata.teamId": 1, createdAt: -1 }); GeoCheckSchema.index({ "metadata.teamId": 1, createdAt: -1 });
GeoCheckSchema.index({ "metadata.teamId": 1, "results.status": 1, createdAt: -1 });
GeoCheckSchema.index({ createdAt: 1 }); GeoCheckSchema.index({ createdAt: 1 });
const GeoCheckModel = model<GeoCheckDocument>("GeoCheck", GeoCheckSchema); const GeoCheckModel = model<GeoCheckDocument>("GeoCheck", GeoCheckSchema);
@@ -1,5 +1,6 @@
import type { Check } from "@/types/index.js"; import type { Check } from "@/types/index.js";
import type { GeoCheck } from "@/types/index.js"; import type { GeoCheck } from "@/types/index.js";
import type Logger from "@/utils/logger.js";
const SERVICE_NAME = "BufferService"; const SERVICE_NAME = "BufferService";
@@ -9,12 +10,13 @@ export interface IBufferService {
removeCheckFromBuffer(check: Check): boolean; removeCheckFromBuffer(check: Check): boolean;
scheduleNextFlush(): void; scheduleNextFlush(): void;
flushBuffer(): Promise<void>; flushBuffer(): Promise<void>;
flushGeoBuffer(): Promise<void>;
} }
class BufferService implements IBufferService { 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: Logger;
private SERVICE_NAME: string; private SERVICE_NAME: string;
private buffer: any[]; private buffer: any[];
private geoBuffer: any[]; private geoBuffer: any[];
@@ -28,7 +30,7 @@ class BufferService implements IBufferService {
geoChecksService, geoChecksService,
settingsService, settingsService,
}: { }: {
logger: any; logger: Logger;
checkService: any; checkService: any;
geoChecksService?: any; geoChecksService?: any;
settingsService: any; settingsService: any;
@@ -123,6 +125,7 @@ class BufferService implements IBufferService {
this.bufferTimer = setTimeout(async () => { this.bufferTimer = setTimeout(async () => {
try { try {
await this.flushBuffer(); await this.flushBuffer();
await this.flushGeoBuffer();
} catch (error: any) { } catch (error: any) {
this.logger.error({ this.logger.error({
message: `Error in flush cycle: ${error.message}`, message: `Error in flush cycle: ${error.message}`,
@@ -139,23 +142,47 @@ class BufferService implements IBufferService {
async flushBuffer() { async flushBuffer() {
try { try {
if (this.buffer.length > 0) { if (this.buffer.length > 0) {
this.logger.debug({
message: `Flushing ${this.buffer.length} checks to database`,
service: this.SERVICE_NAME,
method: "flushBuffer",
});
await this.checksService.createChecks(this.buffer); await this.checksService.createChecks(this.buffer);
} this.buffer = [];
if (this.geoBuffer.length > 0 && this.geoChecksService) {
await this.geoChecksService.createGeoChecks(this.geoBuffer);
} }
} catch (error: any) { } catch (error: any) {
this.logger.error({ this.logger.error({
message: error.message, message: `Error flushing checks buffer: ${error.message}`,
service: this.SERVICE_NAME, service: this.SERVICE_NAME,
method: "flushBuffer", method: "flushBuffer",
stack: error.stack, stack: error.stack,
}); });
// Clear buffer even on error to prevent infinite retry loops
this.buffer = [];
} }
}
this.buffer = []; async flushGeoBuffer() {
this.geoBuffer = []; try {
if (this.geoBuffer.length > 0) {
this.logger.debug({
message: `Flushing ${this.geoBuffer.length} geo checks to database`,
service: this.SERVICE_NAME,
method: "flushGeoBuffer",
});
await this.geoChecksService.createGeoChecks(this.geoBuffer);
this.geoBuffer = [];
}
} catch (error: any) {
this.logger.error({
message: `Error flushing geo checks buffer: ${error.message}`,
service: this.SERVICE_NAME,
method: "flushGeoBuffer",
stack: error.stack,
});
// Clear buffer even on error to prevent infinite retry loops
this.geoBuffer = [];
}
} }
} }
@@ -1,4 +1,5 @@
import type { GeoContinent, GeoCheckResult, GeoCheckTimings, GeoCheckLocation } from "@/types/geoCheck.js"; import type { GeoContinent, GeoCheckResult, GeoCheckTimings, GeoCheckLocation } from "@/types/geoCheck.js";
import type Logger from "@/utils/logger.js";
import got from "got"; import got from "got";
const SERVICE_NAME = "GlobalPingService"; const SERVICE_NAME = "GlobalPingService";
@@ -56,9 +57,9 @@ export interface IGlobalPingService {
class GlobalPingService implements IGlobalPingService { class GlobalPingService implements IGlobalPingService {
static SERVICE_NAME = SERVICE_NAME; static SERVICE_NAME = SERVICE_NAME;
private logger: any; private logger: Logger;
constructor({ logger }: { logger: any }) { constructor({ logger }: { logger: Logger }) {
this.logger = logger; this.logger = logger;
} }
@@ -68,9 +69,12 @@ class GlobalPingService implements IGlobalPingService {
async createMeasurement(url: string, locations: GeoContinent[]): Promise<string | null> { async createMeasurement(url: string, locations: GeoContinent[]): Promise<string | null> {
try { try {
// GlobalPing API expects target without protocol (http:// or https://)
const cleanTarget = url.replace(/^https?:\/\//, "");
const requestBody: GlobalPingMeasurementRequest = { const requestBody: GlobalPingMeasurementRequest = {
type: "http", type: "http",
target: url, target: cleanTarget,
locations: locations.map((continent) => ({ continent })), locations: locations.map((continent) => ({ continent })),
limit: locations.length, limit: locations.length,
}; };
@@ -96,7 +100,7 @@ class GlobalPingService implements IGlobalPingService {
message: "GlobalPing API unavailable, skipping geo check", message: "GlobalPing API unavailable, skipping geo check",
service: SERVICE_NAME, service: SERVICE_NAME,
method: "createMeasurement", method: "createMeasurement",
details: error.message, details: error,
}); });
return null; return null;
} }