Skip to content

Commit 1b7dd27

Browse files
committed
feat: 新增监控页面平均延迟和成功率
1 parent 6911256 commit 1b7dd27

6 files changed

Lines changed: 79 additions & 34 deletions

File tree

app/app.vue

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
<script lang="ts" setup>
22
const monitorData = useState<MonitorInfo[]>('monitorData')
3+
const { locale } = useI18n()
4+
5+
const echartsLocale = computed(() => {
6+
if (locale.value === 'zh-cn') return 'ZH'
7+
return 'EN'
8+
})
9+
10+
provide(
11+
INIT_OPTIONS_KEY,
12+
computed(() => ({
13+
locale: echartsLocale.value
14+
}))
15+
)
316
417
onMounted(async () => {
518
const data = await request()('/api/monitor')

app/pages/monitor.vue

Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,61 @@ definePageMeta({
44
})
55
66
const monitorData = useState<MonitorInfo[]>('monitorData')
7+
const { t, locale } = useI18n()
78
8-
function getColor(value: number): string {
9-
if (value < 0) return '#d9d9d9'
10-
if (value < 200) return '#52c41a'
11-
if (value < 500) return '#faad14'
12-
return '#f5222d'
9+
const timeLocale = computed(() => {
10+
if (locale.value === 'zh-cn') return 'zh-CN'
11+
return 'en-US'
12+
})
13+
14+
function getColor(delay: number, status: boolean): string {
15+
if (!status) return '#f5222d'
16+
if (delay < 500) return '#52c41a'
17+
if (delay < 999) return '#f7ba1e'
18+
return '#fa8c16'
1319
}
1420
1521
function formatTime(timestamp: number): string {
16-
return new Date(timestamp).toLocaleTimeString()
22+
return new Date(timestamp).toLocaleTimeString(timeLocale.value)
23+
}
24+
25+
function getAverageDelay(item: MonitorInfo): number {
26+
if (!item.data.length) return 0
27+
const totalDelay = item.data.reduce((sum, data) => sum + data.delay, 0)
28+
return Math.round(totalDelay / item.data.length)
29+
}
30+
31+
function getSuccessRate(item: MonitorInfo): number {
32+
if (!item.total) return 0
33+
return Number(((item.success / item.total) * 100).toFixed(1))
34+
}
35+
36+
function getDelayTagColor(item: MonitorInfo): 'green' | 'orange' | 'red' {
37+
const avgDelay = getAverageDelay(item)
38+
if (avgDelay < 500) return 'green'
39+
if (avgDelay < 999) return 'orange'
40+
return 'red'
41+
}
42+
43+
function getSuccessRateTagColor(item: MonitorInfo): 'green' | 'orange' | 'red' {
44+
const successRate = getSuccessRate(item)
45+
if (successRate > 90) return 'green'
46+
if (successRate < 60) return 'red'
47+
return 'orange'
1748
}
1849
1950
function getChartOption(item: MonitorInfo): ECOption {
2051
const times = item.data.map(data => formatTime(data.time))
2152
const delays = item.data.map(data => ({
2253
value: data.delay,
2354
itemStyle: {
24-
color: getColor(data.delay)
55+
color: getColor(data.delay, data.status)
2556
}
2657
}))
2758
59+
const maxDelay = item.data.reduce((max, data) => Math.max(max, data.delay), 0)
60+
const yAxisMax = Math.max(1200, Math.ceil(maxDelay / 100) * 100)
61+
2862
return {
2963
tooltip: {
3064
trigger: 'axis'
@@ -41,14 +75,14 @@ function getChartOption(item: MonitorInfo): ECOption {
4175
data: times
4276
},
4377
yAxis: {
44-
max: 999,
78+
max: yAxisMax,
4579
min: 0,
4680
type: 'value',
4781
show: false
4882
},
4983
series: [
5084
{
51-
name: '延迟',
85+
name: t('label.delay'),
5286
type: 'bar',
5387
data: delays,
5488
barWidth: '70%'
@@ -61,7 +95,22 @@ function getChartOption(item: MonitorInfo): ECOption {
6195
<template>
6296
<div class="flex w-full flex-col gap-4" ref="monitor">
6397
<template v-for="item in monitorData" :key="item.host">
64-
<ACard :title="item.host">
98+
<ACard
99+
:title="item.host"
100+
class="[&_.arco-card-body]:!p-0 [&_.arco-card-header]:items-center"
101+
>
102+
<template #extra>
103+
<div
104+
class="flex flex-nowrap items-center justify-end gap-2 whitespace-nowrap"
105+
>
106+
<ATag bordered :color="getDelayTagColor(item)"
107+
>{{ getAverageDelay(item) }}ms</ATag
108+
>
109+
<ATag bordered :color="getSuccessRateTagColor(item)"
110+
>{{ getSuccessRate(item).toFixed(1) }}%</ATag
111+
>
112+
</div>
113+
</template>
65114
<VChart
66115
:autoresize="true"
67116
:option="getChartOption(item)"
@@ -71,9 +120,3 @@ function getChartOption(item: MonitorInfo): ECOption {
71120
</template>
72121
</div>
73122
</template>
74-
75-
<style scoped>
76-
:deep(.arco-card-size-medium .arco-card-body) {
77-
padding: 0px !important;
78-
}
79-
</style>

i18n/locales/en.json

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,7 @@
6666
"download": "Download",
6767
"protocol": "Protocol",
6868
"submit": "Submit",
69-
"rate": "Success Rate",
70-
"delay": "Average Delay",
71-
"total": "Total Requests",
72-
"success-total": "Successes",
73-
"fail-total": "Failures",
74-
"updated-at": "Updated At"
69+
"delay": "Delay"
7570
},
7671
"title": {
7772
"kms-check": "KMS Server Check",

i18n/locales/zh-cn.json

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,7 @@
6666
"download": "下载",
6767
"protocol": "请求协议",
6868
"submit": "提交",
69-
"rate": "成功率",
70-
"delay": "平均延迟",
71-
"total": "总请求",
72-
"success-total": "成功数",
73-
"fail-total": "失败数",
74-
"updated-at": "更新时间"
69+
"delay": "延迟"
7570
},
7671
"title": {
7772
"kms-check": "KMS 服务器检测",

server/api/monitor.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ export default defineEventHandler(async () => {
2727
)
2828

2929
// Sort by success rate and delay
30-
const sortedData = results.sort((a, b) => {
31-
const successRateA = a.success / a.total
32-
const successRateB = b.success / b.total
30+
// const sortedData = results.sort((a, b) => {
31+
// const successRateA = a.success / a.total
32+
// const successRateB = b.success / b.total
3333

34-
return successRateB - successRateA || a.delay - b.delay
35-
})
34+
// return successRateB - successRateA || a.delay - b.delay
35+
// })
3636

3737
return results
3838
})

server/utils/kms.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ const defaultMonitorList = [
1212
'kms.jihuowin.com',
1313
'kms.kuretru.com',
1414
'kms.litbear.cn',
15-
'kms.loli.beer',
1615
'kms.loli.best',
1716
'kms.lolico.moe',
1817
'kms.mc06.net',

0 commit comments

Comments
 (0)