1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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";
21 IS_PROJECT_PANEL_TRASHED,
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";
37 serializeResourceTypeFilters,
38 buildProcessStatusFilters
39 } from 'store/resource-type-filters/resource-type-filters';
40 import { updatePublicFavorites } from 'store/public-favorites/public-favorites-actions';
42 export class ProjectPanelMiddlewareService extends DataExplorerMiddlewareService {
43 constructor(private services: ServiceRepository, id: string) {
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);
53 api.dispatch(projectPanelCurrentUuidIsNotSet());
54 } else if (!dataExplorer) {
55 api.dispatch(projectPanelDataExplorerIsNotSet());
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));
67 api.dispatch(projectPanelActions.SET_ITEMS({
71 rowsPerPage: dataExplorer.rowsPerPage
73 api.dispatch(couldNotFetchProjectContents());
75 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
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]
90 if (containerUuids.length > 0) {
91 await dispatch<any>(loadContainers(
92 new FilterBuilder().addIn('uuid', containerUuids).getFilters()
97 export const setItems = (listResults: ListResults<GroupContentsResource>) =>
98 projectPanelActions.SET_ITEMS({
99 ...listResultsToDataExplorerItemsMeta(listResults),
100 items: listResults.items.map(resource => resource.uuid),
103 export const getParams = (dataExplorer: DataExplorer, isProjectTrashed: boolean) => ({
104 ...dataExplorerToListParams(dataExplorer),
105 order: getOrder(dataExplorer),
106 filters: getFilters(dataExplorer),
107 includeTrash: isProjectTrashed
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
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)
125 // Filter by container status
126 const statusFilters = buildProcessStatusFilters(
128 activeStatusFilter || '',
129 GroupContentsResourcePrefix.PROCESS).getFilters();
138 export const getOrder = (dataExplorer: DataExplorer) => {
139 const sortColumn = getSortColumn(dataExplorer);
140 const order = new OrderBuilder<ProjectResource>();
142 const sortDirection = sortColumn && sortColumn.sortDirection === SortDirection.ASC
144 : OrderDirection.DESC;
146 const columnName = sortColumn && sortColumn.name === ProjectPanelColumnNames.NAME ? "name" : "createdAt";
148 .addOrder(sortDirection, columnName, GroupContentsResourcePrefix.COLLECTION)
149 .addOrder(sortDirection, columnName, GroupContentsResourcePrefix.PROCESS)
150 .addOrder(sortDirection, columnName, GroupContentsResourcePrefix.PROJECT)
153 return order.getOrder();
157 const projectPanelCurrentUuidIsNotSet = () =>
158 snackbarActions.OPEN_SNACKBAR({
159 message: 'Project panel is not opened.',
160 kind: SnackbarKind.ERROR
163 const couldNotFetchProjectContents = () =>
164 snackbarActions.OPEN_SNACKBAR({
165 message: 'Could not fetch project contents.',
166 kind: SnackbarKind.ERROR
169 const projectPanelDataExplorerIsNotSet = () =>
170 snackbarActions.OPEN_SNACKBAR({
171 message: 'Project panel is not ready.',
172 kind: SnackbarKind.ERROR