1import api from '@/store/api';
2import i18n from '@/i18n';
3
4const ProcessorStore = {
5  namespaced: true,
6  state: {
7    processors: [],
8  },
9  getters: {
10    processors: (state) => state.processors,
11  },
12  mutations: {
13    setProcessorsInfo: (state, data) => {
14      state.processors = data.map((processor) => {
15        const {
16          Id,
17          Status = {},
18          PartNumber,
19          SerialNumber,
20          SparePartNumber,
21          InstructionSet,
22          Manufacturer,
23          Model,
24          Name,
25          ProcessorArchitecture,
26          ProcessorType,
27          Version,
28          AssetTag,
29          MinSpeedMHz,
30          MaxSpeedMHz,
31          TotalCores,
32          TotalThreads,
33          Location,
34          LocationIndicatorActive,
35        } = processor;
36        return {
37          id: Id,
38          health: Status.Health,
39          healthRollup: Status.HealthRollup,
40          partNumber: PartNumber,
41          sparePartNumber: SparePartNumber,
42          serialNumber: SerialNumber,
43          statusState: Status.State,
44          instructionSet: InstructionSet,
45          manufacturer: Manufacturer,
46          model: Model,
47          name: Name,
48          processorArchitecture: ProcessorArchitecture,
49          processorType: ProcessorType,
50          version: Version,
51          assetTag: AssetTag,
52          minSpeedMHz: MinSpeedMHz,
53          maxSpeedMHz: MaxSpeedMHz,
54          totalCores: TotalCores,
55          totalThreads: TotalThreads,
56          locationNumber: Location?.PartLocation?.ServiceLabel,
57          identifyLed: LocationIndicatorActive,
58          uri: processor['@odata.id'],
59        };
60      });
61    },
62  },
63  actions: {
64    async getProcessorsInfo({ commit }) {
65      return await api
66        .get(`${await this.dispatch('global/getSystemPath')}/Processors`)
67        .then(({ data: { Members = [] } }) =>
68          Members.map((member) => api.get(member['@odata.id'])),
69        )
70        .then((promises) => api.all(promises))
71        .then((response) => {
72          const data = response.map(({ data }) => data);
73          commit('setProcessorsInfo', data);
74        })
75        .catch((error) => console.log(error));
76    },
77    // Waiting for the following to be merged to test the Identify Led:
78    // https://gerrit.openbmc-project.xyz/c/openbmc/bmcweb/+/37045
79    async updateIdentifyLedValue({ dispatch }, led) {
80      const uri = led.uri;
81      const updatedIdentifyLedValue = {
82        LocationIndicatorActive: led.identifyLed,
83      };
84      return await api
85        .patch(uri, updatedIdentifyLedValue)
86        .then(() => {
87          if (led.identifyLed) {
88            return i18n.global.t(
89              'pageInventory.toast.successEnableIdentifyLed',
90            );
91          } else {
92            return i18n.global.t(
93              'pageInventory.toast.successDisableIdentifyLed',
94            );
95          }
96        })
97        .catch((error) => {
98          dispatch('getProcessorsInfo');
99          console.log('error', error);
100          if (led.identifyLed) {
101            throw new Error(
102              i18n.global.t('pageInventory.toast.errorEnableIdentifyLed'),
103            );
104          } else {
105            throw new Error(
106              i18n.global.t('pageInventory.toast.errorDisableIdentifyLed'),
107            );
108          }
109        });
110    },
111  },
112};
113
114export default ProcessorStore;
115