| 1 | <template> |
| 2 | <n-spin :show="loading"> |
| 3 | <CardStatsMulti title="Agents" hovered class="h-full cursor-pointer" :values @click="routeAgent().navigate()"> |
| 4 | <template #icon> |
| 5 | <CardStatsIcon :icon-name="AgentsIcon" boxed :box-size="30"></CardStatsIcon> |
| 6 | </template> |
| 7 | </CardStatsMulti> |
| 8 | </n-spin> |
| 9 | </template> |
| 10 | |
| 11 | <script setup lang="ts"> |
| 12 | import type { ItemProps } from "@/components/common/cards/CardStatsMulti.vue" |
| 13 | import type { Agent } from "@/types/agents.d" |
| 14 | import { NSpin, useMessage } from "naive-ui" |
| 15 | import { computed, onBeforeMount, ref } from "vue" |
| 16 | import Api from "@/api" |
| 17 | import CardStatsIcon from "@/components/common/cards/CardStatsIcon.vue" |
| 18 | import CardStatsMulti from "@/components/common/cards/CardStatsMulti.vue" |
| 19 | import { useNavigation } from "@/composables/useNavigation" |
| 20 | import { AgentStatus } from "@/types/agents.d" |
| 21 | |
| 22 | const AgentsIcon = "carbon:network-3" |
| 23 | const { routeAgent } = useNavigation() |
| 24 | const message = useMessage() |
| 25 | const loading = ref(false) |
| 26 | const agents = ref<Agent[]>([]) |
| 27 | const total = computed<number>(() => { |
| 28 | return agents.value.length || 0 |
| 29 | }) |
| 30 | const onlineTotal = computed(() => { |
| 31 | return agents.value.filter(({ wazuh_agent_status }) => wazuh_agent_status === AgentStatus.Active).length || 0 |
| 32 | }) |
| 33 | const values = computed<ItemProps[]>(() => [ |
| 34 | { value: total.value, label: "Total" }, |
| 35 | { value: onlineTotal.value, label: "Online", status: onlineTotal.value ? "success" : undefined } |
| 36 | ]) |
| 37 | |
| 38 | function getData() { |
| 39 | loading.value = true |
| 40 | |
| 41 | Api.agents |
| 42 | .getAgents() |
| 43 | .then(res => { |
| 44 | if (res.data.success) { |
| 45 | agents.value = res.data.agents || [] |
| 46 | } else { |
| 47 | message.error(res.data?.message || "An error occurred. Please try again later.") |
| 48 | } |
| 49 | }) |
| 50 | .catch(err => { |
| 51 | message.error(err.response?.data?.message || "An error occurred. Please try again later.") |
| 52 | }) |
| 53 | .finally(() => { |
| 54 | loading.value = false |
| 55 | }) |
| 56 | } |
| 57 | |
| 58 | onBeforeMount(() => { |
| 59 | getData() |
| 60 | }) |
| 61 | </script> |
| 62 | |
| 63 | <style lang="scss" scoped> |
| 64 | .n-spin-container { |
| 65 | :deep() { |
| 66 | .n-spin-content { |
| 67 | height: 100%; |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | </style> |