-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: kms monitoring statistical data
- Loading branch information
Showing
8 changed files
with
116 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,19 @@ | ||
import axios from 'axios' | ||
import dayjs from 'dayjs' | ||
import { Notification } from '@arco-design/web-vue' | ||
import fetch from '@/utils/fetch' | ||
|
||
export const useMonitorStore = defineStore('monitor', () => { | ||
const apiUrl = 'https://api.uptimerobot.com/v2/getMonitors' | ||
const apiKey = ['m794366630-4128d6474ac33847941241ec', 'm790633514-8e1d658a806daa670799a6f5'] | ||
|
||
const timestamps = Array.from({ length: 120 }, (_, num) => { | ||
return ( | ||
dayjs().startOf('day').subtract(num, 'day').unix() + | ||
'_' + | ||
dayjs().startOf('day').subtract(num, 'day').endOf('day').unix() | ||
) | ||
}).reverse() | ||
|
||
const loading = ref(false) | ||
const monitors = useStorage('monitors', []) | ||
|
||
const getMonitors = async () => { | ||
loading.value = true | ||
const _monitors = [] | ||
if (monitors.value.length !== 0) { | ||
loading.value = false | ||
return | ||
} | ||
for (const api_key of apiKey) { | ||
try { | ||
const { data } = await axios.post(apiUrl, { | ||
api_key, | ||
custom_uptime_ranges: timestamps.join('-'), | ||
}) | ||
_monitors.push(...data.monitors) | ||
} catch ({ code, message }) { | ||
Notification.error({ title: code, content: message }) | ||
loading.value = false | ||
} | ||
try { | ||
const { data } = await fetch('/api/server') | ||
data.sort((prev: { rate: number }, next: { rate: number }) => { | ||
return next.rate - prev.rate | ||
}) | ||
monitors.value = data | ||
} catch (err) { | ||
console.log(err) | ||
} | ||
monitors.value = _monitors | ||
loading.value = false | ||
} | ||
|
||
return { getMonitors, loading, monitors } | ||
return { getMonitors, monitors } | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import axios, { AxiosResponse } from 'axios' | ||
import { Notification } from '@arco-design/web-vue' | ||
|
||
const fetch = axios.create({ | ||
baseURL: import.meta.env.VITE_BASE_URL || 'https://kms.ikxin.com', | ||
timeout: 10 * 1000, | ||
}) | ||
|
||
fetch.interceptors.response.use( | ||
(res: AxiosResponse) => res, | ||
(err: Error) => { | ||
Notification.error({ title: 'Error', content: String(err).trim() }) | ||
}, | ||
) | ||
|
||
export default fetch |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,80 @@ | ||
<script setup lang="ts"> | ||
import { useMonitorStore } from '@/store/monitor' | ||
import { TableColumnData } from '@arco-design/web-vue' | ||
import dayjs from 'dayjs' | ||
import * as echarts from 'echarts' | ||
const columns = [ | ||
const columns: TableColumnData[] = [ | ||
{ | ||
dataIndex: 'friendly_name', | ||
title: 'Domain', | ||
width: 160, | ||
dataIndex: 'host', | ||
title: '服务器', | ||
minWidth: 160, | ||
}, | ||
{ | ||
dataIndex: 'custom_uptime_ranges', | ||
title: 'Uptime Ranges', | ||
dataIndex: 'port', | ||
title: '端口', | ||
width: 80, | ||
}, | ||
{ | ||
dataIndex: 'rate', | ||
title: '成功率', | ||
width: 100, | ||
align: 'center', | ||
sortable: { | ||
sortDirections: ['ascend', 'descend'], | ||
}, | ||
render({ record }) { | ||
return h('div', { id: record.id, class: 'h-36' }) | ||
return `${(record.rate * 100).toFixed(2)} %` | ||
}, | ||
}, | ||
{ | ||
dataIndex: 'delay', | ||
title: '平均延迟', | ||
width: 120, | ||
align: 'center', | ||
sortable: { | ||
sortDirections: ['ascend', 'descend'], | ||
}, | ||
render({ record }) { | ||
return `${record.delay.toFixed(2)} ms` | ||
}, | ||
}, | ||
{ | ||
dataIndex: 'total', | ||
title: '总请求', | ||
width: 80, | ||
align: 'center', | ||
}, | ||
{ | ||
dataIndex: 'success', | ||
title: '成功数', | ||
width: 80, | ||
align: 'center', | ||
}, | ||
{ | ||
dataIndex: 'fail', | ||
title: '失败数', | ||
width: 80, | ||
align: 'center', | ||
}, | ||
{ | ||
dataIndex: 'updatedAt', | ||
title: '更新时间', | ||
width: 170, | ||
render({ record }) { | ||
return dayjs(record.updatedAt).format('YYYY-MM-DD HH:mm:ss') | ||
}, | ||
}, | ||
] | ||
const monitorStore = useMonitorStore() | ||
onMounted(() => { | ||
monitorStore.monitors.forEach(item => { | ||
const ranges = Array.from({ length: 120 }, (_, num) => { | ||
return dayjs().subtract(num, 'day').format('YYYY-MM-DD') | ||
}).reverse() | ||
const uptime = item.custom_uptime_ranges.split('-') | ||
const customUptimeRanges = ranges.map((item, index) => [item, Number(uptime[index])]) | ||
const echart = echarts.init(document.getElementById(item.id), null, { locale: 'EN' }) | ||
echart.setOption({ | ||
tooltip: {}, | ||
visualMap: { | ||
align: 'left', | ||
min: 0, | ||
max: 100, | ||
type: 'piecewise', | ||
left: 'right', | ||
top: 'center', | ||
inRange: { | ||
color: ['#ebedf0', '#f44336', '#ffc107', '#41c464'], | ||
symbolSize: [0, 99, 100], | ||
}, | ||
}, | ||
calendar: { | ||
cellSize: [32, 16], | ||
itemStyle: { borderWidth: 0.5 }, | ||
top: 'center', | ||
left: 20, | ||
range: [ranges.at(0), ranges.at(-1)], | ||
yearLabel: { show: false }, | ||
}, | ||
series: { | ||
type: 'heatmap', | ||
coordinateSystem: 'calendar', | ||
data: customUptimeRanges, | ||
}, | ||
}) | ||
}) | ||
}) | ||
</script> | ||
|
||
<template> | ||
<ASpin :loading="monitorStore.loading" dot> | ||
<ATable :columns="columns" :data="monitorStore.monitors"></ATable> | ||
<ASpin :loading="false" dot> | ||
<ATable | ||
:columns="columns" | ||
:data="monitorStore.monitors" | ||
:pagination="false" | ||
></ATable> | ||
</ASpin> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters