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 { ProjectPanelRunColumnNames } from "views/project-panel/project-panel-run";
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 { ContentsArguments, GroupContentsResource, GroupContentsResourcePrefix } from "services/groups-service/groups-service";
19 import { updateFavorites } from "store/favorites/favorites-actions";
20 import { IS_PROJECT_PANEL_TRASHED, getProjectPanelCurrentUuid } from "store/project-panel/project-panel-action";
21 import { projectPanelRunActions } from "store/project-panel/project-panel-action-bind";
22 import { Dispatch, MiddlewareAPI } from "redux";
23 import { ProjectResource } from "models/project";
24 import { updateResources } from "store/resources/resources-actions";
25 import { getProperty } from "store/properties/properties";
26 import { snackbarActions, SnackbarKind } from "store/snackbar/snackbar-actions";
27 import { progressIndicatorActions } from "store/progress-indicator/progress-indicator-actions";
28 import { DataExplorer, getDataExplorer } from "store/data-explorer/data-explorer-reducer";
29 import { ListResults } from "services/common-service/common-service";
30 import { loadContainers } from "store/processes/processes-actions";
31 import { ResourceKind } from "models/resource";
32 import { getSortColumn } from "store/data-explorer/data-explorer-reducer";
33 import { buildProcessStatusFilters, serializeProcessTypeGroupContentsFilters } from "store/resource-type-filters/resource-type-filters";
34 import { updatePublicFavorites } from "store/public-favorites/public-favorites-actions";
35 import { containerRequestFieldsNoMounts } from "models/container-request";
36 import { ContextMenuActionNames } from "views-components/context-menu/context-menu-action-set";
37 import { removeDisabledButton } from "store/multiselect/multiselect-actions";
39 export class ProjectPanelRunMiddlewareService extends DataExplorerMiddlewareService {
40 constructor(private services: ServiceRepository, id: string) {
44 async requestItems(api: MiddlewareAPI<Dispatch, RootState>, criteriaChanged?: boolean, background?: boolean) {
45 const state = api.getState();
46 const dataExplorer = getDataExplorer(state.dataExplorer, this.getId());
47 const projectUuid = getProjectPanelCurrentUuid(state);
48 const isProjectTrashed = getProperty<string>(IS_PROJECT_PANEL_TRASHED)(state.properties);
50 api.dispatch(projectPanelCurrentUuidIsNotSet());
51 } else if (!dataExplorer) {
52 api.dispatch(projectPanelDataExplorerIsNotSet());
55 api.dispatch<any>(projectPanelRunActions.SET_IS_NOT_FOUND({ isNotFound: false }));
56 if (!background) { api.dispatch(progressIndicatorActions.START_WORKING(this.getId())); }
59 const containerRequests = await this.services.groupsService.contents(projectUuid, getParams(dataExplorer, projectUuid, !!isProjectTrashed));
60 const resourceUuids = containerRequests.items.map(item => item.uuid);
61 api.dispatch<any>(updateFavorites(resourceUuids));
62 api.dispatch<any>(updatePublicFavorites(resourceUuids));
63 api.dispatch(updateResources(containerRequests.items));
64 await api.dispatch<any>(loadMissingProcessesInformation(containerRequests.items));
65 api.dispatch(setItems(containerRequests));
68 projectPanelRunActions.SET_ITEMS({
72 rowsPerPage: dataExplorer.rowsPerPage,
75 if (e.status === 404) {
76 api.dispatch<any>(projectPanelRunActions.SET_IS_NOT_FOUND({ isNotFound: true}));
79 api.dispatch(couldNotFetchProjectContents());
83 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
84 api.dispatch<any>(removeDisabledButton(ContextMenuActionNames.MOVE_TO_TRASH))
91 async requestCount() {}
94 export const loadMissingProcessesInformation = (resources: GroupContentsResource[]) => async (dispatch: Dispatch) => {
95 const containerUuids = resources.reduce((uuids, resource) => {
96 return resource.kind === ResourceKind.CONTAINER_REQUEST && resource.containerUuid && !uuids.includes(resource.containerUuid)
97 ? [...uuids, resource.containerUuid]
100 if (containerUuids.length > 0) {
101 await dispatch<any>(loadContainers(containerUuids, false));
105 export const setItems = (listResults: ListResults<GroupContentsResource>) =>
106 projectPanelRunActions.SET_ITEMS({
107 ...listResultsToDataExplorerItemsMeta(listResults),
108 items: listResults.items.map(resource => resource.uuid),
111 export const getParams = (dataExplorer: DataExplorer, projectUuid: string, isProjectTrashed: boolean): ContentsArguments => ({
112 ...dataExplorerToListParams(dataExplorer),
113 order: getOrder(dataExplorer),
114 filters: getFilters(dataExplorer, projectUuid),
115 includeTrash: isProjectTrashed,
116 select: containerRequestFieldsNoMounts,
119 export const getFilters = (dataExplorer: DataExplorer, projectUuid: string) => {
120 const columns = dataExplorer.columns as DataColumns<string, ProjectResource>;
121 const typeFilters = serializeProcessTypeGroupContentsFilters(getDataExplorerColumnFilters(columns, ProjectPanelRunColumnNames.TYPE));
122 const statusColumnFilters = getDataExplorerColumnFilters(columns, ProjectPanelRunColumnNames.STATUS);
123 const activeStatusFilter = Object.keys(statusColumnFilters).find(filterName => statusColumnFilters[filterName].selected);
125 // TODO: Extract group contents name filter
126 const nameFilters = new FilterBuilder()
127 .addEqual('owner_uuid', projectUuid)
128 .addILike("name", dataExplorer.searchValue)
131 // Filter by container status
132 const statusFilters = buildProcessStatusFilters(new FilterBuilder(), activeStatusFilter || "", GroupContentsResourcePrefix.PROCESS).getFilters();
134 return joinFilters(statusFilters, typeFilters, nameFilters);
137 const getOrder = (dataExplorer: DataExplorer) => {
138 const sortColumn = getSortColumn<ProjectResource>(dataExplorer);
139 const order = new OrderBuilder<ProjectResource>();
140 if (sortColumn && sortColumn.sort) {
141 const sortDirection = sortColumn.sort.direction === SortDirection.ASC ? OrderDirection.ASC : OrderDirection.DESC;
143 // Use createdAt as a secondary sort column so we break ties consistently.
145 .addOrder(sortDirection, sortColumn.sort.field, GroupContentsResourcePrefix.PROCESS)
146 .addOrder(OrderDirection.DESC, "createdAt")
149 return order.getOrder();
153 const projectPanelCurrentUuidIsNotSet = () =>
154 snackbarActions.OPEN_SNACKBAR({
155 message: "Project panel is not opened.",
156 kind: SnackbarKind.ERROR,
159 const couldNotFetchProjectContents = () =>
160 snackbarActions.OPEN_SNACKBAR({
161 message: "Could not fetch project contents.",
162 kind: SnackbarKind.ERROR,
165 const projectPanelDataExplorerIsNotSet = () =>
166 snackbarActions.OPEN_SNACKBAR({
167 message: "Project panel is not ready.",
168 kind: SnackbarKind.ERROR,