2026-02-24 18:10:20 +00:00
|
|
|
import type { Monitor, GeoCheck } from "@/types/index.js";
|
2026-02-24 18:19:51 +00:00
|
|
|
import type { GeoCheckResult } from "@/types/geoCheck.js";
|
2026-02-24 18:10:20 +00:00
|
|
|
import { Types } from "mongoose";
|
|
|
|
|
import type { IGeoChecksRepository } from "@/repositories/index.js";
|
|
|
|
|
import type { IGlobalPingService } from "@/service/infrastructure/globalPingService.js";
|
2026-02-24 19:19:32 +00:00
|
|
|
import type { ILogger } from "@/utils/logger.js";
|
|
|
|
|
import type { ISettingsService } from "@/service/system/settingsService.js";
|
2026-02-24 18:10:20 +00:00
|
|
|
|
|
|
|
|
const SERVICE_NAME = "GeoChecksService";
|
|
|
|
|
|
|
|
|
|
export interface IGeoChecksService {
|
|
|
|
|
readonly serviceName: string;
|
2026-02-24 19:19:32 +00:00
|
|
|
buildGeoCheck(monitor: Monitor): Promise<GeoCheck | null>;
|
|
|
|
|
createGeoChecks(geoChecks: GeoCheck[]): Promise<GeoCheck[]>;
|
2026-02-24 18:10:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class GeoChecksService implements IGeoChecksService {
|
|
|
|
|
static SERVICE_NAME = SERVICE_NAME;
|
|
|
|
|
|
2026-02-24 19:19:32 +00:00
|
|
|
private logger: ILogger;
|
2026-02-24 18:10:20 +00:00
|
|
|
private geoChecksRepository: IGeoChecksRepository;
|
|
|
|
|
private globalPingService: IGlobalPingService;
|
2026-02-24 19:19:32 +00:00
|
|
|
|
|
|
|
|
constructor(logger: ILogger, geoChecksRepository: IGeoChecksRepository, globalPingService: IGlobalPingService) {
|
2026-02-24 18:10:20 +00:00
|
|
|
this.logger = logger;
|
|
|
|
|
this.geoChecksRepository = geoChecksRepository;
|
|
|
|
|
this.globalPingService = globalPingService;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get serviceName() {
|
|
|
|
|
return GeoChecksService.SERVICE_NAME;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-02-24 19:19:32 +00:00
|
|
|
* Build a geo-distributed check for a monitor
|
2026-02-24 18:10:20 +00:00
|
|
|
* 1. Create measurement request with GlobalPing API
|
|
|
|
|
* 2. Poll for results (with 30s timeout)
|
2026-02-24 19:19:32 +00:00
|
|
|
* 3. Transform and return GeoCheck document
|
2026-02-24 18:10:20 +00:00
|
|
|
*/
|
2026-02-24 19:19:32 +00:00
|
|
|
async buildGeoCheck(monitor: Monitor): Promise<GeoCheck | null> {
|
2026-02-24 18:10:20 +00:00
|
|
|
try {
|
|
|
|
|
if (!monitor.url) {
|
|
|
|
|
this.logger.warn({
|
|
|
|
|
message: "Monitor missing URL for geo check",
|
|
|
|
|
service: SERVICE_NAME,
|
2026-02-24 19:19:32 +00:00
|
|
|
method: "buildGeoCheck",
|
2026-02-24 18:10:20 +00:00
|
|
|
details: { monitorId: monitor.id },
|
|
|
|
|
});
|
2026-02-24 19:19:32 +00:00
|
|
|
return null;
|
2026-02-24 18:10:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!monitor.geoCheckLocations || monitor.geoCheckLocations.length === 0) {
|
|
|
|
|
this.logger.warn({
|
|
|
|
|
message: "Monitor missing geo check locations",
|
|
|
|
|
service: SERVICE_NAME,
|
2026-02-24 19:19:32 +00:00
|
|
|
method: "buildGeoCheck",
|
2026-02-24 18:10:20 +00:00
|
|
|
details: { monitorId: monitor.id },
|
|
|
|
|
});
|
2026-02-24 19:19:32 +00:00
|
|
|
return null;
|
2026-02-24 18:10:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Step 1: Create measurement request
|
|
|
|
|
const measurementId = await this.globalPingService.createMeasurement(monitor.url, monitor.geoCheckLocations);
|
|
|
|
|
|
|
|
|
|
if (!measurementId) {
|
|
|
|
|
// GlobalPing API is down, skip this check
|
|
|
|
|
this.logger.debug({
|
|
|
|
|
message: "Skipping geo check due to API unavailability",
|
|
|
|
|
service: SERVICE_NAME,
|
2026-02-24 19:19:32 +00:00
|
|
|
method: "buildGeoCheck",
|
2026-02-24 18:10:20 +00:00
|
|
|
details: { monitorId: monitor.id },
|
|
|
|
|
});
|
2026-02-24 19:19:32 +00:00
|
|
|
return null;
|
2026-02-24 18:10:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Step 2: Poll for results
|
|
|
|
|
const results = await this.globalPingService.pollForResults(measurementId);
|
|
|
|
|
|
|
|
|
|
if (results.length === 0) {
|
|
|
|
|
// No successful results (all locations timed out or failed)
|
|
|
|
|
this.logger.debug({
|
|
|
|
|
message: "No successful geo check results",
|
|
|
|
|
service: SERVICE_NAME,
|
2026-02-24 19:19:32 +00:00
|
|
|
method: "buildGeoCheck",
|
2026-02-24 18:10:20 +00:00
|
|
|
details: { monitorId: monitor.id, measurementId },
|
|
|
|
|
});
|
2026-02-24 19:19:32 +00:00
|
|
|
return null;
|
2026-02-24 18:10:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Step 3: Build GeoCheck document
|
2026-02-24 19:19:32 +00:00
|
|
|
const geoCheck = this.createGeoCheckDocument(monitor, results);
|
2026-02-24 18:10:20 +00:00
|
|
|
|
|
|
|
|
this.logger.debug({
|
|
|
|
|
message: `Geo check completed for monitor ${monitor.id}`,
|
|
|
|
|
service: SERVICE_NAME,
|
2026-02-24 19:19:32 +00:00
|
|
|
method: "buildGeoCheck",
|
2026-02-24 18:10:20 +00:00
|
|
|
details: { monitorId: monitor.id, resultsCount: results.length },
|
|
|
|
|
});
|
2026-02-24 19:19:32 +00:00
|
|
|
|
|
|
|
|
return geoCheck;
|
2026-02-24 18:10:20 +00:00
|
|
|
} catch (error: any) {
|
|
|
|
|
this.logger.error({
|
|
|
|
|
message: "Error executing geo check",
|
|
|
|
|
service: SERVICE_NAME,
|
2026-02-24 19:19:32 +00:00
|
|
|
method: "buildGeoCheck",
|
2026-02-24 18:10:20 +00:00
|
|
|
details: { monitorId: monitor.id, error: error.message },
|
|
|
|
|
stack: error.stack,
|
|
|
|
|
});
|
2026-02-24 19:19:32 +00:00
|
|
|
return null;
|
2026-02-24 18:10:20 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-24 19:19:32 +00:00
|
|
|
private createGeoCheckDocument(monitor: Monitor, results: GeoCheckResult[]): GeoCheck {
|
2026-02-24 18:10:20 +00:00
|
|
|
const now = new Date();
|
2026-02-24 19:19:32 +00:00
|
|
|
const ttl = 90 * 24 * 60 * 60 * 1000; // 90 days in ms
|
|
|
|
|
const expiryDate = new Date(now.getTime() + ttl);
|
2026-02-24 18:10:20 +00:00
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
id: new Types.ObjectId().toString(),
|
|
|
|
|
metadata: {
|
|
|
|
|
monitorId: monitor.id,
|
|
|
|
|
teamId: monitor.teamId,
|
|
|
|
|
type: monitor.type,
|
|
|
|
|
},
|
|
|
|
|
results,
|
|
|
|
|
expiry: expiryDate.toISOString(),
|
|
|
|
|
__v: 0,
|
|
|
|
|
createdAt: now.toISOString(),
|
|
|
|
|
updatedAt: now.toISOString(),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
createGeoChecks = async (geoChecks: GeoCheck[]) => {
|
2026-02-24 18:19:51 +00:00
|
|
|
return this.geoChecksRepository.createGeoChecks(geoChecks);
|
2026-02-24 18:10:20 +00:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default GeoChecksService;
|