1import api, { getResponseCount } from '@/store/api';
2import i18n from '@/i18n';
3
4const DumpsStore = {
5  namespaced: true,
6  state: {
7    allDumps: [],
8  },
9  getters: {
10    allDumps: (state) => state.allDumps,
11  },
12  mutations: {
13    setBmcDumps: (state, dumps) => {
14      state.allDumps = dumps.map((dump) => ({
15        data: dump.AdditionalDataURI,
16        dateTime: new Date(dump.Created),
17        dumpType: dump.Name,
18        id: dump.Id,
19        location: dump['@odata.id'],
20        size: dump.AdditionalDataSizeBytes,
21      }));
22    },
23  },
24  actions: {
25    async getBmcDumpEntries({ commit }) {
26      return await api
27        .get('/redfish/v1/Managers/bmc/LogServices/Dump/Entries')
28        .then(({ data = {} }) => commit('setBmcDumps', data.Members || []))
29        .catch((error) => console.log(error));
30    },
31    async createBmcDump() {
32      return await api
33        .post(
34          '/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.CollectDiagnosticData',
35          {
36            DiagnosticDataType: 'Manager',
37            OEMDiagnosticDataType: '',
38          }
39        )
40        .catch((error) => {
41          console.log(error);
42          throw new Error(i18n.t('pageDumps.toast.errorStartBmcDump'));
43        });
44    },
45    async createSystemDump() {
46      return await api
47        .post(
48          '/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.CollectDiagnosticData',
49          {
50            DiagnosticDataType: 'OEM',
51            OEMDiagnosticDataType: 'System',
52          }
53        )
54        .catch((error) => {
55          console.log(error);
56          throw new Error(i18n.t('pageDumps.toast.errorStartSystemDump'));
57        });
58    },
59    async deleteDumps({ dispatch }, dumps) {
60      const promises = dumps.map(({ location }) =>
61        api.delete(location).catch((error) => {
62          console.log(error);
63          return error;
64        })
65      );
66      return await api
67        .all(promises)
68        .then((response) => {
69          dispatch('getBmcDumpEntries');
70          return response;
71        })
72        .then(
73          api.spread((...responses) => {
74            const { successCount, errorCount } = getResponseCount(responses);
75            const toastMessages = [];
76
77            if (successCount) {
78              const message = i18n.tc(
79                'pageDumps.toast.successDeleteDump',
80                successCount
81              );
82              toastMessages.push({ type: 'success', message });
83            }
84
85            if (errorCount) {
86              const message = i18n.tc(
87                'pageDumps.toast.errorDeleteDump',
88                errorCount
89              );
90              toastMessages.push({ type: 'error', message });
91            }
92
93            return toastMessages;
94          })
95        );
96    },
97    async deleteAllDumps({ commit, state }) {
98      const totalDumpCount = state.bmcDumps.length;
99      return await api
100        .post(
101          '/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.ClearLog'
102        )
103        .then(() => {
104          commit('setBmcDumps', []);
105          return i18n.tc('pageDumps.toast.successDeleteDump', totalDumpCount);
106        })
107        .catch((error) => {
108          console.log(error);
109          throw new Error(
110            i18n.tc('pageDumps.toast.errorDeleteDump', totalDumpCount)
111          );
112        });
113    },
114  },
115};
116
117export default DumpsStore;
118