Skip to content

Commit

Permalink
Add hour filters to check queries
Browse files Browse the repository at this point in the history
  • Loading branch information
ajhollid committed Jan 22, 2025
1 parent 2a74f2e commit fc36da4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 32 deletions.
1 change: 1 addition & 0 deletions Server/db/models/Check.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ CheckSchema.index({ createdAt: 1 });
CheckSchema.index({ monitorId: 1, createdAt: 1 });
CheckSchema.index({ monitorId: 1, createdAt: -1 });
CheckSchema.index({ teamId: 1, createdAt: -1 });
CheckSchema.index({ teamId: 1 });

export default mongoose.model("Check", CheckSchema);
export { BaseCheckSchema };
29 changes: 7 additions & 22 deletions Server/db/mongo/modules/checkModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import { ObjectId } from "mongodb";

const SERVICE_NAME = "checkModule";
const dateRangeLookup = {
hour: new Date(new Date().setHours(new Date().getHours() - 1)),
day: new Date(new Date().setDate(new Date().getDate() - 1)),
week: new Date(new Date().setDate(new Date().getDate() - 7)),
month: new Date(new Date().setMonth(new Date().getMonth() - 1)),
all: undefined,
};

/**
Expand Down Expand Up @@ -75,7 +77,7 @@ const getChecksByMonitor = async (req) => {
const matchStage = {
monitorId: ObjectId.createFromHexString(monitorId),
status: false,
...(dateRange && {
...(dateRangeLookup[dateRange] && {
createdAt: {
$gte: dateRangeLookup[dateRange],
},
Expand Down Expand Up @@ -140,18 +142,18 @@ const getChecksByTeam = async (req) => {
let { sortOrder, dateRange, filter, page, rowsPerPage } = req.query;
page = parseInt(page);
rowsPerPage = parseInt(rowsPerPage);
!dateRange && (dateRange = "day");
console.log(dateRangeLookup[dateRange]);
const { teamId } = req.params;
const matchStage = {
teamId: ObjectId.createFromHexString(teamId),
status: false,
...(dateRange && {
...(dateRangeLookup[dateRange] && {
createdAt: {
$gte: dateRangeLookup[dateRange],
},
}),
};

console.log(matchStage);
// Add filter to match stage
if (filter !== undefined) {
switch (filter) {
Expand All @@ -166,7 +168,7 @@ const getChecksByTeam = async (req) => {
logger.warn({
message: "invalid filter",
service: SERVICE_NAME,
method: "getTeamChecks",
method: "getChecksByTeam",
});
break;
}
Expand Down Expand Up @@ -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];
} catch (error) {
error.service = SERVICE_NAME;
Expand Down
17 changes: 7 additions & 10 deletions Server/db/mongo/utils/seedDb.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import Monitor from "../../models/Monitor.js";
import Check from "../../models/Check.js";
import { ObjectId } from "mongodb";
const FAKE_TEAM_ID = "67034bceb4c4ea3e8f75fa93";
const FAKE_USER_ID = "67034bceb4c4ea3e8f75fa95";

const generateRandomUrl = () => {
const domains = ["example.com", "test.org", "demo.net", "sample.io", "mock.dev"];
const paths = ["api", "status", "health", "ping", "check"];
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 endTime = new Date(Date.now() - 10 * 60 * 1000); // 10 minutes ago
const startTime = new Date(endTime - count * 60 * 1000); // count minutes before endTime
Expand All @@ -21,7 +18,7 @@ const generateChecks = (monitorId, count) => {

checks.push({
monitorId,
teamId: FAKE_TEAM_ID,
teamId,
status,
responseTime: Math.floor(Math.random() * 1000), // Random response time between 0-1000ms
createdAt: timestamp,
Expand All @@ -32,7 +29,7 @@ const generateChecks = (monitorId, count) => {
return checks;
};

const seedDb = async () => {
const seedDb = async (userId, teamId) => {
try {
console.log("Deleting all monitors and checks");
await Monitor.deleteMany({});
Expand All @@ -43,13 +40,13 @@ const seedDb = async () => {
name: `Monitor ${i}`,
url: generateRandomUrl(),
type: "http",
userId: FAKE_USER_ID,
teamId: FAKE_TEAM_ID,
userId,
teamId,
interval: 60000,
active: true,
active: false,
});
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);
}
} catch (error) {
Expand Down

0 comments on commit fc36da4

Please sign in to comment.