From 84155ccedd7a37c4c9b3eca3ff3d5536951e42ce Mon Sep 17 00:00:00 2001 From: snowykami Date: Thu, 3 Oct 2024 19:21:24 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E6=96=B0=E9=85=8D=E8=89=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.vue | 10 ++++++++++ src/api/utils.ts | 11 +++++++++-- src/components/Host.vue | 17 +++++++++++++---- 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/App.vue b/src/App.vue index c58b264..74ead23 100644 --- a/src/App.vue +++ b/src/App.vue @@ -1,10 +1,15 @@ diff --git a/src/api/utils.ts b/src/api/utils.ts index 272fc26..0294bf1 100644 --- a/src/api/utils.ts +++ b/src/api/utils.ts @@ -25,8 +25,8 @@ export function formatSizeByUnit(bytes: number, unit: string | null = null, suff if( bytes == 0 ){ return '0' } - if (unit && bytes < 1024) { - return bytes.toFixed(0) + unit + if (bytes < 1024) { + return bytes.toFixed(0) + (suffix ? suffix : '') } const units = ['', 'K', 'M', 'G', 'T', 'P', 'E'] let i = unit ? units.indexOf(unit) : Math.floor(Math.log2(bytes) / 10) @@ -49,4 +49,11 @@ export function format2Size(num1: number, num2: number, suffix: string | null = const r2 = formatSizeToNumAndUnit(num2) const n1 = formatSizeByUnit(num1, r2.unit) return `${n1}/${r2.num.toFixed(1)}${r2.unit}${suffix}` +} + +export function formatDate(timestamp: number, timeOnly: boolean = false){ + const d = new Date(timestamp * 1000) + const date = d.toLocaleDateString() + const time = d.toLocaleTimeString() + return timeOnly ? time : `${date} ${time}` } \ No newline at end of file diff --git a/src/components/Host.vue b/src/components/Host.vue index 3f16479..bf7882f 100644 --- a/src/components/Host.vue +++ b/src/components/Host.vue @@ -2,7 +2,7 @@ import {Status} from "../api"; import {computed, onMounted, ref, watch} from "vue"; import * as echarts from "echarts"; -import {format2Size, formatSizeByUnit, getLinuxReleaseIcon} from "../api/utils.ts"; +import {format2Size, formatDate, formatSizeByUnit, getLinuxReleaseIcon} from "../api/utils.ts"; const props = defineProps<{ status: Status @@ -54,7 +54,11 @@ function onMountedFunc() { function update() { const timeDiff = (Date.now()) / 1000 - status.value.meta.observed_at deltaTime.value = timeDiff.toFixed(1) - netStats.push([status.value.meta.observed_at, status.value.hardware.net.up, status.value.hardware.net.down]) + // 判断该时间与上一个时间不同才push + if (netStats.length === 0 || netStats[netStats.length - 1][0] !== status.value.meta.observed_at){ + netStats.push([status.value.meta.observed_at, status.value.hardware.net.up, status.value.hardware.net.down]) + } + if (netStats.length > 20) { netStats.shift() } @@ -206,7 +210,12 @@ function onMountedFunc() { { type: 'category', boundaryGap: false, - data: netStats.map(item => item[0]) + data: netStats.map(item => item[0]), + axisLabel: { + formatter: function (value: number) { + return formatDate(value, true) + } + } } ], yAxis: [ @@ -295,7 +304,7 @@ onMounted(
CPU
-
{{ status.hardware.cpu.cores }}c{{ status.hardware.cpu.logics }}t
+
{{ status.hardware.cpu.cores }}C {{ status.hardware.cpu.logics }}T