Обновить src/components/NodesStatus.vue
This commit is contained in:
@@ -25,7 +25,7 @@
|
|||||||
<div v-else class="space-y-2 max-w-3xl mx-auto">
|
<div v-else class="space-y-2 max-w-3xl mx-auto">
|
||||||
<div
|
<div
|
||||||
v-for="host in sortedHosts"
|
v-for="host in sortedHosts"
|
||||||
:key="host.address"
|
:key="host.address + host.port"
|
||||||
class="flex items-center justify-between bg-white dark:bg-gray-900 rounded-xl px-4 py-2.5 border"
|
class="flex items-center justify-between bg-white dark:bg-gray-900 rounded-xl px-4 py-2.5 border"
|
||||||
:class="host.isOnline ? 'border-green-500/30' : 'border-red-500/30 opacity-60'"
|
:class="host.isOnline ? 'border-green-500/30' : 'border-red-500/30 opacity-60'"
|
||||||
>
|
>
|
||||||
@@ -35,10 +35,13 @@
|
|||||||
:src="`https://cdn.jsdelivr.net/npm/flag-icons@7.2.3/flags/4x3/${host.countryCode.toLowerCase()}.svg`"
|
:src="`https://cdn.jsdelivr.net/npm/flag-icons@7.2.3/flags/4x3/${host.countryCode.toLowerCase()}.svg`"
|
||||||
class="h-5 w-auto rounded-sm"
|
class="h-5 w-auto rounded-sm"
|
||||||
:alt="host.name"
|
:alt="host.name"
|
||||||
|
@error="handleFlagError"
|
||||||
/>
|
/>
|
||||||
|
<div v-else class="h-5 w-7 rounded-sm bg-gray-200 dark:bg-gray-700 flex items-center justify-center text-xs">
|
||||||
|
🌐
|
||||||
|
</div>
|
||||||
<span class="font-semibold text-gray-800 dark:text-white">{{ host.name }}</span>
|
<span class="font-semibold text-gray-800 dark:text-white">{{ host.name }}</span>
|
||||||
|
|
||||||
<!-- Индикатор состояния -->
|
|
||||||
<span class="relative flex h-2 w-2">
|
<span class="relative flex h-2 w-2">
|
||||||
<span
|
<span
|
||||||
class="animate-ping absolute inline-flex h-full w-full rounded-full opacity-75"
|
class="animate-ping absolute inline-flex h-full w-full rounded-full opacity-75"
|
||||||
@@ -53,7 +56,7 @@
|
|||||||
|
|
||||||
<div class="flex items-center gap-3">
|
<div class="flex items-center gap-3">
|
||||||
<span
|
<span
|
||||||
v-if="host.ping !== undefined && host.ping !== null"
|
v-if="host.ping !== undefined && host.ping !== null && host.ping !== 0"
|
||||||
class="font-mono font-semibold flex items-center gap-1.5"
|
class="font-mono font-semibold flex items-center gap-1.5"
|
||||||
:class="{
|
:class="{
|
||||||
'text-green-600': host.ping < 200,
|
'text-green-600': host.ping < 200,
|
||||||
@@ -64,7 +67,7 @@
|
|||||||
<Wifi :size="14" />
|
<Wifi :size="14" />
|
||||||
{{ host.ping }} ms
|
{{ host.ping }} ms
|
||||||
</span>
|
</span>
|
||||||
<span v-else class="text-gray-300 font-mono">—</span>
|
<span v-else class="text-gray-400 font-mono text-sm">⏳</span>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -88,6 +91,10 @@ const sortedHosts = computed(() => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const handleFlagError = (e) => {
|
||||||
|
e.target.style.display = 'none'
|
||||||
|
}
|
||||||
|
|
||||||
const pingHost = async (host) => {
|
const pingHost = async (host) => {
|
||||||
const url = `https://${host.address}:${host.port}`
|
const url = `https://${host.address}:${host.port}`
|
||||||
const start = performance.now()
|
const start = performance.now()
|
||||||
@@ -105,22 +112,20 @@ const pingHost = async (host) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const fetchHosts = async () => {
|
const loadData = async () => {
|
||||||
try {
|
try {
|
||||||
const res = await fetch('/api/v1/hosts')
|
const res = await fetch('/api/v1/hosts')
|
||||||
if (!res.ok) throw new Error()
|
if (!res.ok) throw new Error(`HTTP ${res.status}`)
|
||||||
const data = await res.json()
|
const data = await res.json()
|
||||||
|
|
||||||
hosts.value = data.map((h) => ({
|
return data.map((h) => ({
|
||||||
...h,
|
...h,
|
||||||
countryCode: h.country_code ? h.country_code.toLowerCase() : null,
|
countryCode: h.country_code ? h.country_code.toLowerCase() : null,
|
||||||
isOnline: true,
|
isOnline: true,
|
||||||
ping: null,
|
ping: null
|
||||||
}))
|
}))
|
||||||
} catch {
|
} catch (err) {
|
||||||
error.value = 'Ошибка загрузки данных'
|
throw new Error('Ошибка загрузки данных')
|
||||||
} finally {
|
|
||||||
loading.value = false
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -129,32 +134,22 @@ const refreshData = async () => {
|
|||||||
error.value = null
|
error.value = null
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const res = await fetch('/api/v1/hosts')
|
const data = await loadData()
|
||||||
if (!res.ok) throw new Error()
|
hosts.value = data
|
||||||
const data = await res.json()
|
|
||||||
|
|
||||||
hosts.value = data.map((h) => ({
|
// Пингуем все хосты
|
||||||
...h,
|
|
||||||
countryCode: h.country_code ? h.country_code.toLowerCase() : null,
|
|
||||||
isOnline: true,
|
|
||||||
ping: null,
|
|
||||||
}))
|
|
||||||
|
|
||||||
// Пингуем все хосты параллельно
|
|
||||||
const pings = await Promise.all(hosts.value.map(h => pingHost(h)))
|
const pings = await Promise.all(hosts.value.map(h => pingHost(h)))
|
||||||
hosts.value.forEach((h, i) => {
|
hosts.value.forEach((h, i) => {
|
||||||
h.ping = pings[i]
|
h.ping = pings[i]
|
||||||
})
|
})
|
||||||
|
} catch (err) {
|
||||||
} catch {
|
error.value = err.message
|
||||||
error.value = 'Ошибка обновления данных'
|
|
||||||
} finally {
|
} finally {
|
||||||
loading.value = false
|
loading.value = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
onMounted(async () => {
|
onMounted(() => {
|
||||||
await fetchHosts()
|
refreshData()
|
||||||
await refreshData()
|
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
Reference in New Issue
Block a user