Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Reports - Current Landscape update risk handling #1395

Merged
merged 3 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions client/public/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@
"noDataStateBody": "Create a new {{what}} to start seeing data here.",
"noDataStateTitle": "No {{what}} available",
"Nquestions": "{{n}} questions",
"ofTotalApplications": "Of {{count}} application",
"ofTotalApplications_plural": "Of {{count}} applications",
"ofTotalAssessments": "Of {{count}} assessment",
"ofTotalAssessments_plural": "Of {{count}} assessments",
"selectMany": "Select {{what}}",
"selectOne": "Select a {{what}}",
"selectAn": "Select an {{what}}",
Expand Down
2 changes: 0 additions & 2 deletions client/public/locales/es/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@
"noDataStateBody": "Cree un(a) nuevo(a) {{what}} para empezar a ver datos acá.",
"noDataStateTitle": "No existen {{what}} disponibles",
"Nquestions": "{{n}} preguntas",
"ofTotalApplications": "De {{count}} aplicación",
"ofTotalApplications_plural": "De {{count}} aplicaciones",
"selectMany": "Seleccione {{what}}",
"selectOne": "Seleccione un {{what}}",
"selectAn": "Seleccione una {{what}}",
Expand Down
46 changes: 28 additions & 18 deletions client/src/app/pages/reports/components/landscape/donut.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@ import { useTranslation } from "react-i18next";
import { ChartDonut } from "@patternfly/react-charts";
import { global_palette_black_300 as black } from "@patternfly/react-tokens";

import { Stack, StackItem, Text, TextContent } from "@patternfly/react-core";
import {
Bullseye,
Stack,
StackItem,
Text,
TextContent,
} from "@patternfly/react-core";

