import api from '@/store/api'; const PciStore = { namespaced: true, state: { pci: [], }, getters: { pciDevices: (state) => state.pci, }, mutations: { setPciDevises: (state, data) => { state.pci = data.map((item) => { const { value: { data: { Id, Name, Manufacturer, DeviceType, PCIeFunctions }, }, } = item; return { id: Id, name: Name, DeviceType: DeviceType, manufacturer: Manufacturer, PCIeFunction: PCIeFunctions, }; }); }, setPciDevisesMembers: (state, data) => { state.pci = data.map((item, index) => { const { value: { data: { Members }, }, } = item; return { ...state.pci[index], Members: Members, }; }); }, setFunctionDevices: (state, data) => { let count = 0; const newData = state.pci.map((item) => { const result = data.slice(count, count + item.Members.length); count += item.Members.length; return result; }); state.pci = newData.map((item, index) => { const functions = item.map((item) => { const { value: { data: { ClassCode, DeviceClass, DeviceId, FunctionType, DeviceName, }, }, } = item; return { classCode: ClassCode, deviceClass: DeviceClass, deviceId: DeviceId, functionType: FunctionType, DeviceName: DeviceName, }; }); if (item.length > 1) { return { ...state.pci[index], Functions: functions, }; } return { ...state.pci[index], ...functions[0], }; }); }, }, actions: { async getDevices({ commit }) { return await api .get('/redfish/v1/Systems/system/PCIeDevices') .then(({ data: { Members = [] } }) => { const devices = Members.map((item) => api.get(item['@odata.id'])); return Promise.allSettled(devices); }) .then((response) => { commit('setPciDevises', response); }) .catch((error) => console.log(error)); }, async getDevicesMembers({ commit, state }) { const Members = state.pci.map((item) => api.get(item.PCIeFunction['@odata.id']) ); return Promise.allSettled(Members) .then((response) => { commit('setPciDevisesMembers', response); const Functions = response .map((item) => item.value.data.Members) .flat(Infinity) .map((item) => api.get(item['@odata.id'])); return Promise.allSettled(Functions); }) .then((response) => commit('setFunctionDevices', response)) .catch((error) => console.log(error)); }, }, }; export default PciStore;