summaryrefslogtreecommitdiff
path: root/src/store/modules/HardwareStatus/PciStore.js
blob: 538b783f36ea80709ed77bc68e1cecfe8cd3807e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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;