diff --git a/src/lib/clientTools.js b/src/lib/clientTools.js index 9818caa..1b65328 100644 --- a/src/lib/clientTools.js +++ b/src/lib/clientTools.js @@ -1,5 +1,6 @@ // @ts-nocheck import { base } from "$app/paths"; +import Cron from "croner"; function siteDataExtractFromDb(data, obj) { let requestedObject = { ...obj }; for (const key in requestedObject) { @@ -254,6 +255,15 @@ function ValidateCronExpression(cronExp) { return { isValid: true, message: "Valid cron expression" }; } +/** + * Get minutes interval between two cron job. + * @param {string} cronExp Cron expression. Needs to be valid first. + */ +function GetCronInterval(cronExp) { + const cronDates = new Cron(cronExp, { paused: true }).nextRuns(2); + return Math.floor((cronDates[1].getTime() - cronDates[0].getTime()) / 60000); +} + function isValidRange(value, min, max) { const num = Number(value); return !isNaN(num) && num >= min && num <= max; @@ -300,6 +310,7 @@ export { IsValidURL, IsValidPort, ValidateCronExpression, + GetCronInterval, SortMonitor, RandomString, GetGameFromId, diff --git a/src/routes/(manage)/manage/(app)/app/events/+page.svelte b/src/routes/(manage)/manage/(app)/app/events/+page.svelte index 596cfb2..6b3ca9d 100644 --- a/src/routes/(manage)/manage/(app)/app/events/+page.svelte +++ b/src/routes/(manage)/manage/(app)/app/events/+page.svelte @@ -34,7 +34,7 @@ import { mode } from "mode-watcher"; import * as Select from "$lib/components/ui/select"; - import { ValidateCronExpression } from "$lib/clientTools.js"; + import { GetCronInterval, ValidateCronExpression } from "$lib/clientTools.js"; export let data; let status = "OPEN"; let loadingData = false; @@ -172,7 +172,8 @@ id: newIncident.id, incident_type: newIncident.incident_type, maintenance_strategy: newIncident.maintenance_strategy, - cron: newIncident.cron + cron: newIncident.cron, + maintenance_duration: newIncident.maintenance_duration }; if ( @@ -197,6 +198,23 @@ invalidFormMessage = "Cron invalid: " + cronValidation.message; return; } + + // Validate maintenance duration. + if (!!!toPost.maintenance_duration) { + invalidFormMessage = "Maintenance duration is required"; + return; + } + + if (!!!toPost.maintenance_duration || isNaN(toPost.maintenance_duration) || toPost.maintenance_duration <= 0) { + invalidFormMessage = "Maintenance duration should be greater than 0"; + return; + } + const maintenance_duration = GetCronInterval(toPost.cron); + if (toPost.maintenance_duration > maintenance_duration) { + invalidFormMessage = "Maintenance duration should be smaller than next scheduled maintenance"; + return; + } + toPost.maintenance_duration = Number(toPost.maintenance_duration); } if (toPost.incident_type == "MAINTENANCE") { @@ -807,7 +825,7 @@ Maintenance Strategy - Single + Single time maintenance Reccuring interval @@ -816,11 +834,22 @@ {#if newIncident.maintenance_strategy == "RECURRING"} -
-