This commit is contained in:
Louis Lam 2024-09-28 01:22:45 +08:00
parent 7046a2e0f6
commit 67ad0f79b3
2 changed files with 37 additions and 7 deletions

View file

@ -6,6 +6,8 @@ const path = require("path");
const { EmbeddedMariaDB } = require("./embedded-mariadb"); const { EmbeddedMariaDB } = require("./embedded-mariadb");
const mysql = require("mysql2/promise"); const mysql = require("mysql2/promise");
const { Settings } = require("./settings"); const { Settings } = require("./settings");
const { UptimeCalculator } = require("./uptime-calculator");
const dayjs = require("dayjs");
/** /**
* Database & App Data Folder * Database & App Data Folder
@ -734,7 +736,7 @@ class Database {
log.debug("db", "Enter Migrate Aggregate Table function"); log.debug("db", "Enter Migrate Aggregate Table function");
// //
let migrated = Settings.get("migratedAggregateTable"); let migrated = await Settings.get("migratedAggregateTable");
if (migrated) { if (migrated) {
log.debug("db", "Migrated, skip migration"); log.debug("db", "Migrated, skip migration");
@ -750,6 +752,7 @@ class Database {
let dates = await trx.raw(` let dates = await trx.raw(`
SELECT DISTINCT DATE(time) AS date SELECT DISTINCT DATE(time) AS date
FROM heartbeat FROM heartbeat
ORDER BY date ASC
`); `);
// Get a list of unique monitors from the heartbeat table, using raw sql // Get a list of unique monitors from the heartbeat table, using raw sql
@ -758,17 +761,41 @@ class Database {
FROM heartbeat FROM heartbeat
`); `);
// Show warning if stat_* tables are not empty // Stop if stat_* tables are not empty
for (let table of [ "stat_minutely", "stat_hourly", "stat_daily" ]) { for (let table of [ "stat_minutely", "stat_hourly", "stat_daily" ]) {
let count = await trx(table).count("*").first(); let countResult = await trx.raw(`SELECT COUNT(*) AS count FROM ${table}`);
if (count.count > 0) { let count = countResult[0].count;
log.warn("db", `Table ${table} is not empty, migration may cause data loss (Maybe you were using 2.0.0-dev?)`); if (count > 0) {
log.warn("db", `Aggregate table ${table} is not empty, migration will not be started (Maybe you were using 2.0.0-dev?)`);
return;
} }
} }
console.log("Dates", dates); console.log("Dates", dates);
console.log("Monitors", monitors); console.log("Monitors", monitors);
for (let monitor of monitors) {
for (let date of dates) {
log.info("db", `Migrating monitor ${monitor.monitor_id} on date ${date.date}`);
// New Uptime Calculator
let calculator = new UptimeCalculator();
// TODO: Pass transaction to the calculator
// calculator.setTransaction(trx);
// Get all the heartbeats for this monitor and date
let heartbeats = await trx("heartbeat")
.where("monitor_id", monitor.monitor_id)
.whereRaw("DATE(time) = ?", [ date.date ])
.orderBy("time", "asc");
for (let heartbeat of heartbeats) {
calculator.update(heartbeat.status, heartbeat.ping, dayjs(heartbeat.time));
}
}
}
trx.commit(); trx.commit();
//await Settings.set("migratedAggregateTable", true); //await Settings.set("migratedAggregateTable", true);

View file

@ -189,11 +189,14 @@ class UptimeCalculator {
/** /**
* @param {number} status status * @param {number} status status
* @param {number} ping Ping * @param {number} ping Ping
* @param {dayjs.Dayjs} date Date (Only for migration)
* @returns {dayjs.Dayjs} date * @returns {dayjs.Dayjs} date
* @throws {Error} Invalid status * @throws {Error} Invalid status
*/ */
async update(status, ping = 0) { async update(status, ping = 0, date) {
let date = this.getCurrentDate(); if (!date) {
date = this.getCurrentDate();
}
let flatStatus = this.flatStatus(status); let flatStatus = this.flatStatus(status);