1import api from '@/store/api';
2
3const PowerSupplyStore = {
4  namespaced: true,
5  state: {
6    powerSupplies: [],
7  },
8  getters: {
9    powerSupplies: (state) => state.powerSupplies,
10  },
11  mutations: {
12    setPowerSupply: (state, data) => {
13      state.powerSupplies = data.map((powerSupply) => {
14        const {
15          EfficiencyPercent,
16          FirmwareVersion,
17          LocationIndicatorActive,
18          MemberId,
19          Manufacturer,
20          Model,
21          Name,
22          PartNumber,
23          PowerInputWatts,
24          SerialNumber,
25          SparePartNumber,
26          Location,
27          Status = {},
28        } = powerSupply;
29        return {
30          id: MemberId,
31          health: Status.Health,
32          partNumber: PartNumber,
33          serialNumber: SerialNumber,
34          efficiencyPercent: EfficiencyPercent,
35          firmwareVersion: FirmwareVersion,
36          identifyLed: LocationIndicatorActive,
37          manufacturer: Manufacturer,
38          model: Model,
39          powerInputWatts: PowerInputWatts,
40          name: Name,
41          sparePartNumber: SparePartNumber,
42          locationNumber: Location?.PartLocation?.ServiceLabel,
43          statusState: Status.State,
44        };
45      });
46    },
47  },
48  actions: {
49    async getChassisCollection() {
50      return await api
51        .get('/redfish/v1/Chassis')
52        .then(({ data: { Members } }) =>
53          Members.map((member) => member['@odata.id'])
54        )
55        .catch((error) => console.log(error));
56    },
57    async getAllPowerSupplies({ dispatch, commit }) {
58      const collection = await dispatch('getChassisCollection');
59      if (!collection) return;
60      return await api
61        .all(collection.map((chassis) => dispatch('getChassisPower', chassis)))
62        .then((supplies) => {
63          let suppliesList = [];
64          supplies.forEach(
65            (supply) => (suppliesList = [...suppliesList, ...supply])
66          );
67          commit('setPowerSupply', suppliesList);
68        })
69        .catch((error) => console.log(error));
70    },
71    async getChassisPower(_, id) {
72      return await api
73        .get(`${id}/Power`)
74        .then(({ data: { PowerSupplies } }) => PowerSupplies || [])
75        .catch((error) => console.log(error));
76    },
77  },
78};
79
80export default PowerSupplyStore;
81