export interface IDonutProps {
id: string;
value: number;
total: number;
color: string;
Expand All @@ -15,6 +22,7 @@ export interface IDonutProps {
}

export const Donut: React.FC<IDonutProps> = ({
id,
value,
total,
color,
Expand All @@ -24,24 +32,26 @@ export const Donut: React.FC<IDonutProps> = ({
const { t } = useTranslation();

return (
<Stack>
<StackItem style={{ height: "200px", width: "200px" }}>
<ChartDonut
ariaDesc="risk-donut-chart"
title={value.toString()}
subTitle={t("composed.ofTotalApplications", {
count: total,
}).toLocaleLowerCase()}
constrainToVisibleArea={true}
data={[
{ x: riskLabel, y: value },
{ x: t("terms.other"), y: total - value },
]}
labels={({ datum }) => `${datum.x}: ${datum.y}`}
colorScale={[color, black.value]}
/>
<Stack id={id} style={{ width: "200px" }}>
<StackItem style={{ height: "200px", width: "100%" }}>
<Bullseye>
<ChartDonut
ariaDesc="risk-donut-chart"
title={value.toString()}
subTitle={t("composed.ofTotalAssessments", {
count: total,
}).toLocaleLowerCase()}
constrainToVisibleArea={true}
data={[
{ x: riskLabel, y: value },
{ x: t("terms.other"), y: total - value },
]}
labels={({ datum }) => `${datum.x}: ${datum.y}`}
colorScale={[color, black.value]}
/>
</Bullseye>
</StackItem>
<StackItem>
<StackItem style={{ width: "100%" }}>
<TextContent className="pf-v5-u-text-align-center">
<Text component="h3">{riskLabel}</Text>
<Text component="small">{riskDescription}</Text>
Expand Down
151 changes: 76 additions & 75 deletions client/src/app/pages/reports/components/landscape/landscape.tsx
Original file line number Diff line number Diff line change
@@ -1,37 +1,29 @@
import React, { useContext, useMemo } from "react";
import React, { useMemo } from "react";
import { useTranslation } from "react-i18next";

import { Skeleton, Split, SplitItem } from "@patternfly/react-core";

import { ConditionalRender } from "@app/components/ConditionalRender";
import { StateError } from "@app/components/StateError";
import { Flex, FlexItem, Skeleton } from "@patternfly/react-core";

import { RISK_LIST } from "@app/Constants";
import { Assessment, AssessmentRisk } from "@app/api/models";

import { ApplicationSelectionContext } from "../../application-selection-context";
import { NoApplicationSelectedEmptyState } from "../no-application-selected-empty-state";
import { Assessment, Questionnaire } from "@app/api/models";
import { ConditionalRender } from "@app/components/ConditionalRender";
import { Donut } from "./donut";
import { useFetchRisks } from "@app/queries/risks";

interface ILandscapeData {
low: number;
medium: number;
high: number;
interface IAggregateRiskData {
green: number;
yellow: number;
red: number;
unknown: number;
unassessed: number;
assessmentCount: number;
}

const extractLandscapeData = (
totalApps: number,
data: AssessmentRisk[]
): ILandscapeData => {
const aggregateRiskData = (assessments: Assessment[]): IAggregateRiskData => {
let low = 0;
let medium = 0;
let high = 0;
let unassessed = 0;
let unknown = 0;

data.forEach((elem) => {
switch (elem.risk) {
assessments?.forEach((assessment) => {
switch (assessment.risk) {
case "green":
low++;
break;
Expand All @@ -41,93 +33,102 @@ const extractLandscapeData = (
case "red":
high++;
break;
case "unknown":
unknown++;
break;
}
});

unassessed = totalApps - low - medium - high;
return { low, medium, high, unassessed };
return {
green: low,
yellow: medium,
red: high,
unknown,
unassessed: assessments.length - low - medium - high,
assessmentCount: assessments.length,
};
};

interface ILandscapeProps {
/**
* The selected questionnaire or `null` if _all questionnaires_ is selected.
*/
questionnaire: Questionnaire | null;

/**
* The set of assessments for the selected questionnaire. Risk values will be
* aggregated from the individual assessment risks.
*/
assessments: Assessment[];
}

export const Landscape: React.FC<ILandscapeProps> = ({ assessments }) => {
export const Landscape: React.FC<ILandscapeProps> = ({
questionnaire,
assessments,
}) => {
const { t } = useTranslation();

// Context
const { allItems: applications } = useContext(ApplicationSelectionContext);

const {
risks: assessmentRisks,
isFetching,
error: fetchError,
} = useFetchRisks(applications.map((app) => app.id!));

const landscapeData = useMemo(() => {
if (applications.length > 0 && assessmentRisks) {
return extractLandscapeData(applications.length, assessmentRisks);
} else {
return undefined;
}
}, [applications, assessmentRisks]);

if (fetchError) {
return <StateError />;
}

if (!isFetching && !landscapeData) {
return <NoApplicationSelectedEmptyState />;
}
const landscapeData = useMemo(
() => aggregateRiskData(assessments),
[assessments]
);

return (
<ConditionalRender
when={isFetching}
when={!questionnaire && !assessments}
then={
<div style={{ height: 200, width: 400 }}>
<Skeleton height="75%" width="100%" />
</div>
}
>
{landscapeData && (
<Split hasGutter>
<SplitItem>
<Flex
justifyContent={{ default: "justifyContentSpaceAround" }}
spaceItems={{ default: "spaceItemsNone" }}
gap={{ default: "gapMd" }}
>
<FlexItem>
<Donut
value={landscapeData.high}
total={applications.length}
color={RISK_LIST["red"].hexColor}
id="landscape-donut-red"
value={landscapeData.red}
total={landscapeData.assessmentCount}
color={RISK_LIST.red.hexColor}
riskLabel={t("colors.red")}
// riskDescription={}
riskDescription={questionnaire?.riskMessages?.red ?? ""}
/>
</SplitItem>
<SplitItem>
</FlexItem>
<FlexItem>
<Donut
value={landscapeData.medium}
total={applications.length}
color={RISK_LIST["yellow"].hexColor}
id="landscape-donut-yellow"
value={landscapeData.yellow}
total={landscapeData.assessmentCount}
color={RISK_LIST.yellow.hexColor}
riskLabel={t("colors.yellow")}
// riskDescription={}
riskDescription={questionnaire?.riskMessages?.yellow ?? ""}
/>
</SplitItem>
<SplitItem>
</FlexItem>
<FlexItem>
<Donut
value={landscapeData.high}
total={applications.length}
color={RISK_LIST["green"].hexColor}
id="landscape-donut-green"
value={landscapeData.green}
total={landscapeData.assessmentCount}
color={RISK_LIST.green.hexColor}
riskLabel={t("colors.green")}
// riskDescription={}
riskDescription={questionnaire?.riskMessages?.green ?? ""}
/>
</SplitItem>
<SplitItem>
</FlexItem>
<FlexItem>
<Donut
id="landscape-donut-unassessed"
value={landscapeData.unassessed}
total={applications.length}
color={RISK_LIST["unknown"].hexColor}
total={landscapeData.assessmentCount}
color={RISK_LIST.unknown.hexColor}
riskLabel={`${t("terms.unassessed")}/${t("terms.unknown")}`}
// riskDescription={}
riskDescription={questionnaire?.riskMessages?.unknown ?? ""}
/>
</SplitItem>
</Split>
</FlexItem>
</Flex>
)}
</ConditionalRender>
);
Expand Down
Loading
Loading