add indexes

This commit is contained in:
Alex Holliday committed 2026-01-13 22:56:51 +00:00
1 parent 52e598a66e
commit 8a986d2e58
6 files changed
+28 -11

No files matched your search

+1 -1
View File
@@ -231,7 +231,7 @@ class MonitorController {
throw new AppError({ message: "Cannot get file from buffer", status: 400 });
}
const monitors = await this.monitorService.createBulkMonitors({ fileData, userId, teamId });
const monitors = await this.monitorService.createBulkMonitors(fileData, userId, teamId);
return res.status(200).json({
success: true,
+2
View File
@@ -288,7 +288,9 @@ const CheckSchema = new Schema<CheckDocument>(
CheckSchema.index({ updatedAt: 1 });
CheckSchema.index({ "metadata.monitorId": 1, updatedAt: 1 });
CheckSchema.index({ "metadata.monitorId": 1, updatedAt: -1 });
CheckSchema.index({ "metadata.monitorId": 1, createdAt: -1 });
CheckSchema.index({ "metadata.teamId": 1, updatedAt: -1 });
CheckSchema.index({ "metadata.teamId": 1, createdAt: -1 });
const CheckModel = model<CheckDocument>("Check", CheckSchema);
@@ -178,10 +178,15 @@ class MongoChecksRepistory implements IChecksRepository {
}
const mongoIds = monitorIds.map((id) => new mongoose.Types.ObjectId(id));
const limitPerMonitor = 25;
const maxIntervalMs = Number(10 * 60 * 1000);
const bufferMs = Number(maxIntervalMs);
const lookbackMs = 25 * maxIntervalMs + bufferMs;
const cutoffDate = new Date(Date.now() - lookbackMs);
const checkGroups = await CheckModel.aggregate([
{
$match: {
"metadata.monitorId": { $in: mongoIds },
createdAt: { $gte: cutoffDate },
},
},
{
@@ -197,6 +202,7 @@ class MongoChecksRepistory implements IChecksRepository {
},
},
]);
return checkGroups.reduce<LatestChecksMap>((acc, group) => {
const monitorId = group._id.toString();
acc[monitorId] = (group.latestChecks ?? []).map((doc: CheckDocument) => this.toEntity(doc));
@@ -13,6 +13,7 @@ export interface TeamQueryConfig {
export interface IMonitorsRepository {
// create
create(monitor: Monitor, teamId: string, userId: string): Promise<Monitor | null>;
createBulkMonitors(monitors: Monitor[]): Promise<Monitor[]>;
// single fetch
findById(monitorId: string): Promise<Monitor | null>;
@@ -12,6 +12,12 @@ class MongoMonitorsRepository implements IMonitorsRepository {
return this.toEntity(saved);
};
createBulkMonitors = async (monitors: Monitor[]): Promise<Monitor[]> => {
const newMonitors = monitors.map((monitor) => new MonitorModel({ ...monitor, notifications: undefined }));
await MonitorModel.bulkSave(newMonitors);
return monitors;
};
findById = async (MonitorModelId: string): Promise<Monitor> => {
const monitor = await MonitorModel.findById(MonitorModelId);
if (!monitor) {
+12 -10
View File
@@ -1,8 +1,10 @@
import { createMonitorsBodyValidation } from "@/validation/joi.js";
import { NormalizeData } from "@/utils/dataUtils.js";
import { type Monitor } from "@/types/index.js";
import type { MonitorType } from "@/types/monitor.js";
import type { IMonitorsRepository } from "@/repositories/index.js";
import { AppError } from "../infrastructure/errorService.js";
const SERVICE_NAME = "MonitorService";
export interface IMonitorService {
@@ -11,7 +13,7 @@ export interface IMonitorService {
// create
createMonitor(teamId: string, userId: string, body: Monitor): Promise<void>;
createBulkMonitors(args: { fileData: string; userId: string; teamId: string }): Promise<any>;
createBulkMonitors(fileData: string, userId: string, teamId: string): Promise<any>;
addDemoMonitors(args: { userId: string; teamId: string }): Promise<any[]>;
// read
@@ -31,7 +33,7 @@ export interface IMonitorService {
getMonitorsByTeamId(args: {
teamId: string;
limit?: number;
type?: string | string[];
type?: MonitorType | MonitorType[];
page?: number;
rowsPerPage?: number;
filter?: string;
@@ -42,7 +44,7 @@ export interface IMonitorService {
getMonitorsWithChecksByTeamId(args: {
teamId: string;
limit?: number;
type?: string | string[];
type?: MonitorType | MonitorType[];
page?: number;
rowsPerPage?: number;
filter?: string;
@@ -136,7 +138,7 @@ export class MonitorService implements IMonitorService {
this.jobQueue.addJob(monitor.id, monitor);
};
createBulkMonitors = async ({ fileData, userId, teamId }: { fileData: string; userId: string; teamId: string }): Promise<any> => {
createBulkMonitors = async (fileData: string, userId: string, teamId: string): Promise<any> => {
const { parse } = this.papaparse;
return new Promise<any>((resolve, reject) => {
@@ -167,10 +169,10 @@ export class MonitorService implements IMonitorService {
throw this.errorService.createServerError("CSV file contains no data rows");
}
const enrichedData = data.map((monitor: any) => ({
const enrichedData = data.map((monitor: Monitor) => ({
...monitor,
userId,
teamId,
...monitor,
description: monitor.description || monitor.name || monitor.url,
name: monitor.name || monitor.url,
type: monitor.type || "http",
@@ -178,11 +180,11 @@ export class MonitorService implements IMonitorService {
await createMonitorsBodyValidation.validateAsync(enrichedData);
const monitors = await this.db.monitorModule.createBulkMonitors(enrichedData);
const monitors = await this.monitorsRepository.createBulkMonitors(enrichedData);
await Promise.all(
monitors.map(async (monitor: any) => {
this.jobQueue.addJob(monitor._id, monitor);
monitors.map(async (monitor: Monitor) => {
this.jobQueue.addJob(monitor.id, monitor);
})
);
@@ -334,7 +336,7 @@ export class MonitorService implements IMonitorService {
}: {
teamId: string;
limit?: number;
type?: string | string[];
type?: MonitorType | MonitorType[];
page?: number;
rowsPerPage?: number;
filter?: string;