Skip to content

Commit

Permalink
Add cron jobs to delete incident and alert metrics older than 180 days
Browse files Browse the repository at this point in the history
  • Loading branch information
simlarsen committed Dec 4, 2024
1 parent 5796db0 commit 396f435
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Common/Server/Services/IncidentService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ export class Service extends DatabaseService<Model> {
isRoot: true,
},
});

// store incident metric

}

protected override async onBeforeCreate(
Expand Down
31 changes: 31 additions & 0 deletions Worker/Jobs/Metrics/DeleteAlertMetricOlderThanXDays.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import OneUptimeDate from "Common/Types/Date";
import RunCron from "../../Utils/Cron";
import { EVERY_DAY } from "Common/Utils/CronTime";
import logger from "Common/Server/Utils/Logger";
import MetricService from "Common/Server/Services/MetricService";
import QueryHelper from "Common/Server/Types/Database/QueryHelper";
import { ServiceType } from "Common/Models/AnalyticsModels/Metric";

RunCron(
"Metric:DeleteAlertMetricsOlderThanXDays",
{ schedule: EVERY_DAY, runOnStartup: true },
async () => {
const olderThanDays: number = 180; // store for 6 months.

logger.debug("Checking Metric:DeleteAlertMetricsOlderThanXDays");

logger.debug(`Deleting Alert Metrics older than ${olderThanDays} days`);

await MetricService.deleteBy({
query: {
createdAt: QueryHelper.lessThan(
OneUptimeDate.getSomeDaysAgo(olderThanDays),
),
serviceType: ServiceType.Alert,
},
props: {
isRoot: true,
},
});
},
);
31 changes: 31 additions & 0 deletions Worker/Jobs/Metrics/DeleteIncidentMetricOlderThanXDays.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import OneUptimeDate from "Common/Types/Date";
import RunCron from "../../Utils/Cron";
import { EVERY_DAY } from "Common/Utils/CronTime";
import logger from "Common/Server/Utils/Logger";
import MetricService from "Common/Server/Services/MetricService";
import QueryHelper from "Common/Server/Types/Database/QueryHelper";
import { ServiceType } from "Common/Models/AnalyticsModels/Metric";

RunCron(
"Metric:DeleteIncidentMetricsOlderThanXDays",
{ schedule: EVERY_DAY, runOnStartup: true },
async () => {
const olderThanDays: number = 180; // store for 6 months.

logger.debug("Checking Metric:DeleteIncidentMetricsOlderThanXDays");

logger.debug(`Deleting Incident Metrics older than ${olderThanDays} days`);

await MetricService.deleteBy({
query: {
createdAt: QueryHelper.lessThan(
OneUptimeDate.getSomeDaysAgo(olderThanDays),
),
serviceType: ServiceType.Incident,
},
props: {
isRoot: true,
},
});
},
);
2 changes: 2 additions & 0 deletions Worker/Routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ import "./Jobs/TelemetryMonitor/MonitorTelemetryMonitor";

//Metric Jobs.
import "./Jobs/Metrics/DeleteMonitorMetricsOlderThanXDays";
import "./Jobs/Metrics/DeleteIncidentMetricOlderThanXDays";
import "./Jobs/Metrics/DeleteAlertMetricOlderThanXDays";

import AnalyticsTableManagement from "./Utils/AnalyticsDatabase/TableManegement";
import RunDatabaseMigrations from "./Utils/DataMigration";
Expand Down

0 comments on commit 396f435

Please sign in to comment.