uptime-kuma/src/components/HeartbeatBar.vue

329 lines
8.9 KiB
Vue
Raw Normal View History

2021-06-25 19:03:06 +00:00
<template>
2021-07-27 17:47:13 +00:00
<div ref="wrap" class="wrap" :style="wrapStyle">
<div class="hp-bar-big" :style="barStyle">
2021-06-29 08:06:20 +00:00
<div
2021-07-27 17:47:13 +00:00
v-for="(beat, index) in shortBeatList"
:key="index"
2021-06-29 08:06:20 +00:00
class="beat"
:class="{ 'empty': (beat === 0), 'down': (beat.status === 0), 'pending': (beat.status === 2), 'maintenance': (beat.status === 3) }"
2021-06-29 08:06:20 +00:00
:style="beatStyle"
:title="getBeatTitle(beat)"
/>
2021-06-25 19:03:06 +00:00
</div>
<div
2023-09-16 18:56:19 +00:00
v-if="!$root.isMobile && size !== 'small' && beatList.length > 4 && $root.styleElapsedTime !== 'none'"
class="d-flex justify-content-between align-items-center word" :style="timeStyle"
>
<div>{{ timeSinceFirstBeat }} ago</div>
<div v-if="$root.styleElapsedTime === 'with-line'" class="connecting-line"></div>
<div>{{ timeSinceLastBeat }}</div>
</div>
2021-06-25 19:03:06 +00:00
</div>
</template>
<script>
import dayjs from "dayjs";
2021-06-25 19:03:06 +00:00
export default {
2021-06-27 08:10:55 +00:00
props: {
/** Size of the heartbeat bar */
2021-06-27 08:10:55 +00:00
size: {
type: String,
2021-07-27 17:47:13 +00:00
default: "big",
2021-06-27 08:10:55 +00:00
},
/** ID of the monitor */
monitorId: {
type: Number,
required: true,
},
/** Array of the monitors heartbeats */
2021-09-11 15:43:07 +00:00
heartbeatList: {
type: Array,
default: null,
}
2021-06-27 08:10:55 +00:00
},
2021-06-25 19:03:06 +00:00
data() {
return {
beatWidth: 10,
beatHeight: 30,
hoverScale: 1.5,
beatMargin: 4,
2021-06-25 19:03:06 +00:00
move: false,
2021-06-26 06:32:12 +00:00
maxBeat: -1,
2022-04-13 16:30:32 +00:00
};
2021-06-25 19:03:06 +00:00
},
computed: {
2021-06-26 06:32:12 +00:00
2021-09-11 15:43:07 +00:00
/**
* If heartbeatList is null, get it from $root.heartbeatList
* @returns {object} Heartbeat list
2021-09-11 15:43:07 +00:00
*/
2021-06-27 08:10:55 +00:00
beatList() {
2021-09-11 15:43:07 +00:00
if (this.heartbeatList === null) {
return this.$root.heartbeatList[this.monitorId];
} else {
return this.heartbeatList;
}
2021-06-27 08:10:55 +00:00
},
/**
* Calculates the amount of beats of padding needed to fill the length of shortBeatList.
* @returns {number} The amount of beats of padding needed to fill the length of shortBeatList.
*/
numPadding() {
if (!this.beatList) {
return 0;
}
let num = this.beatList.length - this.maxBeat;
if (this.move) {
num = num - 1;
}
if (num > 0) {
return 0;
}
return -1 * num;
},
2021-06-26 06:32:12 +00:00
shortBeatList() {
if (!this.beatList) {
return [];
}
let placeholders = [];
2021-06-27 08:10:55 +00:00
2021-06-26 06:32:12 +00:00
let start = this.beatList.length - this.maxBeat;
if (this.move) {
start = start - 1;
}
if (start < 0) {
2021-06-27 08:10:55 +00:00
// Add empty placeholder
for (let i = start; i < 0; i++) {
2022-04-13 16:30:32 +00:00
placeholders.push(0);
2021-06-27 08:10:55 +00:00
}
2021-06-26 06:32:12 +00:00
start = 0;
}
2022-04-13 16:30:32 +00:00
return placeholders.concat(this.beatList.slice(start));
2021-06-26 06:32:12 +00:00
},
2021-06-25 19:03:06 +00:00
wrapStyle() {
let topBottom = (((this.beatHeight * this.hoverScale) - this.beatHeight) / 2);
let leftRight = (((this.beatWidth * this.hoverScale) - this.beatWidth) / 2);
return {
padding: `${topBottom}px ${leftRight}px`,
2021-07-28 15:03:37 +00:00
width: "100%",
2022-04-13 16:30:32 +00:00
};
2021-06-25 19:03:06 +00:00
},
2021-06-26 06:32:12 +00:00
2021-06-25 19:03:06 +00:00
barStyle() {
2021-06-26 06:32:12 +00:00
if (this.move && this.shortBeatList.length > this.maxBeat) {
2021-06-25 19:03:06 +00:00
let width = -(this.beatWidth + this.beatMargin * 2);
2021-06-26 06:32:12 +00:00
return {
2021-06-25 19:03:06 +00:00
transition: "all ease-in-out 0.25s",
transform: `translateX(${width}px)`,
2022-04-13 16:30:32 +00:00
};
2021-06-26 06:32:12 +00:00
2021-06-25 19:03:06 +00:00
}
2021-07-27 17:47:13 +00:00
return {
transform: "translateX(0)",
2022-04-13 16:30:32 +00:00
};
2021-07-27 17:47:13 +00:00
2021-06-25 19:03:06 +00:00
},
2021-06-26 06:32:12 +00:00
2021-06-25 19:03:06 +00:00
beatStyle() {
return {
width: this.beatWidth + "px",
height: this.beatHeight + "px",
margin: this.beatMargin + "px",
"--hover-scale": this.hoverScale,
2022-04-13 16:30:32 +00:00
};
2021-07-27 17:47:13 +00:00
},
2021-06-26 06:32:12 +00:00
/**
* Returns the style object for positioning the time element.
* @returns {object} The style object containing the CSS properties for positioning the time element.
*/
timeStyle() {
return {
"margin-left": this.numPadding * (this.beatWidth + this.beatMargin * 2) + "px",
};
},
/**
* Calculates the time elapsed since the first valid beat.
* @returns {string} The time elapsed in minutes or hours.
*/
timeSinceFirstBeat() {
const firstValidBeat = this.shortBeatList.at(this.numPadding);
const minutes = dayjs().diff(dayjs.utc(firstValidBeat?.time), "minutes");
if (minutes > 60) {
return (minutes / 60).toFixed(0) + "h";
} else {
return minutes + "m";
}
},
/**
* Calculates the elapsed time since the last valid beat was registered.
* @returns {string} The elapsed time in a minutes, hours or "now".
*/
timeSinceLastBeat() {
const lastValidBeat = this.shortBeatList.at(-1);
const seconds = dayjs().diff(dayjs.utc(lastValidBeat?.time), "seconds");
let tolerance = 60 * 2; // default for when monitorList not available
if (this.$root.monitorList[this.monitorId] != null) {
tolerance = this.$root.monitorList[this.monitorId].interval * 2;
}
if (seconds < tolerance) {
return "now";
} else if (seconds < 60 * 60) {
return (seconds / 60).toFixed(0) + "m ago";
} else {
return (seconds / 60 / 60).toFixed(0) + "h ago";
}
}
2021-06-26 06:32:12 +00:00
},
watch: {
beatList: {
handler(val, oldVal) {
this.move = true;
setTimeout(() => {
this.move = false;
2022-04-13 16:30:32 +00:00
}, 300);
2021-06-26 06:32:12 +00:00
},
deep: true,
2021-07-27 17:47:13 +00:00
},
},
unmounted() {
window.removeEventListener("resize", this.resize);
},
beforeMount() {
2021-09-11 15:43:07 +00:00
if (this.heartbeatList === null) {
if (!(this.monitorId in this.$root.heartbeatList)) {
2021-09-11 15:43:07 +00:00
this.$root.heartbeatList[this.monitorId] = [];
}
}
},
2021-07-27 17:47:13 +00:00
mounted() {
if (this.size !== "big") {
this.beatWidth = 5;
this.beatHeight = 16;
this.beatMargin = 2;
}
// Suddenly, have an idea how to handle it universally.
// If the pixel * ratio != Integer, then it causes render issue, round it to solve it!!
const actualWidth = this.beatWidth * window.devicePixelRatio;
const actualMargin = this.beatMargin * window.devicePixelRatio;
if (!Number.isInteger(actualWidth)) {
this.beatWidth = Math.round(actualWidth) / window.devicePixelRatio;
}
if (!Number.isInteger(actualMargin)) {
this.beatMargin = Math.round(actualMargin) / window.devicePixelRatio;
2021-06-26 06:32:12 +00:00
}
2021-07-27 17:47:13 +00:00
window.addEventListener("resize", this.resize);
this.resize();
},
methods: {
/**
* Resize the heartbeat bar
* @returns {void}
*/
2021-07-27 17:47:13 +00:00
resize() {
if (this.$refs.wrap) {
2022-04-13 16:30:32 +00:00
this.maxBeat = Math.floor(this.$refs.wrap.clientWidth / (this.beatWidth + this.beatMargin * 2));
2021-07-27 17:47:13 +00:00
}
},
/**
* Get the title of the beat.
* Used as the hover tooltip on the heartbeat bar.
* @param {object} beat Beat to get title from
* @returns {string} Beat title
*/
getBeatTitle(beat) {
2022-04-13 16:30:32 +00:00
return `${this.$root.datetime(beat.time)}` + ((beat.msg) ? ` - ${beat.msg}` : "");
2022-01-21 16:13:38 +00:00
},
2021-07-27 17:47:13 +00:00
},
2022-04-13 16:30:32 +00:00
};
2021-06-25 19:03:06 +00:00
</script>
<style lang="scss" scoped>
2021-06-25 19:03:06 +00:00
@import "../assets/vars.scss";
.wrap {
overflow: hidden;
2021-06-26 06:32:12 +00:00
width: 100%;
2021-06-25 19:03:06 +00:00
white-space: nowrap;
}
.hp-bar-big {
.beat {
display: inline-block;
2021-06-25 19:03:06 +00:00
background-color: $primary;
border-radius: $border-radius;
2021-06-25 19:03:06 +00:00
2021-06-27 08:10:55 +00:00
&.empty {
2021-07-13 04:16:11 +00:00
background-color: aliceblue;
2021-06-26 06:32:12 +00:00
}
2021-06-27 08:10:55 +00:00
&.down {
background-color: $danger;
}
2021-06-25 19:03:06 +00:00
&.pending {
background-color: $warning;
}
&.maintenance {
background-color: $maintenance;
}
2021-06-27 08:10:55 +00:00
&:not(.empty):hover {
transition: all ease-in-out 0.15s;
2021-06-25 19:03:06 +00:00
opacity: 0.8;
transform: scale(var(--hover-scale));
}
}
}
.dark {
2021-08-24 15:38:25 +00:00
.hp-bar-big .beat.empty {
background-color: #848484;
}
}
.word {
color: $secondary-text;
font-size: 12px;
}
.connecting-line {
flex-grow: 1;
height: 1px;
background-color: #ededed;
margin-left: 10px;
margin-right: 10px;
margin-top: 2px;
.dark & {
background-color: #333;
}
}
2021-06-25 19:03:06 +00:00
</style>