|
| 1 | +<script lang="ts" setup> |
| 2 | +import NeBreadcrumbs, { type BreadcrumbItem } from '@/components/NeBreadcrumbs.vue' |
| 3 | +import { useI18n } from 'vue-i18n' |
| 4 | +import { computed, watchEffect } from 'vue' |
| 5 | +import { useTrafficStats } from '@/composables/useTrafficStats' |
| 6 | +import { |
| 7 | + byteFormat1024, |
| 8 | + getAxiosErrorMessage, |
| 9 | + NeCard, |
| 10 | + NeEmptyState, |
| 11 | + NeInlineNotification, |
| 12 | + NeSkeleton |
| 13 | +} from '@nethesis/vue-components' |
| 14 | +import SimpleStat from '@/components/charts/SimpleStat.vue' |
| 15 | +import { CYAN_500, CYAN_600 } from '@/lib/color' |
| 16 | +import { faEmptySet } from '@nethesis/nethesis-solid-svg-icons' |
| 17 | +import TrafficByHourChart from '@/components/standalone/monitoring/TrafficByHourChart.vue' |
| 18 | +import { useThemeStore } from '@/stores/theme' |
| 19 | +import TrafficCard from '@/components/standalone/monitoring/TrafficCard.vue' |
| 20 | +import { type AvailableFilters, useTrafficFilter } from '@/composables/useTrafficFilter' |
| 21 | +
|
| 22 | +type FilterableBreadcrumbItem = BreadcrumbItem & { |
| 23 | + key: string |
| 24 | +} |
| 25 | +
|
| 26 | +const themeStore = useThemeStore() |
| 27 | +const filters = useTrafficFilter() |
| 28 | +const { data, error, loading, loadData } = useTrafficStats() |
| 29 | +
|
| 30 | +watchEffect(() => { |
| 31 | + loadData(filters.get('client'), filters.get('app')) |
| 32 | +}) |
| 33 | +
|
| 34 | +const { t } = useI18n() |
| 35 | +
|
| 36 | +const filtersToBreadcrumb = computed<FilterableBreadcrumbItem[]>(() => { |
| 37 | + let order = 1 |
| 38 | + // add a default item to the breadcrumb |
| 39 | + const defaultItem: FilterableBreadcrumbItem = { |
| 40 | + key: 'default', |
| 41 | + order: order++, |
| 42 | + label: t('standalone.real_time_monitor.traffic') |
| 43 | + } |
| 44 | + return filters.list.value |
| 45 | + .map((filter) => { |
| 46 | + let label = '' |
| 47 | + switch (filter.key) { |
| 48 | + case 'app': |
| 49 | + if (filter.value.startsWith('netify.')) { |
| 50 | + label = filter.value.slice(7) |
| 51 | + } else { |
| 52 | + label = filter.value |
| 53 | + } |
| 54 | + if (label == 'unknown') { |
| 55 | + label = t('common.unknown') |
| 56 | + } else { |
| 57 | + label = label.charAt(0).toUpperCase() + label.slice(1) |
| 58 | + } |
| 59 | + break |
| 60 | + case 'client': |
| 61 | + label = filter.value |
| 62 | + break |
| 63 | + default: |
| 64 | + label = t(`standalone.real_time_monitor.${filter.key}`) |
| 65 | + } |
| 66 | + return { |
| 67 | + key: filter.key as string, |
| 68 | + order: order++, |
| 69 | + label: label |
| 70 | + } |
| 71 | + }) |
| 72 | + .concat(defaultItem) |
| 73 | +}) |
| 74 | +
|
| 75 | +function handleBreadcrumbClick(item: BreadcrumbItem) { |
| 76 | + filters.remove( |
| 77 | + filtersToBreadcrumb.value |
| 78 | + .filter((breadcrumbItem) => breadcrumbItem.order > item.order) |
| 79 | + .map((breadcrumbItem) => breadcrumbItem.key) as AvailableFilters[] |
| 80 | + ) |
| 81 | +} |
| 82 | +
|
| 83 | +const resolvedHostname = computed( |
| 84 | + (): string | undefined => |
| 85 | + data.value.clients.find((client) => client.id == filters.get('client'))?.label ?? undefined |
| 86 | +) |
| 87 | +
|
| 88 | +const applicationName = computed( |
| 89 | + (): string | undefined => |
| 90 | + data.value.applications.find((app) => app.id == filters.get('app'))?.label ?? undefined |
| 91 | +) |
| 92 | +
|
| 93 | +const hoursLabels = computed(() => { |
| 94 | + return data.value.hourly_traffic.map((value) => value.id) |
| 95 | +}) |
| 96 | +
|
| 97 | +const hoursDatasets = computed(() => { |
| 98 | + return [ |
| 99 | + { |
| 100 | + label: t('standalone.real_time_monitor.traffic'), |
| 101 | + backgroundColor: themeStore.isLight ? CYAN_600 : CYAN_500, |
| 102 | + borderColor: themeStore.isLight ? CYAN_600 : CYAN_500, |
| 103 | + borderRadius: 6, |
| 104 | + borderWidth: 1, |
| 105 | + radius: 0, |
| 106 | + data: data.value.hourly_traffic.map((value) => value.traffic) |
| 107 | + } |
| 108 | + ] |
| 109 | +}) |
| 110 | +</script> |
| 111 | + |
| 112 | +<template> |
| 113 | + <div class="space-y-6"> |
| 114 | + <NeBreadcrumbs :items="filtersToBreadcrumb" @click="handleBreadcrumbClick" /> |
| 115 | + <NeSkeleton v-if="loading" :lines="10" /> |
| 116 | + <NeInlineNotification |
| 117 | + v-else-if="error" |
| 118 | + :description="t(getAxiosErrorMessage(error.message))" |
| 119 | + :title="t('standalone.real_time_monitor.error_fetching_data')" |
| 120 | + kind="error" |
| 121 | + /> |
| 122 | + <template v-else> |
| 123 | + <div class="flex flex-col gap-6 xl:flex-row"> |
| 124 | + <div class="flex flex-col gap-6 sm:w-96"> |
| 125 | + <NeCard v-if="filters.contains('client')" :title="t('standalone.dashboard.hostname')"> |
| 126 | + <SimpleStat> |
| 127 | + <p v-if="resolvedHostname != filters.get('client')">{{ resolvedHostname }}</p> |
| 128 | + <p class="[&:nth-child(2)]:text-lg">{{ filters.get('client') }}</p> |
| 129 | + </SimpleStat> |
| 130 | + </NeCard> |
| 131 | + <NeCard :title="t('standalone.real_time_monitor.daily_total_traffic')"> |
| 132 | + <SimpleStat> |
| 133 | + <span v-if="data.total_traffic != 0">{{ byteFormat1024(data.total_traffic) }}</span> |
| 134 | + <span v-else> N/A </span> |
| 135 | + </SimpleStat> |
| 136 | + </NeCard> |
| 137 | + <NeCard v-if="applicationName" :title="t('standalone.real_time_monitor.application')"> |
| 138 | + <SimpleStat> {{ applicationName }}</SimpleStat> |
| 139 | + </NeCard> |
| 140 | + </div> |
| 141 | + <NeCard |
| 142 | + :title="t('standalone.real_time_monitor.recent_traffic')" |
| 143 | + class="flex-1 justify-self-start" |
| 144 | + > |
| 145 | + <TrafficByHourChart |
| 146 | + v-if="data.hourly_traffic.length > 0" |
| 147 | + :datasets="hoursDatasets" |
| 148 | + :labels="hoursLabels" |
| 149 | + height="25vh" |
| 150 | + /> |
| 151 | + <NeEmptyState |
| 152 | + v-else |
| 153 | + :icon="faEmptySet" |
| 154 | + :title="t('standalone.real_time_monitor.no_data_available')" |
| 155 | + /> |
| 156 | + </NeCard> |
| 157 | + </div> |
| 158 | + <div class="grid gap-6 xl:grid-cols-2"> |
| 159 | + <TrafficCard |
| 160 | + v-if="filters.misses('client')" |
| 161 | + :data="data.clients" |
| 162 | + :title="t('standalone.real_time_monitor.local_hosts')" |
| 163 | + filterable |
| 164 | + filterable-key="client" |
| 165 | + /> |
| 166 | + <TrafficCard |
| 167 | + v-if="filters.misses('app')" |
| 168 | + :data="data.applications" |
| 169 | + :title="t('standalone.real_time_monitor.applications')" |
| 170 | + filterable |
| 171 | + filterable-key="app" |
| 172 | + /> |
| 173 | + <TrafficCard |
| 174 | + :data="data.remote_hosts" |
| 175 | + :title="t('standalone.real_time_monitor.remote_hosts')" |
| 176 | + /> |
| 177 | + <TrafficCard :data="data.protocols" :title="t('standalone.real_time_monitor.protocols')" /> |
| 178 | + </div> |
| 179 | + </template> |
| 180 | + </div> |
| 181 | +</template> |
0 commit comments