add recent checks for infra monitors
This commit is contained in:
1 parent
092f890f7f
commit
dd44e6c34f
4 files changed
+28
-8
No files matched your search
@@ -106,7 +106,7 @@ export class MaintenanceWindowService implements IMaintenanceWindowService {
|
|||||||
start: string;
|
start: string;
|
||||||
end: string;
|
end: string;
|
||||||
}) => {
|
}) => {
|
||||||
const monitors = await this.monitorsRepository.findByIds(monitorIDs, { includeRecentChecks: false });
|
const monitors = await this.monitorsRepository.findByIds(monitorIDs, { recentChecks: "none" });
|
||||||
|
|
||||||
const unauthorizedMonitors = monitors.filter((monitor) => monitor.teamId !== teamId);
|
const unauthorizedMonitors = monitors.filter((monitor) => monitor.teamId !== teamId);
|
||||||
|
|
||||||
@@ -193,7 +193,7 @@ export class MaintenanceWindowService implements IMaintenanceWindowService {
|
|||||||
const update: Partial<MaintenanceWindow> = rest;
|
const update: Partial<MaintenanceWindow> = rest;
|
||||||
|
|
||||||
if (monitors !== undefined) {
|
if (monitors !== undefined) {
|
||||||
const monitorDocs = await this.monitorsRepository.findByIds(monitors, { includeRecentChecks: false });
|
const monitorDocs = await this.monitorsRepository.findByIds(monitors, { recentChecks: "none" });
|
||||||
const unauthorizedMonitors = monitorDocs.filter((monitor) => monitor.teamId !== teamId);
|
const unauthorizedMonitors = monitorDocs.filter((monitor) => monitor.teamId !== teamId);
|
||||||
if (unauthorizedMonitors.length > 0) {
|
if (unauthorizedMonitors.length > 0) {
|
||||||
throw new AppError({
|
throw new AppError({
|
||||||
|
|||||||
@@ -16,6 +16,8 @@ export interface SummaryConfig {
|
|||||||
tags?: string | string[];
|
tags?: string | string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type RecentChecksMode = "all" | "latestHardware" | "none";
|
||||||
|
|
||||||
export interface IMonitorsRepository {
|
export interface IMonitorsRepository {
|
||||||
// create
|
// create
|
||||||
create(monitor: Monitor, teamId: string, userId: string): Promise<Monitor | null>;
|
create(monitor: Monitor, teamId: string, userId: string): Promise<Monitor | null>;
|
||||||
@@ -29,7 +31,7 @@ export interface IMonitorsRepository {
|
|||||||
findAllForScheduling(): Promise<MonitorScheduleFields[]>;
|
findAllForScheduling(): Promise<MonitorScheduleFields[]>;
|
||||||
findByTeamId(teamId: string, config: TeamQueryConfig, options?: { includeRecentChecks?: boolean }): Promise<Monitor[]>;
|
findByTeamId(teamId: string, config: TeamQueryConfig, options?: { includeRecentChecks?: boolean }): Promise<Monitor[]>;
|
||||||
findByTeamIdWithStats(teamId: string, config: TeamQueryConfig): Promise<Monitor[]>;
|
findByTeamIdWithStats(teamId: string, config: TeamQueryConfig): Promise<Monitor[]>;
|
||||||
findByIds(monitorIds: string[], options?: { includeRecentChecks?: boolean }): Promise<Monitor[]>;
|
findByIds(monitorIds: string[], options?: { recentChecks?: RecentChecksMode }): Promise<Monitor[]>;
|
||||||
|
|
||||||
// update
|
// update
|
||||||
updateById(monitorId: string, teamId: string, updates: Partial<Monitor>): Promise<Monitor>;
|
updateById(monitorId: string, teamId: string, updates: Partial<Monitor>): Promise<Monitor>;
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import type { Monitor, MonitorScheduleFields, MonitorStatus, MonitorsSummary } f
|
|||||||
import mongoose, { type FilterQuery, type PipelineStage } from "mongoose";
|
import mongoose, { type FilterQuery, type PipelineStage } from "mongoose";
|
||||||
import { MongoBulkWriteError } from "mongodb";
|
import { MongoBulkWriteError } from "mongodb";
|
||||||
import { AppError } from "@/utils/AppError.js";
|
import { AppError } from "@/utils/AppError.js";
|
||||||
import { IMonitorsRepository, TeamQueryConfig, SummaryConfig } from "@/domain/monitors/monitor.repository.interface.js";
|
import { IMonitorsRepository, TeamQueryConfig, SummaryConfig, RecentChecksMode } from "@/domain/monitors/monitor.repository.interface.js";
|
||||||
import { toStringId, toDateString } from "@/utils/mongoMappers.js";
|
import { toStringId, toDateString } from "@/utils/mongoMappers.js";
|
||||||
import { toCheckSnapshot } from "@/domain/checks/check.snapshot.js";
|
import { toCheckSnapshot } from "@/domain/checks/check.snapshot.js";
|
||||||
class MongoMonitorsRepository implements IMonitorsRepository {
|
class MongoMonitorsRepository implements IMonitorsRepository {
|
||||||
@@ -131,7 +131,26 @@ class MongoMonitorsRepository implements IMonitorsRepository {
|
|||||||
return documents.map((doc) => this.toEntity(doc));
|
return documents.map((doc) => this.toEntity(doc));
|
||||||
};
|
};
|
||||||
|
|
||||||
findByIds = async (monitorIds: string[], options?: { includeRecentChecks?: boolean }): Promise<Monitor[]> => {
|
private recentChecksStages = (mode: RecentChecksMode = "all"): PipelineStage[] => {
|
||||||
|
switch (mode) {
|
||||||
|
case "none":
|
||||||
|
return [{ $project: { recentChecks: 0 } }];
|
||||||
|
case "latestHardware":
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
$set: {
|
||||||
|
recentChecks: {
|
||||||
|
$cond: [{ $eq: ["$type", "hardware"] }, { $slice: [{ $ifNull: ["$recentChecks", []] }, -1] }, []],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
];
|
||||||
|
case "all":
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
findByIds = async (monitorIds: string[], options?: { recentChecks?: "all" | "latestHardware" | "none" }): Promise<Monitor[]> => {
|
||||||
if (!monitorIds.length) {
|
if (!monitorIds.length) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
@@ -140,7 +159,7 @@ class MongoMonitorsRepository implements IMonitorsRepository {
|
|||||||
|
|
||||||
const pipeline: PipelineStage[] = [
|
const pipeline: PipelineStage[] = [
|
||||||
{ $match: { _id: { $in: objectIds } } },
|
{ $match: { _id: { $in: objectIds } } },
|
||||||
...(options?.includeRecentChecks === false ? [{ $project: { recentChecks: 0 } } as PipelineStage] : []),
|
...this.recentChecksStages(options?.recentChecks),
|
||||||
...this.uptimeStatsLookupStages,
|
...this.uptimeStatsLookupStages,
|
||||||
];
|
];
|
||||||
const documents = await MonitorModel.aggregate(pipeline);
|
const documents = await MonitorModel.aggregate(pipeline);
|
||||||
|
|||||||
@@ -134,7 +134,7 @@ export class StatusPageService implements IStatusPageService {
|
|||||||
|
|
||||||
const dbSettings = await this.settingsService.getDBSettings();
|
const dbSettings = await this.settingsService.getDBSettings();
|
||||||
const showURL = dbSettings.showURL;
|
const showURL = dbSettings.showURL;
|
||||||
const monitors = await this.monitorsRepository.findByIds(statusPage.monitors, { includeRecentChecks: range === "latest" });
|
const monitors = await this.monitorsRepository.findByIds(statusPage.monitors, { recentChecks: range === "latest" ? "all" : "latestHardware" });
|
||||||
const order = new Map(statusPage.monitors.map((id, i) => [id, i]));
|
const order = new Map(statusPage.monitors.map((id, i) => [id, i]));
|
||||||
const sorted = [...monitors].sort((a, b) => (order.get(a.id) ?? Number.MAX_SAFE_INTEGER) - (order.get(b.id) ?? Number.MAX_SAFE_INTEGER));
|
const sorted = [...monitors].sort((a, b) => (order.get(a.id) ?? Number.MAX_SAFE_INTEGER) - (order.get(b.id) ?? Number.MAX_SAFE_INTEGER));
|
||||||
|
|
||||||
@@ -162,7 +162,6 @@ export class StatusPageService implements IStatusPageService {
|
|||||||
checkTTLDays: dbSettings.checkTTL,
|
checkTTLDays: dbSettings.checkTTL,
|
||||||
monitors: sorted.map((monitor) => ({
|
monitors: sorted.map((monitor) => ({
|
||||||
...this.toPublicMonitor(monitor, showURL),
|
...this.toPublicMonitor(monitor, showURL),
|
||||||
recentChecks: [],
|
|
||||||
dailyChecks: bucketsByMonitor.get(monitor.id) ?? [],
|
dailyChecks: bucketsByMonitor.get(monitor.id) ?? [],
|
||||||
})),
|
})),
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in new issue
Block a user