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