Feat: Use separate storage for custom chart period

Fix: Fix import error
pull/752/head
Nelson Chan 3 years ago
parent b83c59e308
commit 2f7b60f5e5

@ -31,25 +31,9 @@ async function sendNotificationList(socket) {
* @param toUser True = send to all browsers with the same user id, False = send to the current browser only * @param toUser True = send to all browsers with the same user id, False = send to the current browser only
* @param overwrite Overwrite client-side's heartbeat list * @param overwrite Overwrite client-side's heartbeat list
*/ */
async function sendHeartbeatList(socket, monitorID, toUser = false, overwrite = false, period = null) { async function sendHeartbeatList(socket, monitorID, toUser = false, overwrite = false) {
const timeLogger = new TimeLogger(); const timeLogger = new TimeLogger();
let result;
if (period) {
let list = await R.getAll(`
SELECT * FROM heartbeat
WHERE monitor_id = ? AND
time > DATETIME('now', '-' || ? || ' hours')
ORDER BY time ASC
`, [
monitorID,
period,
]);
result = list;
} else {
let list = await R.getAll(` let list = await R.getAll(`
SELECT * FROM heartbeat SELECT * FROM heartbeat
WHERE monitor_id = ? WHERE monitor_id = ?
@ -59,8 +43,7 @@ async function sendHeartbeatList(socket, monitorID, toUser = false, overwrite =
monitorID, monitorID,
]); ]);
result = list.reverse(); let result = list.reverse();
}
if (toUser) { if (toUser) {
io.to(socket.userID).emit("heartbeatList", monitorID, result, overwrite); io.to(socket.userID).emit("heartbeatList", monitorID, result, overwrite);

@ -650,10 +650,23 @@ exports.entryPage = "dashboard";
console.log(`Get Monitor Beats: ${monitorID} User ID: ${socket.userID}`); console.log(`Get Monitor Beats: ${monitorID} User ID: ${socket.userID}`);
await sendHeartbeatList(socket, monitorID, true, true, period); if (period == null) {
throw new Error("Invalid period.");
}
let list = await R.getAll(`
SELECT * FROM heartbeat
WHERE monitor_id = ? AND
time > DATETIME('now', '-' || ? || ' hours')
ORDER BY time ASC
`, [
monitorID,
period,
]);
callback({ callback({
ok: true ok: true,
data: list,
}); });
} catch (e) { } catch (e) {
callback({ callback({

@ -15,7 +15,7 @@
</div> </div>
</template> </template>
<script> <script lang="ts">
import { BarController, BarElement, Chart, Filler, LinearScale, LineController, LineElement, PointElement, TimeScale, Tooltip } from "chart.js"; import { BarController, BarElement, Chart, Filler, LinearScale, LineController, LineElement, PointElement, TimeScale, Tooltip } from "chart.js";
import dayjs from "dayjs"; import dayjs from "dayjs";
import utc from "dayjs/plugin/utc"; import utc from "dayjs/plugin/utc";
@ -23,6 +23,7 @@ import timezone from "dayjs/plugin/timezone";
import "chartjs-adapter-dayjs"; import "chartjs-adapter-dayjs";
import { LineChart } from "vue-chart-3"; import { LineChart } from "vue-chart-3";
import { useToast } from "vue-toastification"; import { useToast } from "vue-toastification";
import { UP, DOWN, PENDING } from "../util.ts";
dayjs.extend(utc); dayjs.extend(utc);
dayjs.extend(timezone); dayjs.extend(timezone);
@ -151,9 +152,15 @@ export default {
chartData() { chartData() {
let pingData = []; // Ping Data for Line Chart, y-axis contains ping time let pingData = []; // Ping Data for Line Chart, y-axis contains ping time
let downData = []; // Down Data for Bar Chart, y-axis is 1 if target is down, 0 if target is up let downData = []; // Down Data for Bar Chart, y-axis is 1 if target is down, 0 if target is up
if (this.monitorId in this.$root.heartbeatList) {
this.$root.heartbeatList[this.monitorId] let heartbeatList = this.heartbeatList ||
(this.monitorId in this.$root.heartbeatList && this.$root.heartbeatList[this.monitorId]) ||
[];
heartbeatList
.filter( .filter(
// Filtering as data gets appended
// not the most efficient, but works for now
(beat) => dayjs.utc(beat.time).tz(this.$root.timezone).isAfter(dayjs().subtract(Math.max(this.chartPeriodHrs, 6), "hours"))) (beat) => dayjs.utc(beat.time).tz(this.$root.timezone).isAfter(dayjs().subtract(Math.max(this.chartPeriodHrs, 6), "hours")))
.map((beat) => { .map((beat) => {
const x = this.$root.datetime(beat.time); const x = this.$root.datetime(beat.time);
@ -163,10 +170,10 @@ export default {
}); });
downData.push({ downData.push({
x, x,
y: beat.status === 0 ? 1 : 0, y: beat.status === DOWN ? 1 : 0,
}); });
}); });
}
return { return {
datasets: [ datasets: [
{ {
@ -197,14 +204,27 @@ export default {
chartPeriodHrs: function (newPeriod) { chartPeriodHrs: function (newPeriod) {
if (newPeriod == "0") { if (newPeriod == "0") {
newPeriod = null; newPeriod = null;
} this.heartbeatList = null;
} else {
this.$root.getMonitorBeats(this.monitorId, newPeriod, (res) => { this.$root.getMonitorBeats(this.monitorId, newPeriod, (res) => {
if (!res.ok) { if (!res.ok) {
toast.error(res.msg); toast.error(res.msg);
} else {
this.heartbeatList = res.data;
} }
}); });
} }
}
}, },
created() {
// Setup Watcher on the root heartbeatList,
// And mirror latest change to this.heartbeatList
this.$watch(() => this.$root.heartbeatList[this.monitorId], (heartbeatList) => {
if (this.chartPeriodHrs != 0) {
this.heartbeatList.push(heartbeatList.at(-1));
}
}, { deep: true });
}
}; };
</script> </script>
@ -217,7 +237,7 @@ export default {
} }
.period-options { .period-options {
padding: 0.3em 1.5em; padding: 0.3em 2.2em;
margin-bottom: -1.5em; margin-bottom: -1.5em;
float: right; float: right;
position: relative; position: relative;

Loading…
Cancel
Save