21535: added timeouts to test spec Arvados-DCO-1.1-Signed-off-by: Lisa Knox <lisa...
[arvados.git] / services / workbench2 / src / store / processes / processes-middleware-service.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { ServiceRepository } from 'services/services';
6 import { MiddlewareAPI, Dispatch } from 'redux';
7 import {
8     DataExplorerMiddlewareService, dataExplorerToListParams, listResultsToDataExplorerItemsMeta, getDataExplorerColumnFilters, getOrder
9 } from 'store/data-explorer/data-explorer-middleware-service';
10 import { RootState } from 'store/store';
11 import { snackbarActions, SnackbarKind } from 'store/snackbar/snackbar-actions';
12 import { DataExplorer, getDataExplorer } from 'store/data-explorer/data-explorer-reducer';
13 import { BoundDataExplorerActions } from 'store/data-explorer/data-explorer-action';
14 import { updateResources } from 'store/resources/resources-actions';
15 import { ListArguments } from 'services/common-service/common-service';
16 import { ProcessResource } from 'models/process';
17 import { FilterBuilder, joinFilters } from 'services/api/filter-builder';
18 import { DataColumns } from 'components/data-table/data-table';
19 import { ProcessStatusFilter, buildProcessStatusFilters } from '../resource-type-filters/resource-type-filters';
20 import { ContainerRequestResource, containerRequestFieldsNoMounts } from 'models/container-request';
21 import { progressIndicatorActions } from '../progress-indicator/progress-indicator-actions';
22 import { loadMissingProcessesInformation } from '../project-panel/project-panel-middleware-service';
23
24 export class ProcessesMiddlewareService extends DataExplorerMiddlewareService {
25     constructor(private services: ServiceRepository, private actions: BoundDataExplorerActions, id: string) {
26         super(id);
27     }
28
29     getFilters(api: MiddlewareAPI<Dispatch, RootState>, dataExplorer: DataExplorer): string | null {
30         const columns = dataExplorer.columns as DataColumns<string, ContainerRequestResource>;
31         const statusColumnFilters = getDataExplorerColumnFilters(columns, 'Status');
32         const activeStatusFilter = Object.keys(statusColumnFilters).find(
33             filterName => statusColumnFilters[filterName].selected
34         ) || ProcessStatusFilter.ALL;
35
36         const nameFilter = new FilterBuilder().addILike("name", dataExplorer.searchValue).getFilters();
37         const statusFilter = buildProcessStatusFilters(new FilterBuilder(), activeStatusFilter).getFilters();
38
39         return joinFilters(
40             nameFilter,
41             statusFilter,
42         );
43     }
44
45     getParams(api: MiddlewareAPI<Dispatch, RootState>, dataExplorer: DataExplorer): ListArguments | null {
46         const filters = this.getFilters(api, dataExplorer)
47         if (filters === null) {
48             return null;
49         }
50         return {
51             ...dataExplorerToListParams(dataExplorer),
52             order: getOrder<ProcessResource>(dataExplorer),
53             filters
54         };
55     }
56
57     async requestItems(api: MiddlewareAPI<Dispatch, RootState>, criteriaChanged?: boolean, background?: boolean) {
58         const state = api.getState();
59         const dataExplorer = getDataExplorer(state.dataExplorer, this.getId());
60
61         try {
62             if (!background) { api.dispatch(progressIndicatorActions.START_WORKING(this.getId())); }
63
64             const params = this.getParams(api, dataExplorer);
65
66             if (params !== null) {
67                 const containerRequests = await this.services.containerRequestService.list(
68                     {
69                         ...this.getParams(api, dataExplorer),
70                         select: containerRequestFieldsNoMounts
71                     });
72                 api.dispatch(updateResources(containerRequests.items));
73                 await api.dispatch<any>(loadMissingProcessesInformation(containerRequests.items));
74                 api.dispatch(this.actions.SET_ITEMS({
75                     ...listResultsToDataExplorerItemsMeta(containerRequests),
76                     items: containerRequests.items.map(resource => resource.uuid),
77                 }));
78             } else {
79                 api.dispatch(this.actions.SET_ITEMS({
80                     itemsAvailable: 0,
81                     page: 0,
82                     rowsPerPage: dataExplorer.rowsPerPage,
83                     items: [],
84                 }));
85             }
86             if (!background) { api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId())); }
87         } catch {
88             api.dispatch(snackbarActions.OPEN_SNACKBAR({
89                 message: 'Could not fetch process list.',
90                 kind: SnackbarKind.ERROR
91             }));
92             if (!background) { api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId())); }
93         }
94     }
95 }