1import api from '@/store/api';
2
3const FanStore = {
4  namespaced: true,
5  state: {
6    fans: [],
7  },
8  getters: {
9    fans: (state) => state.fans,
10  },
11  mutations: {
12    setFanInfo: (state, data) => {
13      state.fans = data.map((fan) => {
14        const {
15          Id,
16          Name,
17          PartNumber,
18          SerialNumber,
19          SpeedPercent = {},
20          Status = {},
21        } = fan;
22        return {
23          id: Id,
24          health: Status.Health,
25          name: Name,
26          speed: SpeedPercent.Reading,
27          statusState: Status.State,
28          healthRollup: Status.HealthRollup,
29          partNumber: PartNumber,
30          serialNumber: SerialNumber,
31        };
32      });
33    },
34  },
35  actions: {
36    async getChassisCollection() {
37      return await api
38        .get('/redfish/v1/Chassis')
39        .then(({ data: { Members } }) =>
40          api.all(
41            Members.map((member) =>
42              api.get(member['@odata.id']).then((response) => response.data),
43            ),
44          ),
45        )
46        .catch((error) => console.log(error));
47    },
48    async getFanInfo({ dispatch, commit }) {
49      const collection = await dispatch('getChassisCollection');
50      if (!collection || collection.length === 0) return;
51      return await api
52        .all(collection.map((chassis) => dispatch('getChassisFans', chassis)))
53        .then((fansFromChassis) => commit('setFanInfo', fansFromChassis.flat()))
54        .catch((error) => console.log(error));
55    },
56    async getChassisFans(_, chassis) {
57      return await api
58        .get(chassis.ThermalSubsystem['@odata.id'])
59        .then((response) => {
60          return api.get(`${response.data.Fans['@odata.id']}`);
61        })
62        .then(({ data: { Members } }) => {
63          const promises = Members.map((member) =>
64            api.get(member['@odata.id']),
65          );
66          return api.all(promises);
67        })
68        .then((response) => {
69          const data = response.map(({ data }) => data);
70          return data;
71        })
72        .catch((error) => console.log(error));
73    },
74  },
75};
76
77export default FanStore;
78