Skip to content

Commit

Permalink
wf-details: use dedicated endpoint for fetching retention rules
Browse files Browse the repository at this point in the history
  • Loading branch information
audrium committed Nov 23, 2022
1 parent ec9800e commit ae14840
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 19 deletions.
37 changes: 36 additions & 1 deletion reana-ui/src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,25 @@ import client, {
USER_SIGNOUT_URL,
WORKFLOW_LOGS_URL,
WORKFLOW_SPECIFICATION_URL,
WORKFLOW_RETENTION_RULES_URL,
WORKFLOW_FILES_URL,
WORKFLOW_SET_STATUS_URL,
INTERACTIVE_SESSIONS_OPEN_URL,
INTERACTIVE_SESSIONS_CLOSE_URL,
} from "~/client";
import { parseWorkflows, parseLogs, parseFiles, formatSearch } from "~/util";
import {
parseWorkflows,
parseWorkflowRetentionRules,
parseLogs,
parseFiles,
formatSearch,
} from "~/util";
import {
getConfig,
getWorkflow,
getWorkflowLogs,
getWorkflowSpecification,
getWorkflowRetentionRules,
} from "~/selectors";

export const ERROR = "Error";
Expand Down Expand Up @@ -68,6 +76,8 @@ export const WORKFLOW_FILES_RECEIVED = "Workflow files received";
export const WORKFLOW_SPECIFICATION_FETCH = "Fetch workflow specification";
export const WORKFLOW_SPECIFICATION_RECEIVED =
"Workflow specification received";
export const WORKFLOW_RETENTION_RULES_RECEIVED =
"Workflow workflow retention received";
export const WORKFLOW_DELETE_INIT = "Initialize workflow deletion";
export const WORKFLOW_DELETED = "Workflow deleted";
export const OPEN_DELETE_WORKFLOW_MODAL = "Open delete workflow modal";
Expand Down Expand Up @@ -365,6 +375,31 @@ export function fetchWorkflowSpecification(id) {
};
}

export function fetchWorkflowRetentionRules(id) {
return async (dispatch, getStore) => {
const state = getStore();
const retentionRules = getWorkflowRetentionRules(id)(state);
// Only fetch if needed
if (!isEmpty(retentionRules)) {
return retentionRules;
}
return await client
.getWorkflowRetentionRules(id)
.then((resp) =>
dispatch({
type: WORKFLOW_RETENTION_RULES_RECEIVED,
id,
retentionRules: parseWorkflowRetentionRules(
resp.data.retention_rules
),
})
)
.catch((err) => {
dispatch(errorActionCreator(err, WORKFLOW_RETENTION_RULES_URL(id)));
});
};
}

