Merge pull request #3818 from bluewave-labs/feat/chart-cleanup

feat: chart cleanup
This commit is contained in:
Alexander Holliday authored and GitHub committed 2026-07-30 12:16:24 -07:00
commit 093553f9ba
9 files changed
+605 -241

No files matched your search

+343 -13
View File
@@ -536,6 +536,341 @@
"updatedAt": "2026-04-15T14:30:00.000Z"
}
},
"UptimeDetails": {
"type": "object",
"properties": {
"monitorData": {
"type": "object",
"properties": {
"monitor": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"description": {
"type": "string"
},
"type": {
"type": "string",
"enum": ["http", "ping", "pagespeed", "hardware", "docker", "port", "game", "grpc", "websocket", "dns", "unknown"]
},
"url": {
"type": "string"
},
"port": {
"type": "number"
},
"isActive": {
"type": "boolean"
},
"interval": {
"type": "number"
},
"status": {
"type": "string",
"enum": ["up", "down", "paused", "initializing", "maintenance", "breached"]
},
"statusWindowSize": {
"type": "number"
},
"statusWindowThreshold": {
"type": "number"
},
"ignoreTlsErrors": {
"type": "boolean"
},
"useAdvancedMatching": {
"type": "boolean"
},
"jsonPath": {
"type": "string"
},
"expectedValue": {
"type": "string"
},
"matchMethod": {
"type": "string",
"enum": ["equal", "include", "regex"]
},
"method": {
"type": "string",
"enum": ["GET", "HEAD"]
},
"notifications": {
"type": "array",
"items": {
"type": "string"
}
},
"tags": {
"type": "array",
"items": {
"type": "string"
}
},
"customUpCodes": {
"type": "array",
"items": {
"type": "number"
}
},
"secret": {
"type": "string"
},
"cpuAlertThreshold": {
"type": "number"
},
"memoryAlertThreshold": {
"type": "number"
},
"diskAlertThreshold": {
"type": "number"
},
"tempAlertThreshold": {
"type": "number"
},
"selectedDisks": {
"type": "array",
"items": {
"type": "string"
}
},
"gameId": {
"type": "string"
},
"grpcServiceName": {
"type": "string"
},
"group": {
"type": "string",
"nullable": true
},
"geoCheckEnabled": {
"type": "boolean"
},
"geoCheckLocations": {
"type": "array",
"items": {
"type": "string",
"enum": ["EU", "NA", "AS", "SA", "AF", "OC"]
}
},
"geoCheckInterval": {
"type": "number"
},
"dnsServer": {
"type": "string"
},
"dnsRecordType": {
"type": "string",
"enum": ["A", "AAAA", "CNAME", "MX", "TXT", "NS"]
},
"teamId": {
"type": "string"
},
"userId": {
"type": "string"
},
"createdAt": {
"type": "string"
},
"updatedAt": {
"type": "string"
},
"lastEvaluatedAt": {
"type": "number"
},
"id": {
"type": "string"
}
},
"required": [
"name",
"type",
"url",
"isActive",
"interval",
"status",
"statusWindowSize",
"statusWindowThreshold",
"ignoreTlsErrors",
"useAdvancedMatching",
"method",
"notifications",
"tags",
"cpuAlertThreshold",
"memoryAlertThreshold",
"diskAlertThreshold",
"tempAlertThreshold",
"selectedDisks",
"geoCheckEnabled",
"geoCheckLocations",
"geoCheckInterval",
"teamId",
"userId",
"createdAt",
"updatedAt",
"lastEvaluatedAt",
"id"
],
"additionalProperties": {
"nullable": true
}
},
"groupedChecks": {
"type": "array",
"items": {
"type": "object",
"properties": {
"bucketDate": {
"type": "string"
},
"avgResponseTime": {
"type": "number"
},
"totalChecks": {
"type": "number"
},
"avgDns": {
"type": "number"
},
"avgTcp": {
"type": "number"
},
"avgTls": {
"type": "number"
},
"avgRequest": {
"type": "number"
},
"avgFirstByte": {
"type": "number"
},
"avgDownload": {
"type": "number"
}
},
"required": [
"bucketDate",
"avgResponseTime",
"totalChecks",
"avgDns",
"avgTcp",
"avgTls",
"avgRequest",
"avgFirstByte",
"avgDownload"
]
}
},
"groupedUpChecks": {
"type": "array",
"items": {
"type": "object",
"properties": {
"bucketDate": {
"type": "string"
},
"avgResponseTime": {
"type": "number"
},
"totalChecks": {
"type": "number"
}
},
"required": ["bucketDate", "avgResponseTime", "totalChecks"]
}
},
"groupedDownChecks": {
"type": "array",
"items": {
"type": "object",
"properties": {
"bucketDate": {
"type": "string"
},
"avgResponseTime": {
"type": "number"
},
"totalChecks": {
"type": "number"
}
},
"required": ["bucketDate", "avgResponseTime", "totalChecks"]
}
},
"groupedAvgResponseTime": {
"type": "number"
},
"groupedUptimePercentage": {
"type": "number"
}
},
"required": ["monitor", "groupedChecks", "groupedUpChecks", "groupedDownChecks", "groupedAvgResponseTime", "groupedUptimePercentage"]
},
"monitorStats": {
"type": "object",
"nullable": true,
"properties": {
"id": {
"type": "string"
},
"monitorId": {
"type": "string"
},
"avgResponseTime": {
"type": "number"
},
"maxResponseTime": {
"type": "number"
},
"totalChecks": {
"type": "number"
},
"totalUpChecks": {
"type": "number"
},
"totalDownChecks": {
"type": "number"
},
"uptimePercentage": {
"type": "number"
},
"lastCheckTimestamp": {
"type": "number"
},
"lastResponseTime": {
"type": "number"
},
"timeOfLastFailure": {
"type": "number"
},
"createdAt": {
"type": "string"
},
"updatedAt": {
"type": "string"
}
},
"required": [
"id",
"monitorId",
"avgResponseTime",
"maxResponseTime",
"totalChecks",
"totalUpChecks",
"totalDownChecks",
"uptimePercentage",
"lastCheckTimestamp",
"lastResponseTime",
"createdAt",
"updatedAt"
]
}
},
"required": ["monitorData", "monitorStats"]
},
"NotificationChannelBody": {
"oneOf": [
{
@@ -2426,6 +2761,9 @@
"minLength": 8,
"pattern": "^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!?@#$%^&*()\\-_=+[\\]{};:'\",.~`|\\\\/])[A-Za-z0-9!?@#$%^&*()\\-_=+[\\]{};:'\",.~`|\\\\/]+$"
},
"deleteProfileImage": {
"type": "boolean"
},
"profileImage": {
"type": "string",
"format": "binary"
@@ -3747,7 +4085,7 @@
{
"schema": {
"type": "string",
"enum": ["manual", "automatic"]
"enum": ["automatic", "manual"]
},
"required": false,
"name": "resolutionType",
@@ -6013,14 +6351,6 @@
"required": true,
"name": "dateRange",
"in": "query"
},
{
"schema": {
"type": "boolean"
},
"required": false,
"name": "normalize",
"in": "query"
}
],
"responses": {
@@ -6039,10 +6369,10 @@
"type": "string"
},
"data": {
"nullable": true
"$ref": "#/components/schemas/UptimeDetails"
}
},
"required": ["success", "msg"]
"required": ["success", "msg", "data"]
}
}
}
@@ -10538,7 +10868,7 @@
},
"theme": {
"type": "string",
"enum": ["refined", "modern", "bold", "editorial"]
"enum": ["refined", "modern", "bold", "editorial", "minimal"]
},
"themeMode": {
"type": "string",
@@ -10756,7 +11086,7 @@
},
"theme": {
"type": "string",
"enum": ["refined", "modern", "bold", "editorial"]
"enum": ["refined", "modern", "bold", "editorial", "minimal"]
},
"themeMode": {
"type": "string",
+4 -1
View File
@@ -16,6 +16,7 @@ import {
getHardwareDetailsByIdParamValidation,
getHardwareDetailsByIdQueryValidation,
monitorResponseSchema,
uptimeDetailsResponseSchema,
} from "@/api/validation/monitorValidation.js";
import { updateNotificationsValidation } from "@/api/validation/notificationValidation.js";
@@ -81,6 +82,8 @@ registry.registerPath({
responses: { "200": okJson(z.array(z.string())), ...standardErrors },
});
const uptimeDetailsObject = uptimeDetailsResponseSchema.openapi("UptimeDetails");
registry.registerPath({
method: "get",
path: "/monitors/uptime/details/{monitorId}",
@@ -88,7 +91,7 @@ registry.registerPath({
summary: "Get uptime details for a monitor",
security: bearer,
request: { params: getUptimeDetailsByIdParamValidation, query: getUptimeDetailsByIdQueryValidation },
responses: { "200": okUnknown, ...standardErrors },
responses: { "200": okJson(uptimeDetailsObject), ...standardErrors },
});
registry.registerPath({
@@ -311,3 +311,52 @@ export const monitorResponseSchema = z
lastEvaluatedAt: z.number(),
})
.passthrough();
// Grouped-check buckets returned by GET /monitors/uptime/details/{monitorId}. Keep
// aligned with GroupedCheck / GroupedUptimeCheck in domain/checks/check.type.ts.
export const groupedCheckResponseSchema = z.object({
bucketDate: z.string(),
avgResponseTime: z.number(),
totalChecks: z.number(),
});
export const groupedUptimeCheckResponseSchema = groupedCheckResponseSchema.extend({
avgDns: z.number(),
avgTcp: z.number(),
avgTls: z.number(),
avgRequest: z.number(),
avgFirstByte: z.number(),
avgDownload: z.number(),
});
// Keep aligned with MonitorStats in domain/monitor-stats/monitor-stats.type.ts.
export const monitorStatsResponseSchema = z.object({
id: z.string(),
monitorId: z.string(),
avgResponseTime: z.number(),
maxResponseTime: z.number(),
totalChecks: z.number(),
totalUpChecks: z.number(),
totalDownChecks: z.number(),
uptimePercentage: z.number(),
lastCheckTimestamp: z.number(),
lastResponseTime: z.number(),
timeOfLastFailure: z.number().optional(),
createdAt: z.string(),
updatedAt: z.string(),
});
// Response of GET /monitors/uptime/details/{monitorId}. Keep aligned with
// UptimeDetailsResult in domain/monitors/monitor.type.ts; the monitor here is the
// repository's domain entity, which serializes `id` rather than `_id`.
export const uptimeDetailsResponseSchema = z.object({
monitorData: z.object({
monitor: monitorResponseSchema.omit({ _id: true }).extend({ id: z.string() }),
groupedChecks: z.array(groupedUptimeCheckResponseSchema),
groupedUpChecks: z.array(groupedCheckResponseSchema),
groupedDownChecks: z.array(groupedCheckResponseSchema),
groupedAvgResponseTime: z.number(),
groupedUptimePercentage: z.number(),
}),
monitorStats: monitorStatsResponseSchema.nullable(),
});
@@ -0,0 +1,103 @@
import { describe, expect, it, beforeAll, afterAll, beforeEach } from "@jest/globals";
import mongoose from "mongoose";
import { MongoMemoryServer } from "mongodb-memory-server";
import MongoChecksRepository from "../../src/domain/checks/check.repository.mongo.ts";
import { CheckModel } from "../../src/domain/checks/check.model.ts";
import type { UptimeChecksResult } from "../../src/domain/checks/check.type.ts";
import type { ILogger } from "../../src/utils/logger.ts";
import { createMockLogger } from "../helpers/createMockLogger.ts";
// ── Real-Mongo harness ─────────────────────────────────────────────────────────
// The guarantee under test — every phase average uses the full bucket as its
// denominator — IS the $ifNull-inside-$avg behavior of the groupedChecks facet, so it
// can only be exercised against a live aggregation engine. Reused keep-alive sockets
// produce checks with no timings subdocument at all; without the coercion, $avg would
// skip them and average dns/tcp/tls over fresh-connection checks only.
let mongod: MongoMemoryServer;
beforeAll(async () => {
mongod = await MongoMemoryServer.create();
await mongoose.connect(mongod.getUri());
await CheckModel.createCollection(); // timeseries collections must exist before insert
}, 120_000);
afterAll(async () => {
await mongoose.disconnect();
await mongod.stop();
});
beforeEach(async () => {
await CheckModel.deleteMany({});
});
// ── Helpers ──────────────────────────────────────────────────────────────────
const MONITOR_ID = new mongoose.Types.ObjectId();
const TEAM_ID = new mongoose.Types.ObjectId();
// One shared createdAt puts every seeded check in the same hourly bucket of the "day"
// range, keeping the facet's group stage deterministic.
const BUCKET_TIME = new Date(Date.now() - 10 * 60 * 1000);
const seedCheck = (overrides: Record<string, unknown> = {}) =>
CheckModel.create({
metadata: { monitorId: MONITOR_ID, teamId: TEAM_ID, type: "http" },
status: true,
responseTime: 100,
createdAt: BUCKET_TIME,
...overrides,
});
const findGrouped = async (repo: MongoChecksRepository) =>
(await repo.findByDateRangeAndMonitorId(MONITOR_ID.toString(), "day", { type: "http" })) as UptimeChecksResult;
describe("MongoChecksRepository groupedChecks facet", () => {
let repo: MongoChecksRepository;
beforeEach(() => {
repo = new MongoChecksRepository(createMockLogger() as unknown as ILogger);
});
it("averages each phase over the full bucket, counting checks without timings as 0", async () => {
await seedCheck({
responseTime: 100,
timings: { phases: { dns: 10, tcp: 20, tls: 30, request: 8, firstByte: 40, download: 6 } },
});
await seedCheck({ responseTime: 200 }); // reused socket: no timings subdocument at all
const result = await findGrouped(repo);
expect(result.groupedChecks).toHaveLength(1);
// Plain $avg would skip the timings-less check and report 10/20/30/8/40/6 here.
expect(result.groupedChecks[0]).toMatchObject({
totalChecks: 2,
avgDns: 5,
avgTcp: 10,
avgTls: 15,
avgRequest: 4,
avgFirstByte: 20,
avgDownload: 3,
avgResponseTime: 150,
});
});
it("yields 0 (not null) for every phase in a bucket of only timings-less checks", async () => {
await seedCheck();
await seedCheck();
const result = await findGrouped(repo);
expect(result.groupedChecks).toHaveLength(1);
// The client sums these fields for the stacked chart; null would poison the math.
expect(result.groupedChecks[0]).toMatchObject({
totalChecks: 2,
avgDns: 0,
avgTcp: 0,
avgTls: 0,
avgRequest: 0,
avgFirstByte: 0,
avgDownload: 0,
});
});
});