1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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";
20 export const processLogsPanelActions = unionize({
21 RESET_PROCESS_LOGS_PANEL: ofType<{}>(),
22 INIT_PROCESS_LOGS_PANEL: ofType<{ filters: string[], logs: ProcessLogs }>(),
23 SET_PROCESS_LOGS_PANEL_FILTER: ofType<string>(),
24 ADD_PROCESS_LOGS_PANEL_ITEM: ofType<{ logType: string, log: string }>(),
27 export type ProcessLogsPanelAction = UnionOf<typeof processLogsPanelActions>;
29 export const setProcessLogsPanelFilter = (filter: string) =>
30 processLogsPanelActions.SET_PROCESS_LOGS_PANEL_FILTER(filter);
32 export const initProcessLogsPanel = (processUuid: string) =>
33 async (dispatch: Dispatch, getState: () => RootState, { logService }: ServiceRepository) => {
34 dispatch(processLogsPanelActions.RESET_PROCESS_LOGS_PANEL());
35 const process = await dispatch<any>(loadProcess(processUuid));
36 if (process.container) {
37 const logResources = await loadContainerLogs(process.container.uuid, logService);
38 const initialState = createInitialLogPanelState(logResources);
39 dispatch(processLogsPanelActions.INIT_PROCESS_LOGS_PANEL(initialState));
43 export const addProcessLogsPanelItem = (message: ResourceEventMessage<{ text: string }>) =>
44 async (dispatch: Dispatch, getState: () => RootState, { logService }: ServiceRepository) => {
45 if (PROCESS_PANEL_LOG_EVENT_TYPES.indexOf(message.eventType) > -1) {
46 const uuid = getProcessLogsPanelCurrentUuid(getState());
48 const process = getProcess(uuid)(getState().resources);
50 const { containerRequest, container } = process;
51 if (message.objectUuid === containerRequest.uuid
52 || container && message.objectUuid === container.uuid) {
53 dispatch(processLogsPanelActions.ADD_PROCESS_LOGS_PANEL_ITEM({
54 logType: SUMMARIZED_FILTER_TYPE,
55 log: message.properties.text
57 dispatch(processLogsPanelActions.ADD_PROCESS_LOGS_PANEL_ITEM({
58 logType: message.eventType,
59 log: message.properties.text
67 const loadContainerLogs = async (containerUuid: string, logService: LogService) => {
68 const requestFilters = new FilterBuilder()
69 .addEqual('objectUuid', containerUuid)
70 .addIn('eventType', PROCESS_PANEL_LOG_EVENT_TYPES)
72 const requestOrder = new OrderBuilder<LogResource>()
75 const requestParams = {
76 limit: MAX_AMOUNT_OF_LOGS,
77 filters: requestFilters,
80 const { items } = await logService.list(requestParams);
84 const createInitialLogPanelState = (logResources: LogResource[]) => {
85 const allLogs = logsToLines(logResources);
86 const groupedLogResources = groupBy(logResources, log => log.eventType);
87 const groupedLogs = Object
88 .keys(groupedLogResources)
89 .reduce((grouped, key) => ({
91 [key]: logsToLines(groupedLogResources[key])
93 const filters = [SUMMARIZED_FILTER_TYPE, ...Object.keys(groupedLogs)];
94 const logs = { [SUMMARIZED_FILTER_TYPE]: allLogs, ...groupedLogs };
95 return { filters, logs };
98 const logsToLines = (logs: LogResource[]) =>
99 logs.map(({ properties }) => properties.text);
101 const MAX_AMOUNT_OF_LOGS = 10000;
103 const SUMMARIZED_FILTER_TYPE = 'Summarized';
105 const PROCESS_PANEL_LOG_EVENT_TYPES = [
106 LogEventType.ARV_MOUNT,
107 LogEventType.CRUNCH_RUN,
108 LogEventType.CRUNCHSTAT,
109 LogEventType.DISPATCH,
110 LogEventType.HOSTSTAT,
111 LogEventType.NODE_INFO,