|
| 1 | +import { useTypedFetchFromServiceControl } from "@/composables/serviceServiceControlUrls"; |
| 2 | +import { acceptHMRUpdate, defineStore } from "pinia"; |
| 3 | +import { ref, watch } from "vue"; |
| 4 | +import useAutoRefresh from "@/composables/autoRefresh"; |
| 5 | +//import { SortDirection, type GroupPropertyType } from "@/resources/SortOptions"; |
| 6 | +import type { SortInfo } from "@/components/SortInfo"; |
| 7 | +import Message from "@/resources/Message"; |
| 8 | + |
| 9 | +export enum ColumnNames { |
| 10 | + Status = "status", |
| 11 | + MessageId = "messageId", |
| 12 | + MessageType = "messageType", |
| 13 | + TimeSent = "timeSent", |
| 14 | + ProcessingTime = "processingTime", |
| 15 | +} |
| 16 | + |
| 17 | +// const columnSortings = new Map<string, (endpoint: Message) => GroupPropertyType>([ |
| 18 | +// // [ColumnNames.Name, (endpoint) => endpoint.name], |
| 19 | +// // [ColumnNames.InstancesDown, (endpoint) => endpoint.alive_count - endpoint.down_count], |
| 20 | +// // [ColumnNames.InstancesTotal, (endpoint) => endpoint.alive_count + endpoint.down_count], |
| 21 | +// // [ColumnNames.LastHeartbeat, (endpoint) => moment.utc(endpoint.heartbeat_information?.last_report_at ?? "1975-01-01T00:00:00")], |
| 22 | +// // [ColumnNames.Tracked, (endpoint) => endpoint.track_instances], |
| 23 | +// // [ColumnNames.TrackToggle, (endpoint) => endpoint.track_instances], |
| 24 | +// ]); |
| 25 | + |
| 26 | +export const useAuditStore = defineStore("AuditStore", () => { |
| 27 | + const sortByInstances = ref<SortInfo>({ |
| 28 | + property: ColumnNames.MessageId, |
| 29 | + isAscending: true, |
| 30 | + }); |
| 31 | + |
| 32 | + const messageFilterString = ref(""); |
| 33 | + const itemsPerPage = ref(20); |
| 34 | + const messages = ref<Message[]>([]); |
| 35 | + // const sortedMessages = computed<Message[]>(() => |
| 36 | + // [...messages.value].sort(getSortFunction(columnSortings.get(sortByInstances.value.property), sortByInstances.value.isAscending ? SortDirection.Ascending : SortDirection.Descending)) |
| 37 | + // ); |
| 38 | + // const filteredMessages = computed<Message[]>(() => sortedMessages.value.filter((message) => !messageFilterString.value || message.id.toLowerCase().includes(messageFilterString.value.toLowerCase()))); |
| 39 | + watch(messageFilterString, (newValue) => { |
| 40 | + setMessageFilterString(newValue); |
| 41 | + }); |
| 42 | + |
| 43 | + const dataRetriever = useAutoRefresh(async () => { |
| 44 | + try { |
| 45 | + const [, data] = await useTypedFetchFromServiceControl<Message[]>("messages"); |
| 46 | + messages.value = data; |
| 47 | + } catch (e) { |
| 48 | + messages.value = []; |
| 49 | + throw e; |
| 50 | + } |
| 51 | + }, 30000); |
| 52 | + |
| 53 | + function setMessageFilterString(filter: string) { |
| 54 | + messageFilterString.value = filter; |
| 55 | + } |
| 56 | + |
| 57 | + function setItemsPerPage(value: number) { |
| 58 | + itemsPerPage.value = value; |
| 59 | + } |
| 60 | + |
| 61 | + const refresh = dataRetriever.executeAndResetTimer; |
| 62 | + |
| 63 | + // eslint-disable-next-line promise/catch-or-return,promise/prefer-await-to-then,promise/valid-params |
| 64 | + refresh().then(); |
| 65 | + |
| 66 | + return { |
| 67 | + refresh, |
| 68 | + sortByInstances, |
| 69 | + messages, |
| 70 | + messageFilterString, |
| 71 | + itemsPerPage, |
| 72 | + setItemsPerPage, |
| 73 | + }; |
| 74 | +}); |
| 75 | + |
| 76 | +if (import.meta.hot) { |
| 77 | + import.meta.hot.accept(acceptHMRUpdate(useAuditStore, import.meta.hot)); |
| 78 | +} |
| 79 | + |
| 80 | +export type AuditStore = ReturnType<typeof useAuditStore>; |
0 commit comments