snapshots
This commit is contained in:
1 parent
665eb9a67f
commit
4ad09fad4f
3 files changed
+23
-8
No files matched your search
@@ -2,7 +2,7 @@ import type { Check, MonitorType } from "@/types/index.js";
|
|||||||
import type { LatestChecksMap } from "@/repositories/checks/MongoChecksRepistory.js";
|
import type { LatestChecksMap } from "@/repositories/checks/MongoChecksRepistory.js";
|
||||||
|
|
||||||
export interface IChecksRepository {
|
export interface IChecksRepository {
|
||||||
findLatestChecksByMonitorIds(monitorIds: string[]): Promise<LatestChecksMap>;
|
findLatestChecksByMonitorIds(monitorIds: string[], options?: { limitPerMonitor?: number }): Promise<LatestChecksMap>;
|
||||||
findDateRangeChecksByMonitor(
|
findDateRangeChecksByMonitor(
|
||||||
monitorId: string,
|
monitorId: string,
|
||||||
startDate: Date,
|
startDate: Date,
|
||||||
|
|||||||
@@ -177,15 +177,18 @@ class MongoChecksRepistory implements IChecksRepository {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
findLatestChecksByMonitorIds = async (monitorIds: string[]): Promise<LatestChecksMap> => {
|
findLatestChecksByMonitorIds = async (
|
||||||
|
monitorIds: string[],
|
||||||
|
options?: { limitPerMonitor?: number }
|
||||||
|
): Promise<LatestChecksMap> => {
|
||||||
if (monitorIds.length === 0) {
|
if (monitorIds.length === 0) {
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
const mongoIds = monitorIds.map((id) => new mongoose.Types.ObjectId(id));
|
const mongoIds = monitorIds.map((id) => new mongoose.Types.ObjectId(id));
|
||||||
const limitPerMonitor = 25;
|
const limitPerMonitor = options?.limitPerMonitor ?? 25;
|
||||||
const maxIntervalMs = Number(10 * 60 * 1000);
|
const maxIntervalMs = Number(10 * 60 * 1000);
|
||||||
const bufferMs = Number(maxIntervalMs);
|
const bufferMs = Number(maxIntervalMs);
|
||||||
const lookbackMs = 25 * maxIntervalMs + bufferMs;
|
const lookbackMs = limitPerMonitor * maxIntervalMs + bufferMs;
|
||||||
const cutoffDate = new Date(Date.now() - lookbackMs);
|
const cutoffDate = new Date(Date.now() - lookbackMs);
|
||||||
const checkGroups = await CheckModel.aggregate([
|
const checkGroups = await CheckModel.aggregate([
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -404,10 +404,22 @@ export class MonitorService implements IMonitorService {
|
|||||||
order,
|
order,
|
||||||
});
|
});
|
||||||
|
|
||||||
const monitorIds = monitors?.map((m: any) => m.id) ?? [];
|
const monitorsList = (monitors ?? []) as Monitor[];
|
||||||
const checksMap = await this.checksRepository.findLatestChecksByMonitorIds(monitorIds);
|
const snapshotTypes: MonitorType[] = ["hardware", "pagespeed"];
|
||||||
const monitorsWithChecks = (monitors ?? []).map((monitor: any) => {
|
const requestedTypes = Array.isArray(type) ? type : type ? [type] : [];
|
||||||
const checks = NormalizeData(checksMap[monitor.id] ?? [], 10, 100);
|
const snapshotOnlyRequest =
|
||||||
|
requestedTypes.length > 0 && requestedTypes.every((requestedType) => snapshotTypes.includes(requestedType as MonitorType));
|
||||||
|
|
||||||
|
const limitPerMonitor = snapshotOnlyRequest ? 1 : 25;
|
||||||
|
const checksMap = await this.checksRepository.findLatestChecksByMonitorIds(
|
||||||
|
monitorsList.map((monitor) => monitor.id),
|
||||||
|
{ limitPerMonitor }
|
||||||
|
);
|
||||||
|
|
||||||
|
const monitorsWithChecks = monitorsList.map((monitor: Monitor) => {
|
||||||
|
const rawChecks = checksMap[monitor.id] ?? [];
|
||||||
|
const isSnapshotType = snapshotOnlyRequest || snapshotTypes.includes(monitor.type);
|
||||||
|
const checks = isSnapshotType ? rawChecks.slice(0, 1) : NormalizeData(rawChecks, 10, 100);
|
||||||
return {
|
return {
|
||||||
...monitor,
|
...monitor,
|
||||||
checks,
|
checks,
|
||||||
|
|||||||
Reference in new issue
Block a user