1import api, { getResponseCount } from '@/store/api';
2import i18n from '@/i18n';
3
4const getHealthStatus = (events, loadedEvents) => {
5  let status = loadedEvents ? 'OK' : '';
6  for (const event of events) {
7    if (event.severity === 'Warning') {
8      status = 'Warning';
9    }
10    if (event.severity === 'Critical') {
11      status = 'Critical';
12      break;
13    }
14  }
15  return status;
16};
17
18// TODO: High priority events should also check if Log
19// is resolved when the property is available in Redfish
20const getHighPriorityEvents = (events) =>
21  events.filter(({ severity }) => severity === 'Critical');
22
23const EventLogStore = {
24  namespaced: true,
25  state: {
26    allEvents: [],
27    loadedEvents: false,
28  },
29  getters: {
30    allEvents: (state) => state.allEvents,
31    highPriorityEvents: (state) => getHighPriorityEvents(state.allEvents),
32    healthStatus: (state) =>
33      getHealthStatus(state.allEvents, state.loadedEvents),
34  },
35  mutations: {
36    setAllEvents: (state, allEvents) => (
37      (state.allEvents = allEvents), (state.loadedEvents = true)
38    ),
39  },
40  actions: {
41    async getEventLogData({ commit }) {
42      return await api
43        .get('/redfish/v1/Systems/system/LogServices/EventLog/Entries')
44        .then(({ data: { Members = [] } = {} }) => {
45          const eventLogs = Members.map((log) => {
46            const {
47              Id,
48              Severity,
49              Created,
50              EntryType,
51              Message,
52              Name,
53              Modified,
54              Resolved,
55              AdditionalDataURI,
56            } = log;
57            return {
58              id: Id,
59              severity: Severity,
60              date: new Date(Created),
61              type: EntryType,
62              description: Message,
63              name: Name,
64              modifiedDate: new Date(Modified),
65              uri: log['@odata.id'],
66              filterByStatus: Resolved ? 'Resolved' : 'Unresolved',
67              status: Resolved, //true or false
68              additionalDataUri: AdditionalDataURI,
69            };
70          });
71          commit('setAllEvents', eventLogs);
72        })
73        .catch((error) => {
74          console.log('Event Log Data:', error);
75        });
76    },
77    async deleteAllEventLogs({ dispatch }, data) {
78      return await api
79        .post(
80          '/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog'
81        )
82        .then(() => dispatch('getEventLogData'))
83        .then(() => i18n.tc('pageEventLogs.toast.successDelete', data.length))
84        .catch((error) => {
85          console.log(error);
86          throw new Error(
87            i18n.tc('pageEventLogs.toast.errorDelete', data.length)
88          );
89        });
90    },
91    async deleteEventLogs({ dispatch }, uris = []) {
92      const promises = uris.map((uri) =>
93        api.delete(uri).catch((error) => {
94          console.log(error);
95          return error;
96        })
97      );
98      return await api
99        .all(promises)
100        .then((response) => {
101          dispatch('getEventLogData');
102          return response;
103        })
104        .then(
105          api.spread((...responses) => {
106            const { successCount, errorCount } = getResponseCount(responses);
107            const toastMessages = [];
108
109            if (successCount) {
110              const message = i18n.tc(
111                'pageEventLogs.toast.successDelete',
112                successCount
113              );
114              toastMessages.push({ type: 'success', message });
115            }
116
117            if (errorCount) {
118              const message = i18n.tc(
119                'pageEventLogs.toast.errorDelete',
120                errorCount
121              );
122              toastMessages.push({ type: 'error', message });
123            }
124
125            return toastMessages;
126          })
127        );
128    },
129    async resolveEventLogs({ dispatch }, logs) {
130      const promises = logs.map((log) =>
131        api.patch(log.uri, { Resolved: true }).catch((error) => {
132          console.log(error);
133          return error;
134        })
135      );
136      return await api
137        .all(promises)
138        .then((response) => {
139          dispatch('getEventLogData');
140          return response;
141        })
142        .then(
143          api.spread((...responses) => {
144            const { successCount, errorCount } = getResponseCount(responses);
145            const toastMessages = [];
146            if (successCount) {
147              const message = i18n.tc(
148                'pageEventLogs.toast.successResolveLogs',
149                successCount
150              );
151              toastMessages.push({ type: 'success', message });
152            }
153            if (errorCount) {
154              const message = i18n.tc(
155                'pageEventLogs.toast.errorResolveLogs',
156                errorCount
157              );
158              toastMessages.push({ type: 'error', message });
159            }
160            return toastMessages;
161          })
162        );
163    },
164    async unresolveEventLogs({ dispatch }, logs) {
165      const promises = logs.map((log) =>
166        api.patch(log.uri, { Resolved: false }).catch((error) => {
167          console.log(error);
168          return error;
169        })
170      );
171      return await api
172        .all(promises)
173        .then((response) => {
174          dispatch('getEventLogData');
175          return response;
176        })
177        .then(
178          api.spread((...responses) => {
179            const { successCount, errorCount } = getResponseCount(responses);
180            const toastMessages = [];
181            if (successCount) {
182              const message = i18n.tc(
183                'pageEventLogs.toast.successUnresolveLogs',
184                successCount
185              );
186              toastMessages.push({ type: 'success', message });
187            }
188            if (errorCount) {
189              const message = i18n.tc(
190                'pageEventLogs.toast.errorUnresolveLogs',
191                errorCount
192              );
193              toastMessages.push({ type: 'error', message });
194            }
195            return toastMessages;
196          })
197        );
198    },
199    // Single log entry
200    async updateEventLogStatus({ dispatch }, log) {
201      const updatedEventLogStatus = log.status;
202      return await api
203        .patch(log.uri, { Resolved: updatedEventLogStatus })
204        .then(() => {
205          dispatch('getEventLogData');
206        })
207        .then(() => {
208          if (log.status) {
209            return i18n.tc('pageEventLogs.toast.successResolveLogs', 1);
210          } else {
211            return i18n.tc('pageEventLogs.toast.successUnresolveLogs', 1);
212          }
213        })
214        .catch((error) => {
215          console.log(error);
216          throw new Error(i18n.t('pageEventLogs.toast.errorLogStatusUpdate'));
217        });
218    },
219  },
220};
221
222export default EventLogStore;
223