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) => {
|
getUptimeDetailsById = async (req, res, next) => {
|
||||||
try {
|
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({
|
return res.success({
|
||||||
msg: this.stringService.monitorGetByIdSuccess,
|
msg: this.stringService.monitorGetByIdSuccess,
|
||||||
data,
|
data,
|
||||||
@@ -107,7 +114,17 @@ class MonitorController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
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({
|
return res.success({
|
||||||
msg: this.stringService.monitorStatsById,
|
msg: this.stringService.monitorStatsById,
|
||||||
data: monitorStats,
|
data: monitorStats,
|
||||||
@@ -135,7 +152,9 @@ class MonitorController {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
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({
|
return res.success({
|
||||||
msg: this.stringService.monitorGetByIdSuccess,
|
msg: this.stringService.monitorGetByIdSuccess,
|
||||||
data: monitor,
|
data: monitor,
|
||||||
@@ -219,7 +238,7 @@ class MonitorController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const monitor = await this.db.createMonitor(req, res);
|
const monitor = await this.db.createMonitor(req.body);
|
||||||
|
|
||||||
// Add monitor to job queue
|
// Add monitor to job queue
|
||||||
this.jobQueue.addJob(monitor._id, monitor);
|
this.jobQueue.addJob(monitor._id, monitor);
|
||||||
@@ -261,10 +280,10 @@ class MonitorController {
|
|||||||
throw new Error("File is empty");
|
throw new Error("File is empty");
|
||||||
}
|
}
|
||||||
|
|
||||||
const { userId, teamId } = req.body;
|
const { _id, teamId } = req.user;
|
||||||
|
|
||||||
if (!userId || !teamId) {
|
if (!_id || !teamId) {
|
||||||
throw new Error("Missing userId or teamId in form data");
|
throw new Error("Missing userId or teamId");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get file buffer from memory and convert to string
|
// Get file buffer from memory and convert to string
|
||||||
@@ -299,7 +318,7 @@ class MonitorController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const enrichedData = data.map((monitor) => ({
|
const enrichedData = data.map((monitor) => ({
|
||||||
userId,
|
userId: _id,
|
||||||
teamId,
|
teamId,
|
||||||
...monitor,
|
...monitor,
|
||||||
description: monitor.description || monitor.name || monitor.url,
|
description: monitor.description || monitor.name || monitor.url,
|
||||||
@@ -388,7 +407,8 @@ class MonitorController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
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.jobQueue.deleteJob(monitor);
|
||||||
await this.db.deleteStatusPagesByMonitorId(monitor._id);
|
await this.db.deleteStatusPagesByMonitorId(monitor._id);
|
||||||
return res.success({ msg: this.stringService.monitorDelete });
|
return res.success({ msg: this.stringService.monitorDelete });
|
||||||
@@ -410,9 +430,7 @@ class MonitorController {
|
|||||||
*/
|
*/
|
||||||
deleteAllMonitors = async (req, res, next) => {
|
deleteAllMonitors = async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
const token = getTokenFromHeaders(req.headers);
|
const { teamId } = req.user;
|
||||||
const { jwtSecret } = this.settingsService.getSettings();
|
|
||||||
const { teamId } = jwt.verify(token, jwtSecret);
|
|
||||||
const { monitors, deletedCount } = await this.db.deleteAllMonitors(teamId);
|
const { monitors, deletedCount } = await this.db.deleteAllMonitors(teamId);
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
monitors.map(async (monitor) => {
|
monitors.map(async (monitor) => {
|
||||||
@@ -494,18 +512,8 @@ class MonitorController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const monitor = await Monitor.findOneAndUpdate(
|
const monitorId = req.params.monitorId;
|
||||||
{ _id: req.params.monitorId },
|
const monitor = await this.db.pauseMonitor({ monitorId });
|
||||||
[
|
|
||||||
{
|
|
||||||
$set: {
|
|
||||||
isActive: { $not: "$isActive" },
|
|
||||||
status: "$$REMOVE",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
{ new: true }
|
|
||||||
);
|
|
||||||
monitor.isActive === true
|
monitor.isActive === true
|
||||||
? await this.jobQueue.resumeJob(monitor._id, monitor)
|
? await this.jobQueue.resumeJob(monitor._id, monitor)
|
||||||
: await this.jobQueue.pauseJob(monitor);
|
: await this.jobQueue.pauseJob(monitor);
|
||||||
@@ -534,9 +542,7 @@ class MonitorController {
|
|||||||
*/
|
*/
|
||||||
addDemoMonitors = async (req, res, next) => {
|
addDemoMonitors = async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
const token = getTokenFromHeaders(req.headers);
|
const { _id, teamId } = req.user;
|
||||||
const { jwtSecret } = this.settingsService.getSettings();
|
|
||||||
const { _id, teamId } = jwt.verify(token, jwtSecret);
|
|
||||||
const demoMonitors = await this.db.addDemoMonitors(_id, teamId);
|
const demoMonitors = await this.db.addDemoMonitors(_id, teamId);
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
demoMonitors.map((monitor) => this.jobQueue.addJob(monitor._id, monitor))
|
demoMonitors.map((monitor) => this.jobQueue.addJob(monitor._id, monitor))
|
||||||
@@ -603,7 +609,19 @@ class MonitorController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
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({
|
return res.success({
|
||||||
msg: this.stringService.monitorGetByTeamId,
|
msg: this.stringService.monitorGetByTeamId,
|
||||||
data: monitors,
|
data: monitors,
|
||||||
@@ -622,7 +640,15 @@ class MonitorController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
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({
|
return res.success({
|
||||||
msg: "OK", // TODO
|
msg: "OK", // TODO
|
||||||
data: result,
|
data: result,
|
||||||
@@ -641,7 +667,21 @@ class MonitorController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
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({
|
return res.success({
|
||||||
msg: "OK", // TODO
|
msg: "OK", // TODO
|
||||||
data: result,
|
data: result,
|
||||||
@@ -653,11 +693,7 @@ class MonitorController {
|
|||||||
|
|
||||||
seedDb = async (req, res, next) => {
|
seedDb = async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
const { type } = req.body;
|
const { _id, teamId } = req.user;
|
||||||
const token = getTokenFromHeaders(req.headers);
|
|
||||||
const { jwtSecret } = this.settingsService.getSettings();
|
|
||||||
const { _id, teamId } = jwt.verify(token, jwtSecret);
|
|
||||||
|
|
||||||
await seedDb(_id, teamId);
|
await seedDb(_id, teamId);
|
||||||
res.success({ msg: "Database seeded" });
|
res.success({ msg: "Database seeded" });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
@@ -140,7 +140,7 @@ class NotificationController {
|
|||||||
|
|
||||||
getNotificationsByTeamId = async (req, res, next) => {
|
getNotificationsByTeamId = async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
const notifications = await this.db.getNotificationsByTeamId(req.params.teamId);
|
const notifications = await this.db.getNotificationsByTeamId(req.user.teamId);
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: "Notifications fetched successfully",
|
msg: "Notifications fetched successfully",
|
||||||
data: notifications,
|
data: notifications,
|
||||||
|
|||||||
@@ -6,10 +6,7 @@ import {
|
|||||||
buildMonitorSummaryByTeamIdPipeline,
|
buildMonitorSummaryByTeamIdPipeline,
|
||||||
buildMonitorsByTeamIdPipeline,
|
buildMonitorsByTeamIdPipeline,
|
||||||
buildFilteredMonitorsByTeamIdPipeline,
|
buildFilteredMonitorsByTeamIdPipeline,
|
||||||
buildDePINDetailsByDateRange,
|
|
||||||
buildDePINLatestChecks,
|
|
||||||
} from "./monitorModuleQueries.js";
|
} from "./monitorModuleQueries.js";
|
||||||
import { getDateRange } from "./monitorModule.js";
|
|
||||||
|
|
||||||
const getMonitorsByTeamIdExecutionStats = async (req) => {
|
const getMonitorsByTeamIdExecutionStats = async (req) => {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ import MonitorStats from "../../models/MonitorStats.js";
|
|||||||
import Check from "../../models/Check.js";
|
import Check from "../../models/Check.js";
|
||||||
import PageSpeedCheck from "../../models/PageSpeedCheck.js";
|
import PageSpeedCheck from "../../models/PageSpeedCheck.js";
|
||||||
import HardwareCheck from "../../models/HardwareCheck.js";
|
import HardwareCheck from "../../models/HardwareCheck.js";
|
||||||
import Notification from "../../models/Notification.js";
|
|
||||||
import { NormalizeData, NormalizeDataUptimeDetails } from "../../../utils/dataUtils.js";
|
import { NormalizeData, NormalizeDataUptimeDetails } from "../../../utils/dataUtils.js";
|
||||||
import ServiceRegistry from "../../../service/serviceRegistry.js";
|
import ServiceRegistry from "../../../service/serviceRegistry.js";
|
||||||
import StringService from "../../../service/stringService.js";
|
import StringService from "../../../service/stringService.js";
|
||||||
@@ -15,14 +14,11 @@ import { ObjectId } from "mongodb";
|
|||||||
import {
|
import {
|
||||||
buildUptimeDetailsPipeline,
|
buildUptimeDetailsPipeline,
|
||||||
buildHardwareDetailsPipeline,
|
buildHardwareDetailsPipeline,
|
||||||
buildMonitorStatsPipeline,
|
|
||||||
buildMonitorSummaryByTeamIdPipeline,
|
buildMonitorSummaryByTeamIdPipeline,
|
||||||
buildMonitorsByTeamIdPipeline,
|
buildMonitorsByTeamIdPipeline,
|
||||||
buildMonitorsAndSummaryByTeamIdPipeline,
|
buildMonitorsAndSummaryByTeamIdPipeline,
|
||||||
buildMonitorsWithChecksByTeamIdPipeline,
|
buildMonitorsWithChecksByTeamIdPipeline,
|
||||||
buildFilteredMonitorsByTeamIdPipeline,
|
buildFilteredMonitorsByTeamIdPipeline,
|
||||||
buildDePINDetailsByDateRange,
|
|
||||||
buildDePINLatestChecks,
|
|
||||||
} from "./monitorModuleQueries.js";
|
} from "./monitorModuleQueries.js";
|
||||||
const __filename = fileURLToPath(import.meta.url);
|
const __filename = fileURLToPath(import.meta.url);
|
||||||
const __dirname = path.dirname(__filename);
|
const __dirname = path.dirname(__filename);
|
||||||
@@ -335,11 +331,9 @@ const calculateGroupStats = (group) => {
|
|||||||
* @returns {Promise<Monitor>}
|
* @returns {Promise<Monitor>}
|
||||||
* @throws {Error}
|
* @throws {Error}
|
||||||
*/
|
*/
|
||||||
const getUptimeDetailsById = async (req) => {
|
const getUptimeDetailsById = async ({ monitorId, dateRange, normalize }) => {
|
||||||
const stringService = ServiceRegistry.get(StringService.SERVICE_NAME);
|
const stringService = ServiceRegistry.get(StringService.SERVICE_NAME);
|
||||||
try {
|
try {
|
||||||
const { monitorId } = req.params;
|
|
||||||
const { dateRange, normalize } = req.query;
|
|
||||||
const dates = getDateRange(dateRange);
|
const dates = getDateRange(dateRange);
|
||||||
const formatLookup = {
|
const formatLookup = {
|
||||||
recent: "%Y-%m-%dT%H:%M:00Z",
|
recent: "%Y-%m-%dT%H:%M:00Z",
|
||||||
@@ -392,11 +386,16 @@ const getUptimeDetailsById = async (req) => {
|
|||||||
* @returns {Promise<Monitor>}
|
* @returns {Promise<Monitor>}
|
||||||
* @throws {Error}
|
* @throws {Error}
|
||||||
*/
|
*/
|
||||||
const getMonitorStatsById = async (req) => {
|
const getMonitorStatsById = async ({
|
||||||
|
monitorId,
|
||||||
|
limit,
|
||||||
|
sortOrder,
|
||||||
|
dateRange,
|
||||||
|
numToDisplay,
|
||||||
|
normalize,
|
||||||
|
}) => {
|
||||||
const stringService = ServiceRegistry.get(StringService.SERVICE_NAME);
|
const stringService = ServiceRegistry.get(StringService.SERVICE_NAME);
|
||||||
try {
|
try {
|
||||||
const { monitorId } = req.params;
|
|
||||||
|
|
||||||
// Get monitor, if we can't find it, abort with error
|
// Get monitor, if we can't find it, abort with error
|
||||||
const monitor = await Monitor.findById(monitorId);
|
const monitor = await Monitor.findById(monitorId);
|
||||||
if (monitor === null || monitor === undefined) {
|
if (monitor === null || monitor === undefined) {
|
||||||
@@ -404,7 +403,6 @@ const getMonitorStatsById = async (req) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get query params
|
// Get query params
|
||||||
let { limit, sortOrder, dateRange, numToDisplay, normalize } = req.query;
|
|
||||||
const sort = sortOrder === "asc" ? 1 : -1;
|
const sort = sortOrder === "asc" ? 1 : -1;
|
||||||
|
|
||||||
// Get Checks for monitor in date range requested
|
// 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 {
|
try {
|
||||||
const { monitorId } = req.params;
|
|
||||||
const { dateRange } = req.query;
|
|
||||||
const monitor = await Monitor.findById(monitorId);
|
const monitor = await Monitor.findById(monitorId);
|
||||||
const dates = getDateRange(dateRange);
|
const dates = getDateRange(dateRange);
|
||||||
const formatLookup = {
|
const formatLookup = {
|
||||||
@@ -509,8 +505,16 @@ const getMonitorById = async (monitorId) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const getMonitorsByTeamId = async (req) => {
|
const getMonitorsByTeamId = async ({
|
||||||
let { limit, type, page, rowsPerPage, filter, field, order } = req.query;
|
limit,
|
||||||
|
type,
|
||||||
|
page,
|
||||||
|
rowsPerPage,
|
||||||
|
filter,
|
||||||
|
field,
|
||||||
|
order,
|
||||||
|
teamId,
|
||||||
|
}) => {
|
||||||
limit = parseInt(limit);
|
limit = parseInt(limit);
|
||||||
page = parseInt(page);
|
page = parseInt(page);
|
||||||
rowsPerPage = parseInt(rowsPerPage);
|
rowsPerPage = parseInt(rowsPerPage);
|
||||||
@@ -519,7 +523,6 @@ const getMonitorsByTeamId = async (req) => {
|
|||||||
order = "asc";
|
order = "asc";
|
||||||
}
|
}
|
||||||
// Build match stage
|
// Build match stage
|
||||||
const teamId = req.user.teamId;
|
|
||||||
const matchStage = { teamId: ObjectId.createFromHexString(teamId) };
|
const matchStage = { teamId: ObjectId.createFromHexString(teamId) };
|
||||||
if (type !== undefined) {
|
if (type !== undefined) {
|
||||||
matchStage.type = Array.isArray(type) ? { $in: type } : type;
|
matchStage.type = Array.isArray(type) ? { $in: type } : type;
|
||||||
@@ -558,16 +561,15 @@ const getMonitorsByTeamId = async (req) => {
|
|||||||
return { summary, monitors, filteredMonitors: normalizedFilteredMonitors };
|
return { summary, monitors, filteredMonitors: normalizedFilteredMonitors };
|
||||||
};
|
};
|
||||||
|
|
||||||
const getMonitorsAndSummaryByTeamId = async (req) => {
|
const getMonitorsAndSummaryByTeamId = async ({ type, explain, teamId }) => {
|
||||||
try {
|
try {
|
||||||
const { type } = req.query;
|
const parsedTeamId = ObjectId.createFromHexString(teamId);
|
||||||
const teamId = ObjectId.createFromHexString(req.user.teamId);
|
const matchStage = { teamId: parsedTeamId };
|
||||||
const matchStage = { teamId };
|
|
||||||
if (type !== undefined) {
|
if (type !== undefined) {
|
||||||
matchStage.type = Array.isArray(type) ? { $in: type } : type;
|
matchStage.type = Array.isArray(type) ? { $in: type } : type;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (req.explain === true) {
|
if (explain === true) {
|
||||||
return Monitor.aggregate(
|
return Monitor.aggregate(
|
||||||
buildMonitorsAndSummaryByTeamIdPipeline({ matchStage })
|
buildMonitorsAndSummaryByTeamIdPipeline({ matchStage })
|
||||||
).explain("executionStats");
|
).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 {
|
try {
|
||||||
let { limit, type, page, rowsPerPage, filter, field, order } = req.query;
|
|
||||||
limit = parseInt(limit);
|
limit = parseInt(limit);
|
||||||
page = parseInt(page);
|
page = parseInt(page);
|
||||||
rowsPerPage = parseInt(rowsPerPage);
|
rowsPerPage = parseInt(rowsPerPage);
|
||||||
@@ -595,14 +606,14 @@ const getMonitorsWithChecksByTeamId = async (req) => {
|
|||||||
field = "name";
|
field = "name";
|
||||||
order = "asc";
|
order = "asc";
|
||||||
}
|
}
|
||||||
const teamId = ObjectId.createFromHexString(req.user.teamId);
|
const parsedTeamId = ObjectId.createFromHexString(teamId);
|
||||||
// Build match stage
|
// Build match stage
|
||||||
const matchStage = { teamId };
|
const matchStage = { teamId: parsedTeamId };
|
||||||
if (type !== undefined) {
|
if (type !== undefined) {
|
||||||
matchStage.type = Array.isArray(type) ? { $in: type } : type;
|
matchStage.type = Array.isArray(type) ? { $in: type } : type;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (req.explain === true) {
|
if (explain === true) {
|
||||||
return Monitor.aggregate(
|
return Monitor.aggregate(
|
||||||
buildMonitorsWithChecksByTeamIdPipeline({
|
buildMonitorsWithChecksByTeamIdPipeline({
|
||||||
matchStage,
|
matchStage,
|
||||||
@@ -654,9 +665,9 @@ const getMonitorsWithChecksByTeamId = async (req) => {
|
|||||||
* @returns {Promise<Monitor>}
|
* @returns {Promise<Monitor>}
|
||||||
* @throws {Error}
|
* @throws {Error}
|
||||||
*/
|
*/
|
||||||
const createMonitor = async (req, res) => {
|
const createMonitor = async (body) => {
|
||||||
try {
|
try {
|
||||||
const monitor = new Monitor({ ...req.body });
|
const monitor = new Monitor({ ...body });
|
||||||
const saved = await monitor.save();
|
const saved = await monitor.save();
|
||||||
return saved;
|
return saved;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -695,10 +706,8 @@ const createBulkMonitors = async (req) => {
|
|||||||
* @returns {Promise<Monitor>}
|
* @returns {Promise<Monitor>}
|
||||||
* @throws {Error}
|
* @throws {Error}
|
||||||
*/
|
*/
|
||||||
const deleteMonitor = async (req, res) => {
|
const deleteMonitor = async ({ monitorId }) => {
|
||||||
const stringService = ServiceRegistry.get(StringService.SERVICE_NAME);
|
const stringService = ServiceRegistry.get(StringService.SERVICE_NAME);
|
||||||
|
|
||||||
const monitorId = req.params.monitorId;
|
|
||||||
try {
|
try {
|
||||||
const monitor = await Monitor.findByIdAndDelete(monitorId);
|
const monitor = await Monitor.findByIdAndDelete(monitorId);
|
||||||
if (!monitor) {
|
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 {
|
export {
|
||||||
getAllMonitors,
|
getAllMonitors,
|
||||||
getAllMonitorsWithUptimeStats,
|
getAllMonitorsWithUptimeStats,
|
||||||
@@ -806,6 +838,7 @@ export {
|
|||||||
editMonitor,
|
editMonitor,
|
||||||
addDemoMonitors,
|
addDemoMonitors,
|
||||||
getHardwareDetailsById,
|
getHardwareDetailsById,
|
||||||
|
pauseMonitor,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Helper functions
|
// 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 {
|
export {
|
||||||
buildUptimeDetailsPipeline,
|
buildUptimeDetailsPipeline,
|
||||||
buildHardwareDetailsPipeline,
|
buildHardwareDetailsPipeline,
|
||||||
@@ -1008,6 +912,4 @@ export {
|
|||||||
buildMonitorsAndSummaryByTeamIdPipeline,
|
buildMonitorsAndSummaryByTeamIdPipeline,
|
||||||
buildMonitorsWithChecksByTeamIdPipeline,
|
buildMonitorsWithChecksByTeamIdPipeline,
|
||||||
buildFilteredMonitorsByTeamIdPipeline,
|
buildFilteredMonitorsByTeamIdPipeline,
|
||||||
buildDePINDetailsByDateRange,
|
|
||||||
buildDePINLatestChecks,
|
|
||||||
};
|
};
|
||||||
@@ -17,10 +17,7 @@ class NotificationRoutes {
|
|||||||
|
|
||||||
this.router.post("/", this.notificationController.createNotification);
|
this.router.post("/", this.notificationController.createNotification);
|
||||||
|
|
||||||
this.router.get(
|
this.router.get("/team", this.notificationController.getNotificationsByTeamId);
|
||||||
"/team/:teamId",
|
|
||||||
this.notificationController.getNotificationsByTeamId
|
|
||||||
);
|
|
||||||
|
|
||||||
this.router.delete("/:id", this.notificationController.deleteNotification);
|
this.router.delete("/:id", this.notificationController.deleteNotification);
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user