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          EfficiencyRatings = [],
16          FirmwareVersion,
17          LocationIndicatorActive,
18          Id,
19          Manufacturer,
20          Model,
21          Name,
22          PartNumber,
23          PowerInputWatts,
24          SerialNumber,
25          SparePartNumber,
26          Location,
27          Status = {},
28        } = powerSupply;
29        return {
30          id: Id,
31          health: Status.Health,
32          partNumber: PartNumber,
33          serialNumber: SerialNumber,
34          efficiencyPercent: EfficiencyRatings[0].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}/PowerSubsystem`)
74        .then((response) => {
75          return api.get(`${response.data.PowerSupplies['@odata.id']}`);
76        })
77        .then(({ data: { Members } }) => {
78          const promises = Members.map((member) =>
79            api.get(member['@odata.id']),
80          );
81          return api.all(promises);
82        })
83        .then((response) => {
84          const data = response.map(({ data }) => data);
85          return data;
86        })
87        .catch((error) => console.log(error));
88    },
89  },
90};
91
92export default PowerSupplyStore;
93