diff --git a/Common/Models/DatabaseModels/OnCallDutyPolicyExecutionLog.ts b/Common/Models/DatabaseModels/OnCallDutyPolicyExecutionLog.ts index 47c013e688..b9c1258248 100644 --- a/Common/Models/DatabaseModels/OnCallDutyPolicyExecutionLog.ts +++ b/Common/Models/DatabaseModels/OnCallDutyPolicyExecutionLog.ts @@ -271,7 +271,7 @@ export default class OnCallDutyPolicyExecutionLog extends BaseModel { @TableColumn({ type: TableColumnType.ObjectID, title: "Triggered By Alert ID", - required: false, + required: false, description: "ID of the incident which triggered this on-call escalation policy.", }) diff --git a/Common/Models/DatabaseModels/UserOnCallLog.ts b/Common/Models/DatabaseModels/UserOnCallLog.ts index 669ee7c650..6ea4f1f3f3 100644 --- a/Common/Models/DatabaseModels/UserOnCallLog.ts +++ b/Common/Models/DatabaseModels/UserOnCallLog.ts @@ -369,7 +369,7 @@ export default class UserOnCallLog extends BaseModel { @TableColumn({ type: TableColumnType.ObjectID, title: "Triggered By Incident ID", - required: false, + required: false, description: "ID of the incident which triggered this on-call escalation policy.", }) @@ -414,7 +414,7 @@ export default class UserOnCallLog extends BaseModel { @TableColumn({ type: TableColumnType.ObjectID, title: "Triggered By Alert ID", - required: false, + required: false, description: "ID of the Alert which triggered this on-call escalation policy.", }) diff --git a/Common/Server/Services/OnCallDutyPolicyEscalationRuleService.ts b/Common/Server/Services/OnCallDutyPolicyEscalationRuleService.ts index bde8e34322..2fba010c4d 100644 --- a/Common/Server/Services/OnCallDutyPolicyEscalationRuleService.ts +++ b/Common/Server/Services/OnCallDutyPolicyEscalationRuleService.ts @@ -188,7 +188,9 @@ export class Service extends DatabaseService { teamId: ObjectID | null, scheduleId: ObjectID | null, ): Promise => { - logger.debug(`Starting notification rule execution for userId: ${userId.toString()}`); + logger.debug( + `Starting notification rule execution for userId: ${userId.toString()}`, + ); let log: OnCallDutyPolicyExecutionLogTimeline = getNewLog(); log.statusMessage = "Sending notification to user."; log.status = OnCallDutyExecutionLogTimelineStatus.Executing; diff --git a/Common/Server/Services/OnCallDutyPolicyExecutionLogService.ts b/Common/Server/Services/OnCallDutyPolicyExecutionLogService.ts index d7d2736b12..56d11870b2 100644 --- a/Common/Server/Services/OnCallDutyPolicyExecutionLogService.ts +++ b/Common/Server/Services/OnCallDutyPolicyExecutionLogService.ts @@ -113,18 +113,17 @@ export class Service extends DatabaseService { }, }); - let userNotificationEventType: UserNotificationEventType | null = null; - if(createdItem.triggeredByIncidentId) { + if (createdItem.triggeredByIncidentId) { userNotificationEventType = UserNotificationEventType.IncidentCreated; } - if(createdItem.triggeredByAlertId) { + if (createdItem.triggeredByAlertId) { userNotificationEventType = UserNotificationEventType.AlertCreated; } - if(!userNotificationEventType) { + if (!userNotificationEventType) { throw new BadDataException("Invalid userNotificationEventType"); } diff --git a/Common/Server/Services/UserNotificationRuleService.ts b/Common/Server/Services/UserNotificationRuleService.ts index 091a2e4435..ac0a8a7c4c 100644 --- a/Common/Server/Services/UserNotificationRuleService.ts +++ b/Common/Server/Services/UserNotificationRuleService.ts @@ -185,7 +185,7 @@ export class Service extends DatabaseService { if ( options.userNotificationEventType === - UserNotificationEventType.IncidentCreated && + UserNotificationEventType.IncidentCreated && options.triggeredByIncidentId ) { incident = await IncidentService.findOneById({ @@ -214,7 +214,7 @@ export class Service extends DatabaseService { if ( options.userNotificationEventType === - UserNotificationEventType.AlertCreated && + UserNotificationEventType.AlertCreated && options.triggeredByAlertId ) { alert = await AlertService.findOneById({ @@ -252,7 +252,7 @@ export class Service extends DatabaseService { if ( options.userNotificationEventType === - UserNotificationEventType.AlertCreated && + UserNotificationEventType.AlertCreated && alert ) { // create an error log. @@ -297,7 +297,7 @@ export class Service extends DatabaseService { // send email for incident if ( options.userNotificationEventType === - UserNotificationEventType.IncidentCreated && + UserNotificationEventType.IncidentCreated && incident ) { // create an error log. @@ -365,7 +365,7 @@ export class Service extends DatabaseService { //send sms for alert if ( options.userNotificationEventType === - UserNotificationEventType.AlertCreated && + UserNotificationEventType.AlertCreated && alert ) { // create an error log. @@ -409,7 +409,7 @@ export class Service extends DatabaseService { // send sms for incident if ( options.userNotificationEventType === - UserNotificationEventType.IncidentCreated && + UserNotificationEventType.IncidentCreated && incident ) { // create an error log. @@ -476,7 +476,7 @@ export class Service extends DatabaseService { // send call for alert if ( options.userNotificationEventType === - UserNotificationEventType.AlertCreated && + UserNotificationEventType.AlertCreated && alert ) { // create an error log. @@ -520,7 +520,7 @@ export class Service extends DatabaseService { if ( options.userNotificationEventType === - UserNotificationEventType.IncidentCreated && + UserNotificationEventType.IncidentCreated && incident ) { // send call for incident @@ -701,8 +701,9 @@ export class Service extends DatabaseService { const sms: SMS = { to, - message: `This is a message from OneUptime. A new alert has been created. ${alert.title - }. To acknowledge this alert, please click on the following link ${url.toString()}`, + message: `This is a message from OneUptime. A new alert has been created. ${ + alert.title + }. To acknowledge this alert, please click on the following link ${url.toString()}`, }; return sms; @@ -729,8 +730,9 @@ export class Service extends DatabaseService { const sms: SMS = { to, - message: `This is a message from OneUptime. A new incident has been created. ${incident.title - }. To acknowledge this incident, please click on the following link ${url.toString()}`, + message: `This is a message from OneUptime. A new incident has been created. ${ + incident.title + }. To acknowledge this incident, please click on the following link ${url.toString()}`, }; return sms; @@ -909,12 +911,10 @@ export class Service extends DatabaseService { } public async addDefaultIncidentNotificationRuleForUser(data: { - projectId: ObjectID, - userId: ObjectID, - userEmail: UserEmail, - } - ): Promise { - + projectId: ObjectID; + userId: ObjectID; + userEmail: UserEmail; + }): Promise { const { projectId, userId, userEmail } = data; const incidentSeverities: Array = @@ -932,7 +932,6 @@ export class Service extends DatabaseService { }, }); - // create for incident severities. for (const incidentSeverity of incidentSeverities) { //check if this rule already exists. @@ -971,14 +970,11 @@ export class Service extends DatabaseService { } } - public async addDefaultAlertNotificationRulesForUser(data: { - projectId: ObjectID, - userId: ObjectID, - userEmail: UserEmail, - } - ): Promise { - + projectId: ObjectID; + userId: ObjectID; + userEmail: UserEmail; + }): Promise { const { projectId, userId, userEmail } = data; const alertSeverities: Array = @@ -1034,13 +1030,11 @@ export class Service extends DatabaseService { } } - public async addDefaultNotificationRuleForUser( projectId: ObjectID, userId: ObjectID, email: Email, ): Promise { - let userEmail: UserEmail | null = await UserEmailService.findOneBy({ query: { projectId, @@ -1052,7 +1046,6 @@ export class Service extends DatabaseService { }, }); - if (!userEmail) { userEmail = new UserEmail(); userEmail.projectId = projectId; @@ -1069,11 +1062,19 @@ export class Service extends DatabaseService { } // add default incident rules for user - await this.addDefaultIncidentNotificationRuleForUser({projectId, userId, userEmail}); + await this.addDefaultIncidentNotificationRuleForUser({ + projectId, + userId, + userEmail, + }); // add default alert rules for user, just like the incident - await this.addDefaultAlertNotificationRulesForUser({projectId, userId, userEmail}); + await this.addDefaultAlertNotificationRulesForUser({ + projectId, + userId, + userEmail, + }); //check if this rule already exists. const existingRuleOnCall: Model | null = await this.findOneBy({ diff --git a/Common/Server/Services/UserOnCallLogService.ts b/Common/Server/Services/UserOnCallLogService.ts index a97dd28fd8..df484ddb60 100644 --- a/Common/Server/Services/UserOnCallLogService.ts +++ b/Common/Server/Services/UserOnCallLogService.ts @@ -283,7 +283,8 @@ export class Service extends DatabaseService { NotificationRuleType.ON_CALL_EXECUTED; if ( - userNotificationEventType === UserNotificationEventType.IncidentCreated || userNotificationEventType === UserNotificationEventType.AlertCreated + userNotificationEventType === UserNotificationEventType.IncidentCreated || + userNotificationEventType === UserNotificationEventType.AlertCreated ) { notificationRuleType = NotificationRuleType.ON_CALL_EXECUTED; } else { diff --git a/Common/Types/NotificationRule/NotificationRuleType.ts b/Common/Types/NotificationRule/NotificationRuleType.ts index cd87f55683..8b0dea14a0 100644 --- a/Common/Types/NotificationRule/NotificationRuleType.ts +++ b/Common/Types/NotificationRule/NotificationRuleType.ts @@ -1,5 +1,5 @@ enum NotificationRuleType { - ON_CALL_EXECUTED = "When incident is created during on call", + ON_CALL_EXECUTED = "When on-call policy is executed", WHEN_USER_GOES_ON_CALL = "When user goes on call", WHEN_USER_GOES_OFF_CALL = "When user goes off call", } diff --git a/Dashboard/src/Components/OnCallPolicy/ExecutionLogs/ExecutionLogsTable.tsx b/Dashboard/src/Components/OnCallPolicy/ExecutionLogs/ExecutionLogsTable.tsx index 0237f93c1f..a121817917 100644 --- a/Dashboard/src/Components/OnCallPolicy/ExecutionLogs/ExecutionLogsTable.tsx +++ b/Dashboard/src/Components/OnCallPolicy/ExecutionLogs/ExecutionLogsTable.tsx @@ -122,18 +122,20 @@ const ExecutionLogsTable: FunctionComponent = ( return (

Incident:

- +
); } - if(item.triggeredByAlert){ + if (item.triggeredByAlert) { return (

Alert:

- -
- ) + + + ); } return

-

; diff --git a/Probe/Jobs/Alive.ts b/Probe/Jobs/Alive.ts index ab7271388a..832372d8a7 100644 --- a/Probe/Jobs/Alive.ts +++ b/Probe/Jobs/Alive.ts @@ -17,7 +17,6 @@ BasicCron({ runOnStartup: false, }, runFunction: async () => { - logger.debug("Checking if probe is alive..."); const probeId: string | undefined = LocalCache.getString( @@ -40,12 +39,10 @@ BasicCron({ ProbeAPIRequest.getDefaultRequestBody(), ); - if(result.isSuccess()){ + if (result.isSuccess()) { logger.debug("Probe update sent to server successfully."); - }else{ + } else { logger.error("Failed to send probe update to server."); } - - }, }); diff --git a/ProbeIngest/API/Probe.ts b/ProbeIngest/API/Probe.ts index 9374f16634..a5f20e38ec 100644 --- a/ProbeIngest/API/Probe.ts +++ b/ProbeIngest/API/Probe.ts @@ -29,33 +29,28 @@ import OneUptimeDate from "Common/Types/Date"; const router: ExpressRouter = Express.getRouter(); - router.post( "/alive", ProbeAuthorization.isAuthorizedServiceMiddleware, - async ( - req: ExpressRequest, - res: ExpressResponse, -): Promise => { + async (req: ExpressRequest, res: ExpressResponse): Promise => { + // Update last alive in probe and return success response. - // Update last alive in probe and return success response. + const data: JSONObject = req.body; - const data: JSONObject = req.body; + const probeId: ObjectID = new ObjectID(data["probeId"] as string); - const probeId: ObjectID = new ObjectID(data["probeId"] as string); + await ProbeService.updateOneById({ + id: probeId, + data: { + lastAlive: OneUptimeDate.getCurrentDate(), + }, + props: { + isRoot: true, + }, + }); - await ProbeService.updateOneById({ - id: probeId, - data: { - lastAlive: OneUptimeDate.getCurrentDate(), - }, - props: { - isRoot: true, - }, - }); - - return Response.sendEmptySuccessResponse(req, res); -} + return Response.sendEmptySuccessResponse(req, res); + }, ); router.post( diff --git a/Worker/DataMigrations/Index.ts b/Worker/DataMigrations/Index.ts index 420e52fe31..04d84149f4 100644 --- a/Worker/DataMigrations/Index.ts +++ b/Worker/DataMigrations/Index.ts @@ -40,6 +40,7 @@ import RefreshDefaultUserNotificationSetting from "./RefreshUserNotificationSett import AddServiceTypeColumnToMetricsTable from "./AddServiceTypeColumnToMetricTable"; import AddIsSubscriptionConfirmedToSubscribers from "./AddIsSubscriptionConfirmedToSubscribers"; import AddIncidentNumber from "./AddIncidentNumber"; +import RenameRuleTypeInUserNotificationRule from "./RenameRuleTypeInUserNotificationRule"; // This is the order in which the migrations will be run. Add new migrations to the end of the array. @@ -85,6 +86,7 @@ const DataMigrations: Array = [ new AddServiceTypeColumnToMetricsTable(), new AddIsSubscriptionConfirmedToSubscribers(), new AddIncidentNumber(), + new RenameRuleTypeInUserNotificationRule(), ]; export default DataMigrations; diff --git a/Worker/DataMigrations/RenameRuleTypeInUserNotificationRule.ts b/Worker/DataMigrations/RenameRuleTypeInUserNotificationRule.ts new file mode 100644 index 0000000000..cf45176ad2 --- /dev/null +++ b/Worker/DataMigrations/RenameRuleTypeInUserNotificationRule.ts @@ -0,0 +1,65 @@ +import Project from "Common/Models/DatabaseModels/Project"; +import DataMigrationBase from "./DataMigrationBase"; +import LIMIT_MAX from "Common/Types/Database/LimitMax"; +import UserNotificationRule from "Common/Models/DatabaseModels/UserNotificationRule"; +import UserNotificationRuleService from "Common/Server/Services/UserNotificationRuleService"; +import ProjectService from "Common/Server/Services/ProjectService"; + +export default class RenameRuleTypeInUserNotificationRule extends DataMigrationBase { + public constructor() { + super("RenameRuleTypeInUserNotificationRule"); + } + + public override async migrate(): Promise { + // get all projects. + // for each project get all UserNotifiacationRules with ruleType ""When incident is created during on call" + // update ruleType to "When on-call policy is executed" + // update UserNotificationRule + + const projects: Array = await ProjectService.findBy({ + query: {}, + select: { + _id: true, + }, + skip: 0, + limit: LIMIT_MAX, + props: { + isRoot: true, + }, + }); + + for (const project of projects) { + const userNotificationRules: Array = + await UserNotificationRuleService.findBy({ + query: { + projectId: project._id, + ruleType: "When incident is created during on call" as any, + }, + select: { + _id: true, + }, + skip: 0, + limit: LIMIT_MAX, + props: { + isRoot: true, + }, + }); + + for (const userNotificationRule of userNotificationRules) { + await UserNotificationRuleService.updateOneById({ + id: userNotificationRule.id!, + data: { + ruleType: "When on-call policy is executed" as any, + }, + props: { + isRoot: true, + }, + }); + } + } + } + + public override async rollback(): Promise { + return; + } +} diff --git a/Worker/Jobs/AlertOwners/SendStateChangeNotification.ts b/Worker/Jobs/AlertOwners/SendStateChangeNotification.ts index 416d963bc0..def1ac998c 100644 --- a/Worker/Jobs/AlertOwners/SendStateChangeNotification.ts +++ b/Worker/Jobs/AlertOwners/SendStateChangeNotification.ts @@ -70,7 +70,7 @@ RunCron( select: { _id: true, title: true, - projectId: true, + projectId: true, description: true, monitor: { name: true, diff --git a/Worker/Jobs/OnCallDutyPolicyExecutionLog/ExecutePendingExecutions.ts b/Worker/Jobs/OnCallDutyPolicyExecutionLog/ExecutePendingExecutions.ts index a9e229b1ff..19ffb54792 100644 --- a/Worker/Jobs/OnCallDutyPolicyExecutionLog/ExecutePendingExecutions.ts +++ b/Worker/Jobs/OnCallDutyPolicyExecutionLog/ExecutePendingExecutions.ts @@ -67,27 +67,18 @@ const executeOnCallPolicy: ExecuteOnCallPolicyFunction = async ( executionLog: OnCallDutyPolicyExecutionLog, ): Promise => { try { - - - logger.debug( - `Executing on-call policy execution log: ${executionLog.id}`, - ) + logger.debug(`Executing on-call policy execution log: ${executionLog.id}`); // get trigger by alert if (executionLog.triggeredByAlertId) { - - logger.debug( - `Triggered by alert: ${executionLog.triggeredByAlertId}`, - ) + logger.debug(`Triggered by alert: ${executionLog.triggeredByAlertId}`); // check if this alert is ack. const isAcknowledged: boolean = await AlertService.isAlertAcknowledged({ alertId: executionLog.triggeredByAlertId, }); - logger.debug( - `Alert is acknowledged: ${isAcknowledged}`, - ) + logger.debug(`Alert is acknowledged: ${isAcknowledged}`); if (isAcknowledged) { // then mark this policy as executed. @@ -107,10 +98,9 @@ const executeOnCallPolicy: ExecuteOnCallPolicyFunction = async ( // get trigger by incident if (executionLog.triggeredByIncidentId) { - logger.debug( `Triggered by incident: ${executionLog.triggeredByIncidentId}`, - ) + ); // check if this incident is ack. const isAcknowledged: boolean = @@ -118,9 +108,7 @@ const executeOnCallPolicy: ExecuteOnCallPolicyFunction = async ( incidentId: executionLog.triggeredByIncidentId, }); - logger.debug( - `Incident is acknowledged: ${isAcknowledged}`, - ) + logger.debug(`Incident is acknowledged: ${isAcknowledged}`); if (isAcknowledged) { // then mark this policy as executed. @@ -189,8 +177,8 @@ const executeOnCallPolicy: ExecuteOnCallPolicyFunction = async ( if ( executionLog.onCallPolicyExecutionRepeatCount && executionLog.onCallPolicyExecutionRepeatCount < - executionLog.onCallDutyPolicy! - .repeatPolicyIfNoOneAcknowledgesNoOfTimes! + executionLog.onCallDutyPolicy! + .repeatPolicyIfNoOneAcknowledgesNoOfTimes! ) { // repeating execution