refactor(editor): Migrate LogStreaming.store.ts to composition API (#10719)
Some checks are pending
Test Master / install-and-build (push) Waiting to run
Test Master / Unit tests (18.x) (push) Blocked by required conditions
Test Master / Unit tests (20.x) (push) Blocked by required conditions
Test Master / Unit tests (22.4) (push) Blocked by required conditions
Test Master / Lint (push) Blocked by required conditions
Test Master / Notify Slack on failure (push) Blocked by required conditions
Benchmark Docker Image CI / build (push) Waiting to run

This commit is contained in:
Ricardo Espinoza 2024-09-19 09:15:01 -04:00 committed by GitHub
parent f5474ff791
commit cee57b6504
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 212 additions and 190 deletions

View file

@ -7,8 +7,9 @@ import {
hasDestinationId, hasDestinationId,
saveDestinationToDb, saveDestinationToDb,
sendTestMessageToDestination, sendTestMessageToDestination,
} from '../api/eventbus.ee'; } from '@/api/eventbus.ee';
import { useRootStore } from './root.store'; import { useRootStore } from './root.store';
import { ref } from 'vue';
export interface EventSelectionItem { export interface EventSelectionItem {
selected: boolean; selected: boolean;
@ -32,221 +33,242 @@ export interface DestinationSettingsStore {
[key: string]: DestinationStoreItem; [key: string]: DestinationStoreItem;
} }
export const useLogStreamingStore = defineStore('logStreaming', { export const useLogStreamingStore = defineStore('logStreaming', () => {
state: () => ({ const items = ref<DestinationSettingsStore>({});
items: {} as DestinationSettingsStore, const eventNames = ref(new Set<string>());
eventNames: new Set<string>(),
}), const rootStore = useRootStore();
getters: {},
actions: { const addDestination = (destination: MessageEventBusDestinationOptions) => {
addDestination(destination: MessageEventBusDestinationOptions) { if (destination.id && items.value[destination.id]) {
if (destination.id && this.items[destination.id]) { items.value[destination.id].destination = destination;
this.items[destination.id].destination = destination; } else {
} else { setSelectionAndBuildItems(destination);
this.setSelectionAndBuildItems(destination); }
};
const setSelectionAndBuildItems = (destination: MessageEventBusDestinationOptions) => {
if (destination.id) {
if (!items.value[destination.id]) {
items.value[destination.id] = {
destination,
selectedEvents: new Set<string>(),
eventGroups: [],
isNew: false,
} as DestinationStoreItem;
} }
}, items.value[destination.id]?.selectedEvents?.clear();
getDestination(destinationId: string): MessageEventBusDestinationOptions | undefined { if (destination.subscribedEvents) {
if (this.items[destinationId]) { for (const eventName of destination.subscribedEvents) {
return this.items[destinationId].destination; items.value[destination.id]?.selectedEvents?.add(eventName);
} else { }
return;
} }
}, items.value[destination.id].eventGroups = eventGroupsFromStringList(
getAllDestinations(): MessageEventBusDestinationOptions[] { eventNames.value,
const destinations: MessageEventBusDestinationOptions[] = []; items.value[destination.id]?.selectedEvents,
for (const key of Object.keys(this.items)) { );
destinations.push(this.items[key].destination); }
} };
return destinations;
}, const getDestination = (destinationId: string) => {
updateDestination(destination: MessageEventBusDestinationOptions) { if (items.value[destinationId]) {
if (destination.id && this.items[destination.id]) { return items.value[destinationId].destination;
this.$patch((state) => { } else {
if (destination.id && this.items[destination.id]) { return;
state.items[destination.id].destination = destination; }
} };
// to trigger refresh
state.items = { ...state.items }; const getAllDestinations = () => {
}); const destinations: MessageEventBusDestinationOptions[] = [];
} for (const key of Object.keys(items)) {
}, destinations.push(items.value[key].destination);
removeDestination(destinationId: string) { }
if (!destinationId) return; return destinations;
delete this.items[destinationId]; };
if (this.items[destinationId]) {
this.$patch({ const clearDestinations = () => {
items: { items.value = {};
...this.items, };
},
}); const addEventName = (name: string) => {
} eventNames.value.add(name);
}, };
clearDestinations() {
this.items = {}; const removeEventName = (name: string) => {
}, eventNames.value.delete(name);
addEventName(name: string) { };
this.eventNames.add(name);
}, const clearEventNames = () => {
removeEventName(name: string) { eventNames.value.clear();
this.eventNames.delete(name); };
},
clearEventNames() { const addSelectedEvent = (id: string, name: string) => {
this.eventNames.clear(); items.value[id]?.selectedEvents?.add(name);
}, setSelectedInGroup(id, name, true);
addSelectedEvent(id: string, name: string) { };
this.items[id]?.selectedEvents?.add(name);
this.setSelectedInGroup(id, name, true); const removeSelectedEvent = (id: string, name: string) => {
}, items.value[id]?.selectedEvents?.delete(name);
removeSelectedEvent(id: string, name: string) { setSelectedInGroup(id, name, false);
this.items[id]?.selectedEvents?.delete(name); };
this.setSelectedInGroup(id, name, false);
}, const setSelectedInGroup = (destinationId: string, name: string, isSelected: boolean) => {
getSelectedEvents(destinationId: string): string[] { if (items.value[destinationId]) {
const selectedEvents: string[] = []; const groupName = eventGroupFromEventName(name);
if (this.items[destinationId]) { const groupIndex = items.value[destinationId].eventGroups.findIndex(
for (const group of this.items[destinationId].eventGroups) { (e) => e.name === groupName,
if (group.selected) { );
selectedEvents.push(group.name);
} if (groupIndex > -1) {
for (const event of group.children) { if (groupName === name) {
if (event.selected) { items.value[destinationId].eventGroups[groupIndex].selected = isSelected;
selectedEvents.push(event.name); } else {
const eventIndex = items.value[destinationId].eventGroups[groupIndex].children.findIndex(
(e) => e.name === name,
);
if (eventIndex > -1) {
items.value[destinationId].eventGroups[groupIndex].children[eventIndex].selected =
isSelected;
if (isSelected) {
items.value[destinationId].eventGroups[groupIndex].indeterminate = isSelected;
} else {
let anySelected = false;
for (
let i = 0;
i < items.value[destinationId].eventGroups[groupIndex].children.length;
i++
) {
anySelected =
anySelected ||
items.value[destinationId].eventGroups[groupIndex].children[i].selected;
}
items.value[destinationId].eventGroups[groupIndex].indeterminate = anySelected;
} }
} }
} }
} }
return selectedEvents; }
}, };
setSelectedInGroup(destinationId: string, name: string, isSelected: boolean) {
if (this.items[destinationId]) { const removeDestinationItemTree = (id: string) => {
const groupName = eventGroupFromEventName(name); delete items.value[id];
const groupIndex = this.items[destinationId].eventGroups.findIndex( };
(e) => e.name === groupName,
); const updateDestination = (destination: MessageEventBusDestinationOptions) => {
if (groupIndex > -1) { if (destination.id && items.value[destination.id]) {
if (groupName === name) { items.value[destination.id].destination = destination;
this.$patch((state) => { }
state.items[destinationId].eventGroups[groupIndex].selected = isSelected; };
});
} else { const removeDestination = (destinationId: string) => {
const eventIndex = this.items[destinationId].eventGroups[groupIndex].children.findIndex( if (!destinationId) return;
(e) => e.name === name, delete items.value[destinationId];
); };
if (eventIndex > -1) {
this.$patch((state) => { const getSelectedEvents = (destinationId: string): string[] => {
state.items[destinationId].eventGroups[groupIndex].children[eventIndex].selected = const selectedEvents: string[] = [];
isSelected; if (items.value[destinationId]) {
if (isSelected) { for (const group of items.value[destinationId].eventGroups) {
state.items[destinationId].eventGroups[groupIndex].indeterminate = isSelected; if (group.selected) {
} else { selectedEvents.push(group.name);
let anySelected = false; }
for ( for (const event of group.children) {
let i = 0; if (event.selected) {
i < state.items[destinationId].eventGroups[groupIndex].children.length; selectedEvents.push(event.name);
i++
) {
anySelected =
anySelected ||
state.items[destinationId].eventGroups[groupIndex].children[i].selected;
}
state.items[destinationId].eventGroups[groupIndex].indeterminate = anySelected;
}
});
}
} }
} }
} }
}, }
removeDestinationItemTree(id: string) { return selectedEvents;
delete this.items[id]; };
},
clearDestinationItemTrees() {
this.items = {} as DestinationSettingsStore;
},
setSelectionAndBuildItems(destination: MessageEventBusDestinationOptions) {
if (destination.id) {
if (!this.items[destination.id]) {
this.items[destination.id] = {
destination,
selectedEvents: new Set<string>(),
eventGroups: [],
isNew: false,
} as DestinationStoreItem;
}
this.items[destination.id]?.selectedEvents?.clear();
if (destination.subscribedEvents) {
for (const eventName of destination.subscribedEvents) {
this.items[destination.id]?.selectedEvents?.add(eventName);
}
}
this.items[destination.id].eventGroups = eventGroupsFromStringList(
this.eventNames,
this.items[destination.id]?.selectedEvents,
);
}
},
async saveDestination(destination: MessageEventBusDestinationOptions): Promise<boolean> {
if (!hasDestinationId(destination)) {
return false;
}
const rootStore = useRootStore(); const saveDestination = async (
const selectedEvents = this.getSelectedEvents(destination.id); destination: MessageEventBusDestinationOptions,
try { ): Promise<boolean> => {
await saveDestinationToDb(rootStore.restApiContext, destination, selectedEvents); if (!hasDestinationId(destination)) {
this.updateDestination(destination); return false;
return true; }
} catch (e) {
return false;
}
},
async sendTestMessage(destination: MessageEventBusDestinationOptions): Promise<boolean> {
if (!hasDestinationId(destination)) {
return false;
}
const rootStore = useRootStore(); const selectedEvents = getSelectedEvents(destination.id);
const testResult = await sendTestMessageToDestination(rootStore.restApiContext, destination); try {
return testResult; await saveDestinationToDb(rootStore.restApiContext, destination, selectedEvents);
}, updateDestination(destination);
async fetchEventNames(): Promise<string[]> { return true;
const rootStore = useRootStore(); } catch (e) {
return await getEventNamesFromBackend(rootStore.restApiContext); return false;
}, }
async fetchDestinations(): Promise<MessageEventBusDestinationOptions[]> { };
const rootStore = useRootStore();
return await getDestinationsFromBackend(rootStore.restApiContext); const sendTestMessage = async (
}, destination: MessageEventBusDestinationOptions,
async deleteDestination(destinationId: string) { ): Promise<boolean> => {
const rootStore = useRootStore(); if (!hasDestinationId(destination)) {
await deleteDestinationFromDb(rootStore.restApiContext, destinationId); return false;
this.removeDestination(destinationId); }
},
}, const testResult = await sendTestMessageToDestination(rootStore.restApiContext, destination);
return testResult;
};
const fetchEventNames = async () => {
return await getEventNamesFromBackend(rootStore.restApiContext);
};
const fetchDestinations = async (): Promise<MessageEventBusDestinationOptions[]> => {
return await getDestinationsFromBackend(rootStore.restApiContext);
};
const deleteDestination = async (destinationId: string) => {
await deleteDestinationFromDb(rootStore.restApiContext, destinationId);
removeDestination(destinationId);
};
return {
addDestination,
setSelectionAndBuildItems,
getDestination,
getAllDestinations,
clearDestinations,
addEventName,
removeEventName,
clearEventNames,
addSelectedEvent,
removeSelectedEvent,
setSelectedInGroup,
removeDestinationItemTree,
updateDestination,
removeDestination,
getSelectedEvents,
saveDestination,
sendTestMessage,
fetchEventNames,
fetchDestinations,
deleteDestination,
items,
};
}); });
export function eventGroupFromEventName(eventName: string): string | undefined { export const eventGroupFromEventName = (eventName: string): string | undefined => {
const matches = eventName.match(/^[\w\s]+\.[\w\s]+/); const matches = eventName.match(/^[\w\s]+\.[\w\s]+/);
if (matches && matches?.length > 0) { if (matches && matches?.length > 0) {
return matches[0]; return matches[0];
} }
return undefined; return undefined;
} };
function prettifyEventName(label: string, group = ''): string { const prettifyEventName = (label: string, group = ''): string => {
label = label.replace(group + '.', ''); label = label.replace(group + '.', '');
if (label.length > 0) { if (label.length > 0) {
label = label[0].toUpperCase() + label.substring(1); label = label[0].toUpperCase() + label.substring(1);
label = label.replaceAll('.', ' '); label = label.replaceAll('.', ' ');
} }
return label; return label;
} };
export function eventGroupsFromStringList( export const eventGroupsFromStringList = (
dottedList: Set<string>, dottedList: Set<string>,
selectionList: Set<string> = new Set(), selectionList: Set<string> = new Set(),
) { ) => {
const result = [] as EventSelectionGroup[]; const result = [] as EventSelectionGroup[];
const eventNameArray = Array.from(dottedList.values()); const eventNameArray = Array.from(dottedList.values());
@ -287,4 +309,4 @@ export function eventGroupsFromStringList(
result.push(collection); result.push(collection);
} }
return result; return result;
} };

View file

@ -95,7 +95,7 @@ export default defineComponent({
}, },
async getDestinationDataFromBackend(): Promise<void> { async getDestinationDataFromBackend(): Promise<void> {
this.logStreamingStore.clearEventNames(); this.logStreamingStore.clearEventNames();
this.logStreamingStore.clearDestinationItemTrees(); this.logStreamingStore.clearDestinations();
this.allDestinations = []; this.allDestinations = [];
const eventNamesData = await this.logStreamingStore.fetchEventNames(); const eventNamesData = await this.logStreamingStore.fetchEventNames();
if (eventNamesData) { if (eventNamesData) {