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";
19 import { navigateTo } from '~/store/navigation/navigation-action';
20 import { snackbarActions, SnackbarKind } from '~/store/snackbar/snackbar-actions';
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 }>(),
29 export type ProcessLogsPanelAction = UnionOf<typeof processLogsPanelActions>;
31 export const setProcessLogsPanelFilter = (filter: string) =>
32 processLogsPanelActions.SET_PROCESS_LOGS_PANEL_FILTER(filter);
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));
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());
50 const process = getProcess(uuid)(getState().resources);
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
59 dispatch(processLogsPanelActions.ADD_PROCESS_LOGS_PANEL_ITEM({
60 logType: message.eventType,
61 log: message.properties.text
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)
74 const requestOrder = new OrderBuilder<LogResource>()
77 const requestParams = {
78 limit: MAX_AMOUNT_OF_LOGS,
79 filters: requestFilters,
82 const { items } = await logService.list(requestParams);
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) => ({
93 [key]: logsToLines(groupedLogResources[key])
95 const filters = [SUMMARIZED_FILTER_TYPE, ...Object.keys(groupedLogs)];
96 const logs = { [SUMMARIZED_FILTER_TYPE]: allLogs, ...groupedLogs };
97 return { filters, logs };
100 const logsToLines = (logs: LogResource[]) =>
101 logs.map(({ properties }) => properties.text);
103 export const navigateToLogCollection = (uuid: string) =>
104 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
106 await services.collectionService.get(uuid);
107 dispatch<any>(navigateTo(uuid));
109 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'This collection does not exists!', hideDuration: 2000, kind: SnackbarKind.ERROR }));
113 const MAX_AMOUNT_OF_LOGS = 10000;
115 const SUMMARIZED_FILTER_TYPE = 'Summarized';
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,