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