2022-01-23 16:22:00 +02:00
|
|
|
const { BeanModel } = require("redbean-node/dist/bean-model");
|
2022-10-12 11:02:16 +02:00
|
|
|
const { parseTimeObject, parseTimeFromTimeObject, utcToLocal, localToUTC, log } = require("../../src/util");
|
2022-09-25 13:38:28 +02:00
|
|
|
const { timeObjectToUTC, timeObjectToLocal } = require("../util-server");
|
2022-10-10 19:45:30 +02:00
|
|
|
const { R } = require("redbean-node");
|
|
|
|
const dayjs = require("dayjs");
|
2022-01-23 16:22:00 +02:00
|
|
|
|
|
|
|
class Maintenance extends BeanModel {
|
|
|
|
|
|
|
|
/**
|
2022-04-30 15:50:05 +02:00
|
|
|
* Return an object that ready to parse to JSON for public
|
2022-01-23 16:22:00 +02:00
|
|
|
* Only show necessary data to public
|
2022-04-30 15:50:05 +02:00
|
|
|
* @returns {Object}
|
2022-01-23 16:22:00 +02:00
|
|
|
*/
|
2022-10-10 14:48:11 +02:00
|
|
|
async toPublicJSON() {
|
2022-09-24 13:18:24 +02:00
|
|
|
|
|
|
|
let dateRange = [];
|
|
|
|
if (this.start_date) {
|
2022-10-09 20:28:03 +02:00
|
|
|
dateRange.push(utcToLocal(this.start_date));
|
2022-09-24 13:18:24 +02:00
|
|
|
if (this.end_date) {
|
2022-10-09 20:28:03 +02:00
|
|
|
dateRange.push(utcToLocal(this.end_date));
|
2022-09-24 13:18:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let timeRange = [];
|
2022-10-10 14:48:11 +02:00
|
|
|
let startTime = timeObjectToLocal(parseTimeObject(this.start_time));
|
2022-09-24 13:18:24 +02:00
|
|
|
timeRange.push(startTime);
|
2022-10-10 14:48:11 +02:00
|
|
|
let endTime = timeObjectToLocal(parseTimeObject(this.end_time));
|
2022-09-24 13:18:24 +02:00
|
|
|
timeRange.push(endTime);
|
|
|
|
|
|
|
|
let obj = {
|
2022-01-23 16:22:00 +02:00
|
|
|
id: this.id,
|
|
|
|
title: this.title,
|
|
|
|
description: this.description,
|
2022-09-23 20:33:29 +02:00
|
|
|
strategy: this.strategy,
|
2022-09-24 13:18:24 +02:00
|
|
|
intervalDay: this.interval_day,
|
2022-09-23 20:33:29 +02:00
|
|
|
active: !!this.active,
|
2022-09-24 13:18:24 +02:00
|
|
|
dateRange: dateRange,
|
|
|
|
timeRange: timeRange,
|
|
|
|
weekdays: (this.weekdays) ? JSON.parse(this.weekdays) : [],
|
|
|
|
daysOfMonth: (this.days_of_month) ? JSON.parse(this.days_of_month) : [],
|
2022-10-11 12:23:17 +02:00
|
|
|
timeslotList: [],
|
2022-01-23 16:22:00 +02:00
|
|
|
};
|
2022-09-24 13:18:24 +02:00
|
|
|
|
2022-10-11 12:23:17 +02:00
|
|
|
const timeslotList = await this.getTimeslotList();
|
|
|
|
|
|
|
|
for (let timeslot of timeslotList) {
|
|
|
|
obj.timeslotList.push(await timeslot.toPublicJSON());
|
|
|
|
}
|
|
|
|
|
2022-10-12 16:13:07 +02:00
|
|
|
if (!Array.isArray(obj.weekdays)) {
|
2022-09-24 13:18:24 +02:00
|
|
|
obj.weekdays = [];
|
|
|
|
}
|
|
|
|
|
2022-10-12 16:13:07 +02:00
|
|
|
if (!Array.isArray(obj.daysOfMonth)) {
|
2022-09-24 13:18:24 +02:00
|
|
|
obj.daysOfMonth = [];
|
|
|
|
}
|
|
|
|
|
2022-10-10 19:45:30 +02:00
|
|
|
// Maintenance Status
|
|
|
|
if (!obj.active) {
|
|
|
|
obj.status = "inactive";
|
2022-10-11 12:23:17 +02:00
|
|
|
} else if (obj.strategy === "manual") {
|
|
|
|
obj.status = "under-maintenance";
|
|
|
|
} else if (obj.timeslotList.length > 0) {
|
2022-10-12 11:02:16 +02:00
|
|
|
let currentTimestamp = dayjs().unix();
|
|
|
|
|
2022-10-10 19:45:30 +02:00
|
|
|
for (let timeslot of obj.timeslotList) {
|
2022-10-12 11:02:16 +02:00
|
|
|
if (dayjs.utc(timeslot.startDate).unix() <= currentTimestamp && dayjs.utc(timeslot.endDate).unix() >= currentTimestamp) {
|
|
|
|
log.debug("timeslot", "Timeslot ID: " + timeslot.id);
|
|
|
|
log.debug("timeslot", "currentTimestamp:" + currentTimestamp);
|
|
|
|
log.debug("timeslot", "timeslot.start_date:" + dayjs.utc(timeslot.startDate).unix());
|
|
|
|
log.debug("timeslot", "timeslot.end_date:" + dayjs.utc(timeslot.endDate).unix());
|
|
|
|
|
2022-10-10 19:45:30 +02:00
|
|
|
obj.status = "under-maintenance";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!obj.status) {
|
|
|
|
obj.status = "scheduled";
|
|
|
|
}
|
|
|
|
} else if (obj.timeslotList.length === 0) {
|
|
|
|
obj.status = "ended";
|
|
|
|
} else {
|
|
|
|
obj.status = "unknown";
|
|
|
|
}
|
|
|
|
|
2022-09-24 13:18:24 +02:00
|
|
|
return obj;
|
2022-01-23 16:22:00 +02:00
|
|
|
}
|
|
|
|
|
2022-10-10 19:45:30 +02:00
|
|
|
/**
|
|
|
|
* Only get future or current timeslots only
|
|
|
|
* @returns {Promise<[]>}
|
|
|
|
*/
|
|
|
|
async getTimeslotList() {
|
2022-10-11 12:23:17 +02:00
|
|
|
return R.convertToBeans("maintenance_timeslot", await R.getAll(`
|
2022-10-10 19:45:30 +02:00
|
|
|
SELECT maintenance_timeslot.*
|
|
|
|
FROM maintenance_timeslot, maintenance
|
|
|
|
WHERE maintenance_timeslot.maintenance_id = maintenance.id
|
|
|
|
AND maintenance.id = ?
|
|
|
|
AND ${Maintenance.getActiveAndFutureMaintenanceSQLCondition()}
|
|
|
|
`, [
|
|
|
|
this.id
|
2022-10-11 12:23:17 +02:00
|
|
|
]));
|
2022-10-10 19:45:30 +02:00
|
|
|
}
|
|
|
|
|
2022-01-23 16:22:00 +02:00
|
|
|
/**
|
2022-04-30 15:50:05 +02:00
|
|
|
* Return an object that ready to parse to JSON
|
2022-09-25 13:38:28 +02:00
|
|
|
* @param {string} timezone If not specified, the timeRange will be in UTC
|
2022-04-30 15:50:05 +02:00
|
|
|
* @returns {Object}
|
2022-01-23 16:22:00 +02:00
|
|
|
*/
|
2022-09-25 13:38:28 +02:00
|
|
|
async toJSON(timezone = null) {
|
|
|
|
return this.toPublicJSON(timezone);
|
2022-01-23 16:22:00 +02:00
|
|
|
}
|
2022-09-24 13:18:24 +02:00
|
|
|
|
2023-01-06 00:19:05 +02:00
|
|
|
/**
|
|
|
|
* Get a list of weekdays that the maintenance is active for
|
|
|
|
* Monday=1, Tuesday=2 etc.
|
|
|
|
* @returns {number[]} Array of active weekdays
|
|
|
|
*/
|
2022-10-15 12:49:09 +02:00
|
|
|
getDayOfWeekList() {
|
|
|
|
log.debug("timeslot", "List: " + this.weekdays);
|
|
|
|
return JSON.parse(this.weekdays).sort(function (a, b) {
|
|
|
|
return a - b;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-01-06 00:19:05 +02:00
|
|
|
/**
|
|
|
|
* Get a list of days in month that maintenance is active for
|
|
|
|
* @returns {number[]} Array of active days in month
|
|
|
|
*/
|
2022-10-15 12:49:09 +02:00
|
|
|
getDayOfMonthList() {
|
|
|
|
return JSON.parse(this.days_of_month).sort(function (a, b) {
|
|
|
|
return a - b;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-01-06 00:19:05 +02:00
|
|
|
/**
|
|
|
|
* Get the start date and time for maintenance
|
|
|
|
* @returns {dayjs.Dayjs} Start date and time
|
|
|
|
*/
|
2022-10-15 12:49:09 +02:00
|
|
|
getStartDateTime() {
|
|
|
|
let startOfTheDay = dayjs.utc(this.start_date).format("HH:mm");
|
|
|
|
log.debug("timeslot", "startOfTheDay: " + startOfTheDay);
|
|
|
|
|
|
|
|
// Start Time
|
|
|
|
let startTimeSecond = dayjs.utc(this.start_time, "HH:mm").diff(dayjs.utc(startOfTheDay, "HH:mm"), "second");
|
|
|
|
log.debug("timeslot", "startTime: " + startTimeSecond);
|
|
|
|
|
|
|
|
// Bake StartDate + StartTime = Start DateTime
|
|
|
|
return dayjs.utc(this.start_date).add(startTimeSecond, "second");
|
|
|
|
}
|
|
|
|
|
2023-01-06 00:19:05 +02:00
|
|
|
/**
|
|
|
|
* Get the duraction of maintenance in seconds
|
|
|
|
* @returns {number} Duration of maintenance
|
|
|
|
*/
|
2022-10-15 12:49:09 +02:00
|
|
|
getDuration() {
|
|
|
|
let duration = dayjs.utc(this.end_time, "HH:mm").diff(dayjs.utc(this.start_time, "HH:mm"), "second");
|
|
|
|
// Add 24hours if it is across day
|
|
|
|
if (duration < 0) {
|
|
|
|
duration += 24 * 3600;
|
|
|
|
}
|
|
|
|
return duration;
|
|
|
|
}
|
|
|
|
|
2023-01-06 00:19:05 +02:00
|
|
|
/**
|
|
|
|
* Convert data from socket to bean
|
|
|
|
* @param {Bean} bean Bean to fill in
|
|
|
|
* @param {Object} obj Data to fill bean with
|
|
|
|
* @returns {Bean} Filled bean
|
|
|
|
*/
|
2022-10-10 14:48:11 +02:00
|
|
|
static jsonToBean(bean, obj) {
|
2022-09-24 13:18:24 +02:00
|
|
|
if (obj.id) {
|
|
|
|
bean.id = obj.id;
|
|
|
|
}
|
|
|
|
|
2022-09-25 13:38:28 +02:00
|
|
|
// Apply timezone offset to timeRange, as it cannot apply automatically.
|
2022-10-10 14:48:11 +02:00
|
|
|
if (obj.timeRange[0]) {
|
|
|
|
timeObjectToUTC(obj.timeRange[0]);
|
|
|
|
if (obj.timeRange[1]) {
|
|
|
|
timeObjectToUTC(obj.timeRange[1]);
|
2022-09-25 13:38:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-24 13:18:24 +02:00
|
|
|
bean.title = obj.title;
|
|
|
|
bean.description = obj.description;
|
|
|
|
bean.strategy = obj.strategy;
|
|
|
|
bean.interval_day = obj.intervalDay;
|
|
|
|
bean.active = obj.active;
|
|
|
|
|
|
|
|
if (obj.dateRange[0]) {
|
2022-10-09 20:28:03 +02:00
|
|
|
bean.start_date = localToUTC(obj.dateRange[0]);
|
2022-09-24 13:18:24 +02:00
|
|
|
|
|
|
|
if (obj.dateRange[1]) {
|
2022-10-09 20:28:03 +02:00
|
|
|
bean.end_date = localToUTC(obj.dateRange[1]);
|
2022-09-24 13:18:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-25 13:38:28 +02:00
|
|
|
bean.start_time = parseTimeFromTimeObject(obj.timeRange[0]);
|
|
|
|
bean.end_time = parseTimeFromTimeObject(obj.timeRange[1]);
|
2022-09-24 13:18:24 +02:00
|
|
|
|
|
|
|
bean.weekdays = JSON.stringify(obj.weekdays);
|
|
|
|
bean.days_of_month = JSON.stringify(obj.daysOfMonth);
|
|
|
|
|
|
|
|
return bean;
|
|
|
|
}
|
2022-09-27 18:20:17 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* SQL conditions for active maintenance
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
static getActiveMaintenanceSQLCondition() {
|
|
|
|
return `
|
2022-12-09 15:03:12 +02:00
|
|
|
(
|
|
|
|
(maintenance_timeslot.start_date <= DATETIME('now')
|
|
|
|
AND maintenance_timeslot.end_date >= DATETIME('now')
|
|
|
|
AND maintenance.active = 1)
|
|
|
|
OR
|
|
|
|
(maintenance.strategy = 'manual' AND active = 1)
|
|
|
|
)
|
2022-09-27 18:20:17 +02:00
|
|
|
`;
|
|
|
|
}
|
2022-10-10 19:45:30 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* SQL conditions for active and future maintenance
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
static getActiveAndFutureMaintenanceSQLCondition() {
|
|
|
|
return `
|
2022-12-09 15:03:12 +02:00
|
|
|
(
|
|
|
|
((maintenance_timeslot.end_date >= DATETIME('now')
|
|
|
|
AND maintenance.active = 1)
|
|
|
|
OR
|
|
|
|
(maintenance.strategy = 'manual' AND active = 1))
|
|
|
|
)
|
2022-10-10 19:45:30 +02:00
|
|
|
`;
|
|
|
|
}
|
2022-01-23 16:22:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Maintenance;
|