Skip to content

Commit

Permalink
[Nu-7330] Use activities endpoint to compute ranges in counts window
Browse files Browse the repository at this point in the history
  • Loading branch information
Piotr Rudnicki committed Dec 17, 2024
1 parent 5069ac1 commit 47965a8
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 6 deletions.
2 changes: 1 addition & 1 deletion designer/client/cypress/e2e/counts.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe("Counts", () => {

cy.get("@button").click();
cy.get("[data-testid=window]").contains("Quick ranges").should("be.visible");
cy.contains(/^previous deployments...$/i)
cy.contains(/^previous activities...$/i)
.should("be.visible")
.click();
cy.get("[data-testid=window]").matchImage({ maxDiffThreshold });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { useTranslation } from "react-i18next";
import { useSelector } from "react-redux";
import { getProcessName } from "../../../reducers/selectors/graph";
import { CountsRangesButtons } from "./CountsRangesButtons";
import { useDeployHistory } from "./useDeployHistory";
import { predefinedRanges } from "./utils";
import { StyledRangesWrapper } from "./CountsStyled";
import { useActivityHistory } from "./useActivityHistory";

interface RangesProps {
label: string;
Expand All @@ -16,16 +16,16 @@ interface RangesProps {
export function CountsRanges({ label, onChange }: RangesProps): JSX.Element {
const { t } = useTranslation<string>();
const processName = useSelector(getProcessName);
const deploys = useDeployHistory(processName);
const activities = useActivityHistory(processName);
const dates = useMemo(() => predefinedRanges(t), [t]);

return (
<>
<p>{label}</p>
<StyledRangesWrapper>
<CountsRangesButtons ranges={dates} onChange={onChange} />
<CountsRangesButtons ranges={deploys} onChange={onChange} limit={1}>
{t("calculateCounts.deployments", "Previous deployments...")}
<CountsRangesButtons ranges={activities} onChange={onChange} limit={1}>
{t("calculateCounts.activities", "Previous activities...")}
</CountsRangesButtons>
</StyledRangesWrapper>
</>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
import HttpService from "../../../http/HttpService";
import { DATE_FORMAT } from "../../../config";
import { Range } from "./CountsRangesButtons";
import moment from "moment";
import { PredefinedActivityType } from "../../toolbars/activities/types";

function displayableNameOfPredefinedActivityType(predefinedActivityType: PredefinedActivityType) {
switch (predefinedActivityType) {
case PredefinedActivityType.ScenarioCanceled:
return "Cancel";
case PredefinedActivityType.ScenarioDeployed:
return "Deployment";
case PredefinedActivityType.PerformedScheduledExecution:
return "Scheduled deployment";
case PredefinedActivityType.PerformedSingleExecution:
return "Run now";
default:
return "Unknown activity type";
}
}

export function useActivityHistory(processName: string): Range[] {
const { t } = useTranslation();
const [activities, setActivities] = useState<Range[]>([]);

useEffect(() => {
HttpService.fetchProcessesActivities(processName)
.then((activities) =>
activities.map((current, i, all) => {
const from = moment(current.date);
const to = all[i - 1]?.date;
const isOmitted = current.type === PredefinedActivityType.ScenarioCanceled;
return {
from: () => from,
to: () => (to ? moment(to) : moment().add(1, "day").startOf("day")),
name: i
? t("calculateCounts.range.prevAction", "Previous {{activity}} #{{i}} {{date}}", {
activity: displayableNameOfPredefinedActivityType(current.type),
i: all.length - i,
date: from.format(DATE_FORMAT),
})
: t("calculateCounts.range.lastDeploy", "Latest deploy"),
isOmitted,
};
}),
)
.then((res) => res.filter((activity) => !activity.isOmitted))
.then(setActivities);
}, [t, processName]);

return activities;
}
7 changes: 7 additions & 0 deletions designer/client/src/components/toolbars/activities/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ export type ActivityType =
| "AUTOMATIC_UPDATE"
| "CUSTOM_ACTION";

export enum PredefinedActivityType {
ScenarioDeployed = "SCENARIO_DEPLOYED",
ScenarioCanceled = "SCENARIO_CANCELED",
PerformedSingleExecution = "PERFORMED_SINGLE_EXECUTION",
PerformedScheduledExecution = "PERFORMED_SCHEDULED_EXECUTION",
}

export interface ActivityMetadata {
type: ActivityType;
displayableName: string;
Expand Down
24 changes: 23 additions & 1 deletion designer/client/src/http/HttpService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ import {
Scenario,
StatusDefinitionType,
} from "../components/Process/types";
import { ActivitiesResponse, ActivityMetadataResponse } from "../components/toolbars/activities/types";
import {
ActivitiesResponse,
ActivityMetadataResponse,
ActivityType,
PredefinedActivityType,
} from "../components/toolbars/activities/types";
import { ToolbarsConfig } from "../components/toolbarSettings/types";
import { EventTrackingSelectorType, EventTrackingType } from "../containers/event-tracking";
import { BackendNotification } from "../containers/Notifications";
Expand Down Expand Up @@ -321,6 +326,23 @@ class HttpService {
return promise;
}

fetchProcessesActivities(processName: string) {
return api
.get<{ activities: { date: string; type: ActivityType }[] }>(
`/processes/${encodeURIComponent(processName)}/activity/activities`,
)
.then((res) => {
return res.data.activities.filter(
({ date, type }) =>
type === PredefinedActivityType.ScenarioDeployed ||
type === PredefinedActivityType.ScenarioCanceled ||
type === PredefinedActivityType.PerformedSingleExecution ||
type === PredefinedActivityType.PerformedScheduledExecution,
);
})
.then((res) => res.reverse().map((item) => ({ ...item, type: item.type as PredefinedActivityType })));
}

fetchProcessesDeployments(processName: string) {
return api
.get<
Expand Down

0 comments on commit 47965a8

Please sign in to comment.