purge distributed uptime
This commit is contained in:
1 parent
5690d91d59
commit
274595a533
70 files changed
+17
-4494
No files matched your search
@@ -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);
|
||||
@@ -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,
|
||||
|
||||
@@ -18,7 +18,7 @@ const StatusPageSchema = mongoose.Schema(
|
||||
type: String,
|
||||
required: true,
|
||||
default: "uptime",
|
||||
enum: ["uptime", "distributed"],
|
||||
enum: ["uptime"],
|
||||
},
|
||||
companyName: {
|
||||
type: String,
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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,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,
|
||||
};
|
||||
@@ -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: [],
|
||||
},
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
@@ -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;
|
||||
Reference in new issue
Block a user