Merge pull request #845 from bluewave-labs/fix/db-index

Fix/db index
This commit is contained in:
Alexander Holliday authored and GitHub committed 2024-09-16 21:59:40 -07:00
commit daa1e07ef8
2 files changed
+10 -7

No files matched your search

+5 -7
View File
@@ -149,17 +149,15 @@ const getTeamChecks = async (req) => {
let { sortOrder, limit, dateRange, filter, page, rowsPerPage } = req.query; let { sortOrder, limit, dateRange, filter, page, rowsPerPage } = req.query;
// Get monitorIDs // Get monitorIDs
const userMonitors = await Monitor.find({ teamId: teamId }); const userMonitors = await Monitor.find({ teamId: teamId }).select("_id");
const monitorIds = userMonitors.map((monitor) => monitor._id);
//Build check query //Build check query
// Default limit to 0 if not provided // Default limit to 0 if not provided
limit = limit === "undefined" ? 0 : limit; limit = limit === undefined ? 0 : limit;
// Default sort order is newest -> oldest // Default sort order is newest -> oldest
sortOrder = sortOrder === "asc" ? 1 : -1; sortOrder = sortOrder === "asc" ? 1 : -1;
checksQuery = { monitorId: { $in: monitorIds } }; checksQuery = { monitorId: { $in: userMonitors } };
if (filter !== undefined) { if (filter !== undefined) {
checksQuery.status = false; checksQuery.status = false;
@@ -192,8 +190,8 @@ const getTeamChecks = async (req) => {
const checks = await Check.find(checksQuery) const checks = await Check.find(checksQuery)
.skip(skip) .skip(skip)
.limit(rowsPerPage) .limit(rowsPerPage)
.sort({ createdAt: sortOrder }); .sort({ createdAt: sortOrder })
.select(["monitorId", "status", "responseTime", "statusCode", "message"]);
return { checksCount, checks }; return { checksCount, checks };
}; };
+5
View File
@@ -19,6 +19,7 @@ const CheckSchema = mongoose.Schema(
type: mongoose.Schema.Types.ObjectId, type: mongoose.Schema.Types.ObjectId,
ref: "Monitor", ref: "Monitor",
immutable: true, immutable: true,
index: true,
}, },
/** /**
* Status of the check (true for up, false for down). * Status of the check (true for up, false for down).
@@ -27,6 +28,7 @@ const CheckSchema = mongoose.Schema(
*/ */
status: { status: {
type: Boolean, type: Boolean,
index: true,
}, },
/** /**
* Response time of the check in milliseconds. * Response time of the check in milliseconds.
@@ -43,6 +45,7 @@ const CheckSchema = mongoose.Schema(
*/ */
statusCode: { statusCode: {
type: Number, type: Number,
index: true,
}, },
/** /**
* Message or description of the check result. * Message or description of the check result.
@@ -69,4 +72,6 @@ const CheckSchema = mongoose.Schema(
} }
); );
CheckSchema.index({ createdAt: 1 });
module.exports = mongoose.model("Check", CheckSchema); module.exports = mongoose.model("Check", CheckSchema);