main
ts 103 lines 3.04 KB
Raw
1 import type { Notification } from "./useNotifications"
2 import _capitalize from "lodash/capitalize"
3 import { computed, watch } from "vue"
4 import { useHealthcheckStore } from "@/stores/healthcheck"
5 import { IndexHealth } from "@/types/indices.d"
6 import { useNavigation } from "./useNavigation"
7 import { useNotifications } from "./useNotifications"
8
9 export function useHealthchecksNotify() {
10 return {
11 init: () => {
12 const { routeHealthcheck, routeIndex, routeGraylogMetrics } = useNavigation()
13
14 const uncommittedJournalEntriesThreshold = useHealthcheckStore().uncommittedJournalEntriesThreshold
15 const uncommittedJournalEntries = computed(() => useHealthcheckStore().uncommittedJournalEntries)
16 const clusterName = computed(() => useHealthcheckStore().clusterName)
17 const clusterStatus = computed(() => useHealthcheckStore().clusterStatus)
18 const alerts = computed(() => useHealthcheckStore().alerts)
19
20 useHealthcheckStore().start()
21
22 watch(uncommittedJournalEntries, (val, old) => {
23 if (val !== old) {
24 const obj: Notification = {
25 id: "uncommittedJournalEntries",
26 category: "alert",
27 type: "error",
28 title: "Error check",
29 description: "Uncommitted Journal Entries",
30 read: false,
31 date: new Date(),
32 action() {
33 routeGraylogMetrics().navigate()
34 },
35 actionTitle: "See Graylog Metrics"
36 }
37
38 if (val !== null && val >= uncommittedJournalEntriesThreshold) {
39 obj.type = "warning"
40 obj.title = "Uncommitted Journal Entries"
41 obj.description = `Value ${val} (over ${uncommittedJournalEntriesThreshold})`
42 }
43
44 useNotifications().prepend(obj, { autoNotify: true })
45 }
46 })
47
48 watch(clusterStatus, (val, old) => {
49 if (val !== old) {
50 const obj: Notification = {
51 id: "clusterHealth",
52 category: "alert",
53 type: "error",
54 title: "Error check",
55 description: "Cluster Health",
56 read: false,
57 date: new Date(),
58 action() {
59 routeIndex().navigate()
60 },
61 actionTitle: "See Cluster"
62 }
63
64 if (val !== null && val !== IndexHealth.GREEN) {
65 obj.type = val === IndexHealth.YELLOW ? "warning" : "error"
66 obj.title = "Cluster Health"
67 obj.description = `${_capitalize(clusterName.value || "Cluster")} is ${val.toUpperCase()}`
68 }
69
70 useNotifications().prepend(obj, { autoNotify: true })
71 }
72 })
73
74 watch(
75 alerts,
76 (val, old) => {
77 if (JSON.stringify(val) !== JSON.stringify(old)) {
78 if (val !== null && val.length) {
79 const obj: Notification = {
80 id: "influxDBAlert",
81 category: "alert",
82 type: "warning",
83 title: "Influx Alert",
84 description: `${val.length} Critical ${val.length > 1 ? "issues" : "issue"}`,
85 read: false,
86 date: new Date(),
87 action() {
88 routeHealthcheck().navigate()
89 },
90 actionTitle: "See Healthcheck"
91 }
92
93 useNotifications().prepend(obj, { autoNotify: true })
94 } else {
95 useNotifications().deleteOne("influxDBAlert")
96 }
97 }
98 },
99 { deep: true }
100 )
101 }
102 }
103 }