diff --git a/server/src/db/models/monitors/Monitor.ts b/server/src/db/models/monitors/Monitor.ts index f18de6d58..95fab6135 100644 --- a/server/src/db/models/monitors/Monitor.ts +++ b/server/src/db/models/monitors/Monitor.ts @@ -163,5 +163,8 @@ MonitorSchema.index({ type: 1 }); MonitorSchema.index({ lastCheckedAt: 1 }); MonitorSchema.index({ createdBy: 1 }); MonitorSchema.index({ updatedBy: 1 }); +// Common filter keys used across services and routes +MonitorSchema.index({ teamId: 1 }); +MonitorSchema.index({ orgId: 1 }); export const Monitor = mongoose.model("Monitor", MonitorSchema); diff --git a/server/src/db/models/monitors/MonitorStats.ts b/server/src/db/models/monitors/MonitorStats.ts index def5ea947..15bda5578 100644 --- a/server/src/db/models/monitors/MonitorStats.ts +++ b/server/src/db/models/monitors/MonitorStats.ts @@ -26,7 +26,7 @@ const MonitorStatsSchema = new Schema( type: mongoose.Schema.Types.ObjectId, ref: "Monitor", immutable: true, - index: true, + unique: true, }, avgResponseTime: { type: Number, diff --git a/server/src/db/models/monitors/StatsDaily.ts b/server/src/db/models/monitors/StatsDaily.ts index 1d41a5604..a2832a7b9 100644 --- a/server/src/db/models/monitors/StatsDaily.ts +++ b/server/src/db/models/monitors/StatsDaily.ts @@ -92,8 +92,18 @@ const InfraNetEntrySchema = new Schema( const StatsDailySchema = new Schema( { - monitorId: { type: Schema.Types.ObjectId, ref: "Monitor", index: true, required: true }, - teamId: { type: Schema.Types.ObjectId, ref: "Team", index: true, required: true }, + monitorId: { + type: Schema.Types.ObjectId, + ref: "Monitor", + index: true, + required: true, + }, + teamId: { + type: Schema.Types.ObjectId, + ref: "Team", + index: true, + required: true, + }, type: { type: String, required: true }, windowStart: { type: Date, required: true, index: true }, windowEnd: { type: Date, required: true }, @@ -126,7 +136,6 @@ const StatsDailySchema = new Schema( { timestamps: true, collection: "stats_daily" } ); -// One document per (monitorId, windowStart) StatsDailySchema.index({ monitorId: 1, windowStart: 1 }, { unique: true }); export const StatsDaily = mongoose.model( diff --git a/server/src/db/models/monitors/StatsHourly.ts b/server/src/db/models/monitors/StatsHourly.ts index 34a9a7ab1..ffdb3846a 100644 --- a/server/src/db/models/monitors/StatsHourly.ts +++ b/server/src/db/models/monitors/StatsHourly.ts @@ -87,7 +87,10 @@ export interface IInfraNetEntry { fifo_out?: number; } -export interface IStatsHourly extends mongoose.Document, IStatsBase, IPageSpeedMetrics { +export interface IStatsHourly + extends mongoose.Document, + IStatsBase, + IPageSpeedMetrics { cpu?: IInfraCpu; memory?: IInfraMemory; disk?: IInfraDiskEntry[]; @@ -166,8 +169,18 @@ const InfraNetEntrySchema = new Schema( const StatsHourlySchema = new Schema( { - monitorId: { type: Schema.Types.ObjectId, ref: "Monitor", index: true, required: true }, - teamId: { type: Schema.Types.ObjectId, ref: "Team", index: true, required: true }, + monitorId: { + type: Schema.Types.ObjectId, + ref: "Monitor", + index: true, + required: true, + }, + teamId: { + type: Schema.Types.ObjectId, + ref: "Team", + index: true, + required: true, + }, type: { type: String, required: true }, windowStart: { type: Date, required: true, index: true }, windowEnd: { type: Date, required: true }, @@ -200,7 +213,6 @@ const StatsHourlySchema = new Schema( { timestamps: true, collection: "stats_hourly" } ); -// One document per (monitorId, windowStart) StatsHourlySchema.index({ monitorId: 1, windowStart: 1 }, { unique: true }); export const StatsHourly = mongoose.model(