export function deleteWorkflow(id, workspace = true) {
return async (dispatch) => {
dispatch({ type: WORKFLOW_DELETE_INIT });
Expand Down
6 changes: 6 additions & 0 deletions reana-ui/src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export const WORKFLOWS_URL = (params) =>
export const WORKFLOW_LOGS_URL = (id) => `${api}/api/workflows/${id}/logs`;
export const WORKFLOW_SPECIFICATION_URL = (id) =>
`${api}/api/workflows/${id}/specification`;
export const WORKFLOW_RETENTION_RULES_URL = (id) =>
`${api}/api/workflows/${id}/retention_rules`;
export const WORKFLOW_FILES_URL = (id, params) =>
`${api}/api/workflows/${id}/workspace?${stringifyQueryParams(params)}`;
export const WORKFLOW_FILE_URL = (id, filename, preview = true) =>
Expand Down Expand Up @@ -141,6 +143,10 @@ class Client {
return this._request(WORKFLOW_SPECIFICATION_URL(id));
}

getWorkflowRetentionRules(id) {
return this._request(WORKFLOW_RETENTION_RULES_URL(id));
}

deleteWorkflow(id, data) {
return this._request(WORKFLOW_SET_STATUS_URL(id, { status: "deleted" }), {
data,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import { useEffect, useState } from "react";
import { useDispatch, useSelector } from "react-redux";
import { Accordion, Icon, Label, Table } from "semantic-ui-react";

import { fetchWorkflow } from "~/actions";
import { getWorkflow } from "~/selectors";
import { fetchWorkflowRetentionRules } from "~/actions";
import { getWorkflowRetentionRules } from "~/selectors";

import styles from "./WorkflowRetentionRules.module.scss";

Expand All @@ -24,22 +24,18 @@ function LabelRule({ workspaceFiles }) {

export default function WorkflowRetentionRules({ id }) {
const dispatch = useDispatch();
const workflow = useSelector(getWorkflow(id));

const [showRules, setShowRules] = useState(false);

useEffect(() => {
dispatch(fetchWorkflow(id));
dispatch(fetchWorkflowRetentionRules(id));
}, [dispatch, id]);

const handleClick = () => {
setShowRules((showRules) => !showRules);
};

const rules = workflow.retentionRules || [];
if (rules.length === 0) {
return null;
}
const rules = useSelector(getWorkflowRetentionRules(id)) || [];
if (!rules.length) return null;

// First rule that still needs to be applied
const nextRule = rules.find(({ active, created }) => active || created);
Expand Down
12 changes: 12 additions & 0 deletions reana-ui/src/reducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
WORKFLOW_SPECIFICATION_RECEIVED,
WORKFLOW_FILES_FETCH,
WORKFLOW_FILES_RECEIVED,
WORKFLOW_RETENTION_RULES_RECEIVED,
OPEN_DELETE_WORKFLOW_MODAL,
CLOSE_DELETE_WORKFLOW_MODAL,
} from "~/actions";
Expand Down Expand Up @@ -276,6 +277,17 @@ const details = (state = detailsInitialState, action) => {
},
loadingDetails: false,
};
case WORKFLOW_RETENTION_RULES_RECEIVED:
return {
...state,
details: {
...state.details,
[action.id]: {
...state.details[action.id],
retentionRules: action.retentionRules,
},
},
};
default:
return state;
}
Expand Down
2 changes: 2 additions & 0 deletions reana-ui/src/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,5 @@ export const getWorkflowSpecification = (id) => (state) =>
id in state.details.details && state.details.details[id].specification;
export const getWorkflowParameters = (id) => (state) =>
id in state.details.details && state.details.details[id].parameters;
export const getWorkflowRetentionRules = (id) => (state) =>
id in state.details.details && state.details.details[id].retentionRules;
12 changes: 3 additions & 9 deletions reana-ui/src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ export function parseWorkflows(workflows) {
workflow.total = total.total;
workflow.launcherURL = workflow.launcher_url;
workflow = parseWorkflowDates(workflow);
workflow = parseWorkflowRetentionRules(workflow);

obj[workflow.id] = workflow;
return obj;
Expand All @@ -67,7 +66,7 @@ export function parseWorkflows(workflows) {
/**
* Parses workflow's retention rules.
*/
function parseWorkflowRetentionRules(workflow) {
export function parseWorkflowRetentionRules(retentionRules) {
const getTimeBeforeExecution = (applyOn, currentTime) => {
const diff = moment.duration(applyOn.diff(currentTime));
if (diff.asDays() < 1) {
Expand All @@ -84,13 +83,9 @@ function parseWorkflowRetentionRules(workflow) {
return timeBeforeExecution;
};

const retentionRules = workflow.retention_rules;
if (!retentionRules) {
return workflow;
}
delete workflow.retention_rules;
if (!Array.isArray(retentionRules)) return [];
const currentTime = moment.now();
workflow.retentionRules = sortBy(
return sortBy(
retentionRules.map(
({ apply_on, retention_days, status, workspace_files }) => {
const applyOn = apply_on ? moment(apply_on) : null;
Expand All @@ -113,7 +108,6 @@ function parseWorkflowRetentionRules(workflow) {
),
[({ retentionDays }) => retentionDays]
);
return workflow;
}

/**
Expand Down

0 comments on commit ae14840

Please sign in to comment.