-
Notifications
You must be signed in to change notification settings - Fork 232
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1688 from OneUptime/sm-templates
Scheduled Maintenance Templates
- Loading branch information
Showing
62 changed files
with
4,635 additions
and
437 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file removed
BIN
-21.2 KB
App/FeatureSet/Home/Static/fonts/camphor-ss/400-regular-italic.woff
Binary file not shown.
Binary file removed
BIN
-15.7 KB
App/FeatureSet/Home/Static/fonts/camphor-ss/400-regular-italic.woff2
Binary file not shown.
Binary file not shown.
Binary file removed
BIN
-15.3 KB
App/FeatureSet/Home/Static/fonts/camphor-ss/500-medium-italic.woff2
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed
BIN
-63.2 KB
App/FeatureSet/Home/Static/fonts/camphor/4b0f2143-ed99-4a36-8778-557047d0a0a3.woff2
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
214 changes: 214 additions & 0 deletions
214
App/FeatureSet/Workers/Jobs/ScheduledMaintenance/ScheduleRecurringEvents.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,214 @@ | ||
import RunCron from "../../Utils/Cron"; | ||
import LIMIT_MAX from "Common/Types/Database/LimitMax"; | ||
import OneUptimeDate from "Common/Types/Date"; | ||
import { EVERY_MINUTE } from "Common/Utils/CronTime"; | ||
import ScheduledMaintenanceService from "Common/Server/Services/ScheduledMaintenanceService"; | ||
import QueryHelper from "Common/Server/Types/Database/QueryHelper"; | ||
import ScheduledMaintenanceTemplate from "Common/Models/DatabaseModels/ScheduledMaintenanceTemplate"; | ||
import ScheduledMaintenanceTemplateService from "Common/Server/Services/ScheduledMaintenanceTemplateService"; | ||
import ScheduledMaintenanceTemplateOwnerUserService from "Common/Server/Services/ScheduledMaintenanceTemplateOwnerUserService"; | ||
import ScheduledMaintenanceOwnerUser from "Common/Models/DatabaseModels/ScheduledMaintenanceOwnerUser"; | ||
import ScheduledMaintenanceTemplateOwnerUser from "Common/Models/DatabaseModels/ScheduledMaintenanceTemplateOwnerUser"; | ||
import ScheduledMaintenanceOwnerTeamService from "Common/Server/Services/ScheduledMaintenanceOwnerTeamService"; | ||
import ScheduledMaintenanceTemplateOwnerTeamService from "Common/Server/Services/ScheduledMaintenanceTemplateOwnerTeamService"; | ||
import ScheduledMaintenance from "Common/Models/DatabaseModels/ScheduledMaintenance"; | ||
import ScheduledMaintenanceOwnerUserService from "Common/Server/Services/ScheduledMaintenanceOwnerUserService"; | ||
import ScheduledMaintenanceOwnerTeam from "Common/Models/DatabaseModels/ScheduledMaintenanceOwnerTeam"; | ||
import logger from "Common/Server/Utils/Logger"; | ||
import Recurring from "Common/Types/Events/Recurring"; | ||
|
||
RunCron( | ||
"ScheduledMaintenance:ScheduleRecurringEvents", | ||
{ schedule: EVERY_MINUTE, runOnStartup: false }, | ||
async () => { | ||
// get all scheduled events of all the projects. | ||
const recurringTemplates: Array<ScheduledMaintenanceTemplate> = | ||
await ScheduledMaintenanceTemplateService.findBy({ | ||
query: { | ||
isRecurringEvent: true, | ||
scheduleNextEventAt: QueryHelper.lessThanEqualTo( | ||
OneUptimeDate.getCurrentDate(), | ||
), | ||
}, | ||
props: { | ||
isRoot: true, | ||
}, | ||
limit: LIMIT_MAX, | ||
skip: 0, | ||
select: { | ||
_id: true, | ||
projectId: true, | ||
changeMonitorStatusToId: true, | ||
shouldStatusPageSubscribersBeNotifiedWhenEventChangedToEnded: true, | ||
shouldStatusPageSubscribersBeNotifiedOnEventCreated: true, | ||
shouldStatusPageSubscribersBeNotifiedWhenEventChangedToOngoing: true, | ||
monitors: true, | ||
statusPages: true, | ||
scheduleNextEventAt: true, | ||
firstEventStartsAt: true, | ||
firstEventEndsAt: true, | ||
firstEventScheduledAt: true, | ||
title: true, | ||
description: true, | ||
labels: true, | ||
isRecurringEvent: true, | ||
recurringInterval: true, | ||
}, | ||
}); | ||
|
||
// change their state to Ongoing. | ||
|
||
for (const recurringTemplate of recurringTemplates) { | ||
try { | ||
if (recurringTemplate.recurringInterval === undefined) { | ||
continue; | ||
} | ||
|
||
// update the next scheduled time for this event. | ||
const recurringInterval: Recurring = | ||
recurringTemplate.recurringInterval!; | ||
const nextScheduledTime: Date = | ||
ScheduledMaintenanceTemplateService.getNextEventTime({ | ||
dateAndTime: recurringTemplate.scheduleNextEventAt!, | ||
recurringInterval, | ||
}); | ||
|
||
await ScheduledMaintenanceTemplateService.updateOneById({ | ||
id: recurringTemplate.id!, | ||
data: { | ||
scheduleNextEventAt: nextScheduledTime, | ||
}, | ||
props: { | ||
isRoot: true, | ||
}, | ||
}); | ||
|
||
// get owner users for this template. | ||
const ownerUsers: Array<ScheduledMaintenanceTemplateOwnerUser> = | ||
await ScheduledMaintenanceTemplateOwnerUserService.findBy({ | ||
query: { | ||
scheduledMaintenanceTemplateId: recurringTemplate.id!, | ||
}, | ||
props: { | ||
isRoot: true, | ||
}, | ||
limit: LIMIT_MAX, | ||
skip: 0, | ||
select: { | ||
userId: true, | ||
}, | ||
}); | ||
|
||
// owner teams. | ||
const ownerTeams: Array<ScheduledMaintenanceOwnerTeam> = | ||
await ScheduledMaintenanceTemplateOwnerTeamService.findBy({ | ||
query: { | ||
scheduledMaintenanceTemplateId: recurringTemplate.id!, | ||
}, | ||
props: { | ||
isRoot: true, | ||
}, | ||
limit: LIMIT_MAX, | ||
skip: 0, | ||
select: { | ||
teamId: true, | ||
}, | ||
}); | ||
|
||
// now create a new scheduled maintenance event for this template. | ||
let scheduledMaintenanceEvent: ScheduledMaintenance = | ||
new ScheduledMaintenance(); | ||
scheduledMaintenanceEvent.projectId = recurringTemplate.projectId!; | ||
scheduledMaintenanceEvent.changeMonitorStatusToId = | ||
recurringTemplate.changeMonitorStatusToId!; | ||
scheduledMaintenanceEvent.shouldStatusPageSubscribersBeNotifiedWhenEventChangedToEnded = | ||
recurringTemplate.shouldStatusPageSubscribersBeNotifiedWhenEventChangedToEnded!; | ||
scheduledMaintenanceEvent.shouldStatusPageSubscribersBeNotifiedOnEventCreated = | ||
recurringTemplate.shouldStatusPageSubscribersBeNotifiedOnEventCreated!; | ||
scheduledMaintenanceEvent.shouldStatusPageSubscribersBeNotifiedWhenEventChangedToOngoing = | ||
recurringTemplate.shouldStatusPageSubscribersBeNotifiedWhenEventChangedToOngoing!; | ||
scheduledMaintenanceEvent.monitors = recurringTemplate.monitors!; | ||
scheduledMaintenanceEvent.statusPages = recurringTemplate.statusPages!; | ||
scheduledMaintenanceEvent.title = recurringTemplate.title!; | ||
scheduledMaintenanceEvent.description = recurringTemplate.description!; | ||
scheduledMaintenanceEvent.labels = recurringTemplate.labels!; | ||
|
||
const eventscheduledTime: Date = recurringTemplate.scheduleNextEventAt!; | ||
|
||
const firstScheduledTime: Date = | ||
recurringTemplate.firstEventScheduledAt!; | ||
const firstStartTime: Date = recurringTemplate.firstEventStartsAt!; | ||
const firstEndTime: Date = recurringTemplate.firstEventEndsAt!; | ||
|
||
const minutesBetwenScheduledAndStartTime: number = | ||
OneUptimeDate.getMinutesBetweenTwoDates( | ||
eventscheduledTime, | ||
firstStartTime, | ||
); | ||
const minutesBetweenScheduledAndEndTime: number = | ||
OneUptimeDate.getMinutesBetweenTwoDates( | ||
eventscheduledTime, | ||
firstEndTime, | ||
); | ||
|
||
// set the scheduled time for this event. | ||
scheduledMaintenanceEvent.createdAt = eventscheduledTime!; | ||
scheduledMaintenanceEvent.startsAt = OneUptimeDate.addRemoveMinutes( | ||
firstScheduledTime, | ||
minutesBetwenScheduledAndStartTime, | ||
); | ||
scheduledMaintenanceEvent.endsAt = OneUptimeDate.addRemoveMinutes( | ||
firstScheduledTime, | ||
minutesBetweenScheduledAndEndTime, | ||
); | ||
|
||
// now create this event. | ||
|
||
scheduledMaintenanceEvent = await ScheduledMaintenanceService.create({ | ||
data: scheduledMaintenanceEvent, | ||
props: { | ||
isRoot: true, | ||
}, | ||
}); | ||
|
||
// now add owners and teams to this event. | ||
|
||
for (const ownerUser of ownerUsers) { | ||
const scheduledMaintenanceOwnerUser: ScheduledMaintenanceOwnerUser = | ||
new ScheduledMaintenanceOwnerUser(); | ||
scheduledMaintenanceOwnerUser.scheduledMaintenanceId = | ||
scheduledMaintenanceEvent.id!; | ||
scheduledMaintenanceOwnerUser.projectId = | ||
scheduledMaintenanceEvent.projectId!; | ||
scheduledMaintenanceOwnerUser.userId = ownerUser.userId!; | ||
await ScheduledMaintenanceOwnerUserService.create({ | ||
data: scheduledMaintenanceOwnerUser, | ||
props: { | ||
isRoot: true, | ||
}, | ||
}); | ||
} | ||
|
||
// now do the same for owner teams. | ||
|
||
for (const ownerTeam of ownerTeams) { | ||
const scheduledMaintenanceOwnerTeam: ScheduledMaintenanceOwnerTeam = | ||
new ScheduledMaintenanceOwnerTeam(); | ||
scheduledMaintenanceOwnerTeam.scheduledMaintenanceId = | ||
scheduledMaintenanceEvent.id!; | ||
scheduledMaintenanceOwnerTeam.projectId = | ||
scheduledMaintenanceEvent.projectId!; | ||
scheduledMaintenanceOwnerTeam.teamId = ownerTeam.teamId!; | ||
await ScheduledMaintenanceOwnerTeamService.create({ | ||
data: scheduledMaintenanceOwnerTeam, | ||
props: { | ||
isRoot: true, | ||
}, | ||
}); | ||
} | ||
} catch (e) { | ||
logger.error(e); | ||
} | ||
} | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.