2025-04-20 11:29:53 -07:00
import {
getMonitorByIdParamValidation ,
getMonitorByIdQueryValidation ,
getMonitorsByTeamIdParamValidation ,
getMonitorsByTeamIdQueryValidation ,
createMonitorBodyValidation ,
createMonitorsBodyValidation ,
getMonitorURLByQueryValidation ,
editMonitorBodyValidation ,
pauseMonitorParamValidation ,
getMonitorStatsByIdParamValidation ,
getMonitorStatsByIdQueryValidation ,
getCertificateParamValidation ,
getHardwareDetailsByIdParamValidation ,
getHardwareDetailsByIdQueryValidation ,
} from "../validation/joi.js" ;
import sslChecker from "ssl-checker" ;
import logger from "../utils/logger.js" ;
import axios from "axios" ;
import seedDb from "../db/mongo/utils/seedDb.js" ;
2025-04-29 12:47:24 +05:30
import pkg from "papaparse" ;
2025-07-15 16:33:35 -07:00
import { asyncHandler , createServerError } from "../utils/errorUtils.js" ;
2025-07-21 13:58:01 -07:00
import { fetchMonitorCertificate } from "./controllerUtils.js" ;
2025-04-20 11:29:53 -07:00
2025-07-15 16:33:35 -07:00
const SERVICE_NAME = "monitorController" ;
2025-04-20 11:29:53 -07:00
class MonitorController {
2025-05-06 00:08:11 +05:30
constructor ( db , settingsService , jobQueue , stringService , emailService ) {
2025-04-20 11:29:53 -07:00
this . db = db ;
this . settingsService = settingsService ;
this . jobQueue = jobQueue ;
this . stringService = stringService ;
2025-05-06 00:08:11 +05:30
this . emailService = emailService ;
2025-04-20 11:29:53 -07:00
}
2025-07-21 13:58:01 -07:00
async verifyTeamAccess ( teamId , monitorId ) {
const monitor = await this . db . getMonitorById ( monitorId );
if ( ! monitor . teamId . equals ( teamId )) {
const error = new Error ( "Unauthorized" );
error . status = 403 ;
throw error ;
}
}
2025-04-20 11:29:53 -07:00
/**
* Returns all monitors
* @async
* @param {Express.Request} req
* @param {Express.Response} res
* @param {function} next
* @returns {Promise<Express.Response>}
* @throws {Error}
*/
2025-07-15 16:33:35 -07:00
getAllMonitors = asyncHandler (
async ( req , res , next ) => {
2025-04-20 11:29:53 -07:00
const monitors = await this . db . getAllMonitors ();
return res . success ({
msg : this . stringService . monitorGetAll ,
data : monitors ,
});
2025-07-15 16:33:35 -07:00
},
SERVICE_NAME ,
"getAllMonitors"
);
2025-04-20 11:29:53 -07:00
2025-07-15 16:33:35 -07:00
getUptimeDetailsById = asyncHandler (
async ( req , res , next ) => {
2025-06-13 11:49:03 +08:00
const { monitorId } = req . params ;
const { dateRange , normalize } = req . query ;
2025-07-21 13:58:01 -07:00
const teamId = req ? . user ? . teamId ;
if ( ! teamId ) {
throw new Error ( "Team ID is required" );
}
await this . verifyTeamAccess ( teamId , monitorId );
2025-06-13 11:49:03 +08:00
const data = await this . db . getUptimeDetailsById ({
monitorId ,
dateRange ,
normalize ,
});
2025-04-20 11:29:53 -07:00
return res . success ({
msg : this . stringService . monitorGetByIdSuccess ,
data ,
});
2025-07-15 16:33:35 -07:00
},
SERVICE_NAME ,
"getUptimeDetailsById"
);
2025-04-20 11:29:53 -07:00
/**
* Returns monitor stats for monitor with matching ID
* @async
* @param {Express.Request} req
* @param {Express.Response} res
* @param {function} next
* @returns {Promise<Express.Response>}
* @throws {Error}
*/
2025-07-15 16:33:35 -07:00
getMonitorStatsById = asyncHandler (
async ( req , res , next ) => {
2025-04-20 11:29:53 -07:00
await getMonitorStatsByIdParamValidation . validateAsync ( req . params );
await getMonitorStatsByIdQueryValidation . validateAsync ( req . query );
2025-06-13 11:49:03 +08:00
let { limit , sortOrder , dateRange , numToDisplay , normalize } = req . query ;
const { monitorId } = req . params ;
2025-07-21 13:58:01 -07:00
const teamId = req ? . user ? . teamId ;
if ( ! teamId ) {
throw new Error ( "Team ID is required" );
}
await this . verifyTeamAccess ( teamId , monitorId );
2025-06-13 11:49:03 +08:00
const monitorStats = await this . db . getMonitorStatsById ({
monitorId ,
limit ,
sortOrder ,
dateRange ,
numToDisplay ,
normalize ,
});
2025-04-20 11:29:53 -07:00
return res . success ({
msg : this . stringService . monitorStatsById ,
data : monitorStats ,
});
2025-07-15 16:33:35 -07:00
},
SERVICE_NAME ,
"getMonitorStatsById"
);
2025-04-20 11:29:53 -07:00
/**
* Get hardware details for a specific monitor by ID
* @async
* @param {Express.Request} req - Express request object containing monitorId in params
* @param {Express.Response} res - Express response object
* @param {Express.NextFunction} next - Express next middleware function
* @returns {Promise<Express.Response>}
* @throws {Error} - Throws error if monitor not found or other database errors
*/
2025-07-15 16:33:35 -07:00
getHardwareDetailsById = asyncHandler (
async ( req , res , next ) => {
2025-04-20 11:29:53 -07:00
await getHardwareDetailsByIdParamValidation . validateAsync ( req . params );
await getHardwareDetailsByIdQueryValidation . validateAsync ( req . query );
2025-07-15 16:33:35 -07:00
2025-07-21 13:58:01 -07:00
const monitorId = req ? . params ? . monitorId ;
const dateRange = req ? . query ? . dateRange ;
const teamId = req ? . user ? . teamId ;
if ( ! teamId ) {
throw new Error ( "Team ID is required" );
}
await this . verifyTeamAccess ( teamId , monitorId );
2025-06-13 11:49:03 +08:00
const monitor = await this . db . getHardwareDetailsById ({ monitorId , dateRange });
2025-04-20 11:29:53 -07:00
return res . success ({
msg : this . stringService . monitorGetByIdSuccess ,
data : monitor ,
});
2025-07-15 16:33:35 -07:00
},
SERVICE_NAME ,
"getHardwareDetailsById"
);
2025-04-20 11:29:53 -07:00
2025-07-15 16:33:35 -07:00
getMonitorCertificate = asyncHandler (
2025-07-21 13:58:01 -07:00
async ( req , res , next ) => {
2025-04-20 11:29:53 -07:00
await getCertificateParamValidation . validateAsync ( req . params );
const { monitorId } = req . params ;
const monitor = await this . db . getMonitorById ( monitorId );
const certificate = await fetchMonitorCertificate ( sslChecker , monitor );
return res . success ({
msg : this . stringService . monitorCertificate ,
data : {
certificateDate : new Date ( certificate . validTo ),
},
});
2025-07-15 16:33:35 -07:00
},
SERVICE_NAME ,
"getMonitorCertificate"
);
2025-04-20 11:29:53 -07:00
/**
* Retrieves a monitor by its ID.
* @async
* @param {Object} req - The Express request object.
* @property {Object} req.params - The parameters of the request.
* @property {string} req.params.monitorId - The ID of the monitor to be retrieved.
* @param {Object} res - The Express response object.
* @param {function} next - The next middleware function.
* @returns {Object} The response object with a success status, a message, and the retrieved monitor data.
* @throws {Error} If there is an error during the process, especially if the monitor is not found (404) or if there is a validation error (422).
*/
2025-07-15 16:33:35 -07:00
getMonitorById = asyncHandler (
async ( req , res , next ) => {
2025-04-20 11:29:53 -07:00
await getMonitorByIdParamValidation . validateAsync ( req . params );
await getMonitorByIdQueryValidation . validateAsync ( req . query );
const monitor = await this . db . getMonitorById ( req . params . monitorId );
2025-07-21 13:58:01 -07:00
const teamId = req ? . user ? . teamId ;
if ( ! teamId ) {
throw new Error ( "Team ID is required" );
}
if ( ! monitor . teamId . equals ( teamId )) {
const error = new Error ( "Unauthorized" );
error . status = 403 ;
throw error ;
}
2025-04-20 11:29:53 -07:00
return res . success ({
msg : this . stringService . monitorGetByIdSuccess ,
data : monitor ,
});
2025-07-15 16:33:35 -07:00
},
SERVICE_NAME ,
"getMonitorById"
);
2025-04-20 11:29:53 -07:00
/**
* Creates a new monitor and adds it to the job queue.
* @async
* @param {Object} req - The Express request object.
* @property {Object} req.body - The body of the request.
* @property {Array} req.body.notifications - The notifications associated with the monitor.
* @param {Object} res - The Express response object.
* @param {function} next - The next middleware function.
* @returns {Object} The response object with a success status, a message indicating the creation of the monitor, and the created monitor data.
* @throws {Error} If there is an error during the process, especially if there is a validation error (422).
*/
2025-07-15 16:33:35 -07:00
createMonitor = asyncHandler (
async ( req , res , next ) => {
2025-04-20 11:29:53 -07:00
await createMonitorBodyValidation . validateAsync ( req . body );
2025-06-13 12:16:52 +08:00
const { _id , teamId } = req . user ;
const monitor = await this . db . createMonitor ({
body : req . body ,
teamId ,
userId : _id ,
});
2025-04-20 11:29:53 -07:00
// Add monitor to job queue
this . jobQueue . addJob ( monitor . _id , monitor );
return res . success ({
msg : this . stringService . monitorCreate ,
data : monitor ,
});
2025-07-15 16:33:35 -07:00
},
SERVICE_NAME ,
"createMonitor"
);
2025-04-20 11:29:53 -07:00
/**
2025-04-29 12:47:24 +05:30
* Creates bulk monitors and adds them to the job queue after parsing CSV.
2025-04-20 11:29:53 -07:00
* @async
* @param {Object} req - The Express request object.
2025-04-29 12:47:24 +05:30
* @property {Object} req.file - The uploaded CSV file.
2025-04-20 11:29:53 -07:00
* @param {Object} res - The Express response object.
* @param {function} next - The next middleware function.
2025-04-29 12:47:24 +05:30
* @returns {Object} The response object with a success status and message.
2025-04-20 11:29:53 -07:00
* @throws {Error} If there is an error during the process, especially if there is a validation error (422).
*/
2025-07-15 16:33:35 -07:00
createBulkMonitors = asyncHandler (
async ( req , res , next ) => {
2025-04-29 12:47:24 +05:30
const { parse } = pkg ;
2025-05-02 02:54:39 +05:30
// validate the file
2025-04-29 12:47:24 +05:30
if ( ! req . file ) {
2025-05-02 02:54:39 +05:30
throw new Error ( "No file uploaded" );
}
// Check if the file is a CSV
if ( ! req . file . mimetype . includes ( "csv" )) {
throw new Error ( "File is not a CSV" );
2025-04-29 12:47:24 +05:30
}
2025-05-02 02:54:39 +05:30
// Validate if the file is empty
if ( req . file . size === 0 ) {
throw new Error ( "File is empty" );
}
2025-06-13 11:49:03 +08:00
const { _id , teamId } = req . user ;
2025-05-02 02:54:39 +05:30
2025-06-13 11:49:03 +08:00
if ( ! _id || ! teamId ) {
throw new Error ( "Missing userId or teamId" );
2025-04-29 12:47:24 +05:30
}
2025-05-02 02:54:39 +05:30
2025-04-29 12:47:24 +05:30
// Get file buffer from memory and convert to string
const fileData = req . file . buffer . toString ( "utf-8" );
2025-05-02 02:54:39 +05:30
2025-05-01 12:05:28 +05:30
// Parse the CSV data
2025-04-29 12:47:24 +05:30
parse ( fileData , {
header : true ,
skipEmptyLines : true ,
transform : ( value , header ) => {
2025-05-01 12:05:28 +05:30
if ( value === "" ) return undefined ; // Empty fields become undefined
2025-05-02 02:54:39 +05:30
2025-05-01 12:05:28 +05:30
// Handle 'port' and 'interval' fields, check if they're valid numbers
2025-04-29 12:47:24 +05:30
if ([ "port" , "interval" ]. includes ( header )) {
const num = parseInt ( value , 10 );
2025-05-01 12:05:28 +05:30
if ( isNaN ( num )) {
2025-05-02 02:54:39 +05:30
throw new Error ( ` ${ header } should be a valid number, got: ${ value } ` );
2025-05-01 12:05:28 +05:30
}
return num ;
2025-04-29 12:47:24 +05:30
}
2025-05-02 02:54:39 +05:30
2025-04-29 12:47:24 +05:30
return value ;
},
complete : async ({ data , errors }) => {
2025-07-15 16:33:35 -07:00
if ( errors . length > 0 ) {
throw createServerError ( "Error parsing CSV" );
}
2025-05-02 02:54:39 +05:30
2025-07-15 16:33:35 -07:00
if ( ! data || data . length === 0 ) {
throw createServerError ( "CSV file contains no data rows" );
2025-04-29 12:47:24 +05:30
}
2025-07-15 16:33:35 -07:00
const enrichedData = data . map (( monitor ) => ({
userId : _id ,
teamId ,
... monitor ,
description : monitor . description || monitor . name || monitor . url ,
name : monitor . name || monitor . url ,
type : monitor . type || "http" ,
}));
await createMonitorsBodyValidation . validateAsync ( enrichedData );
const monitors = await this . db . createBulkMonitors ( enrichedData );
await Promise . all (
monitors . map ( async ( monitor , index ) => {
this . jobQueue . addJob ( monitor . _id , monitor );
})
);
return res . success ({
msg : this . stringService . bulkMonitorsCreate ,
data : monitors ,
});
2025-04-29 12:47:24 +05:30
},
2025-04-20 11:29:53 -07:00
});
2025-07-15 16:33:35 -07:00
},
SERVICE_NAME ,
"createBulkMonitors"
);
2025-04-20 11:29:53 -07:00
/**
* Checks if the endpoint can be resolved
* @async
* @param {Object} req - The Express request object.
* @property {Object} req.query - The query parameters of the request.
* @param {Object} res - The Express response object.
* @param {function} next - The next middleware function.
* @returns {Object} The response object with a success status, a message, and the resolution result.
* @throws {Error} If there is an error during the process, especially if there is a validation error (422).
*/
2025-07-15 16:33:35 -07:00
checkEndpointResolution = asyncHandler (
async ( req , res , next ) => {
2025-04-20 11:29:53 -07:00
await getMonitorURLByQueryValidation . validateAsync ( req . query );
const { monitorURL } = req . query ;
const parsedUrl = new URL ( monitorURL );
const response = await axios . get ( parsedUrl , {
timeout : 5000 ,
validateStatus : () => true ,
});
return res . success ({
status : response . status ,
msg : response . statusText ,
});
2025-07-15 16:33:35 -07:00
},
SERVICE_NAME ,
"checkEndpointResolution"
);
2025-04-20 11:29:53 -07:00
/**
* Deletes a monitor by its ID and also deletes associated checks, alerts, and notifications.
* @async
* @param {Object} req - The Express request object.
* @property {Object} req.params - The parameters of the request.
* @property {string} req.params.monitorId - The ID of the monitor to be deleted.
* @param {Object} res - The Express response object.
* @param {function} next - The next middleware function.
* @returns {Object} The response object with a success status and a message indicating the deletion of the monitor.
* @throws {Error} If there is an error during the process, especially if there is a validation error (422) or an error in deleting associated records.
*/
2025-07-15 16:33:35 -07:00
deleteMonitor = asyncHandler (
async ( req , res , next ) => {
2025-04-20 11:29:53 -07:00
await getMonitorByIdParamValidation . validateAsync ( req . params );
2025-06-13 11:49:03 +08:00
const monitorId = req . params . monitorId ;
2025-07-21 13:58:01 -07:00
const teamId = req ? . user ? . teamId ;
if ( ! teamId ) {
throw new Error ( "Team ID is required" );
}
await this . verifyTeamAccess ( teamId , monitorId );
2025-06-13 11:49:03 +08:00
const monitor = await this . db . deleteMonitor ({ monitorId });
2025-06-10 09:49:53 +08:00
await this . jobQueue . deleteJob ( monitor );
await this . db . deleteStatusPagesByMonitorId ( monitor . _id );
2025-04-20 11:29:53 -07:00
return res . success ({ msg : this . stringService . monitorDelete });
2025-07-15 16:33:35 -07:00
},
SERVICE_NAME ,
"deleteMonitor"
);
2025-04-20 11:29:53 -07:00
/**
* Deletes all monitors associated with a team.
* @async
* @param {Object} req - The Express request object.
* @property {Object} req.headers - The headers of the request.
* @property {string} req.headers.authorization - The authorization header containing the JWT token.
* @param {Object} res - The Express response object.
* @param {function} next
* @returns {Object} The response object with a success status and a message indicating the number of deleted monitors.
* @throws {Error} If there is an error during the deletion process.
*/
2025-07-15 16:33:35 -07:00
deleteAllMonitors = asyncHandler (
async ( req , res , next ) => {
2025-07-21 13:58:01 -07:00
const teamId = req ? . user ? . teamId ;
2025-04-20 11:29:53 -07:00
const { monitors , deletedCount } = await this . db . deleteAllMonitors ( teamId );
await Promise . all (
monitors . map ( async ( monitor ) => {
try {
await this . jobQueue . deleteJob ( monitor );
await this . db . deleteChecks ( monitor . _id );
await this . db . deletePageSpeedChecksByMonitorId ( monitor . _id );
await this . db . deleteNotificationsByMonitorId ( monitor . _id );
} catch ( error ) {
2025-07-15 16:33:35 -07:00
logger . warn ({
2025-04-20 11:29:53 -07:00
message : `Error deleting associated records for monitor ${ monitor . _id } with name ${ monitor . name } ` ,
service : SERVICE_NAME ,
method : "deleteAllMonitors" ,
stack : error . stack ,
});
}
})
);
return res . success ({ msg : `Deleted ${ deletedCount } monitors` });
2025-07-15 16:33:35 -07:00
},
SERVICE_NAME ,
"deleteAllMonitors"
);
2025-04-20 11:29:53 -07:00
/**
* Edits a monitor by its ID, updates its notifications, and updates its job in the job queue.
* @async
* @param {Object} req - The Express request object.
* @property {Object} req.params - The parameters of the request.
* @property {string} req.params.monitorId - The ID of the monitor to be edited.
* @property {Object} req.body - The body of the request.
* @property {Array} req.body.notifications - The notifications to be associated with the monitor.
* @param {Object} res - The Express response object.
* @param {function} next - The next middleware function.
* @returns {Object} The response object with a success status, a message indicating the editing of the monitor, and the edited monitor data.
* @throws {Error} If there is an error during the process, especially if there is a validation error (422).
*/
2025-07-15 16:33:35 -07:00
editMonitor = asyncHandler (
async ( req , res , next ) => {
2025-04-20 11:29:53 -07:00
await getMonitorByIdParamValidation . validateAsync ( req . params );
await editMonitorBodyValidation . validateAsync ( req . body );
2025-07-21 13:58:01 -07:00
const monitorId = req ? . params ? . monitorId ;
const teamId = req ? . user ? . teamId ;
if ( ! teamId ) {
throw new Error ( "Team ID is required" );
}
await this . verifyTeamAccess ( teamId , monitorId );
2025-04-20 11:29:53 -07:00
const editedMonitor = await this . db . editMonitor ( monitorId , req . body );
2025-06-08 08:41:42 +08:00
await this . jobQueue . updateJob ( editedMonitor );
2025-04-20 11:29:53 -07:00
return res . success ({
msg : this . stringService . monitorEdit ,
data : editedMonitor ,
});
2025-07-15 16:33:35 -07:00
},
SERVICE_NAME ,
"editMonitor"
);
2025-04-20 11:29:53 -07:00
/**
* Pauses or resumes a monitor based on its current state.
* @async
* @param {Object} req - The Express request object.
* @property {Object} req.params - The parameters of the request.
* @property {string} req.params.monitorId - The ID of the monitor to be paused or resumed.
* @param {Object} res - The Express response object.
* @param {function} next - The next middleware function.
* @returns {Object} The response object with a success status, a message indicating the new state of the monitor, and the updated monitor data.
* @throws {Error} If there is an error during the process.
*/
2025-07-15 16:33:35 -07:00
pauseMonitor = asyncHandler (
async ( req , res , next ) => {
2025-04-20 11:29:53 -07:00
await pauseMonitorParamValidation . validateAsync ( req . params );
2025-06-13 11:49:03 +08:00
const monitorId = req . params . monitorId ;
2025-07-21 13:58:01 -07:00
const teamId = req ? . user ? . teamId ;
if ( ! teamId ) {
throw new Error ( "Team ID is required" );
}
await this . verifyTeamAccess ( teamId , monitorId );
2025-06-13 11:49:03 +08:00
const monitor = await this . db . pauseMonitor ({ monitorId });
2025-07-21 10:34:16 -07:00
monitor . isActive === true ? await this . jobQueue . resumeJob ( monitor . _id , monitor ) : await this . jobQueue . pauseJob ( monitor );
2025-04-20 11:29:53 -07:00
return res . success ({
2025-07-21 10:34:16 -07:00
msg : monitor . isActive ? this . stringService . monitorResume : this . stringService . monitorPause ,
2025-04-20 11:29:53 -07:00
data : monitor ,
});
2025-07-15 16:33:35 -07:00
},
SERVICE_NAME ,
"pauseMonitor"
);
2025-04-20 11:29:53 -07:00
/**
* Adds demo monitors for a team.
* @async
* @param {Object} req - The Express request object.
* @property {Object} req.headers - The headers of the request.
* @property {string} req.headers.authorization - The authorization header containing the JWT token.
* @param {Object} res - The Express response object.
* @param {function} next - The next middleware function.
* @returns {Object} The response object with a success status, a message indicating the addition of demo monitors, and the number of demo monitors added.
* @throws {Error} If there is an error during the process.
*/
2025-07-15 16:33:35 -07:00
addDemoMonitors = asyncHandler (
async ( req , res , next ) => {
2025-06-13 11:49:03 +08:00
const { _id , teamId } = req . user ;
2025-04-20 11:29:53 -07:00
const demoMonitors = await this . db . addDemoMonitors ( _id , teamId );
2025-07-21 10:34:16 -07:00
await Promise . all ( demoMonitors . map (( monitor ) => this . jobQueue . addJob ( monitor . _id , monitor )));
2025-04-20 11:29:53 -07:00
return res . success ({
msg : this . stringService . monitorDemoAdded ,
data : demoMonitors . length ,
});
2025-07-15 16:33:35 -07:00
},
SERVICE_NAME ,
"addDemoMonitors"
);
2025-04-20 11:29:53 -07:00
2025-05-06 00:08:11 +05:30
/**
* Sends a test email to verify email delivery functionality.
* @async
* @param {Object} req - The Express request object.
* @property {Object} req.body - The body of the request.
* @property {string} req.body.to - The email address to send the test email to.
* @param {Object} res - The Express response object.
* @param {function} next - The next middleware function.
* @returns {Object} The response object with a success status and the email delivery message ID.
* @throws {Error} If there is an error while sending the test email.
*/
2025-07-15 16:33:35 -07:00
sendTestEmail = asyncHandler (
async ( req , res , next ) => {
2025-05-06 00:08:11 +05:30
const { to } = req . body ;
if ( ! to || typeof to !== "string" ) {
2025-05-09 14:53:53 -07:00
throw new Error ( this . stringService . errorForValidEmailAddress );
2025-05-06 00:08:11 +05:30
}
2025-05-06 00:38:38 +05:30
const subject = this . stringService . testEmailSubject ;
2025-05-06 00:08:11 +05:30
const context = { testName : "Monitoring System" };
2025-06-18 10:44:59 +08:00
const html = await this . emailService . buildEmail ( "testEmailTemplate" , context );
const messageId = await this . emailService . sendEmail ( to , subject , html );
2025-05-06 00:08:11 +05:30
2025-05-06 00:22:26 +05:30
if ( ! messageId ) {
2025-07-15 16:33:35 -07:00
throw createServerError ( "Failed to send test email." );
2025-05-06 00:22:26 +05:30
}
2025-05-06 00:08:11 +05:30
return res . success ({
2025-05-06 00:27:27 +05:30
msg : this . stringService . sendTestEmail ,
2025-05-06 00:08:11 +05:30
data : { messageId },
});
2025-07-15 16:33:35 -07:00
},
SERVICE_NAME ,
"sendTestEmail"
);
2025-05-06 00:08:11 +05:30
2025-07-15 16:33:35 -07:00
getMonitorsByTeamId = asyncHandler (
async ( req , res , next ) => {
2025-04-20 11:29:53 -07:00
await getMonitorsByTeamIdParamValidation . validateAsync ( req . params );
await getMonitorsByTeamIdQueryValidation . validateAsync ( req . query );
2025-06-13 11:49:03 +08:00
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 ,
});
2025-04-20 11:29:53 -07:00
return res . success ({
msg : this . stringService . monitorGetByTeamId ,
data : monitors ,
});
2025-07-15 16:33:35 -07:00
},
SERVICE_NAME ,
"getMonitorsByTeamId"
);
2025-04-20 11:29:53 -07:00
2025-07-15 16:33:35 -07:00
getMonitorsAndSummaryByTeamId = asyncHandler (
async ( req , res , next ) => {
2025-04-20 11:29:53 -07:00
await getMonitorsByTeamIdParamValidation . validateAsync ( req . params );
await getMonitorsByTeamIdQueryValidation . validateAsync ( req . query );
2025-06-13 11:49:03 +08:00
const { explain } = req ;
const { type } = req . query ;
const { teamId } = req . user ;
const result = await this . db . getMonitorsAndSummaryByTeamId ({
type ,
explain ,
teamId ,
});
2025-04-20 11:29:53 -07:00
return res . success ({
msg : "OK" , // TODO
data : result ,
});
2025-07-15 16:33:35 -07:00
},
SERVICE_NAME ,
"getMonitorsAndSummaryByTeamId"
);
2025-04-20 11:29:53 -07:00
2025-07-15 16:33:35 -07:00
getMonitorsWithChecksByTeamId = asyncHandler (
async ( req , res , next ) => {
2025-04-20 11:29:53 -07:00
await getMonitorsByTeamIdParamValidation . validateAsync ( req . params );
await getMonitorsByTeamIdQueryValidation . validateAsync ( req . query );
2025-06-13 11:49:03 +08:00
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 ,
});
2025-04-20 11:29:53 -07:00
return res . success ({
2025-06-18 01:07:22 +05:30
msg : "OK" ,
2025-04-20 11:29:53 -07:00
data : result ,
});
2025-07-15 16:33:35 -07:00
},
SERVICE_NAME ,
"getMonitorsWithChecksByTeamId"
);
2025-04-20 11:29:53 -07:00
2025-07-15 16:33:35 -07:00
seedDb = asyncHandler (
async ( req , res , next ) => {
2025-06-13 11:49:03 +08:00
const { _id , teamId } = req . user ;
2025-06-09 10:17:09 +08:00
await seedDb ( _id , teamId );
2025-04-20 11:29:53 -07:00
res . success ({ msg : "Database seeded" });
2025-07-15 16:33:35 -07:00
},
SERVICE_NAME ,
"seedDb"
);
2025-06-17 00:11:33 +05:30
2025-07-15 16:33:35 -07:00
exportMonitorsToCSV = asyncHandler (
async ( req , res , next ) => {
2025-06-17 00:11:33 +05:30
const { teamId } = req . user ;
const monitors = await this . db . getMonitorsByTeamId ({ teamId });
2025-06-18 01:07:22 +05:30
if ( ! monitors || monitors . length === 0 ) {
return res . success ({
msg : this . stringService . noMonitorsFound ,
data : null ,
});
}
2025-06-17 00:11:33 +05:30
const csvData = monitors ? . filteredMonitors ? . map (( monitor ) => ({
name : monitor . name ,
description : monitor . description ,
type : monitor . type ,
url : monitor . url ,
interval : monitor . interval ,
port : monitor . port ,
ignoreTlsErrors : monitor . ignoreTlsErrors ,
isActive : monitor . isActive ,
}));
const csv = pkg . unparse ( csvData );
2025-06-19 22:23:10 +05:30
return res . file ({
2025-06-18 01:07:22 +05:30
data : csv ,
headers : {
"Content-Type" : "text/csv" ,
"Content-Disposition" : "attachment; filename=monitors.csv" ,
},
});
2025-07-15 16:33:35 -07:00
},
SERVICE_NAME ,
"exportMonitorsToCSV"
);
2025-04-20 11:29:53 -07:00
}
export default MonitorController ;