Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(monitoring): added filtering in monitoring section #452

Merged
merged 2 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions src/components/standalone/monitoring/TrafficCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,25 @@ import { useThemeStore } from '@/stores/theme'
import TrafficTable from '@/components/standalone/monitoring/TrafficTable.vue'
import { useI18n } from 'vue-i18n'
import { faEmptySet } from '@nethesis/nethesis-solid-svg-icons'
import type { AvailableFilters } from '@/composables/useTrafficFilter'

const themeStore = useThemeStore()

const { t } = useI18n()

const { title, data } = defineProps<{
const {
title,
data,
filterable = false
} = defineProps<{
title: string
data: TrafficRecord[]
data?: TrafficRecord[]
filterable?: boolean
filterableKey?: AvailableFilters
}>()

const dataLimited = computed(() => {
return data.slice(0, 5) ?? []
return data?.slice(0, 5) ?? []
})

const pieDatasets = computed(() => {
Expand Down Expand Up @@ -85,10 +92,15 @@ const pieLabels = computed(() => {
</script>

<template>
<NeCard :title="title">
<div v-if="data.length > 0" class="space-y-4">
<NeCard v-if="data" :title="title">
<div v-if="data.length > 0" class="space-y-6">
<BasicPieChart :datasets="pieDatasets" :labels="pieLabels" byteFormat height="25vh" />
<TrafficTable :title="title" :traffic-entries="data" />
<TrafficTable
:filterable="filterable"
:filterable-key="filterableKey"
:title="title"
:traffic-entries="data"
/>
</div>
<NeEmptyState
v-else
Expand Down
158 changes: 129 additions & 29 deletions src/components/standalone/monitoring/TrafficMonitor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,94 @@ import {
} from '@nethesis/vue-components'
import SimpleStat from '@/components/charts/SimpleStat.vue'
import TrafficByHourChart from '@/components/standalone/monitoring/TrafficByHourChart.vue'
import { computed } from 'vue'
import { computed, watchEffect } from 'vue'
import { CYAN_500, CYAN_600 } from '@/lib/color'
import { useThemeStore } from '@/stores/theme'
import { useTrafficStats } from '@/composables/useTrafficStats'
import TrafficCard from '@/components/standalone/monitoring/TrafficCard.vue'
import { faEmptySet } from '@nethesis/nethesis-solid-svg-icons'
import { type AvailableFilters, useTrafficFilter } from '@/composables/useTrafficFilter'
import NeBreadcrumbs, { type BreadcrumbItem } from '@/components/NeBreadcrumbs.vue'

const { t } = useI18n()
type FilterableBreadcrumbItem = BreadcrumbItem & {
key: string
}

const { loading, error, data } = useTrafficStats()
const themeStore = useThemeStore()
const { get, list, remove, misses, contains } = useTrafficFilter()
const { data, error, loading, loadData } = useTrafficStats()

watchEffect(() => {
loadData(get('client'), get('app'), get('protocol'), get('host'))
})

const { t } = useI18n()

const filtersToBreadcrumb = computed<FilterableBreadcrumbItem[]>(() => {
let order = 1
// add a default item to the breadcrumb
const defaultItem: FilterableBreadcrumbItem = {
key: 'default',
order: order++,
label: t('standalone.real_time_monitor.traffic')
}
return list.value
.map((filter) => {
let label = ''
switch (filter.key) {
case 'app':
if (filter.value.startsWith('netify.')) {
label = filter.value.slice(7)
} else {
label = filter.value
}
if (label == 'unknown') {
label = t('common.unknown')
} else {
label = label.charAt(0).toUpperCase() + label.slice(1)
}
break
case 'client':
label = filter.value
break
case 'host':
label = filter.value
break
case 'protocol':
label = filter.value.toUpperCase()
break
default:
label = t(`standalone.real_time_monitor.${filter.key}`)
}
return {
key: filter.key as string,
order: order++,
label: label
}
})
.concat(defaultItem)
})

function handleBreadcrumbClick(item: BreadcrumbItem) {
remove(
filtersToBreadcrumb.value
.filter((breadcrumbItem) => breadcrumbItem.order > item.order)
.map((breadcrumbItem) => breadcrumbItem.key) as AvailableFilters[]
)
}

const resolvedHostname = computed(
(): string | undefined =>
data.value.clients.find((client) => client.id == get('client'))?.label ?? undefined
)

const applicationName = computed(
(): string | undefined =>
data.value.applications?.find((app) => app.id == get('app'))?.label ?? undefined
)

const hoursLabels = computed(() => {
return data.value?.hourly_traffic.map((value) => value.id) ?? []
return data.value.hourly_traffic.map((value) => value.id)
})

const hoursDatasets = computed(() => {
Expand All @@ -40,14 +114,19 @@ const hoursDatasets = computed(() => {
borderRadius: 6,
borderWidth: 1,
radius: 0,
data: data.value?.hourly_traffic.map((value) => value.traffic)
data: data.value.hourly_traffic.map((value) => value.traffic)
}
]
})
</script>

<template>
<div class="space-y-12">
<div class="space-y-6">
<NeBreadcrumbs
v-if="filtersToBreadcrumb.length > 1"
:items="filtersToBreadcrumb"
@click="handleBreadcrumbClick"
/>
<NeSkeleton v-if="loading" :lines="10" />
<NeInlineNotification
v-else-if="error"
Expand All @@ -56,51 +135,72 @@ const hoursDatasets = computed(() => {
kind="error"
/>
<template v-else>
<div class="grid grid-cols-1 gap-x-6 gap-y-6 sm:grid-cols-12">
<NeCard
:title="t('standalone.real_time_monitor.daily_total_traffic')"
class="sm:col-span-6 md:col-span-6 lg:col-span-4 xl:col-span-3 2xl:col-span-3"
>
<SimpleStat>
<span v-if="data?.total_traffic != 0">
{{ byteFormat1024(data?.total_traffic) }}
</span>
<span v-else> N/A </span>
</SimpleStat>
</NeCard>
<NeCard
:title="t('standalone.real_time_monitor.recent_traffic')"
class="row-span-2 sm:col-span-12 xl:col-span-9 2xl:col-span-9"
>
<div class="flex flex-col gap-6 xl:flex-row">
<div class="flex flex-col gap-6 sm:w-96">
<NeCard :title="t('standalone.real_time_monitor.daily_total_traffic')">
<SimpleStat>
<span v-if="data.total_traffic != 0">{{ byteFormat1024(data.total_traffic) }}</span>
<span v-else> N/A </span>
</SimpleStat>
</NeCard>
<NeCard v-if="contains('client')" :title="t('standalone.dashboard.hostname')">
<SimpleStat>
<p v-if="resolvedHostname != get('client')">{{ resolvedHostname }}</p>
<p class="[&:nth-child(2)]:text-lg">{{ get('client') }}</p>
</SimpleStat>
</NeCard>
<NeCard v-if="applicationName" :title="t('standalone.real_time_monitor.application')">
<SimpleStat> {{ applicationName }}</SimpleStat>
</NeCard>
<NeCard v-if="contains('protocol')" :title="t('standalone.real_time_monitor.protocol')">
<SimpleStat> {{ get('protocol').toUpperCase() }}</SimpleStat>
</NeCard>
<NeCard v-if="contains('host')" :title="t('standalone.real_time_monitor.remote_hosts')">
<SimpleStat> {{ get('host') }}</SimpleStat>
</NeCard>
</div>
<NeCard :title="t('standalone.real_time_monitor.recent_traffic')" class="flex-1 self-start">
<TrafficByHourChart
v-if="data?.hourly_traffic.length ?? 0 > 0"
v-if="data.hourly_traffic.length > 0"
:datasets="hoursDatasets"
:labels="hoursLabels"
height="25vh"
/>
<NeEmptyState
v-else
:title="t('standalone.real_time_monitor.no_data_available')"
:icon="faEmptySet"
:title="t('standalone.real_time_monitor.no_data_available')"
/>
</NeCard>
</div>
<div class="grid grid-cols-1 gap-6 xl:grid-cols-2">
<div class="grid gap-6 xl:grid-cols-2">
<TrafficCard
v-if="misses('client')"
:data="data.clients"
:title="t('standalone.real_time_monitor.local_hosts')"
:data="data?.clients ?? []"
filterable
filterable-key="client"
/>
<TrafficCard
v-if="misses('app')"
:data="data.applications"
:title="t('standalone.real_time_monitor.applications')"
:data="data?.applications ?? []"
filterable
filterable-key="app"
/>
<TrafficCard
v-if="misses('host')"
:data="data.remote_hosts"
:title="t('standalone.real_time_monitor.remote_hosts')"
:data="data?.remote_hosts ?? []"
filterable
filterable-key="host"
/>
<TrafficCard
v-if="misses('protocol')"
:data="data.protocols"
:title="t('standalone.real_time_monitor.protocols')"
:data="data?.protocols ?? []"
filterable
filterable-key="protocol"
/>
</div>
</template>
Expand Down
27 changes: 23 additions & 4 deletions src/components/standalone/monitoring/TrafficTable.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts" setup>
import { faSearch } from '@fortawesome/free-solid-svg-icons'
import { faSearch, faTable } from '@fortawesome/free-solid-svg-icons'
import {
byteFormat1024,
NeEmptyState,
Expand All @@ -11,21 +11,27 @@ import {
NeTableHeadCell,
NeTableRow,
NeTextInput,
useItemPagination
useItemPagination,
NeLink
} from '@nethesis/vue-components'
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'
import { computed, ref } from 'vue'
import { refDebounced } from '@vueuse/core'
import { useI18n } from 'vue-i18n'
import type { TrafficRecord } from '@/composables/useTrafficStats'
import { type AvailableFilters, useTrafficFilter } from '@/composables/useTrafficFilter'

const { trafficEntries, title } = defineProps<{
trafficEntries: TrafficRecord[]
title: string
filterable?: boolean
filterableKey?: AvailableFilters
}>()

const { t } = useI18n()

const filters = useTrafficFilter()

const filter = ref('')
const filterDebounced = refDebounced(filter, 400)

Expand Down Expand Up @@ -59,15 +65,28 @@ const { currentPage, paginatedItems } = useItemPagination(() => trafficEntriesFi
<NeTableRow v-if="!paginatedItems.length">
<NeTableCell colspan="9">
<NeEmptyState
:icon="['fas', 'table']"
:icon="faTable"
:title="t('common.no_data_available')"
class="bg-white dark:bg-gray-950"
/>
</NeTableCell>
</NeTableRow>
<NeTableRow v-for="(item, index) in paginatedItems as TrafficRecord[]" v-else :key="index">
<NeTableCell :data-label="title">
{{ item?.label ?? item.id }}
<template v-if="filterable">
<NeLink
@click="
filters.push({
key: filterableKey!,
value: item.id
})
"
>{{ item?.label ?? item.id }}</NeLink
>
</template>
<template v-else>
{{ item?.label ?? item.id }}
</template>
</NeTableCell>
<NeTableCell :data-label="t('standalone.real_time_monitor.traffic')">
{{ byteFormat1024(item.traffic) }}
Expand Down
59 changes: 59 additions & 0 deletions src/composables/useTrafficFilter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { useRoute, useRouter } from 'vue-router'
import { computed } from 'vue'

const FILTERS = ['client', 'app', 'protocol', 'host']
export type AvailableFilters = 'client' | 'app' | 'protocol' | 'host'

export type Filter = {
key: AvailableFilters
value: string
}

export function useTrafficFilter() {
const route = useRoute()
const router = useRouter()

const list = computed<Filter[]>(() => {
return Object.keys(route.query)
.filter((key) => FILTERS.includes(key))
.map((key) => ({ key: key as AvailableFilters, value: route.query[key] as string }))
})

const active = computed<boolean>(() => list.value.length > 0)

function remove(keys: AvailableFilters | AvailableFilters[]) {
if (!Array.isArray(keys)) {
keys = [keys]
}
const query = { ...route.query }
keys.forEach((key) => delete query[key])
router.push({ query })
}

function push(filter: Filter) {
const query = { ...route.query, [filter.key]: filter.value }
router.push({ query })
}

function contains(string: AvailableFilters): boolean {
return route.query[string] != undefined
}

function misses(string: AvailableFilters): boolean {
return !contains(string)
}

function get(key: AvailableFilters): string {
return route.query[key] as string
}

return {
list,
active,
remove,
push,
contains,
get,
misses
}
}
Loading