16583: Fix test, add "intermediate" filter test
[arvados.git] / src / store / project-panel / project-panel-middleware-service.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import {
6     DataExplorerMiddlewareService,
7     dataExplorerToListParams,
8     getDataExplorerColumnFilters,
9     listResultsToDataExplorerItemsMeta
10 } from 'store/data-explorer/data-explorer-middleware-service';
11 import { ProjectPanelColumnNames } from "views/project-panel/project-panel";
12 import { RootState } from "store/store";
13 import { DataColumns } from "components/data-table/data-table";
14 import { ServiceRepository } from "services/services";
15 import { SortDirection } from "components/data-table/data-column";
16 import { OrderBuilder, OrderDirection } from "services/api/order-builder";
17 import { FilterBuilder, joinFilters } from "services/api/filter-builder";
18 import { GroupContentsResource, GroupContentsResourcePrefix } from "services/groups-service/groups-service";
19 import { updateFavorites } from "store/favorites/favorites-actions";
20 import {
21     IS_PROJECT_PANEL_TRASHED,
22     projectPanelActions,
23     getProjectPanelCurrentUuid
24 } from 'store/project-panel/project-panel-action';
25 import { Dispatch, MiddlewareAPI } from "redux";
26 import { ProjectResource } from "models/project";
27 import { updateResources } from "store/resources/resources-actions";
28 import { getProperty } from "store/properties/properties";
29 import { snackbarActions, SnackbarKind } from 'store/snackbar/snackbar-actions';
30 import { progressIndicatorActions } from 'store/progress-indicator/progress-indicator-actions';
31 import { DataExplorer, getDataExplorer } from 'store/data-explorer/data-explorer-reducer';
32 import { ListResults } from 'services/common-service/common-service';
33 import { loadContainers } from 'store/processes/processes-actions';
34 import { ResourceKind } from 'models/resource';
35 import { getSortColumn } from "store/data-explorer/data-explorer-reducer";
36 import {
37     serializeResourceTypeFilters,
38     buildProcessStatusFilters
39 } from 'store/resource-type-filters/resource-type-filters';
40 import { updatePublicFavorites } from 'store/public-favorites/public-favorites-actions';
41
42 export class ProjectPanelMiddlewareService extends DataExplorerMiddlewareService {
43     constructor(private services: ServiceRepository, id: string) {
44         super(id);
45     }
46
47     async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
48         const state = api.getState();
49         const dataExplorer = getDataExplorer(state.dataExplorer, this.getId());
50         const projectUuid = getProjectPanelCurrentUuid(state);
51         const isProjectTrashed = getProperty<string>(IS_PROJECT_PANEL_TRASHED)(state.properties);
52         if (!projectUuid) {
53             api.dispatch(projectPanelCurrentUuidIsNotSet());
54         } else if (!dataExplorer) {
55             api.dispatch(projectPanelDataExplorerIsNotSet());
56         } else {
57             try {
58                 api.dispatch(progressIndicatorActions.START_WORKING(this.getId()));
59                 const response = await this.services.groupsService.contents(projectUuid, getParams(dataExplorer, !!isProjectTrashed));
60                 const resourceUuids = response.items.map(item => item.uuid);
61                 api.dispatch<any>(updateFavorites(resourceUuids));
62                 api.dispatch<any>(updatePublicFavorites(resourceUuids));
63                 api.dispatch(updateResources(response.items));
64                 await api.dispatch<any>(loadMissingProcessesInformation(response.items));
65                 api.dispatch(setItems(response));
66             } catch (e) {
67                 api.dispatch(projectPanelActions.SET_ITEMS({
68                     items: [],
69                     itemsAvailable: 0,
70                     page: 0,
71                     rowsPerPage: dataExplorer.rowsPerPage
72                 }));
73                 api.dispatch(couldNotFetchProjectContents());
74             } finally {
75                 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
76             }
77         }
78     }
79 }
80
81 export const loadMissingProcessesInformation = (resources: GroupContentsResource[]) =>
82     async (dispatch: Dispatch) => {
83         const containerUuids = resources.reduce((uuids, resource) => {
84             return resource.kind === ResourceKind.CONTAINER_REQUEST
85                 ? resource.containerUuid
86                     ? [...uuids, resource.containerUuid]
87                     : uuids
88                 : uuids;
89         }, []);
90         if (containerUuids.length > 0) {
91             await dispatch<any>(loadContainers(
92                 new FilterBuilder().addIn('uuid', containerUuids).getFilters()
93             ));
94         }
95     };
96
97 export const setItems = (listResults: ListResults<GroupContentsResource>) =>
98     projectPanelActions.SET_ITEMS({
99         ...listResultsToDataExplorerItemsMeta(listResults),
100         items: listResults.items.map(resource => resource.uuid),
101     });
102
103 export const getParams = (dataExplorer: DataExplorer, isProjectTrashed: boolean) => ({
104     ...dataExplorerToListParams(dataExplorer),
105     order: getOrder(dataExplorer),
106     filters: getFilters(dataExplorer),
107     includeTrash: isProjectTrashed
108 });
109
110 export const getFilters = (dataExplorer: DataExplorer) => {
111     const columns = dataExplorer.columns as DataColumns<string>;
112     const typeFilters = serializeResourceTypeFilters(getDataExplorerColumnFilters(columns, ProjectPanelColumnNames.TYPE));
113     const statusColumnFilters = getDataExplorerColumnFilters(columns, 'Status');
114     const activeStatusFilter = Object.keys(statusColumnFilters).find(
115         filterName => statusColumnFilters[filterName].selected
116     );
117
118     // TODO: Extract group contents name filter
119     const nameFilters = new FilterBuilder()
120         .addILike("name", dataExplorer.searchValue, GroupContentsResourcePrefix.COLLECTION)
121         .addILike("name", dataExplorer.searchValue, GroupContentsResourcePrefix.PROCESS)
122         .addILike("name", dataExplorer.searchValue, GroupContentsResourcePrefix.PROJECT)
123         .getFilters();
124
125     // Filter by container status
126     const statusFilters = buildProcessStatusFilters(
127         new FilterBuilder(),
128         activeStatusFilter || '',
129         GroupContentsResourcePrefix.PROCESS).getFilters();
130
131     return joinFilters(
132         statusFilters,
133         typeFilters,
134         nameFilters,
135     );
136 };
137
138 export const getOrder = (dataExplorer: DataExplorer) => {
139     const sortColumn = getSortColumn(dataExplorer);
140     const order = new OrderBuilder<ProjectResource>();
141     if (sortColumn) {
142         const sortDirection = sortColumn && sortColumn.sortDirection === SortDirection.ASC
143             ? OrderDirection.ASC
144             : OrderDirection.DESC;
145
146         const columnName = sortColumn && sortColumn.name === ProjectPanelColumnNames.NAME ? "name" : "createdAt";
147         return order
148             .addOrder(sortDirection, columnName, GroupContentsResourcePrefix.COLLECTION)
149             .addOrder(sortDirection, columnName, GroupContentsResourcePrefix.PROCESS)
150             .addOrder(sortDirection, columnName, GroupContentsResourcePrefix.PROJECT)
151             .getOrder();
152     } else {
153         return order.getOrder();
154     }
155 };
156
157 const projectPanelCurrentUuidIsNotSet = () =>
158     snackbarActions.OPEN_SNACKBAR({
159         message: 'Project panel is not opened.',
160         kind: SnackbarKind.ERROR
161     });
162
163 const couldNotFetchProjectContents = () =>
164     snackbarActions.OPEN_SNACKBAR({
165         message: 'Could not fetch project contents.',
166         kind: SnackbarKind.ERROR
167     });
168
169 const projectPanelDataExplorerIsNotSet = () =>
170     snackbarActions.OPEN_SNACKBAR({
171         message: 'Project panel is not ready.',
172         kind: SnackbarKind.ERROR
173     });