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          IndicatorLED,
16          Location,
17          MemberId,
18          Name,
19          Reading,
20          ReadingUnits,
21          Status = {},
22          PartNumber,
23          SerialNumber,
24        } = fan;
25        return {
26          id: MemberId,
27          health: Status.Health,
28          partNumber: PartNumber,
29          serialNumber: SerialNumber,
30          healthRollup: Status.HealthRollup,
31          identifyLed: IndicatorLED,
32          locationNumber: Location,
33          name: Name,
34          speed: Reading + ' ' + ReadingUnits,
35          statusState: Status.State,
36        };
37      });
38    },
39  },
40  actions: {
41    async getChassisCollection() {
42      return await api
43        .get('/redfish/v1/Chassis')
44        .then(({ data: { Members } }) =>
45          api.all(
46            Members.map((member) =>
47              api.get(member['@odata.id']).then((response) => response.data)
48            )
49          )
50        )
51        .catch((error) => console.log(error));
52    },
53    async getFanInfo({ dispatch, commit }) {
54      const collection = await dispatch('getChassisCollection');
55      if (!collection || collection.length === 0) return;
56      return await api
57        .all(collection.map((chassis) => dispatch('getChassisFans', chassis)))
58        .then((fansFromChassis) => commit('setFanInfo', fansFromChassis.flat()))
59        .catch((error) => console.log(error));
60    },
61    async getChassisFans(_, chassis) {
62      return await api
63        .get(chassis.Thermal['@odata.id'])
64        .then(({ data: { Fans } }) => Fans || [])
65        .catch((error) => console.log(error));
66    },
67  },
68};
69
70export default FanStore;
71