mirror of
https://github.com/OneUptime/oneuptime.git
synced 2026-04-06 00:32:12 +02:00
chore: Add new data migration to include missing end dates in MonitorStatusTimeline
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
import DataMigrationBase from './DataMigrationBase';
|
||||
import LIMIT_MAX from 'Common/Types/Database/LimitMax';
|
||||
import Project from 'Model/Models/Project';
|
||||
import ProjectService from 'CommonServer/Services/ProjectService';
|
||||
import Monitor from 'Model/Models/Monitor';
|
||||
import MonitorService from 'CommonServer/Services/MonitorService';
|
||||
import MonitorStatusTimeline from 'Model/Models/MonitorStatusTimeline';
|
||||
import MonitorStatusTimelineService from 'CommonServer/Services/MonitorStatusTimelineService';
|
||||
import SortOrder from 'Common/Types/BaseDatabase/SortOrder';
|
||||
|
||||
export default class AddEndDateToMonitorStatusTimelineWhereEndDateIsMissing extends DataMigrationBase {
|
||||
public constructor() {
|
||||
super('AddEndDateToMonitorStatusTimelineWhereEndDateIsMissing');
|
||||
}
|
||||
|
||||
public override async migrate(): Promise<void> {
|
||||
// get all the users with email isVerified true.
|
||||
|
||||
const projects: Array<Project> = await ProjectService.findBy({
|
||||
query: {},
|
||||
select: {
|
||||
_id: true,
|
||||
},
|
||||
skip: 0,
|
||||
limit: LIMIT_MAX,
|
||||
props: {
|
||||
isRoot: true,
|
||||
},
|
||||
});
|
||||
|
||||
for (const project of projects) {
|
||||
// add ended scheduled maintenance state for each of these projects.
|
||||
// first fetch resolved state. Ended state order is -1 of resolved state.
|
||||
|
||||
const monitors: Array<Monitor> = await MonitorService.findBy({
|
||||
query: {
|
||||
projectId: project.id!,
|
||||
},
|
||||
select: {
|
||||
_id: true,
|
||||
},
|
||||
skip: 0,
|
||||
limit: LIMIT_MAX,
|
||||
props: {
|
||||
isRoot: true,
|
||||
},
|
||||
});
|
||||
|
||||
for (const monitor of monitors) {
|
||||
const statusTimelines: Array<MonitorStatusTimeline> =
|
||||
await MonitorStatusTimelineService.findBy({
|
||||
query: {
|
||||
monitorId: monitor.id!,
|
||||
},
|
||||
select: {
|
||||
_id: true,
|
||||
createdAt: true,
|
||||
},
|
||||
skip: 0,
|
||||
limit: LIMIT_MAX,
|
||||
props: {
|
||||
isRoot: true,
|
||||
},
|
||||
sort: {
|
||||
createdAt: SortOrder.Descending,
|
||||
},
|
||||
});
|
||||
|
||||
// reverse the status timelines
|
||||
statusTimelines.reverse();
|
||||
|
||||
for (let i: number = 0; i < statusTimelines.length; i++) {
|
||||
const statusTimeline: MonitorStatusTimeline | undefined =
|
||||
statusTimelines[i];
|
||||
|
||||
if (!statusTimeline) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (statusTimeline.endsAt) {
|
||||
continue;
|
||||
}
|
||||
|
||||
let endDate: Date | null = statusTimeline.endsAt || null;
|
||||
|
||||
if (
|
||||
!endDate &&
|
||||
statusTimelines[i + 1] &&
|
||||
statusTimelines[i + 1]?.createdAt
|
||||
) {
|
||||
endDate = statusTimelines[i + 1]!.createdAt!;
|
||||
}
|
||||
|
||||
if (endDate) {
|
||||
await MonitorStatusTimelineService.updateOneById({
|
||||
id: statusTimeline!.id!,
|
||||
data: {
|
||||
endsAt: endDate,
|
||||
},
|
||||
props: {
|
||||
isRoot: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override async rollback(): Promise<void> {
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -24,6 +24,7 @@ import UpdateGlobalConfigFromEnv from './UpdateGlobalCongfigFromEnv';
|
||||
import AddTelemetryServiceColor from './AddTelemetryServiceColor';
|
||||
import MoveGreenlockCertsToAcmeCerts from './MoveGreenlockCertsToAcmeCerts';
|
||||
import GenerateNewCertsForStatusPage from './GenerateNewCertsForStatusPage';
|
||||
import AddEndDateToMonitorStatusTimelineWhereEndDateIsMissing from './AddEndDateToMonitorStatusTimelineWhereEndDateIsMissing';
|
||||
|
||||
// This is the order in which the migrations will be run. Add new migrations to the end of the array.
|
||||
|
||||
@@ -53,6 +54,7 @@ const DataMigrations: Array<DataMigrationBase> = [
|
||||
new AddTelemetryServiceColor(),
|
||||
new MoveGreenlockCertsToAcmeCerts(),
|
||||
new GenerateNewCertsForStatusPage(),
|
||||
new AddEndDateToMonitorStatusTimelineWhereEndDateIsMissing(),
|
||||
];
|
||||
|
||||
export default DataMigrations;
|
||||
|
||||
Reference in New Issue
Block a user