19836: Added security state check
[arvados.git] / src / store / all-processes-panel / all-processes-panel-middleware-service.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { DataExplorerMiddlewareService, dataExplorerToListParams, getDataExplorerColumnFilters } from "store/data-explorer/data-explorer-middleware-service";
6 import { RootState } from "../store";
7 import { ServiceRepository } from "services/services";
8 import { FilterBuilder, joinFilters } from "services/api/filter-builder";
9 import { allProcessesPanelActions } from "./all-processes-panel-action";
10 import { Dispatch, MiddlewareAPI } from "redux";
11 import { resourcesActions } from "store/resources/resources-actions";
12 import { snackbarActions, SnackbarKind } from 'store/snackbar/snackbar-actions';
13 import { progressIndicatorActions } from 'store/progress-indicator/progress-indicator-actions';
14 import { getDataExplorer, DataExplorer, getSortColumn } from "store/data-explorer/data-explorer-reducer";
15 import { loadMissingProcessesInformation } from "store/project-panel/project-panel-middleware-service";
16 import { DataColumns } from "components/data-table/data-table";
17 import {
18     ProcessStatusFilter,
19     buildProcessStatusFilters,
20     serializeOnlyProcessTypeFilters
21 } from "../resource-type-filters/resource-type-filters";
22 import { AllProcessesPanelColumnNames } from "views/all-processes-panel/all-processes-panel";
23 import { OrderBuilder, OrderDirection } from "services/api/order-builder";
24 import { ProcessResource } from "models/process";
25 import { SortDirection } from "components/data-table/data-column";
26 import { containerRequestFieldsNoMounts } from "models/container-request";
27
28 export class AllProcessesPanelMiddlewareService extends DataExplorerMiddlewareService {
29     constructor(private services: ServiceRepository, id: string) {
30         super(id);
31     }
32
33     async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
34         const dataExplorer = getDataExplorer(api.getState().dataExplorer, this.getId());
35         if (!dataExplorer) {
36             api.dispatch(allProcessesPanelDataExplorerIsNotSet());
37         } else {
38             try {
39                 api.dispatch(progressIndicatorActions.START_WORKING(this.getId()));
40                 const processItems = await this.services.containerRequestService.list(
41                     {
42                         ...getParams(dataExplorer),
43                         // Omit mounts when viewing all process panel
44                         select: containerRequestFieldsNoMounts,
45                     });
46
47                 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
48                 api.dispatch(resourcesActions.SET_RESOURCES(processItems.items));
49                 await api.dispatch<any>(loadMissingProcessesInformation(processItems.items));
50                 api.dispatch(allProcessesPanelActions.SET_ITEMS({
51                     items: processItems.items.map((resource: any) => resource.uuid),
52                     itemsAvailable: processItems.itemsAvailable,
53                     page: Math.floor(processItems.offset / processItems.limit),
54                     rowsPerPage: processItems.limit
55                 }));
56             } catch {
57                 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
58                 api.dispatch(allProcessesPanelActions.SET_ITEMS({
59                     items: [],
60                     itemsAvailable: 0,
61                     page: 0,
62                     rowsPerPage: dataExplorer.rowsPerPage
63                 }));
64                 api.dispatch(couldNotFetchAllProcessesListing());
65             }
66         }
67     }
68 }
69
70 const getParams = ( dataExplorer: DataExplorer ) => ({
71     ...dataExplorerToListParams(dataExplorer),
72     order: getOrder(dataExplorer),
73     filters: getFilters(dataExplorer)
74 });
75
76 const getFilters = ( dataExplorer: DataExplorer ) => {
77     const columns = dataExplorer.columns as DataColumns<string>;
78     const statusColumnFilters = getDataExplorerColumnFilters(columns, 'Status');
79     const activeStatusFilter = Object.keys(statusColumnFilters).find(
80         filterName => statusColumnFilters[filterName].selected
81     ) || ProcessStatusFilter.ALL;
82
83     const nameFilter = new FilterBuilder().addILike("name", dataExplorer.searchValue).getFilters();
84     const statusFilter = buildProcessStatusFilters(new FilterBuilder(), activeStatusFilter).getFilters();
85     const typeFilters = serializeOnlyProcessTypeFilters(getDataExplorerColumnFilters(columns, AllProcessesPanelColumnNames.TYPE));
86
87     return joinFilters(
88         nameFilter,
89         statusFilter,
90         typeFilters
91     );
92 };
93
94 const getOrder = (dataExplorer: DataExplorer) => {
95     const sortColumn = getSortColumn(dataExplorer);
96     const order = new OrderBuilder<ProcessResource>();
97     if (sortColumn) {
98         const sortDirection = sortColumn && sortColumn.sortDirection === SortDirection.ASC
99             ? OrderDirection.ASC
100             : OrderDirection.DESC;
101
102         const columnName = sortColumn && sortColumn.name === AllProcessesPanelColumnNames.NAME ? "name" : "createdAt";
103         return order
104             .addOrder(sortDirection, columnName)
105             .getOrder();
106     } else {
107         return order.getOrder();
108     }
109 };
110
111 const allProcessesPanelDataExplorerIsNotSet = () =>
112     snackbarActions.OPEN_SNACKBAR({
113         message: 'All Processes panel is not ready.',
114         kind: SnackbarKind.ERROR
115     });
116
117 const couldNotFetchAllProcessesListing = () =>
118     snackbarActions.OPEN_SNACKBAR({
119         message: 'Could not fetch All Processes listing.',
120         kind: SnackbarKind.ERROR
121     });