Add hour filters to check queries
This commit is contained in:
1 parent
2a74f2e8da
commit
fc36da4f7b
3 files changed
+15
-32
No files matched your search
@@ -77,6 +77,7 @@ CheckSchema.index({ createdAt: 1 });
|
|||||||
CheckSchema.index({ monitorId: 1, createdAt: 1 });
|
CheckSchema.index({ monitorId: 1, createdAt: 1 });
|
||||||
CheckSchema.index({ monitorId: 1, createdAt: -1 });
|
CheckSchema.index({ monitorId: 1, createdAt: -1 });
|
||||||
CheckSchema.index({ teamId: 1, createdAt: -1 });
|
CheckSchema.index({ teamId: 1, createdAt: -1 });
|
||||||
|
CheckSchema.index({ teamId: 1 });
|
||||||
|
|
||||||
export default mongoose.model("Check", CheckSchema);
|
export default mongoose.model("Check", CheckSchema);
|
||||||
export { BaseCheckSchema };
|
export { BaseCheckSchema };
|
||||||
@@ -6,9 +6,11 @@ import { ObjectId } from "mongodb";
|
|||||||
|
|
||||||
const SERVICE_NAME = "checkModule";
|
const SERVICE_NAME = "checkModule";
|
||||||
const dateRangeLookup = {
|
const dateRangeLookup = {
|
||||||
|
hour: new Date(new Date().setHours(new Date().getHours() - 1)),
|
||||||
day: new Date(new Date().setDate(new Date().getDate() - 1)),
|
day: new Date(new Date().setDate(new Date().getDate() - 1)),
|
||||||
week: new Date(new Date().setDate(new Date().getDate() - 7)),
|
week: new Date(new Date().setDate(new Date().getDate() - 7)),
|
||||||
month: new Date(new Date().setMonth(new Date().getMonth() - 1)),
|
month: new Date(new Date().setMonth(new Date().getMonth() - 1)),
|
||||||
|
all: undefined,
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -75,7 +77,7 @@ const getChecksByMonitor = async (req) => {
|
|||||||
const matchStage = {
|
const matchStage = {
|
||||||
monitorId: ObjectId.createFromHexString(monitorId),
|
monitorId: ObjectId.createFromHexString(monitorId),
|
||||||
status: false,
|
status: false,
|
||||||
...(dateRange && {
|
...(dateRangeLookup[dateRange] && {
|
||||||
createdAt: {
|
createdAt: {
|
||||||
$gte: dateRangeLookup[dateRange],
|
$gte: dateRangeLookup[dateRange],
|
||||||
},
|
},
|
||||||
@@ -140,18 +142,18 @@ const getChecksByTeam = async (req) => {
|
|||||||
let { sortOrder, dateRange, filter, page, rowsPerPage } = req.query;
|
let { sortOrder, dateRange, filter, page, rowsPerPage } = req.query;
|
||||||
page = parseInt(page);
|
page = parseInt(page);
|
||||||
rowsPerPage = parseInt(rowsPerPage);
|
rowsPerPage = parseInt(rowsPerPage);
|
||||||
!dateRange && (dateRange = "day");
|
console.log(dateRangeLookup[dateRange]);
|
||||||
const { teamId } = req.params;
|
const { teamId } = req.params;
|
||||||
const matchStage = {
|
const matchStage = {
|
||||||
teamId: ObjectId.createFromHexString(teamId),
|
teamId: ObjectId.createFromHexString(teamId),
|
||||||
status: false,
|
status: false,
|
||||||
...(dateRange && {
|
...(dateRangeLookup[dateRange] && {
|
||||||
createdAt: {
|
createdAt: {
|
||||||
$gte: dateRangeLookup[dateRange],
|
$gte: dateRangeLookup[dateRange],
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
};
|
};
|
||||||
|
console.log(matchStage);
|
||||||
// Add filter to match stage
|
// Add filter to match stage
|
||||||
if (filter !== undefined) {
|
if (filter !== undefined) {
|
||||||
switch (filter) {
|
switch (filter) {
|
||||||
@@ -166,7 +168,7 @@ const getChecksByTeam = async (req) => {
|
|||||||
logger.warn({
|
logger.warn({
|
||||||
message: "invalid filter",
|
message: "invalid filter",
|
||||||
service: SERVICE_NAME,
|
service: SERVICE_NAME,
|
||||||
method: "getTeamChecks",
|
method: "getChecksByTeam",
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -198,23 +200,6 @@ const getChecksByTeam = async (req) => {
|
|||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const queryPlan = await Check.aggregate([
|
|
||||||
{ $match: matchStage },
|
|
||||||
{ $sort: { createdAt: sortOrder } },
|
|
||||||
{
|
|
||||||
$facet: {
|
|
||||||
summary: [{ $count: "checksCount" }],
|
|
||||||
checks: [{ $skip: skip }, { $limit: rowsPerPage }],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
$project: {
|
|
||||||
checksCount: { $arrayElemAt: ["$summary.checksCount", 0] },
|
|
||||||
checks: "$checks",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
]).explain("executionStats");
|
|
||||||
console.log(queryPlan);
|
|
||||||
return checks[0];
|
return checks[0];
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
error.service = SERVICE_NAME;
|
error.service = SERVICE_NAME;
|
||||||
|
|||||||
@@ -1,8 +1,5 @@
|
|||||||
import Monitor from "../../models/Monitor.js";
|
import Monitor from "../../models/Monitor.js";
|
||||||
import Check from "../../models/Check.js";
|
import Check from "../../models/Check.js";
|
||||||
import { ObjectId } from "mongodb";
|
|
||||||
const FAKE_TEAM_ID = "67034bceb4c4ea3e8f75fa93";
|
|
||||||
const FAKE_USER_ID = "67034bceb4c4ea3e8f75fa95";
|
|
||||||
|
|
||||||
const generateRandomUrl = () => {
|
const generateRandomUrl = () => {
|
||||||
const domains = ["example.com", "test.org", "demo.net", "sample.io", "mock.dev"];
|
const domains = ["example.com", "test.org", "demo.net", "sample.io", "mock.dev"];
|
||||||
@@ -10,7 +7,7 @@ const generateRandomUrl = () => {
|
|||||||
return `https://${domains[Math.floor(Math.random() * domains.length)]}/${paths[Math.floor(Math.random() * paths.length)]}`;
|
return `https://${domains[Math.floor(Math.random() * domains.length)]}/${paths[Math.floor(Math.random() * paths.length)]}`;
|
||||||
};
|
};
|
||||||
|
|
||||||
const generateChecks = (monitorId, count) => {
|
const generateChecks = (monitorId, teamId, count) => {
|
||||||
const checks = [];
|
const checks = [];
|
||||||
const endTime = new Date(Date.now() - 10 * 60 * 1000); // 10 minutes ago
|
const endTime = new Date(Date.now() - 10 * 60 * 1000); // 10 minutes ago
|
||||||
const startTime = new Date(endTime - count * 60 * 1000); // count minutes before endTime
|
const startTime = new Date(endTime - count * 60 * 1000); // count minutes before endTime
|
||||||
@@ -21,7 +18,7 @@ const generateChecks = (monitorId, count) => {
|
|||||||
|
|
||||||
checks.push({
|
checks.push({
|
||||||
monitorId,
|
monitorId,
|
||||||
teamId: FAKE_TEAM_ID,
|
teamId,
|
||||||
status,
|
status,
|
||||||
responseTime: Math.floor(Math.random() * 1000), // Random response time between 0-1000ms
|
responseTime: Math.floor(Math.random() * 1000), // Random response time between 0-1000ms
|
||||||
createdAt: timestamp,
|
createdAt: timestamp,
|
||||||
@@ -32,7 +29,7 @@ const generateChecks = (monitorId, count) => {
|
|||||||
return checks;
|
return checks;
|
||||||
};
|
};
|
||||||
|
|
||||||
const seedDb = async () => {
|
const seedDb = async (userId, teamId) => {
|
||||||
try {
|
try {
|
||||||
console.log("Deleting all monitors and checks");
|
console.log("Deleting all monitors and checks");
|
||||||
await Monitor.deleteMany({});
|
await Monitor.deleteMany({});
|
||||||
@@ -43,13 +40,13 @@ const seedDb = async () => {
|
|||||||
name: `Monitor ${i}`,
|
name: `Monitor ${i}`,
|
||||||
url: generateRandomUrl(),
|
url: generateRandomUrl(),
|
||||||
type: "http",
|
type: "http",
|
||||||
userId: FAKE_USER_ID,
|
userId,
|
||||||
teamId: FAKE_TEAM_ID,
|
teamId,
|
||||||
interval: 60000,
|
interval: 60000,
|
||||||
active: true,
|
active: false,
|
||||||
});
|
});
|
||||||
console.log(`Adding monitor and checks for monitor ${i}`);
|
console.log(`Adding monitor and checks for monitor ${i}`);
|
||||||
const checks = generateChecks(monitor._id, 10000);
|
const checks = generateChecks(monitor._id, teamId, 10000);
|
||||||
await Check.insertMany(checks);
|
await Check.insertMany(checks);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
Reference in new issue
Block a user