diff --git a/Common/Server/Services/IncidentService.ts b/Common/Server/Services/IncidentService.ts index 23e984ec39..bcf04b451d 100644 --- a/Common/Server/Services/IncidentService.ts +++ b/Common/Server/Services/IncidentService.ts @@ -139,6 +139,9 @@ export class Service extends DatabaseService { isRoot: true, }, }); + + // store incident metric + } protected override async onBeforeCreate( diff --git a/Worker/Jobs/Metrics/DeleteAlertMetricOlderThanXDays.ts b/Worker/Jobs/Metrics/DeleteAlertMetricOlderThanXDays.ts new file mode 100644 index 0000000000..8fe1a9d1a9 --- /dev/null +++ b/Worker/Jobs/Metrics/DeleteAlertMetricOlderThanXDays.ts @@ -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, + }, + }); + }, +); diff --git a/Worker/Jobs/Metrics/DeleteIncidentMetricOlderThanXDays.ts b/Worker/Jobs/Metrics/DeleteIncidentMetricOlderThanXDays.ts new file mode 100644 index 0000000000..54f44136ce --- /dev/null +++ b/Worker/Jobs/Metrics/DeleteIncidentMetricOlderThanXDays.ts @@ -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, + }, + }); + }, +); diff --git a/Worker/Routes.ts b/Worker/Routes.ts index 1e7206202d..a3418beb7e 100644 --- a/Worker/Routes.ts +++ b/Worker/Routes.ts @@ -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";