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          Status = {},
27        } = powerSupply;
28        return {
29          id: MemberId,
30          health: Status.Health,
31          partNumber: PartNumber,
32          serialNumber: SerialNumber,
33          efficiencyPercent: EfficiencyPercent,
34          firmwareVersion: FirmwareVersion,
35          identifyLed: LocationIndicatorActive,
36          manufacturer: Manufacturer,
37          model: Model,
38          powerInputWatts: PowerInputWatts,
39          name: Name,
40          sparePartNumber: SparePartNumber,
41          statusState: Status.State,
42        };
43      });
44    },
45  },
46  actions: {
47    async getChassisCollection() {
48      return await api
49        .get('/redfish/v1/Chassis')
50        .then(({ data: { Members } }) =>
51          Members.map((member) => member['@odata.id'])
52        )
53        .catch((error) => console.log(error));
54    },
55    async getAllPowerSupplies({ dispatch, commit }) {
56      const collection = await dispatch('getChassisCollection');
57      if (!collection) return;
58      return await api
59        .all(collection.map((chassis) => dispatch('getChassisPower', chassis)))
60        .then((supplies) => {
61          let suppliesList = [];
62          supplies.forEach(
63            (supply) => (suppliesList = [...suppliesList, ...supply])
64          );
65          commit('setPowerSupply', suppliesList);
66        })
67        .catch((error) => console.log(error));
68    },
69    async getChassisPower(_, id) {
70      return await api
71        .get(`${id}/Power`)
72        .then(({ data: { PowerSupplies } }) => PowerSupplies || [])
73        .catch((error) => console.log(error));
74    },
75  },
76};
77
78export default PowerSupplyStore;
79