Handling DATE_ADD

pull/3529/head^2
Louis Lam 2 years ago
parent 5388a37a26
commit 2e2747fb52

@ -31,6 +31,7 @@ async function createTables() {
table.boolean("public").notNullable().defaultTo(false); table.boolean("public").notNullable().defaultTo(false);
table.boolean("active").notNullable().defaultTo(true); table.boolean("active").notNullable().defaultTo(true);
table.integer("weight").notNullable().defaultTo(1000); table.integer("weight").notNullable().defaultTo(1000);
table.integer("status_page_id").unsigned();
}); });
// proxy // proxy

@ -683,6 +683,15 @@ class Database {
static async shrink() { static async shrink() {
await R.exec("VACUUM"); await R.exec("VACUUM");
} }
static sqlHourOffset() {
if (this.dbConfig.client === "sqlite3") {
return "DATETIME('now', ? || ' hours')";
} else {
return "DATE_ADD(NOW(), INTERVAL ? HOUR)";
}
}
} }
module.exports = Database; module.exports = Database;

@ -1,6 +1,7 @@
const { log, exit, connectDb } = require("./util-worker"); const { log, exit, connectDb } = require("./util-worker");
const { R } = require("redbean-node"); const { R } = require("redbean-node");
const { setSetting, setting } = require("../util-server"); const { setSetting, setting } = require("../util-server");
const Database = require("../database");
const DEFAULT_KEEP_PERIOD = 180; const DEFAULT_KEEP_PERIOD = 180;
@ -31,10 +32,12 @@ const DEFAULT_KEEP_PERIOD = 180;
log(`Clearing Data older than ${parsedPeriod} days...`); log(`Clearing Data older than ${parsedPeriod} days...`);
const sqlHourOffset = Database.sqlHourOffset();
try { try {
await R.exec( await R.exec(
"DELETE FROM heartbeat WHERE time < DATETIME('now', '-' || ? || ' days') ", "DELETE FROM heartbeat WHERE time < " + sqlHourOffset,
[ parsedPeriod ] [ parsedPeriod * -24 ]
); );
} catch (e) { } catch (e) {
log(`Failed to clear old data: ${e.message}`); log(`Failed to clear old data: ${e.message}`);

@ -19,6 +19,7 @@ const { DockerHost } = require("../docker");
const Maintenance = require("./maintenance"); const Maintenance = require("./maintenance");
const { UptimeCacheList } = require("../uptime-cache-list"); const { UptimeCacheList } = require("../uptime-cache-list");
const Gamedig = require("gamedig"); const Gamedig = require("gamedig");
const Database = require("../database");
/** /**
* status: * status:
@ -935,11 +936,12 @@ class Monitor extends BeanModel {
*/ */
static async sendAvgPing(duration, io, monitorID, userID) { static async sendAvgPing(duration, io, monitorID, userID) {
const timeLogger = new TimeLogger(); const timeLogger = new TimeLogger();
const sqlHourOffset = Database.sqlHourOffset();
let avgPing = parseInt(await R.getCell(` let avgPing = parseInt(await R.getCell(`
SELECT AVG(ping) SELECT AVG(ping)
FROM heartbeat FROM heartbeat
WHERE time > DATETIME('now', ? || ' hours') WHERE time > ${sqlHourOffset}
AND ping IS NOT NULL AND ping IS NOT NULL
AND monitor_id = ? `, [ AND monitor_id = ? `, [
-duration, -duration,

@ -9,6 +9,7 @@ const StatusPage = require("../model/status_page");
const { UptimeKumaServer } = require("../uptime-kuma-server"); const { UptimeKumaServer } = require("../uptime-kuma-server");
const { makeBadge } = require("badge-maker"); const { makeBadge } = require("badge-maker");
const { badgeConstants } = require("../config"); const { badgeConstants } = require("../config");
const Database = require("../database");
let router = express.Router(); let router = express.Router();
@ -268,10 +269,12 @@ router.get("/api/badge/:id/ping/:duration?", cache("5 minutes"), async (request,
const requestedDuration = Math.min(request.params.duration ? parseInt(request.params.duration, 10) : 24, 720); const requestedDuration = Math.min(request.params.duration ? parseInt(request.params.duration, 10) : 24, 720);
const overrideValue = value && parseFloat(value); const overrideValue = value && parseFloat(value);
const sqlHourOffset = Database.sqlHourOffset();
const publicAvgPing = parseInt(await R.getCell(` const publicAvgPing = parseInt(await R.getCell(`
SELECT AVG(ping) FROM monitor_group, \`group\`, heartbeat SELECT AVG(ping) FROM monitor_group, \`group\`, heartbeat
WHERE monitor_group.group_id = \`group\`.id WHERE monitor_group.group_id = \`group\`.id
AND heartbeat.time > DATETIME('now', ? || ' hours') AND heartbeat.time > ${sqlHourOffset}
AND heartbeat.ping IS NOT NULL AND heartbeat.ping IS NOT NULL
AND public = 1 AND public = 1
AND heartbeat.monitor_id = ? AND heartbeat.monitor_id = ?
@ -334,10 +337,12 @@ router.get("/api/badge/:id/avg-response/:duration?", cache("5 minutes"), async (
); );
const overrideValue = value && parseFloat(value); const overrideValue = value && parseFloat(value);
const sqlHourOffset = Database.sqlHourOffset();
const publicAvgPing = parseInt(await R.getCell(` const publicAvgPing = parseInt(await R.getCell(`
SELECT AVG(ping) FROM monitor_group, \`group\`, heartbeat SELECT AVG(ping) FROM monitor_group, \`group\`, heartbeat
WHERE monitor_group.group_id = \`group\`.id WHERE monitor_group.group_id = \`group\`.id
AND heartbeat.time > DATETIME('now', ? || ' hours') AND heartbeat.time > ${sqlHourOffset}
AND heartbeat.ping IS NOT NULL AND heartbeat.ping IS NOT NULL
AND public = 1 AND public = 1
AND heartbeat.monitor_id = ? AND heartbeat.monitor_id = ?

@ -831,15 +831,17 @@ let needSetup = false;
throw new Error("Invalid period."); throw new Error("Invalid period.");
} }
const sqlHourOffset = Database.sqlHourOffset();
let list = await R.getAll(` let list = await R.getAll(`
SELECT * SELECT *
FROM heartbeat FROM heartbeat
WHERE monitor_id = ? WHERE monitor_id = ?
AND time > DATETIME('now', '-' || ? || ' hours') AND time > ${sqlHourOffset}
ORDER BY time ASC ORDER BY time ASC
`, [ `, [
monitorID, monitorID,
period, -period,
]); ]);
callback({ callback({

@ -27,6 +27,9 @@ const {
}, },
} = require("node-radius-utils"); } = require("node-radius-utils");
const dayjs = require("dayjs"); const dayjs = require("dayjs");
const readline = require("readline");
const rl = readline.createInterface({ input: process.stdin,
output: process.stdout });
const isWindows = process.platform === /^win/.test(process.platform); const isWindows = process.platform === /^win/.test(process.platform);
@ -859,3 +862,5 @@ module.exports.grpcQuery = async (options) => {
}); });
}; };
module.exports.prompt = (query) => new Promise((resolve) => rl.question(query, resolve));

Loading…
Cancel
Save