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) => { |
Yoshie Muranaka | be3af33 | 2020-05-11 08:23:04 -0700 | [diff] [blame] | 46 | const { Id, Severity, Created, EntryType, Message } = log; |
| 47 | return { |
| 48 | id: Id, |
| 49 | severity: Severity, |
| 50 | date: new Date(Created), |
| 51 | type: EntryType, |
| 52 | description: Message, |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 53 | uri: log['@odata.id'], |
Yoshie Muranaka | be3af33 | 2020-05-11 08:23:04 -0700 | [diff] [blame] | 54 | }; |
| 55 | }); |
Yoshie Muranaka | 1ace1d9 | 2020-02-06 13:47:28 -0800 | [diff] [blame] | 56 | commit('setAllEvents', eventLogs); |
Dixsie Wolmers | f65ee34 | 2020-01-22 19:47:56 -0600 | [diff] [blame] | 57 | }) |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 58 | .catch((error) => { |
Dixsie Wolmers | f65ee34 | 2020-01-22 19:47:56 -0600 | [diff] [blame] | 59 | console.log('Event Log Data:', error); |
| 60 | }); |
Yoshie Muranaka | be3af33 | 2020-05-11 08:23:04 -0700 | [diff] [blame] | 61 | }, |
Sukanya Pandey | 2f6d552 | 2020-10-28 10:38:00 +0530 | [diff] [blame] | 62 | async deleteAllEventLogs({ dispatch }, data) { |
| 63 | return await api |
| 64 | .post( |
| 65 | '/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog' |
| 66 | ) |
| 67 | .then(() => dispatch('getEventLogData')) |
| 68 | .then(() => i18n.tc('pageEventLogs.toast.successDelete', data)) |
| 69 | .catch((error) => { |
| 70 | console.log(error); |
| 71 | throw new Error(i18n.tc('pageEventLogs.toast.errorDelete', data)); |
| 72 | }); |
| 73 | }, |
Yoshie Muranaka | be3af33 | 2020-05-11 08:23:04 -0700 | [diff] [blame] | 74 | async deleteEventLogs({ dispatch }, uris = []) { |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 75 | const promises = uris.map((uri) => |
| 76 | api.delete(uri).catch((error) => { |
Yoshie Muranaka | be3af33 | 2020-05-11 08:23:04 -0700 | [diff] [blame] | 77 | console.log(error); |
| 78 | return error; |
| 79 | }) |
| 80 | ); |
| 81 | return await api |
| 82 | .all(promises) |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 83 | .then((response) => { |
Yoshie Muranaka | be3af33 | 2020-05-11 08:23:04 -0700 | [diff] [blame] | 84 | dispatch('getEventLogData'); |
| 85 | return response; |
| 86 | }) |
| 87 | .then( |
| 88 | api.spread((...responses) => { |
| 89 | const { successCount, errorCount } = getResponseCount(responses); |
| 90 | const toastMessages = []; |
| 91 | |
| 92 | if (successCount) { |
| 93 | const message = i18n.tc( |
| 94 | 'pageEventLogs.toast.successDelete', |
| 95 | successCount |
| 96 | ); |
| 97 | toastMessages.push({ type: 'success', message }); |
| 98 | } |
| 99 | |
| 100 | if (errorCount) { |
| 101 | const message = i18n.tc( |
| 102 | 'pageEventLogs.toast.errorDelete', |
| 103 | errorCount |
| 104 | ); |
| 105 | toastMessages.push({ type: 'error', message }); |
| 106 | } |
| 107 | |
| 108 | return toastMessages; |
| 109 | }) |
| 110 | ); |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 111 | }, |
| 112 | }, |
Dixsie Wolmers | f65ee34 | 2020-01-22 19:47:56 -0600 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | export default EventLogStore; |