blob: a5ef3757f7a3bc1a6fbb4958cfb85d27beb45d83 [file] [log] [blame]
Yoshie Muranaka6f102342020-05-05 09:45:39 -07001<template>
2 <b-container fluid="xl">
3 <page-title />
4 <b-row>
5 <b-col class="text-right">
6 <table-filter :filters="tableFilters" @filterChange="onFilterChange" />
7 </b-col>
8 </b-row>
9 <b-row>
10 <b-col>
Yoshie Muranakabe3af332020-05-11 08:23:04 -070011 <table-toolbar
12 ref="toolbar"
13 :selected-items-count="selectedRows.length"
14 :actions="batchActions"
15 @clearSelected="clearSelectedRows($refs.table)"
16 @batchAction="onBatchAction"
17 >
18 <template v-slot:export>
19 <table-toolbar-export
20 :data="batchExportData"
21 :file-name="$t('appPageTitle.eventLogs')"
22 />
23 </template>
24 </table-toolbar>
Yoshie Muranaka6f102342020-05-05 09:45:39 -070025 <b-table
Yoshie Muranakaf9832b02020-05-12 12:04:46 -070026 id="table-event-logs"
Yoshie Muranakabe3af332020-05-11 08:23:04 -070027 ref="table"
28 selectable
29 no-select-on-click
Yoshie Muranaka6f102342020-05-05 09:45:39 -070030 sort-icon-left
31 no-sort-reset
32 sort-desc
33 show-empty
34 sort-by="date"
Yoshie Muranakabe3af332020-05-11 08:23:04 -070035 :fields="fields"
36 :items="filteredLogs"
Yoshie Muranaka6f102342020-05-05 09:45:39 -070037 :sort-compare="onSortCompare"
38 :empty-text="$t('pageEventLogs.table.emptyMessage')"
Yoshie Muranakaf9832b02020-05-12 12:04:46 -070039 :per-page="perPage"
40 :current-page="currentPage"
Yoshie Muranakabe3af332020-05-11 08:23:04 -070041 @row-selected="onRowSelected($event, filteredLogs.length)"
Yoshie Muranaka6f102342020-05-05 09:45:39 -070042 >
Yoshie Muranakabe3af332020-05-11 08:23:04 -070043 <!-- Checkbox column -->
44 <template v-slot:head(checkbox)>
45 <b-form-checkbox
46 v-model="tableHeaderCheckboxModel"
47 :indeterminate="tableHeaderCheckboxIndeterminate"
48 @change="onChangeHeaderCheckbox($refs.table)"
49 />
50 </template>
51 <template v-slot:cell(checkbox)="row">
52 <b-form-checkbox
53 v-model="row.rowSelected"
54 @change="toggleSelectRow($refs.table, row.index)"
55 />
56 </template>
57
58 <!-- Severity column -->
Yoshie Muranaka6f102342020-05-05 09:45:39 -070059 <template v-slot:cell(severity)="{ value }">
60 <status-icon :status="getStatus(value)" />
61 {{ value }}
62 </template>
Yoshie Muranakabe3af332020-05-11 08:23:04 -070063
64 <!-- Date column -->
Yoshie Muranaka6f102342020-05-05 09:45:39 -070065 <template v-slot:cell(date)="{ value }">
66 {{ value | formatDate }} {{ value | formatTime }}
67 </template>
Yoshie Muranakabe3af332020-05-11 08:23:04 -070068
69 <!-- Actions column -->
70 <template v-slot:cell(actions)="{ item }">
71 <table-row-action
72 v-for="(action, index) in item.actions"
73 :key="index"
74 :value="action.value"
75 :title="action.title"
76 :row-data="item"
77 :export-name="item.id"
78 @click:tableAction="onTableRowAction($event, item)"
79 >
80 <template v-slot:icon>
81 <icon-export v-if="action.value === 'export'" />
82 <icon-trashcan v-if="action.value === 'delete'" />
83 </template>
84 </table-row-action>
85 </template>
Yoshie Muranaka6f102342020-05-05 09:45:39 -070086 </b-table>
87 </b-col>
88 </b-row>
Yoshie Muranakaf9832b02020-05-12 12:04:46 -070089
90 <!-- Table pagination -->
91 <b-row>
92 <b-col class="d-md-flex justify-content-between">
93 <b-form-group
94 class="table-pagination-select"
95 :label="$t('global.table.itemsPerPage')"
96 label-for="pagination-items-per-page"
97 >
98 <b-form-select
99 id="pagination-items-per-page"
100 v-model="perPage"
101 :options="itemsPerPageOptions"
102 />
103 </b-form-group>
104 <b-pagination
105 v-model="currentPage"
106 first-number
107 last-number
108 :per-page="perPage"
109 :total-rows="getTotalRowCount(filteredLogs.length)"
110 aria-controls="table-event-logs"
111 />
112 </b-col>
113 </b-row>
Yoshie Muranaka6f102342020-05-05 09:45:39 -0700114 </b-container>
115</template>
116
117<script>
Yoshie Muranakabe3af332020-05-11 08:23:04 -0700118import IconTrashcan from '@carbon/icons-vue/es/trash-can/20';
119import IconExport from '@carbon/icons-vue/es/export/20';
120import { omit } from 'lodash';
121
Yoshie Muranaka6f102342020-05-05 09:45:39 -0700122import PageTitle from '@/components/Global/PageTitle';
123import StatusIcon from '@/components/Global/StatusIcon';
124import TableFilter from '@/components/Global/TableFilter';
Yoshie Muranakabe3af332020-05-11 08:23:04 -0700125import TableRowAction from '@/components/Global/TableRowAction';
126import TableToolbar from '@/components/Global/TableToolbar';
127import TableToolbarExport from '@/components/Global/TableToolbarExport';
Yoshie Muranaka6f102342020-05-05 09:45:39 -0700128
129import LoadingBarMixin from '@/components/Mixins/LoadingBarMixin';
130import TableFilterMixin from '@/components/Mixins/TableFilterMixin';
Yoshie Muranakaf9832b02020-05-12 12:04:46 -0700131import BVPaginationMixin from '@/components/Mixins/BVPaginationMixin';
Yoshie Muranakabe3af332020-05-11 08:23:04 -0700132import BVTableSelectableMixin from '@/components/Mixins/BVTableSelectableMixin';
133import BVToastMixin from '@/components/Mixins/BVToastMixin';
Yoshie Muranaka6f102342020-05-05 09:45:39 -0700134
135const SEVERITY = ['OK', 'Warning', 'Critical'];
136
137export default {
Yoshie Muranakaf9832b02020-05-12 12:04:46 -0700138 components: {
Yoshie Muranakabe3af332020-05-11 08:23:04 -0700139 IconExport,
140 IconTrashcan,
Yoshie Muranakaf9832b02020-05-12 12:04:46 -0700141 PageTitle,
142 StatusIcon,
Yoshie Muranakabe3af332020-05-11 08:23:04 -0700143 TableFilter,
144 TableRowAction,
145 TableToolbar,
146 TableToolbarExport
Yoshie Muranakaf9832b02020-05-12 12:04:46 -0700147 },
Yoshie Muranakabe3af332020-05-11 08:23:04 -0700148 mixins: [
149 BVPaginationMixin,
150 BVTableSelectableMixin,
151 BVToastMixin,
152 LoadingBarMixin,
153 TableFilterMixin
154 ],
Yoshie Muranaka6f102342020-05-05 09:45:39 -0700155 data() {
156 return {
157 fields: [
158 {
Yoshie Muranakabe3af332020-05-11 08:23:04 -0700159 key: 'checkbox',
160 sortable: false
161 },
162 {
Yoshie Muranaka6f102342020-05-05 09:45:39 -0700163 key: 'id',
164 label: this.$t('pageEventLogs.table.id'),
165 sortable: true
166 },
167 {
168 key: 'severity',
169 label: this.$t('pageEventLogs.table.severity'),
170 sortable: true
171 },
172 {
173 key: 'type',
174 label: this.$t('pageEventLogs.table.type'),
175 sortable: true
176 },
177 {
178 key: 'date',
179 label: this.$t('pageEventLogs.table.date'),
180 sortable: true
181 },
182 {
183 key: 'description',
184 label: this.$t('pageEventLogs.table.description')
Yoshie Muranakabe3af332020-05-11 08:23:04 -0700185 },
186 {
187 key: 'actions',
188 sortable: false,
189 label: '',
190 tdClass: 'text-right'
Yoshie Muranaka6f102342020-05-05 09:45:39 -0700191 }
192 ],
193 tableFilters: [
194 {
195 label: this.$t('pageEventLogs.table.severity'),
196 values: SEVERITY
197 }
198 ],
Yoshie Muranakabe3af332020-05-11 08:23:04 -0700199 activeFilters: [],
200 batchActions: [
201 {
202 value: 'delete',
203 label: this.$t('global.action.delete')
204 }
205 ]
Yoshie Muranaka6f102342020-05-05 09:45:39 -0700206 };
207 },
208 computed: {
209 allLogs() {
Yoshie Muranakabe3af332020-05-11 08:23:04 -0700210 return this.$store.getters['eventLog/allEvents'].map(event => {
211 return {
212 ...event,
213 actions: [
214 {
215 value: 'export',
216 title: this.$t('global.action.export')
217 },
218 {
219 value: 'delete',
220 title: this.$t('global.action.delete')
221 }
222 ]
223 };
224 });
Yoshie Muranaka6f102342020-05-05 09:45:39 -0700225 },
226 filteredLogs: {
227 get: function() {
228 return this.getFilteredTableData(this.allLogs, this.activeFilters);
229 },
230 set: function(newVal) {
231 return newVal;
232 }
Yoshie Muranakabe3af332020-05-11 08:23:04 -0700233 },
234 batchExportData() {
235 return this.selectedRows.map(row => omit(row, 'actions'));
Yoshie Muranaka6f102342020-05-05 09:45:39 -0700236 }
237 },
238 created() {
239 this.startLoader();
240 this.$store
241 .dispatch('eventLog/getEventLogData')
242 .finally(() => this.endLoader());
243 },
Yoshie Muranakae3a8d692020-05-28 13:30:40 -0700244 beforeRouteLeave(to, from, next) {
245 // Hide loader if the user navigates to another page
246 // before request is fulfilled.
247 this.hideLoader();
248 next();
249 },
Yoshie Muranaka6f102342020-05-05 09:45:39 -0700250 methods: {
251 getStatus(serverity) {
252 switch (serverity) {
253 case SEVERITY[2]:
254 return 'danger';
255 case SEVERITY[1]:
256 return 'warning';
257 case SEVERITY[0]:
258 return 'success';
259 default:
260 return '';
261 }
262 },
Yoshie Muranakabe3af332020-05-11 08:23:04 -0700263 deleteLogs(uris) {
264 this.$store.dispatch('eventLog/deleteEventLogs', uris).then(messages => {
265 messages.forEach(({ type, message }) => {
266 if (type === 'success') {
267 this.successToast(message);
268 } else if (type === 'error') {
269 this.errorToast(message);
270 }
271 });
272 });
273 },
Yoshie Muranaka6f102342020-05-05 09:45:39 -0700274 onFilterChange({ activeFilters }) {
275 this.activeFilters = activeFilters;
276 this.filteredLogs = this.getFilteredTableData(
277 this.allLogs,
278 activeFilters
279 );
280 },
281 onSortCompare(a, b, key) {
282 if (key === 'severity') {
283 return SEVERITY.indexOf(a.status) - SEVERITY.indexOf(b.status);
284 }
Yoshie Muranakabe3af332020-05-11 08:23:04 -0700285 },
286 onTableRowAction(action, { uri }) {
287 if (action === 'delete') {
288 this.$bvModal
289 .msgBoxConfirm(this.$tc('pageEventLogs.modal.deleteMessage'), {
290 title: this.$tc('pageEventLogs.modal.deleteTitle'),
291 okTitle: this.$t('global.action.delete')
292 })
293 .then(deleteConfirmed => {
294 if (deleteConfirmed) this.deleteLogs([uri]);
295 });
296 }
297 },
298 onBatchAction(action) {
299 if (action === 'delete') {
300 const uris = this.selectedRows.map(row => row.uri);
301 this.$bvModal
302 .msgBoxConfirm(
303 this.$tc(
304 'pageEventLogs.modal.deleteMessage',
305 this.selectedRows.length
306 ),
307 {
308 title: this.$tc(
309 'pageEventLogs.modal.deleteTitle',
310 this.selectedRows.length
311 ),
312 okTitle: this.$t('global.action.delete')
313 }
314 )
315 .then(deleteConfirmed => {
316 if (deleteConfirmed) this.deleteLogs(uris);
317 });
318 }
Yoshie Muranaka6f102342020-05-05 09:45:39 -0700319 }
320 }
321};
322</script>