remove team id from monitor controller/db methods, standardize parameters
This commit is contained in:
1 parent
4168c86734
commit
b9fbfd2a25
6 files changed
+138
-173
No files matched your search
@@ -78,7 +78,14 @@ class MonitorController {
|
||||
|
||||
getUptimeDetailsById = async (req, res, next) => {
|
||||
try {
|
||||
const data = await this.db.getUptimeDetailsById(req);
|
||||
const { monitorId } = req.params;
|
||||
const { dateRange, normalize } = req.query;
|
||||
|
||||
const data = await this.db.getUptimeDetailsById({
|
||||
monitorId,
|
||||
dateRange,
|
||||
normalize,
|
||||
});
|
||||
return res.success({
|
||||
msg: this.stringService.monitorGetByIdSuccess,
|
||||
data,
|
||||
@@ -107,7 +114,17 @@ class MonitorController {
|
||||
}
|
||||
|
||||
try {
|
||||
const monitorStats = await this.db.getMonitorStatsById(req);
|
||||
let { limit, sortOrder, dateRange, numToDisplay, normalize } = req.query;
|
||||
const { monitorId } = req.params;
|
||||
|
||||
const monitorStats = await this.db.getMonitorStatsById({
|
||||
monitorId,
|
||||
limit,
|
||||
sortOrder,
|
||||
dateRange,
|
||||
numToDisplay,
|
||||
normalize,
|
||||
});
|
||||
return res.success({
|
||||
msg: this.stringService.monitorStatsById,
|
||||
data: monitorStats,
|
||||
@@ -135,7 +152,9 @@ class MonitorController {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const monitor = await this.db.getHardwareDetailsById(req);
|
||||
const { monitorId } = req.params;
|
||||
const { dateRange } = req.query;
|
||||
const monitor = await this.db.getHardwareDetailsById({ monitorId, dateRange });
|
||||
return res.success({
|
||||
msg: this.stringService.monitorGetByIdSuccess,
|
||||
data: monitor,
|
||||
@@ -219,7 +238,7 @@ class MonitorController {
|
||||
}
|
||||
|
||||
try {
|
||||
const monitor = await this.db.createMonitor(req, res);
|
||||
const monitor = await this.db.createMonitor(req.body);
|
||||
|
||||
// Add monitor to job queue
|
||||
this.jobQueue.addJob(monitor._id, monitor);
|
||||
@@ -261,10 +280,10 @@ class MonitorController {
|
||||
throw new Error("File is empty");
|
||||
}
|
||||
|
||||
const { userId, teamId } = req.body;
|
||||
const { _id, teamId } = req.user;
|
||||
|
||||
if (!userId || !teamId) {
|
||||
throw new Error("Missing userId or teamId in form data");
|
||||
if (!_id || !teamId) {
|
||||
throw new Error("Missing userId or teamId");
|
||||
}
|
||||
|
||||
// Get file buffer from memory and convert to string
|
||||
@@ -299,7 +318,7 @@ class MonitorController {
|
||||
}
|
||||
|
||||
const enrichedData = data.map((monitor) => ({
|
||||
userId,
|
||||
userId: _id,
|
||||
teamId,
|
||||
...monitor,
|
||||
description: monitor.description || monitor.name || monitor.url,
|
||||
@@ -388,7 +407,8 @@ class MonitorController {
|
||||
}
|
||||
|
||||
try {
|
||||
const monitor = await this.db.deleteMonitor(req, res, next);
|
||||
const monitorId = req.params.monitorId;
|
||||
const monitor = await this.db.deleteMonitor({ monitorId });
|
||||
await this.jobQueue.deleteJob(monitor);
|
||||
await this.db.deleteStatusPagesByMonitorId(monitor._id);
|
||||
return res.success({ msg: this.stringService.monitorDelete });
|
||||
@@ -410,9 +430,7 @@ class MonitorController {
|
||||
*/
|
||||
deleteAllMonitors = async (req, res, next) => {
|
||||
try {
|
||||
const token = getTokenFromHeaders(req.headers);
|
||||
const { jwtSecret } = this.settingsService.getSettings();
|
||||
const { teamId } = jwt.verify(token, jwtSecret);
|
||||
const { teamId } = req.user;
|
||||
const { monitors, deletedCount } = await this.db.deleteAllMonitors(teamId);
|
||||
await Promise.all(
|
||||
monitors.map(async (monitor) => {
|
||||
@@ -494,18 +512,8 @@ class MonitorController {
|
||||
}
|
||||
|
||||
try {
|
||||
const monitor = await Monitor.findOneAndUpdate(
|
||||
{ _id: req.params.monitorId },
|
||||
[
|
||||
{
|
||||
$set: {
|
||||
isActive: { $not: "$isActive" },
|
||||
status: "$$REMOVE",
|
||||
},
|
||||
},
|
||||
],
|
||||
{ new: true }
|
||||
);
|
||||
const monitorId = req.params.monitorId;
|
||||
const monitor = await this.db.pauseMonitor({ monitorId });
|
||||
monitor.isActive === true
|
||||
? await this.jobQueue.resumeJob(monitor._id, monitor)
|
||||
: await this.jobQueue.pauseJob(monitor);
|
||||
@@ -534,9 +542,7 @@ class MonitorController {
|
||||
*/
|
||||
addDemoMonitors = async (req, res, next) => {
|
||||
try {
|
||||
const token = getTokenFromHeaders(req.headers);
|
||||
const { jwtSecret } = this.settingsService.getSettings();
|
||||
const { _id, teamId } = jwt.verify(token, jwtSecret);
|
||||
const { _id, teamId } = req.user;
|
||||
const demoMonitors = await this.db.addDemoMonitors(_id, teamId);
|
||||
await Promise.all(
|
||||
demoMonitors.map((monitor) => this.jobQueue.addJob(monitor._id, monitor))
|
||||
@@ -603,7 +609,19 @@ class MonitorController {
|
||||
}
|
||||
|
||||
try {
|
||||
const monitors = await this.db.getMonitorsByTeamId(req);
|
||||
let { limit, type, page, rowsPerPage, filter, field, order } = req.query;
|
||||
const teamId = req.user.teamId;
|
||||
|
||||
const monitors = await this.db.getMonitorsByTeamId({
|
||||
limit,
|
||||
type,
|
||||
page,
|
||||
rowsPerPage,
|
||||
filter,
|
||||
field,
|
||||
order,
|
||||
teamId,
|
||||
});
|
||||
return res.success({
|
||||
msg: this.stringService.monitorGetByTeamId,
|
||||
data: monitors,
|
||||
@@ -622,7 +640,15 @@ class MonitorController {
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await this.db.getMonitorsAndSummaryByTeamId(req);
|
||||
const { explain } = req;
|
||||
const { type } = req.query;
|
||||
const { teamId } = req.user;
|
||||
|
||||
const result = await this.db.getMonitorsAndSummaryByTeamId({
|
||||
type,
|
||||
explain,
|
||||
teamId,
|
||||
});
|
||||
return res.success({
|
||||
msg: "OK", // TODO
|
||||
data: result,
|
||||
@@ -641,7 +667,21 @@ class MonitorController {
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await this.db.getMonitorsWithChecksByTeamId(req);
|
||||
const { explain } = req;
|
||||
let { limit, type, page, rowsPerPage, filter, field, order } = req.query;
|
||||
const { teamId } = req.user;
|
||||
|
||||
const result = await this.db.getMonitorsWithChecksByTeamId({
|
||||
limit,
|
||||
type,
|
||||
page,
|
||||
rowsPerPage,
|
||||
filter,
|
||||
field,
|
||||
order,
|
||||
teamId,
|
||||
explain,
|
||||
});
|
||||
return res.success({
|
||||
msg: "OK", // TODO
|
||||
data: result,
|
||||
@@ -653,11 +693,7 @@ class MonitorController {
|
||||
|
||||
seedDb = async (req, res, next) => {
|
||||
try {
|
||||
const { type } = req.body;
|
||||
const token = getTokenFromHeaders(req.headers);
|
||||
const { jwtSecret } = this.settingsService.getSettings();
|
||||
const { _id, teamId } = jwt.verify(token, jwtSecret);
|
||||
|
||||
const { _id, teamId } = req.user;
|
||||
await seedDb(_id, teamId);
|
||||
res.success({ msg: "Database seeded" });
|
||||
} catch (error) {
|
||||
|
||||
@@ -140,7 +140,7 @@ class NotificationController {
|
||||
|
||||
getNotificationsByTeamId = async (req, res, next) => {
|
||||
try {
|
||||
const notifications = await this.db.getNotificationsByTeamId(req.params.teamId);
|
||||
const notifications = await this.db.getNotificationsByTeamId(req.user.teamId);
|
||||
return res.success({
|
||||
msg: "Notifications fetched successfully",
|
||||
data: notifications,
|
||||
|
||||
@@ -6,10 +6,7 @@ import {
|
||||
buildMonitorSummaryByTeamIdPipeline,
|
||||
buildMonitorsByTeamIdPipeline,
|
||||
buildFilteredMonitorsByTeamIdPipeline,
|
||||
buildDePINDetailsByDateRange,
|
||||
buildDePINLatestChecks,
|
||||
} from "./monitorModuleQueries.js";
|
||||
import { getDateRange } from "./monitorModule.js";
|
||||
|
||||
const getMonitorsByTeamIdExecutionStats = async (req) => {
|
||||
try {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -902,102 +902,6 @@ const buildGetMonitorsByTeamIdPipeline = (req) => {
|
||||
];
|
||||
};
|
||||
|
||||
const buildDePINDetailsByDateRange = (monitor, dates, dateString) => {
|
||||
return [
|
||||
{
|
||||
$match: {
|
||||
monitorId: monitor._id,
|
||||
updatedAt: { $gte: dates.start, $lte: dates.end },
|
||||
},
|
||||
},
|
||||
{
|
||||
$project: {
|
||||
_id: 0,
|
||||
city: 1,
|
||||
updatedAt: 1,
|
||||
"location.lat": 1,
|
||||
"location.lng": 1,
|
||||
responseTime: 1,
|
||||
},
|
||||
},
|
||||
{
|
||||
$facet: {
|
||||
groupedMapChecks: [
|
||||
{
|
||||
$group: {
|
||||
_id: {
|
||||
city: "$city",
|
||||
lat: "$location.lat",
|
||||
lng: "$location.lng",
|
||||
},
|
||||
|
||||
avgResponseTime: {
|
||||
$avg: "$responseTime",
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
groupedChecks: [
|
||||
{
|
||||
$group: {
|
||||
_id: {
|
||||
date: {
|
||||
$dateToString: {
|
||||
format: dateString,
|
||||
date: "$updatedAt",
|
||||
},
|
||||
},
|
||||
},
|
||||
avgResponseTime: {
|
||||
$avg: "$responseTime",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
$sort: {
|
||||
"_id.date": 1,
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
{
|
||||
$project: {
|
||||
groupedMapChecks: "$groupedMapChecks",
|
||||
groupedChecks: "$groupedChecks",
|
||||
},
|
||||
},
|
||||
];
|
||||
};
|
||||
|
||||
const buildDePINLatestChecks = (monitor) => {
|
||||
return [
|
||||
{
|
||||
$match: {
|
||||
monitorId: monitor._id,
|
||||
},
|
||||
},
|
||||
{
|
||||
$sort: { updatedAt: -1 },
|
||||
},
|
||||
{
|
||||
$limit: 5,
|
||||
},
|
||||
{
|
||||
$project: {
|
||||
responseTime: 1,
|
||||
city: 1,
|
||||
countryCode: 1,
|
||||
uptBurnt: {
|
||||
$toString: {
|
||||
$ifNull: ["$uptBurnt", 0],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
];
|
||||
};
|
||||
|
||||
export {
|
||||
buildUptimeDetailsPipeline,
|
||||
buildHardwareDetailsPipeline,
|
||||
@@ -1008,6 +912,4 @@ export {
|
||||
buildMonitorsAndSummaryByTeamIdPipeline,
|
||||
buildMonitorsWithChecksByTeamIdPipeline,
|
||||
buildFilteredMonitorsByTeamIdPipeline,
|
||||
buildDePINDetailsByDateRange,
|
||||
buildDePINLatestChecks,
|
||||
};
|
||||
@@ -17,10 +17,7 @@ class NotificationRoutes {
|
||||
|
||||
this.router.post("/", this.notificationController.createNotification);
|
||||
|
||||
this.router.get(
|
||||
"/team/:teamId",
|
||||
this.notificationController.getNotificationsByTeamId
|
||||
);
|
||||
this.router.get("/team", this.notificationController.getNotificationsByTeamId);
|
||||
|
||||
this.router.delete("/:id", this.notificationController.deleteNotification);
|
||||
|
||||
|
||||
Reference in new issue
Block a user