purge distributed uptime

This commit is contained in:
Alex Holliday committed 2025-06-09 10:17:09 +08:00
1 parent 5690d91d59
commit 274595a533
70 files changed
+17 -4494

No files matched your search

@@ -4,25 +4,11 @@ const SERVICE_NAME = "diagnosticController";
class DiagnosticController {
constructor(db) {
this.db = db;
this.getDistributedUptimeDbExecutionStats =
this.getDistributedUptimeDbExecutionStats.bind(this);
this.getMonitorsByTeamIdExecutionStats =
this.getMonitorsByTeamIdExecutionStats.bind(this);
this.getDbStats = this.getDbStats.bind(this);
}
async getDistributedUptimeDbExecutionStats(req, res, next) {
try {
const data = await this.db.getDistributedUptimeDbExecutionStats(req);
return res.success({
msg: "OK",
data,
});
} catch (error) {
next(handleError(error, SERVICE_NAME, "getDbExecutionStats"));
}
}
async getMonitorsByTeamIdExecutionStats(req, res, next) {
try {
const data = await this.db.getMonitorsByTeamIdExecutionStats(req);
@@ -1,339 +0,0 @@
import { handleError } from "./controllerUtils.js";
import Monitor from "../db/models/Monitor.js";
import DistributedUptimeCheck from "../db/models/DistributedUptimeCheck.js";
const SERVICE_NAME = "DistributedUptimeQueueController";
class DistributedUptimeController {
constructor({ db, http, statusService, logger }) {
this.db = db;
this.http = http;
this.statusService = statusService;
this.logger = logger;
this.resultsCallback = this.resultsCallback.bind(this);
this.getDistributedUptimeMonitors = this.getDistributedUptimeMonitors.bind(this);
this.subscribeToDistributedUptimeMonitors =
this.subscribeToDistributedUptimeMonitors.bind(this);
this.subscribeToDistributedUptimeMonitorDetails =
this.subscribeToDistributedUptimeMonitorDetails.bind(this);
this.getDistributedUptimeMonitorDetails =
this.getDistributedUptimeMonitorDetails.bind(this);
}
async resultsCallback(req, res, next) {
try {
const { id, result } = req.body;
// Calculate response time
const {
first_byte_took,
body_read_took,
dns_took,
conn_took,
connect_took,
tls_took,
status_code,
error,
upt_burnt,
} = result;
// Calculate response time
const responseTime = first_byte_took / 1_000_000;
if (!isFinite(responseTime) || responseTime <= 0 || responseTime > 30000) {
this.logger.info({
message: `Unreasonable response time detected: ${responseTime}ms from first_byte_took: ${first_byte_took}ns`,
service: SERVICE_NAME,
method: "resultsCallback",
});
return;
}
// Calculate if server is up or down
const isErrorStatus = status_code >= 400;
const hasError = error !== "";
const status = isErrorStatus || hasError ? false : true;
// Build response
const distributedUptimeResponse = {
monitorId: id,
type: "distributed_http",
payload: result,
status,
code: status_code,
responseTime,
first_byte_took,
body_read_took,
dns_took,
conn_took,
connect_took,
tls_took,
upt_burnt,
};
if (error) {
const code = status_code || this.NETWORK_ERROR;
distributedUptimeResponse.code = code;
distributedUptimeResponse.message =
this.http.STATUS_CODES[code] || "Network Error";
} else {
distributedUptimeResponse.message = this.http.STATUS_CODES[status_code];
}
await this.statusService.updateStatus(distributedUptimeResponse);
res.status(200).json({ message: "OK" });
} catch (error) {
next(handleError(error, SERVICE_NAME, "resultsCallback"));
}
}
async getDistributedUptimeMonitors(req, res, next) {
try {
const monitors = await this.db.getMonitorsWithChecksByTeamId(req);
return res.success({
msg: "OK",
data: monitors,
});
} catch (error) {
next(handleError(error, SERVICE_NAME, "getDistributedUptimeMonitors"));
}
}
async subscribeToDistributedUptimeMonitors(req, res, next) {
try {
res.setHeader("Content-Type", "text/event-stream");
res.setHeader("Cache-Control", "no-cache");
res.setHeader("Connection", "keep-alive");
res.setHeader("Access-Control-Allow-Origin", "*");
// Disable compression
req.headers["accept-encoding"] = "identity";
res.removeHeader("Content-Encoding");
const BATCH_DELAY = 1000;
let batchTimeout = null;
let opInProgress = false;
let monitorStream = null;
let checksStream = null;
// Do things here
const notifyChange = async () => {
if (opInProgress) {
// Get data
const { count, monitors } = await this.db.getMonitorsWithChecksByTeamId(req);
res.write(`data: ${JSON.stringify({ count, monitors })}\n\n`);
opInProgress = false;
}
batchTimeout = null;
};
const handleChange = () => {
opInProgress = true;
if (batchTimeout) clearTimeout(batchTimeout);
batchTimeout = setTimeout(notifyChange, BATCH_DELAY);
};
const createMonitorStream = () => {
if (monitorStream) {
try {
monitorStream.close();
} catch (error) {
this.logger.error({
message: "Error closing monitor stream",
service: SERVICE_NAME,
method: "subscribeToDistributedUptimeMonitors",
stack: error.stack,
});
}
}
monitorStream = Monitor.watch(
[{ $match: { operationType: { $in: ["insert", "update", "delete"] } } }],
{ fullDocument: "updateLookup" }
);
monitorStream.on("change", handleChange);
monitorStream.on("error", (error) => {
this.logger.error({
message: "Error in monitor stream",
service: SERVICE_NAME,
method: "subscribeToDistributedUptimeMonitors",
stack: error.stack,
});
createMonitorStream();
});
monitorStream.on("close", () => {
monitorStream = null;
});
};
const createChecksStream = () => {
if (checksStream) {
try {
checksStream.close();
} catch (error) {
this.logger.error({
message: "Error closing checks stream",
service: SERVICE_NAME,
method: "subscribeToDistributedUptimeMonitors",
details: error,
});
}
}
checksStream = DistributedUptimeCheck.watch(
[{ $match: { operationType: { $in: ["insert", "update", "delete"] } } }],
{ fullDocument: "updateLookup" }
);
checksStream.on("change", handleChange);
checksStream.on("error", (error) => {
this.logger.error({
message: "Error in checks stream",
service: SERVICE_NAME,
method: "subscribeToDistributedUptimeMonitors",
stack: error.stack,
});
createChecksStream();
});
checksStream.on("close", () => {
checksStream = null;
});
};
createMonitorStream();
createChecksStream();
req.on("close", () => {
if (batchTimeout) {
clearTimeout(batchTimeout);
}
monitorStream.close();
checksStream.close();
clearInterval(keepAlive);
});
// Keep connection alive
const keepAlive = setInterval(() => {
res.write(": keepalive\n\n");
}, 10000);
// Clean up on close
req.on("close", () => {
clearInterval(keepAlive);
});
} catch (error) {
this.logger.error({
message: "Error in subscribeToDistributedUptimeMonitors",
service: SERVICE_NAME,
method: "subscribeToDistributedUptimeMonitors",
stack: error.stack,
});
next(handleError(error, SERVICE_NAME, "subscribeToDistributedUptimeMonitors"));
}
}
async getDistributedUptimeMonitorDetails(req, res, next) {
try {
const monitor = await this.db.getDistributedUptimeDetailsById(req);
return res.success({
msg: "OK",
data: monitor,
});
} catch (error) {
next(handleError(error, SERVICE_NAME, "getDistributedUptimeMonitorDetails"));
}
}
async subscribeToDistributedUptimeMonitorDetails(req, res, next) {
try {
res.setHeader("Content-Type", "text/event-stream");
res.setHeader("Cache-Control", "no-cache");
res.setHeader("Connection", "keep-alive");
res.setHeader("Access-Control-Allow-Origin", "*");
// disable compression
req.headers["accept-encoding"] = "identity";
res.removeHeader("Content-Encoding");
const BATCH_DELAY = 1000;
let batchTimeout = null;
let opInProgress = false;
let checksStream = null;
// Do things here
const notifyChange = async () => {
try {
if (opInProgress) {
// Get data
const monitor = await this.db.getDistributedUptimeDetailsById(req);
res.write(`data: ${JSON.stringify({ monitor })}\n\n`);
opInProgress = false;
}
batchTimeout = null;
} catch (error) {
opInProgress = false;
batchTimeout = null;
this.logger.error({
message: "Error in notifyChange",
service: SERVICE_NAME,
method: "subscribeToDistributedUptimeMonitorDetails",
stack: error.stack,
});
next(handleError(error, SERVICE_NAME, "getDistributedUptimeMonitorDetails"));
}
};
const handleChange = () => {
opInProgress = true;
if (batchTimeout) clearTimeout(batchTimeout);
batchTimeout = setTimeout(notifyChange, BATCH_DELAY);
};
const createCheckStream = () => {
if (checksStream) {
try {
checksStream.close();
} catch (error) {
this.logger.error({
message: "Error closing checks stream",
service: SERVICE_NAME,
method: "subscribeToDistributedUptimeMonitorDetails",
stack: error.stack,
});
}
}
checksStream = DistributedUptimeCheck.watch(
[{ $match: { operationType: { $in: ["insert", "update", "delete"] } } }],
{ fullDocument: "updateLookup" }
);
checksStream.on("change", handleChange);
checksStream.on("error", (error) => {
this.logger.error({
message: "Error in checks stream",
service: SERVICE_NAME,
method: "subscribeToDistributedUptimeMonitorDetails",
stack: error.stack,
});
createCheckStream();
});
checksStream.on("close", () => {
checksStream = null;
});
};
createCheckStream();
// Handle client disconnect
req.on("close", () => {
if (batchTimeout) {
clearTimeout(batchTimeout);
}
checksStream.close();
clearInterval(keepAlive);
});
// Keep connection alive
const keepAlive = setInterval(() => {
res.write(": keepalive\n\n");
}, 10000);
} catch (error) {
next(handleError(error, SERVICE_NAME, "getDistributedUptimeMonitorDetails"));
}
}
}
export default DistributedUptimeController;
+2 -10
View File
@@ -22,7 +22,6 @@ import logger from "../utils/logger.js";
import { handleError, handleValidationError } from "./controllerUtils.js";
import axios from "axios";
import seedDb from "../db/mongo/utils/seedDb.js";
import { seedDistributedTest } from "../db/mongo/utils/seedDb.js";
const SERVICE_NAME = "monitorController";
import pkg from "papaparse";
@@ -431,10 +430,6 @@ class MonitorController {
name: "deleteHardwareChecks",
fn: () => this.db.deleteHardwareChecksByMonitorId(monitor._id),
},
{
name: "deleteDistributedUptimeChecks",
fn: () => this.db.deleteDistributedChecksByMonitorId(monitor._id),
},
// TODO We don't actually want to delete the status page if there are other monitors in it
// We actually just want to remove the monitor being deleted from the status page.
@@ -743,11 +738,8 @@ class MonitorController {
const token = getTokenFromHeaders(req.headers);
const { jwtSecret } = this.settingsService.getSettings();
const { _id, teamId } = jwt.verify(token, jwtSecret);
if (type === "distributed_test") {
await seedDistributedTest(_id, teamId);
} else {
await seedDb(_id, teamId);
}
await seedDb(_id, teamId);
res.success({ msg: "Database seeded" });
} catch (error) {
next(handleError(error, SERVICE_NAME, "seedDb"));
@@ -71,29 +71,6 @@ class StatusPageController {
}
};
getDistributedStatusPageByUrl = async (req, res, next) => {
try {
await getStatusPageParamValidation.validateAsync(req.params);
await getStatusPageQueryValidation.validateAsync(req.query);
} catch (error) {
next(handleValidationError(error, SERVICE_NAME));
return;
}
try {
const statusPage = await this.db.getDistributedStatusPageByUrl({
url: req.params.url,
daysToShow: req.params.timeFrame,
});
return res.success({
msg: this.stringService.statusPageByUrl,
data: statusPage,
});
} catch (error) {
next(handleError(error, SERVICE_NAME, "getDistributedStatusPageByUrl"));
}
};
getStatusPageByUrl = async (req, res, next) => {
try {
await getStatusPageParamValidation.validateAsync(req.params);
-137
View File
@@ -1,137 +0,0 @@
import mongoose from "mongoose";
import { BaseCheckSchema } from "./Check.js";
// {
// "id": "12123",
// "result": {
// "task_arrived": "2025-01-13T19:21:37.463466602Z",
// "dns_start": "2025-01-14T00:21:33.1801319+05:00",
// "dns_end": "2025-01-14T00:21:33.4582552+05:00",
// "conn_start": "2025-01-14T00:21:33.1801319+05:00",
// "conn_end": "2025-01-14T00:21:33.7076318+05:00",
// "connect_start": "2025-01-14T00:21:33.4582552+05:00",
// "connect_end": "2025-01-14T00:21:33.541899+05:00",
// "tls_hand_shake_start": "2025-01-14T00:21:33.541899+05:00",
// "tls_hand_shake_end": "2025-01-14T00:21:33.7076318+05:00",
// "body_read_start": "2025-01-14T00:21:34.1894707+05:00",
// "body_read_end": "2025-01-14T00:21:34.1894707+05:00",
// "wrote_request": "2025-01-14T00:21:33.7076318+05:00",
// "got_first_response_byte": "2025-01-14T00:21:34.1327652+05:00",
// "first_byte_took": 425133400,
// "body_read_took": 56030000,
// "dns_took": 278123300,
// "conn_took": 527499900,
// "connect_took": 83643800,
// "tls_took": 165732800,
// "sni_name": "uprock.com",
// "status_code": 200,
// "body_size": 19320,
// "request_header_size": 95,
// "response_header_size": 246,
// "response_headers": "X-Vercel-Id: bom1::iad1::sm87v-1736796096856-aec270c01f23\nDate: Mon, 13 Jan 2025 19:21:37 GMT\nServer: Vercel\nStrict-Transport-Security: max-age=63072000\nVary: RSC, Next-Router-State-Tree, Next-Router-Prefetch, Next-Url\nX-Matched-Path: /\nX-Powered-By: Next.js\nX-Vercel-Cache: MISS\nAge: 0\nCache-Control: private, no-cache, no-store, max-age=0, must-revalidate\nContent-Type: text/html; charset=utf-8",
// "error": "",
// "device_id": "d5f578e143a2cd603dd6bf5f846a86a538bde4a8fbe2ad1fca284ad9f033daf8",
// "ip_address": "223.123.19.0",
// "proof": "",
// "created_at": "2025-01-13T19:21:37.463466912Z",
// "continent": "AS",
// "country_code": "PK",
// "city": "Muzaffargarh",
// "upt_burnt" : "0.01",
// "location": {
// "lat": 71.0968,
// "lng": 30.0208
// },
// "payload": {
// "callback": "https://webhook.site/2a15b0af-545a-4ac2-b913-153b97592d7a",
// "x": "y"
// }
// }
// }
const LocationSchema = new mongoose.Schema(
{
lat: { type: Number, required: true },
lng: { type: Number, required: true },
},
{ _id: false }
);
const DistributedUptimeCheckSchema = mongoose.Schema(
{
...BaseCheckSchema.obj,
first_byte_took: {
type: Number,
required: false,
},
body_read_took: {
type: Number,
required: false,
},
dns_took: {
type: Number,
required: false,
},
conn_took: {
type: Number,
required: false,
},
connect_took: {
type: Number,
required: false,
},
tls_took: {
type: Number,
required: false,
},
location: {
type: LocationSchema,
required: false,
},
continent: {
type: String,
required: false,
},
countryCode: {
type: String,
required: false,
},
city: {
type: String,
required: false,
},
uptBurnt: {
type: mongoose.Schema.Types.Decimal128,
required: false,
},
count: {
type: Number,
required: false,
},
},
{ timestamps: true }
);
DistributedUptimeCheckSchema.pre("save", function (next) {
if (this.isModified("uptBurnt") && typeof this.uptBurnt === "string") {
this.uptBurnt = mongoose.Types.Decimal128.fromString(this.uptBurnt);
}
next();
});
DistributedUptimeCheckSchema.index({ createdAt: 1 });
DistributedUptimeCheckSchema.index({ monitorId: 1, updatedAt: 1 });
DistributedUptimeCheckSchema.index({ monitorId: 1, updatedAt: -1 });
DistributedUptimeCheckSchema.index(
{
monitorId: 1,
createdAt: -1,
city: 1,
"location.lat": 1,
"location.lng": 1,
responseTime: 1,
},
{ background: true }
);
export default mongoose.model("DistributedUptimeCheck", DistributedUptimeCheckSchema);
+1 -10
View File
@@ -28,16 +28,7 @@ const MonitorSchema = mongoose.Schema(
type: {
type: String,
required: true,
enum: [
"http",
"ping",
"pagespeed",
"hardware",
"docker",
"port",
"distributed_http",
"distributed_test",
],
enum: ["http", "ping", "pagespeed", "hardware", "docker", "port"],
},
ignoreTlsErrors: {
type: Boolean,
+1 -1
View File
@@ -18,7 +18,7 @@ const StatusPageSchema = mongoose.Schema(
type: String,
required: true,
default: "uptime",
enum: ["uptime", "distributed"],
enum: ["uptime"],
},
companyName: {
type: String,
-6
View File
@@ -43,11 +43,6 @@ import * as hardwareCheckModule from "./modules/hardwareCheckModule.js";
import * as checkModule from "./modules/checkModule.js";
//****************************************
// Distributed Checks
//****************************************
import * as distributedCheckModule from "./modules/distributedCheckModule.js";
//****************************************
// Maintenance Window
//****************************************
@@ -85,7 +80,6 @@ class MongoDB {
Object.assign(this, pageSpeedCheckModule);
Object.assign(this, hardwareCheckModule);
Object.assign(this, checkModule);
Object.assign(this, distributedCheckModule);
Object.assign(this, maintenanceWindowModule);
Object.assign(this, notificationModule);
Object.assign(this, settingsModule);
+1 -9
View File
@@ -2,7 +2,6 @@ import Check from "../../models/Check.js";
import Monitor from "../../models/Monitor.js";
import HardwareCheck from "../../models/HardwareCheck.js";
import PageSpeedCheck from "../../models/PageSpeedCheck.js";
import DistributedUptimeCheck from "../../models/DistributedUptimeCheck.js";
import User from "../../models/User.js";
import logger from "../../../utils/logger.js";
import { ObjectId } from "mongodb";
@@ -111,8 +110,6 @@ const getChecksByMonitor = async (req) => {
port: Check,
pagespeed: PageSpeedCheck,
hardware: HardwareCheck,
distributed_http: DistributedUptimeCheck,
distributed_test: DistributedUptimeCheck,
};
const Model = checkModels[type];
@@ -202,12 +199,7 @@ const getChecksByTeam = async (req) => {
pipeline: [{ $match: matchStage }],
},
},
{
$unionWith: {
coll: "distributeduptimechecks",
pipeline: [{ $match: matchStage }],
},
},
{ $sort: { createdAt: sortOrder } },
{
$facet: {
+1 -49
View File
@@ -1,4 +1,3 @@
import DistributedUptimeCheck from "../../models/DistributedUptimeCheck.js";
import Monitor from "../../models/Monitor.js";
import { ObjectId } from "mongodb";
@@ -12,53 +11,6 @@ import {
} from "./monitorModuleQueries.js";
import { getDateRange } from "./monitorModule.js";
const getDistributedUptimeDbExecutionStats = async (req) => {
try {
const { monitorId } = req?.params ?? {};
if (typeof monitorId === "undefined") {
throw new Error();
}
const monitor = await Monitor.findById(monitorId);
if (monitor === null || monitor === undefined) {
throw new Error(this.stringService.dbFindMonitorById(monitorId));
}
const { dateRange } = req.query;
const dates = getDateRange(dateRange);
const formatLookup = {
recent: "%Y-%m-%dT%H:%M:00Z",
day: {
$concat: [
{ $dateToString: { format: "%Y-%m-%dT%H:", date: "$createdAt" } },
{
$cond: [{ $lt: [{ $minute: "$createdAt" }, 30] }, "00:00Z", "30:00Z"],
},
],
},
week: "%Y-%m-%dT%H:00:00Z",
month: "%Y-%m-%dT00:00:00Z",
};
const dateString = formatLookup[dateRange];
const dePINDetailsByDateRangeStats = await DistributedUptimeCheck.aggregate(
buildDePINDetailsByDateRange(monitor, dates, dateString)
).explain("executionStats");
const latestChecksStats = await DistributedUptimeCheck.aggregate(
buildDePINLatestChecks(monitor)
).explain("executionStats");
return {
dePINDetailsByDateRangeStats,
latestChecksStats,
};
} catch (error) {
error.service = SERVICE_NAME;
error.method = "getAllMonitorsWithUptimeStats";
throw error;
}
};
const getMonitorsByTeamIdExecutionStats = async (req) => {
try {
let { limit, type, page, rowsPerPage, filter, field, order } = req.query;
@@ -104,4 +56,4 @@ const getMonitorsByTeamIdExecutionStats = async (req) => {
}
};
export { getDistributedUptimeDbExecutionStats, getMonitorsByTeamIdExecutionStats };
export { getMonitorsByTeamIdExecutionStats };
@@ -1,156 +0,0 @@
import DistributedUptimeCheck from "../../models/DistributedUptimeCheck.js";
import { ObjectId } from "mongodb";
const SERVICE_NAME = "distributedCheckModule";
const createDistributedCheck = async (checkData) => {
try {
if (typeof checkData.monitorId === "string") {
checkData.monitorId = ObjectId.createFromHexString(checkData.monitorId);
}
const check = await DistributedUptimeCheck.findOneAndUpdate(
{
monitorId: checkData.monitorId,
city: checkData.city,
},
[
{
$set: {
...checkData,
responseTime: {
$cond: {
if: { $ifNull: ["$count", false] },
then: {
$cond: {
// Check if the new value is an outlier (3x the current average)
if: {
$and: [
{ $gt: ["$responseTime", 0] },
{
$gt: [
checkData.responseTime,
{ $multiply: ["$responseTime", 3] },
],
},
],
},
then: "$responseTime", // Keep the current value if it's an outlier
else: {
// Normal case - calculate new average
$round: [
{
$divide: [
{
$add: [
{ $multiply: ["$responseTime", "$count"] },
checkData.responseTime,
],
},
{ $add: ["$count", 1] },
],
},
2,
],
},
},
},
else: checkData.responseTime,
},
},
count: { $add: [{ $ifNull: ["$count", 0] }, 1] },
},
},
],
{
upsert: true,
new: true,
runValidators: true,
}
);
return check;
} catch (error) {
error.service = SERVICE_NAME;
error.method = "createCheck";
throw error;
}
};
const createDistributedChecks = async (checksData) => {
try {
if (!Array.isArray(checksData) || checksData.length === 0) {
return;
}
const bulkOps = checksData.map((checkData) => {
if (typeof checkData.monitorId === "string") {
checkData.monitorId = ObjectId.createFromHexString(checkData.monitorId);
}
return {
updateOne: {
filter: {
monitorId: checkData.monitorId,
city: checkData.city,
},
update: [
{
$set: {
...checkData,
responseTime: {
$cond: {
if: { $ifNull: ["$count", false] },
then: {
$round: [
{
$divide: [
{
$add: [
{ $multiply: ["$responseTime", "$count"] },
checkData.responseTime,
],
},
{ $add: ["$count", 1] },
],
},
2,
],
},
else: checkData.responseTime,
},
},
count: { $add: [{ $ifNull: ["$count", 0] }, 1] },
},
},
],
upsert: true,
},
};
});
await DistributedUptimeCheck.bulkWrite(bulkOps, {
ordered: false,
});
} catch (error) {
error.service = SERVICE_NAME;
error.method = "createDistributedChecks";
throw error;
}
};
const deleteDistributedChecksByMonitorId = async (monitorId) => {
try {
const result = await DistributedUptimeCheck.deleteMany({ monitorId });
return result.deletedCount;
} catch (error) {
error.service = SERVICE_NAME;
error.method = "deleteDistributedChecksByMonitorId";
throw error;
}
};
export {
createDistributedCheck,
createDistributedChecks,
deleteDistributedChecksByMonitorId,
};
-68
View File
@@ -3,7 +3,6 @@ import MonitorStats from "../../models/MonitorStats.js";
import Check from "../../models/Check.js";
import PageSpeedCheck from "../../models/PageSpeedCheck.js";
import HardwareCheck from "../../models/HardwareCheck.js";
import DistributedUptimeCheck from "../../models/DistributedUptimeCheck.js";
import Notification from "../../models/Notification.js";
import { NormalizeData, NormalizeDataUptimeDetails } from "../../../utils/dataUtils.js";
import ServiceRegistry from "../../../service/serviceRegistry.js";
@@ -385,72 +384,6 @@ const getUptimeDetailsById = async (req) => {
}
};
const getDistributedUptimeDetailsById = async (req) => {
try {
const { monitorId } = req?.params ?? {};
if (typeof monitorId === "undefined") {
throw new Error();
}
const monitor = await Monitor.findById(monitorId);
if (monitor === null || monitor === undefined) {
throw new Error(this.stringService.dbFindMonitorById(monitorId));
}
const { dateRange, normalize } = req.query;
const dates = getDateRange(dateRange);
const formatLookup = {
recent: "%Y-%m-%dT%H:%M:00Z",
day: {
$concat: [
{ $dateToString: { format: "%Y-%m-%dT%H:", date: "$updatedAt" } },
{
$cond: [{ $lt: [{ $minute: "$updatedAt" }, 30] }, "00:00Z", "30:00Z"],
},
],
},
week: "%Y-%m-%dT%H:00:00Z",
month: "%Y-%m-%dT00:00:00Z",
};
const dateString = formatLookup[dateRange];
const monitorStatsResult = await MonitorStats.aggregate(
buildMonitorStatsPipeline(monitor)
);
const monitorStats = monitorStatsResult[0];
const dePINDetailsByDateRange = await DistributedUptimeCheck.aggregate(
buildDePINDetailsByDateRange(monitor, dates, dateString)
);
const latestChecks = await DistributedUptimeCheck.aggregate(
buildDePINLatestChecks(monitor)
);
const checkData = dePINDetailsByDateRange[0];
const normalizedGroupChecks = NormalizeDataUptimeDetails(
checkData.groupedChecks,
10,
100
);
const data = {
...monitor.toObject(),
latestChecks,
totalChecks: monitorStats?.totalChecks,
avgResponseTime: monitorStats?.avgResponseTime,
uptimePercentage: monitorStats?.uptimePercentage,
timeSinceLastCheck: monitorStats?.timeSinceLastCheck,
uptBurnt: monitorStats?.uptBurnt,
groupedChecks: normalizedGroupChecks,
groupedMapChecks: checkData.groupedMapChecks,
};
return data;
} catch (error) {
error.service = SERVICE_NAME;
error.method = "getDistributedUptimeDetailsById";
throw error;
}
};
/**
* Get stats by monitor ID
* @async
@@ -876,7 +809,6 @@ export {
getMonitorsAndSummaryByTeamId,
getMonitorsWithChecksByTeamId,
getUptimeDetailsById,
getDistributedUptimeDetailsById,
createMonitor,
createBulkMonitors,
deleteMonitor,
@@ -613,8 +613,6 @@ const buildMonitorsWithChecksByTeamIdPipeline = ({
checksCollection = "pagespeedchecks";
} else if (type === "hardware") {
checksCollection = "hardwarechecks";
} else if (type === "distributed_http" || type === "distributed_test") {
checksCollection = "distributeduptimechecks";
}
monitorsPipeline.push({
$lookup: {
@@ -702,8 +700,6 @@ const buildFilteredMonitorsByTeamIdPipeline = ({
checksCollection = "pagespeedchecks";
} else if (type === "hardware") {
checksCollection = "hardwarechecks";
} else if (type === "distributed_http" || type === "distributed_test") {
checksCollection = "distributeduptimechecks";
}
pipeline.push({
$lookup: {
@@ -861,26 +857,6 @@ const buildGetMonitorsByTeamIdPipeline = (req) => {
},
]
: []),
...(limit
? [
{
$lookup: {
from: "distributeduptimechecks",
let: { monitorId: "$_id" },
pipeline: [
{
$match: {
$expr: { $eq: ["$monitorId", "$$monitorId"] },
},
},
{ $sort: { createdAt: -1 } },
...(limit ? [{ $limit: limit }] : []),
],
as: "distributeduptimechecks",
},
},
]
: []),
{
$addFields: {
@@ -899,14 +875,6 @@ const buildGetMonitorsByTeamIdPipeline = (req) => {
case: { $eq: ["$type", "hardware"] },
then: "$hardwarechecks",
},
{
case: { $eq: ["$type", "distributed_http"] },
then: "$distributeduptimechecks",
},
{
case: { $eq: ["$type", "distributed_test"] },
then: "$distributeduptimechecks",
},
],
default: [],
},
-100
View File
@@ -61,105 +61,6 @@ const updateStatusPage = async (statusPageData, image) => {
}
};
const getDistributedStatusPageByUrl = async ({ url, daysToShow = 30 }) => {
const stringService = ServiceRegistry.get(StringService.SERVICE_NAME);
try {
const statusPage = await StatusPage.findOne({ url }).lean();
if (!statusPage) {
const error = new Error(stringService.statusPageNotFound);
error.status = 404;
throw error;
}
// No sub monitors, return status page
if (statusPage.subMonitors.length === 0) {
return statusPage;
}
// Sub monitors, return status page with sub monitors
const daysAgo = new Date();
daysAgo.setDate(daysAgo.getDate() - daysToShow);
const subMonitors = await Monitor.aggregate([
{ $match: { _id: { $in: statusPage.subMonitors } } },
{
$addFields: {
orderIndex: { $indexOfArray: [statusPage.subMonitors, "$_id"] },
},
},
// Return 30 days of checks by default
{
$lookup: {
from: "checks",
let: { monitorId: "$_id" },
pipeline: [
{
$match: {
$expr: {
$and: [
{ $eq: ["$monitorId", "$$monitorId"] },
{ $gte: ["$updatedAt", daysAgo] },
],
},
},
},
{
$group: {
_id: {
$dateToString: { format: "%Y-%m-%d", date: "$updatedAt" },
},
responseTime: {
$avg: "$responseTime",
},
trueCount: {
$sum: {
$cond: [{ $eq: ["$status", true] }, 1, 0],
},
},
totalCount: {
$sum: 1,
},
},
},
{
$project: {
_id: 1,
responseTime: 1,
upPercentage: {
$cond: [
{ $eq: ["$totalCount", 0] },
0,
{ $multiply: [{ $divide: ["$trueCount", "$totalCount"] }, 100] },
],
},
},
},
{
$sort: { _id: -1 },
},
],
as: "checks",
},
},
{ $sort: { orderIndex: 1 } },
{ $project: { orderIndex: 0 } },
]);
const normalizedSubMonitors = subMonitors.map((monitor) => {
return {
...monitor,
checks: NormalizeData(monitor.checks, 10, 100),
};
});
return { ...statusPage, subMonitors: normalizedSubMonitors };
} catch (error) {
error.service = SERVICE_NAME;
error.method = "getDistributedStatusPageByUrl";
throw error;
}
};
const getStatusPageByUrl = async (url, type) => {
// TODO This is deprecated, can remove and have controller call getStatusPage
try {
@@ -306,7 +207,6 @@ export {
getStatusPagesByTeamId,
getStatusPage,
getStatusPageByUrl,
getDistributedStatusPageByUrl,
deleteStatusPage,
deleteStatusPagesByMonitorId,
};
-102
View File
@@ -1,6 +1,5 @@
import Monitor from "../../models/Monitor.js";
import Check from "../../models/Check.js";
import DistributedUptimeCheck from "../../models/DistributedUptimeCheck.js";
import logger from "../../../utils/logger.js";
const generateRandomUrl = () => {
@@ -73,105 +72,4 @@ const seedDb = async (userId, teamId) => {
}
};
const generateDistributedChecks = (monitorId, teamId, count = 2880) => {
const checks = [];
const endTime = new Date();
const startTime = new Date(endTime - 48 * 60 * 60 * 1000);
// Sample locations for variety
const locations = [
{
city: "New York",
countryCode: "US",
continent: "NA",
location: { lat: 40.7128, lng: -74.006 },
},
{
city: "London",
countryCode: "GB",
continent: "EU",
location: { lat: 51.5074, lng: -0.1278 },
},
{
city: "Singapore",
countryCode: "SG",
continent: "AS",
location: { lat: 1.3521, lng: 103.8198 },
},
];
for (let i = 0; i < count; i++) {
const timestamp = new Date(startTime.getTime() + i * 60 * 1000);
const location = locations[Math.floor(Math.random() * locations.length)];
const status = Math.random() > 0.05; // 95% success rate
checks.push({
monitorId,
teamId,
status,
responseTime: Math.floor(Math.random() * 1000), // Random response time between 0-1000ms
first_byte_took: Math.floor(Math.random() * 300000), // 0-300ms
body_read_took: Math.floor(Math.random() * 100000), // 0-100ms
dns_took: Math.floor(Math.random() * 100000), // 0-100ms
conn_took: Math.floor(Math.random() * 200000), // 0-200ms
connect_took: Math.floor(Math.random() * 150000), // 0-150ms
tls_took: Math.floor(Math.random() * 200000), // 0-200ms
location: location.location,
continent: location.continent,
countryCode: location.countryCode,
city: location.city,
uptBurnt: "0.01", // Will be converted to Decimal128 by the schema
createdAt: timestamp,
updatedAt: timestamp,
});
}
return checks;
};
export const seedDistributedTest = async (userId, teamId) => {
try {
logger.info({
message: "Deleting all test monitors and checks",
service: "DB",
method: "seedDistributedTest",
});
const testMonitors = await Monitor.find({
type: "distributed_test",
});
testMonitors.forEach(async (monitor) => {
await DistributedUptimeCheck.deleteMany({ monitorId: monitor._id });
await Monitor.deleteOne({ _id: monitor._id });
});
logger.info({
message: "Adding test monitors and checks",
service: "DB",
method: "seedDistributedTest",
});
const monitor = await Monitor.create({
name: "Distributed Test",
url: "https://distributed-test.com",
type: "distributed_test",
userId,
teamId,
interval: 60000,
active: false,
});
const checks = generateDistributedChecks(monitor._id, teamId, 2800);
await DistributedUptimeCheck.insertMany(checks);
return monitor;
} catch (error) {
logger.error({
message: "Error seeding distributed test",
service: "DB",
method: "seedDistributedTest",
stack: error.stack,
});
throw error;
}
};
export default seedDb;
+1 -14
View File
@@ -36,9 +36,6 @@ import StatusPageController from "./controllers/statusPageController.js";
import QueueRoutes from "./routes/queueRoute.js";
import QueueController from "./controllers/queueController.js";
import DistributedUptimeRoutes from "./routes/distributedUptimeRoute.js";
import DistributedUptimeController from "./controllers/distributedUptimeController.js";
import NotificationRoutes from "./routes/notificationRoute.js";
import NotificationController from "./controllers/notificationController.js";
@@ -300,13 +297,6 @@ const startApp = async () => {
ServiceRegistry.get(StatusService.SERVICE_NAME)
);
const distributedUptimeController = new DistributedUptimeController({
db: ServiceRegistry.get(MongoDB.SERVICE_NAME),
http,
statusService: ServiceRegistry.get(StatusService.SERVICE_NAME),
logger,
});
const diagnosticController = new DiagnosticController(
ServiceRegistry.get(MongoDB.SERVICE_NAME)
);
@@ -322,9 +312,7 @@ const startApp = async () => {
);
const queueRoutes = new QueueRoutes(queueController);
const statusPageRoutes = new StatusPageRoutes(statusPageController);
const distributedUptimeRoutes = new DistributedUptimeRoutes(
distributedUptimeController
);
const notificationRoutes = new NotificationRoutes(notificationController);
const diagnosticRoutes = new DiagnosticRoutes(diagnosticController);
// Middleware
@@ -375,7 +363,6 @@ const startApp = async () => {
app.use("/api/v1/checks", verifyJWT, checkRoutes.getRouter());
app.use("/api/v1/maintenance-window", verifyJWT, maintenanceWindowRoutes.getRouter());
app.use("/api/v1/queue", verifyJWT, queueRoutes.getRouter());
app.use("/api/v1/distributed-uptime", distributedUptimeRoutes.getRouter());
app.use("/api/v1/status-page", statusPageRoutes.getRouter());
app.use("/api/v1/notifications", verifyJWT, notificationRoutes.getRouter());
app.use("/api/v1/diagnostic", verifyJWT, diagnosticRoutes.getRouter());
-5
View File
@@ -7,11 +7,6 @@ class DiagnosticRoutes {
this.initRoutes();
}
initRoutes() {
this.router.get(
"/db/execution-stats/:monitorId",
this.diagnosticController.getDistributedUptimeDbExecutionStats
);
this.router.get(
"/db/get-monitors-by-team-id/:teamId",
this.diagnosticController.getMonitorsByTeamIdExecutionStats
-44
View File
@@ -1,44 +0,0 @@
import { Router } from "express";
import { verifyJWT } from "../middleware/verifyJWT.js";
class DistributedUptimeRoutes {
constructor(distributedUptimeController) {
this.router = Router();
this.distributedUptimeController = distributedUptimeController;
this.initRoutes();
}
initRoutes() {
this.router.post("/callback", this.distributedUptimeController.resultsCallback);
this.router.get(
"/monitors/:teamId/initial",
verifyJWT,
this.distributedUptimeController.getDistributedUptimeMonitors
);
this.router.get(
"/monitors/:teamId",
this.distributedUptimeController.subscribeToDistributedUptimeMonitors
);
this.router.get(
"/monitors/details/:monitorId/initial",
verifyJWT,
this.distributedUptimeController.getDistributedUptimeMonitorDetails
);
this.router.get(
"/monitors/details/public/:monitorId/initial",
this.distributedUptimeController.getDistributedUptimeMonitorDetails
);
this.router.get(
"/monitors/details/:monitorId",
this.distributedUptimeController.subscribeToDistributedUptimeMonitorDetails
);
}
getRouter() {
return this.router;
}
}
export default DistributedUptimeRoutes;
-4
View File
@@ -18,10 +18,6 @@ class StatusPageRoutes {
this.statusPageController.getStatusPagesByTeamId
);
this.router.get("/:url", this.statusPageController.getStatusPageByUrl);
this.router.get(
"/distributed/:url",
this.statusPageController.getDistributedStatusPageByUrl
);
this.router.post(
"/",
+1 -2
View File
@@ -1,4 +1,4 @@
const QUEUE_NAMES = ["uptime", "pagespeed", "hardware", "distributed"];
const QUEUE_NAMES = ["uptime", "pagespeed", "hardware"];
const SERVICE_NAME = "JobQueue";
const HEALTH_CHECK_INTERVAL = 10 * 60 * 1000; // 10 minutes
const QUEUE_LOOKUP = {
@@ -8,7 +8,6 @@ const QUEUE_LOOKUP = {
port: "uptime",
docker: "uptime",
pagespeed: "pagespeed",
distributed_http: "distributed",
};
const getSchedulerId = (monitor) => `scheduler:${monitor.type}:${monitor._id}`;
@@ -256,13 +256,6 @@ class JobQueueHelper {
await job.updateProgress(30);
const monitor = job.data;
const networkResponse = await this.networkService.getStatus(monitor);
if (
job.data.type === "distributed_http" ||
job.data.type === "distributed_test"
) {
await job.updateProgress(100);
return true;
}
// If the network response is not found, we're done
if (!networkResponse) {
@@ -29,10 +29,6 @@ class PulseQueueHelper {
}
const networkResponse = await this.networkService.getStatus(monitor);
if (monitor.type === "distributed_http" || monitor.type === "distributed_test") {
await job.updateProgress(100);
return true;
}
if (!networkResponse) {
throw new Error("No network response");
-3
View File
@@ -7,7 +7,6 @@ const TYPE_MAP = {
docker: "checks",
pagespeed: "pagespeedChecks",
hardware: "hardwareChecks",
distributed_http: "distributedChecks",
};
class BufferService {
@@ -19,13 +18,11 @@ class BufferService {
checks: [],
pagespeedChecks: [],
hardwareChecks: [],
distributedChecks: [],
};
this.OPERATION_MAP = {
checks: this.db.createChecks,
pagespeedChecks: this.db.createPageSpeedChecks,
hardwareChecks: this.db.createHardwareChecks,
distributedChecks: this.db.createDistributedChecks,
};
this.scheduleNextFlush();
-34
View File
@@ -23,8 +23,6 @@ class NetworkService {
this.TYPE_HARDWARE = "hardware";
this.TYPE_DOCKER = "docker";
this.TYPE_PORT = "port";
this.TYPE_DISTRIBUTED_HTTP = "distributed_http";
this.TYPE_DISTRIBUTED_TEST = "distributed_test";
this.SERVICE_NAME = SERVICE_NAME;
this.NETWORK_ERROR = 5000;
this.PING_ERROR = 5001;
@@ -414,34 +412,6 @@ class NetworkService {
}
}
async requestDistributedHttp(monitor) {
try {
const CALLBACK_URL = process.env.CALLBACK_URL;
const response = await this.axios.post(
UPROCK_ENDPOINT,
{
id: monitor._id,
url: monitor.url,
callback: `${CALLBACK_URL}/api/v1/distributed-uptime/callback`,
},
{
headers: {
"Content-Type": "application/json",
"x-checkmate-key": process.env.UPROCK_API_KEY,
},
}
);
if (response.data.success === false) {
throw new Error(response.data.message);
}
} catch (error) {
error.service = this.SERVICE_NAME;
error.method = "requestDistributedHttp";
throw error;
}
}
/**
* Handles unsupported job types by throwing an error with details.
*
@@ -517,10 +487,6 @@ class NetworkService {
return await this.requestDocker(monitor);
case this.TYPE_PORT:
return await this.requestPort(monitor);
case this.TYPE_DISTRIBUTED_HTTP:
return await this.requestDistributedHttp(monitor);
case this.TYPE_DISTRIBUTED_TEST:
return;
default:
return this.handleUnsupportedType(type);
}
-17
View File
@@ -209,23 +209,6 @@ class StatusService {
tls_took,
};
if (type === "distributed_http") {
if (typeof payload === "undefined") {
return undefined;
}
check.continent = payload.continent;
check.countryCode = payload.country_code;
check.city = payload.city;
check.location = payload.location;
check.uptBurnt = payload.upt_burnt;
check.first_byte_took = payload.first_byte_took;
check.body_read_took = payload.body_read_took;
check.dns_took = payload.dns_took;
check.conn_took = payload.conn_took;
check.connect_took = payload.connect_took;
check.tls_took = payload.tls_took;
}
if (type === "pagespeed") {
if (typeof payload === "undefined") {
this.logger.warn({
+3 -14
View File
@@ -303,18 +303,7 @@ const getChecksParamValidation = joi.object({
});
const getChecksQueryValidation = joi.object({
type: joi
.string()
.valid(
"http",
"ping",
"pagespeed",
"hardware",
"docker",
"port",
"distributed_http",
"distributed_test"
),
type: joi.string().valid("http", "ping", "pagespeed", "hardware", "docker", "port"),
sortOrder: joi.string().valid("asc", "desc"),
limit: joi.number(),
dateRange: joi.string().valid("recent", "hour", "day", "week", "month", "all"),
@@ -446,14 +435,14 @@ const getStatusPageParamValidation = joi.object({
});
const getStatusPageQueryValidation = joi.object({
type: joi.string().valid("uptime", "distributed").required(),
type: joi.string().valid("uptime").required(),
timeFrame: joi.number().optional(),
});
const createStatusPageBodyValidation = joi.object({
userId: joi.string().required(),
teamId: joi.string().required(),
type: joi.string().valid("uptime", "distributed").required(),
type: joi.string().valid("uptime").required(),
companyName: joi.string().required(),
url: joi
.string()