parse form for submission

This commit is contained in:
Alex Holliday committed 2024-10-04 12:09:08 +08:00
1 parent 5f96b17595
commit c8418705b1
3 files changed
+43 -15

No files matched your search

@@ -1,7 +1,7 @@
import { Box, Button, Stack, Typography } from "@mui/material"; import { Box, Button, Stack, Typography } from "@mui/material";
import { useSelector } from "react-redux"; import { useSelector } from "react-redux";
import { useTheme } from "@emotion/react"; import { useTheme } from "@emotion/react";
import { useEffect, useState } from "react"; import { act, useEffect, useState } from "react";
import { ConfigBox } from "./styled"; import { ConfigBox } from "./styled";
import { AdapterDayjs } from "@mui/x-date-pickers/AdapterDayjs"; import { AdapterDayjs } from "@mui/x-date-pickers/AdapterDayjs";
import { LocalizationProvider } from "@mui/x-date-pickers"; import { LocalizationProvider } from "@mui/x-date-pickers";
@@ -30,6 +30,12 @@ const MS_LOOKUP = {
days: MS_PER_DAY, days: MS_PER_DAY,
}; };
const REPEAT_LOOKUP = {
none: 0,
daily: MS_PER_DAY,
weekly: MS_PER_DAY * 7,
};
const repeatConfig = [ const repeatConfig = [
{ _id: 0, name: "Don't repeat", value: "none" }, { _id: 0, name: "Don't repeat", value: "none" },
{ {
@@ -158,8 +164,8 @@ const CreateMaintenance = () => {
return; return;
} }
// Build timestamp for maintenance window from startDate and startTime // Build timestamp for maintenance window from startDate and startTime
const startTimestamp = dayjs(form.startDate); const start = dayjs(form.startDate);
startTimestamp start
.set("hour", form.startTime.hour()) .set("hour", form.startTime.hour())
.set("minute", form.startTime.minute()) .set("minute", form.startTime.minute())
.set("second", form.startTime.second()) .set("second", form.startTime.second())
@@ -168,9 +174,24 @@ const CreateMaintenance = () => {
// Build end timestamp for maintenance window // Build end timestamp for maintenance window
const MS_MULTIPLIER = MS_LOOKUP[form.durationUnit]; const MS_MULTIPLIER = MS_LOOKUP[form.durationUnit];
const durationInMs = form.duration * MS_MULTIPLIER; const durationInMs = form.duration * MS_MULTIPLIER;
const endTimeStamp = startTimestamp.add(durationInMs); const end = start.add(durationInMs);
console.log(startTimestamp.toISOString());
console.log(endTimeStamp.toISOString()); // Get repeat value in milliseconds
const repeat = REPEAT_LOOKUP[form.repeat];
const submit = {
monitors: form.monitors.map((monitor) => monitor._id),
name: form.name,
start: start.toISOString(),
end: end.toISOString(),
repeat,
};
if (repeat === 0) {
submit.expiry = end;
}
console.log(submit);
}; };
return ( return (
+6 -5
View File
@@ -6,7 +6,7 @@ const mongoose = require("mongoose");
* @typedef {Object} MaintenanceWindow * @typedef {Object} MaintenanceWindow
* @property {mongoose.Schema.Types.ObjectId} monitorId - The ID of the monitor. This is a reference to the Monitor model and is immutable. * @property {mongoose.Schema.Types.ObjectId} monitorId - The ID of the monitor. This is a reference to the Monitor model and is immutable.
* @property {Boolean} active - Indicates whether the maintenance window is active. * @property {Boolean} active - Indicates whether the maintenance window is active.
* @property {Boolean} oneTime - Indicates whether the maintenance window is a one-time event. * @property {Number} repeat - Indicates how often this maintenance window should repeat.
* @property {Date} start - The start date and time of the maintenance window. * @property {Date} start - The start date and time of the maintenance window.
* @property {Date} end - The end date and time of the maintenance window. * @property {Date} end - The end date and time of the maintenance window.
* @property {Date} expiry - The expiry date and time of the maintenance window. This is used for MongoDB's TTL index to automatically delete the document at this time. This field is set to the same value as `end` when `oneTime` is `true`. * @property {Date} expiry - The expiry date and time of the maintenance window. This is used for MongoDB's TTL index to automatically delete the document at this time. This field is set to the same value as `end` when `oneTime` is `true`.
@@ -16,12 +16,12 @@ const mongoose = require("mongoose");
* let maintenanceWindow = new MaintenanceWindow({ * let maintenanceWindow = new MaintenanceWindow({
* monitorId: monitorId, * monitorId: monitorId,
* active: active, * active: active,
* oneTime: oneTime, * repeat: repeat,
* start: start, * start: start,
* end: end, * end: end,
* }); * });
* *
* if (oneTime) { * if (repeat === 0) {
* maintenanceWindow.expiry = end; * maintenanceWindow.expiry = end;
* } * }
* *
@@ -41,9 +41,10 @@ const MaintenanceWindow = mongoose.Schema(
}, },
active: { active: {
type: Boolean, type: Boolean,
default: true,
}, },
oneTime: { repeat: {
type: Boolean, type: Number,
}, },
start: { start: {
type: Date, type: Date,
+10 -4
View File
@@ -84,7 +84,15 @@ class JobQueue {
if (window.active) { if (window.active) {
const start = new Date(window.start); const start = new Date(window.start);
const end = new Date(window.end); const end = new Date(window.end);
if (start < new Date() && end > new Date()) { const now = new Date();
const repeatInterval = window.repeat || 0;
while (start < now) {
start.setDate(start.getTime() + repeatInterval);
end.setDate(end.getTime() + repeatInterval);
}
if (start < now && end > now) {
return true; return true;
} }
} }
@@ -139,9 +147,7 @@ class JobQueue {
return { jobs, load }; return { jobs, load };
} catch (error) { } catch (error) {
error.service === undefined ? (error.service = SERVICE_NAME) : null; error.service === undefined ? (error.service = SERVICE_NAME) : null;
error.method === undefined error.method === undefined ? (error.method = "getWorkerStats") : null;
? (error.method = "getWorkerStats")
: null;
throw error; throw error;
} }
} }