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    setAllDumps: (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() {
26      return api
27        .get(`${await this.dispatch('global/getBmcPath')}`)
28        .then((response) => api.get(response.data.LogServices['@odata.id']))
29        .then((response) => api.get(`${response.data['@odata.id']}/Dump`))
30        .then((response) => api.get(response.data.Entries['@odata.id']))
31        .catch((error) => console.log(error));
32    },
33    async getSystemDumpEntries() {
34      return api
35        .get(`${await this.dispatch('global/getSystemPath')}`)
36        .then((response) => api.get(response.data.LogServices['@odata.id']))
37        .then((response) => api.get(`${response.data['@odata.id']}/Dump`))
38        .then((response) => api.get(response.data.Entries['@odata.id']))
39        .catch((error) => console.log(error));
40    },
41    async getAllDumps({ commit, dispatch }) {
42      return await api
43        .all([dispatch('getBmcDumpEntries'), dispatch('getSystemDumpEntries')])
44        .then((response) => {
45          const bmcDumpEntries = response[0].data?.Members || [];
46          const systemDumpEntries = response[1].data?.Members || [];
47          const allDumps = [...bmcDumpEntries, ...systemDumpEntries];
48          commit('setAllDumps', allDumps);
49        })
50        .catch((error) => console.log(error));
51    },
52    async createBmcDump() {
53      return await api
54        .post(
55          `${await this.dispatch('global/getBmcPath')}/LogServices/Dump/Actions/LogService.CollectDiagnosticData`,
56          {
57            DiagnosticDataType: 'Manager',
58            OEMDiagnosticDataType: '',
59          },
60        )
61        .catch((error) => {
62          console.log(error);
63          throw new Error(i18n.t('pageDumps.toast.errorStartBmcDump'));
64        });
65    },
66    async createSystemDump() {
67      return await api
68        .post(
69          `${await this.dispatch('global/getSystemPath')}/LogServices/Dump/Actions/LogService.CollectDiagnosticData`,
70          {
71            DiagnosticDataType: 'OEM',
72            OEMDiagnosticDataType: 'System',
73          },
74        )
75        .catch((error) => {
76          console.log(error);
77          throw new Error(i18n.t('pageDumps.toast.errorStartSystemDump'));
78        });
79    },
80    async deleteDumps({ dispatch }, dumps) {
81      const promises = dumps.map(({ location }) =>
82        api.delete(location).catch((error) => {
83          console.log(error);
84          return error;
85        }),
86      );
87      return await api
88        .all(promises)
89        .then((response) => {
90          dispatch('getAllDumps');
91          return response;
92        })
93        .then(
94          api.spread((...responses) => {
95            const { successCount, errorCount } = getResponseCount(responses);
96            const toastMessages = [];
97
98            if (successCount) {
99              const message = i18n.tc(
100                'pageDumps.toast.successDeleteDump',
101                successCount,
102              );
103              toastMessages.push({ type: 'success', message });
104            }
105
106            if (errorCount) {
107              const message = i18n.tc(
108                'pageDumps.toast.errorDeleteDump',
109                errorCount,
110              );
111              toastMessages.push({ type: 'error', message });
112            }
113
114            return toastMessages;
115          }),
116        );
117    },
118    async deleteAllDumps({ commit, state }) {
119      const totalDumpCount = state.allDumps.length;
120      return await api
121        .post(
122          `${await this.dispatch('global/getBmcPath')}/LogServices/Dump/Actions/LogService.ClearLog`,
123        )
124        .then(() => {
125          commit('setAllDumps', []);
126          return i18n.tc('pageDumps.toast.successDeleteDump', totalDumpCount);
127        })
128        .catch((error) => {
129          console.log(error);
130          throw new Error(
131            i18n.tc('pageDumps.toast.errorDeleteDump', totalDumpCount),
132          );
133        });
134    },
135  },
136};
137
138export default DumpsStore;
139