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.global.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(
78            i18n.global.t('pageDumps.toast.errorStartSystemDump'),
79          );
80        });
81    },
82    async deleteDumps({ dispatch }, dumps) {
83      const promises = dumps.map(({ location }) =>
84        api.delete(location).catch((error) => {
85          console.log(error);
86          return error;
87        }),
88      );
89      return await api
90        .all(promises)
91        .then((response) => {
92          dispatch('getAllDumps');
93          return response;
94        })
95        .then(
96          api.spread((...responses) => {
97            const { successCount, errorCount } = getResponseCount(responses);
98            const toastMessages = [];
99
100            if (successCount) {
101              const message = i18n.global.t(
102                'pageDumps.toast.successDeleteDump',
103                successCount,
104              );
105              toastMessages.push({ type: 'success', message });
106            }
107
108            if (errorCount) {
109              const message = i18n.global.t(
110                'pageDumps.toast.errorDeleteDump',
111                errorCount,
112              );
113              toastMessages.push({ type: 'error', message });
114            }
115
116            return toastMessages;
117          }),
118        );
119    },
120    async deleteAllDumps({ commit, state }) {
121      const totalDumpCount = state.allDumps.length;
122      return await api
123        .post(
124          `${await this.dispatch('global/getBmcPath')}/LogServices/Dump/Actions/LogService.ClearLog`,
125        )
126        .then(() => {
127          commit('setAllDumps', []);
128          return i18n.global.t(
129            'pageDumps.toast.successDeleteDump',
130            totalDumpCount,
131          );
132        })
133        .catch((error) => {
134          console.log(error);
135          throw new Error(
136            i18n.global.t('pageDumps.toast.errorDeleteDump', totalDumpCount),
137          );
138        });
139    },
140  },
141};
142
143export default DumpsStore;
144