Merge branch 'master' into 16848-token-handling-improvements
[arvados.git] / src / store / process-logs-panel / process-logs-panel-actions.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { unionize, ofType, UnionOf } from "~/common/unionize";
6 import { ProcessLogs, getProcessLogsPanelCurrentUuid } from './process-logs-panel';
7 import { LogEventType } from '~/models/log';
8 import { RootState } from '~/store/store';
9 import { ServiceRepository } from '~/services/services';
10 import { Dispatch } from 'redux';
11 import { groupBy } from 'lodash';
12 import { loadProcess } from '~/store/processes/processes-actions';
13 import { LogResource } from '~/models/log';
14 import { LogService } from '~/services/log-service/log-service';
15 import { ResourceEventMessage } from '~/websocket/resource-event-message';
16 import { getProcess } from '~/store/processes/process';
17 import { FilterBuilder } from "~/services/api/filter-builder";
18 import { OrderBuilder } from "~/services/api/order-builder";
19 import { navigateTo } from '~/store/navigation/navigation-action';
20 import { snackbarActions, SnackbarKind } from '~/store/snackbar/snackbar-actions';
21
22 export const processLogsPanelActions = unionize({
23     RESET_PROCESS_LOGS_PANEL: ofType<{}>(),
24     INIT_PROCESS_LOGS_PANEL: ofType<{ filters: string[], logs: ProcessLogs }>(),
25     SET_PROCESS_LOGS_PANEL_FILTER: ofType<string>(),
26     ADD_PROCESS_LOGS_PANEL_ITEM: ofType<{ logType: string, log: string }>(),
27 });
28
29 export type ProcessLogsPanelAction = UnionOf<typeof processLogsPanelActions>;
30
31 export const setProcessLogsPanelFilter = (filter: string) =>
32     processLogsPanelActions.SET_PROCESS_LOGS_PANEL_FILTER(filter);
33
34 export const initProcessLogsPanel = (processUuid: string) =>
35     async (dispatch: Dispatch, getState: () => RootState, { logService }: ServiceRepository) => {
36         dispatch(processLogsPanelActions.RESET_PROCESS_LOGS_PANEL());
37         const process = await dispatch<any>(loadProcess(processUuid));
38         if (process.container) {
39             const logResources = await loadContainerLogs(process.container.uuid, logService);
40             const initialState = createInitialLogPanelState(logResources);
41             dispatch(processLogsPanelActions.INIT_PROCESS_LOGS_PANEL(initialState));
42         }
43     };
44
45 export const addProcessLogsPanelItem = (message: ResourceEventMessage<{ text: string }>) =>
46     async (dispatch: Dispatch, getState: () => RootState, { logService }: ServiceRepository) => {
47         if (PROCESS_PANEL_LOG_EVENT_TYPES.indexOf(message.eventType) > -1) {
48             const uuid = getProcessLogsPanelCurrentUuid(getState());
49             if (uuid) {
50                 const process = getProcess(uuid)(getState().resources);
51                 if (process) {
52                     const { containerRequest, container } = process;
53                     if (message.objectUuid === containerRequest.uuid
54                         || container && message.objectUuid === container.uuid) {
55                         dispatch(processLogsPanelActions.ADD_PROCESS_LOGS_PANEL_ITEM({
56                             logType: SUMMARIZED_FILTER_TYPE,
57                             log: message.properties.text
58                         }));
59                         dispatch(processLogsPanelActions.ADD_PROCESS_LOGS_PANEL_ITEM({
60                             logType: message.eventType,
61                             log: message.properties.text
62                         }));
63                     }
64                 }
65             }
66         }
67     };
68
69 const loadContainerLogs = async (containerUuid: string, logService: LogService) => {
70     const requestFilters = new FilterBuilder()
71         .addEqual('object_uuid', containerUuid)
72         .addIn('event_type', PROCESS_PANEL_LOG_EVENT_TYPES)
73         .getFilters();
74     const requestOrder = new OrderBuilder<LogResource>()
75         .addAsc('eventAt')
76         .getOrder();
77     const requestParams = {
78         limit: MAX_AMOUNT_OF_LOGS,
79         filters: requestFilters,
80         order: requestOrder,
81     };
82     const { items } = await logService.list(requestParams);
83     return items;
84 };
85
86 const createInitialLogPanelState = (logResources: LogResource[]) => {
87     const allLogs = logsToLines(logResources);
88     const groupedLogResources = groupBy(logResources, log => log.eventType);
89     const groupedLogs = Object
90         .keys(groupedLogResources)
91         .reduce((grouped, key) => ({
92             ...grouped,
93             [key]: logsToLines(groupedLogResources[key])
94         }), {});
95     const filters = [SUMMARIZED_FILTER_TYPE, ...Object.keys(groupedLogs)];
96     const logs = { [SUMMARIZED_FILTER_TYPE]: allLogs, ...groupedLogs };
97     return { filters, logs };
98 };
99
100 const logsToLines = (logs: LogResource[]) =>
101     logs.map(({ properties }) => properties.text);
102
103 export const navigateToLogCollection = (uuid: string) =>
104     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
105         try {
106             await services.collectionService.get(uuid);
107             dispatch<any>(navigateTo(uuid));
108         } catch {
109             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'This collection does not exists!', hideDuration: 2000, kind: SnackbarKind.ERROR }));
110         }
111     };
112
113 const MAX_AMOUNT_OF_LOGS = 10000;
114
115 const SUMMARIZED_FILTER_TYPE = 'Summarized';
116
117 const PROCESS_PANEL_LOG_EVENT_TYPES = [
118     LogEventType.ARV_MOUNT,
119     LogEventType.CRUNCH_RUN,
120     LogEventType.CRUNCHSTAT,
121     LogEventType.DISPATCH,
122     LogEventType.HOSTSTAT,
123     LogEventType.NODE_INFO,
124     LogEventType.STDERR,
125     LogEventType.STDOUT,
126 ];