remove team id from monitor controller/db methods, standardize parameters

This commit is contained in:
Alex Holliday committed 2025-06-13 11:49:03 +08:00
1 parent 4168c86734
commit b9fbfd2a25
6 files changed
+138 -173

No files matched your search

+65 -32
View File
@@ -3,7 +3,6 @@ import MonitorStats from "../../models/MonitorStats.js";
import Check from "../../models/Check.js";
import PageSpeedCheck from "../../models/PageSpeedCheck.js";
import HardwareCheck from "../../models/HardwareCheck.js";
import Notification from "../../models/Notification.js";
import { NormalizeData, NormalizeDataUptimeDetails } from "../../../utils/dataUtils.js";
import ServiceRegistry from "../../../service/serviceRegistry.js";
import StringService from "../../../service/stringService.js";
@@ -15,14 +14,11 @@ import { ObjectId } from "mongodb";
import {
buildUptimeDetailsPipeline,
buildHardwareDetailsPipeline,
buildMonitorStatsPipeline,
buildMonitorSummaryByTeamIdPipeline,
buildMonitorsByTeamIdPipeline,
buildMonitorsAndSummaryByTeamIdPipeline,
buildMonitorsWithChecksByTeamIdPipeline,
buildFilteredMonitorsByTeamIdPipeline,
buildDePINDetailsByDateRange,
buildDePINLatestChecks,
} from "./monitorModuleQueries.js";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
@@ -335,11 +331,9 @@ const calculateGroupStats = (group) => {
* @returns {Promise<Monitor>}
* @throws {Error}
*/
const getUptimeDetailsById = async (req) => {
const getUptimeDetailsById = async ({ monitorId, dateRange, normalize }) => {
const stringService = ServiceRegistry.get(StringService.SERVICE_NAME);
try {
const { monitorId } = req.params;
const { dateRange, normalize } = req.query;
const dates = getDateRange(dateRange);
const formatLookup = {
recent: "%Y-%m-%dT%H:%M:00Z",
@@ -392,11 +386,16 @@ const getUptimeDetailsById = async (req) => {
* @returns {Promise<Monitor>}
* @throws {Error}
*/
const getMonitorStatsById = async (req) => {
const getMonitorStatsById = async ({
monitorId,
limit,
sortOrder,
dateRange,
numToDisplay,
normalize,
}) => {
const stringService = ServiceRegistry.get(StringService.SERVICE_NAME);
try {
const { monitorId } = req.params;
// Get monitor, if we can't find it, abort with error
const monitor = await Monitor.findById(monitorId);
if (monitor === null || monitor === undefined) {
@@ -404,7 +403,6 @@ const getMonitorStatsById = async (req) => {
}
// Get query params
let { limit, sortOrder, dateRange, numToDisplay, normalize } = req.query;
const sort = sortOrder === "asc" ? 1 : -1;
// Get Checks for monitor in date range requested
@@ -454,10 +452,8 @@ const getMonitorStatsById = async (req) => {
}
};
const getHardwareDetailsById = async (req) => {
const getHardwareDetailsById = async ({ monitorId, dateRange }) => {
try {
const { monitorId } = req.params;
const { dateRange } = req.query;
const monitor = await Monitor.findById(monitorId);
const dates = getDateRange(dateRange);
const formatLookup = {
@@ -509,8 +505,16 @@ const getMonitorById = async (monitorId) => {
}
};
const getMonitorsByTeamId = async (req) => {
let { limit, type, page, rowsPerPage, filter, field, order } = req.query;
const getMonitorsByTeamId = async ({
limit,
type,
page,
rowsPerPage,
filter,
field,
order,
teamId,
}) => {
limit = parseInt(limit);
page = parseInt(page);
rowsPerPage = parseInt(rowsPerPage);
@@ -519,7 +523,6 @@ const getMonitorsByTeamId = async (req) => {
order = "asc";
}
// Build match stage
const teamId = req.user.teamId;
const matchStage = { teamId: ObjectId.createFromHexString(teamId) };
if (type !== undefined) {
matchStage.type = Array.isArray(type) ? { $in: type } : type;
@@ -558,16 +561,15 @@ const getMonitorsByTeamId = async (req) => {
return { summary, monitors, filteredMonitors: normalizedFilteredMonitors };
};
const getMonitorsAndSummaryByTeamId = async (req) => {
const getMonitorsAndSummaryByTeamId = async ({ type, explain, teamId }) => {
try {
const { type } = req.query;
const teamId = ObjectId.createFromHexString(req.user.teamId);
const matchStage = { teamId };
const parsedTeamId = ObjectId.createFromHexString(teamId);
const matchStage = { teamId: parsedTeamId };
if (type !== undefined) {
matchStage.type = Array.isArray(type) ? { $in: type } : type;
}
if (req.explain === true) {
if (explain === true) {
return Monitor.aggregate(
buildMonitorsAndSummaryByTeamIdPipeline({ matchStage })
).explain("executionStats");
@@ -585,9 +587,18 @@ const getMonitorsAndSummaryByTeamId = async (req) => {
}
};
const getMonitorsWithChecksByTeamId = async (req) => {
const getMonitorsWithChecksByTeamId = async ({
limit,
type,
page,
rowsPerPage,
filter,
field,
order,
teamId,
explain,
}) => {
try {
let { limit, type, page, rowsPerPage, filter, field, order } = req.query;
limit = parseInt(limit);
page = parseInt(page);
rowsPerPage = parseInt(rowsPerPage);
@@ -595,14 +606,14 @@ const getMonitorsWithChecksByTeamId = async (req) => {
field = "name";
order = "asc";
}
const teamId = ObjectId.createFromHexString(req.user.teamId);
const parsedTeamId = ObjectId.createFromHexString(teamId);
// Build match stage
const matchStage = { teamId };
const matchStage = { teamId: parsedTeamId };
if (type !== undefined) {
matchStage.type = Array.isArray(type) ? { $in: type } : type;
}
if (req.explain === true) {
if (explain === true) {
return Monitor.aggregate(
buildMonitorsWithChecksByTeamIdPipeline({
matchStage,
@@ -654,9 +665,9 @@ const getMonitorsWithChecksByTeamId = async (req) => {
* @returns {Promise<Monitor>}
* @throws {Error}
*/
const createMonitor = async (req, res) => {
const createMonitor = async (body) => {
try {
const monitor = new Monitor({ ...req.body });
const monitor = new Monitor({ ...body });
const saved = await monitor.save();
return saved;
} catch (error) {
@@ -695,10 +706,8 @@ const createBulkMonitors = async (req) => {
* @returns {Promise<Monitor>}
* @throws {Error}
*/
const deleteMonitor = async (req, res) => {
const deleteMonitor = async ({ monitorId }) => {
const stringService = ServiceRegistry.get(StringService.SERVICE_NAME);
const monitorId = req.params.monitorId;
try {
const monitor = await Monitor.findByIdAndDelete(monitorId);
if (!monitor) {
@@ -789,6 +798,29 @@ const addDemoMonitors = async (userId, teamId) => {
}
};
const pauseMonitor = async ({ monitorId }) => {
try {
const monitor = await Monitor.findOneAndUpdate(
{ _id: monitorId },
[
{
$set: {
isActive: { $not: "$isActive" },
status: "$$REMOVE",
},
},
],
{ new: true }
);
return monitor;
} catch (error) {
error.service = SERVICE_NAME;
error.method = "pauseMonitor";
throw error;
}
};
export {
getAllMonitors,
getAllMonitorsWithUptimeStats,
@@ -806,6 +838,7 @@ export {
editMonitor,
addDemoMonitors,
getHardwareDetailsById,
pauseMonitor,
};
// Helper functions