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

🐛 Harden status for assessed archetypes #1671

Closed
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { Spinner } from "@patternfly/react-core";
import { EmptyTextMessage } from "@app/components/EmptyTextMessage";
import { Application } from "@app/api/models";
import { IconedStatus, IconedStatusPreset } from "@app/components/IconedStatus";
import { useFetchAssessmentsByItemId } from "@app/queries/assessments";
import {
useFetchAllAssessmentsWithArchetypes,
useFetchAssessmentsByItemId,
} from "@app/queries/assessments";
import { useFetchArchetypes } from "@app/queries/archetypes";
interface ApplicationAssessmentStatusProps {
application: Application;
Expand All @@ -16,19 +19,61 @@ export const ApplicationAssessmentStatus: React.FC<
> = ({ application }) => {
const { t } = useTranslation();

const { archetypes, isFetching } = useFetchArchetypes();
const { archetypes, isFetching } = useFetchArchetypes(application);
console.log("app assessment status", archetypes, application.name);

const applicationArchetypes = application.archetypes?.map((archetypeRef) => {
return archetypes?.find((archetype) => archetype.id === archetypeRef.id);
});
const {
assessmentsWithArchetypes,
isLoading: isFetchingAllAssessmentsWithArchetypesLoading,
} = useFetchAllAssessmentsWithArchetypes(archetypes);

const assessedArchetypesWithARequiredAssessment = assessmentsWithArchetypes
?.filter((assessmentsWithArchetype) => {
return (
assessmentsWithArchetype.archetype.assessed &&
assessmentsWithArchetype.assessments.some(
(assessment) => assessment?.required === true
)
);
})
.map((assessmentsWithArchetype) => assessmentsWithArchetype.archetype);

const allArchetypesAssessed =
assessmentsWithArchetypes.length > 0 &&
assessmentsWithArchetypes?.every((assessmentsWithArchetype) => {
const requiredAssessments = assessmentsWithArchetype.assessments.filter(
(assessment) => assessment?.required === true
);
return (
assessmentsWithArchetype.archetype.assessed &&
assessmentsWithArchetype.assessments.length > 0 &&
requiredAssessments.length > 0 &&
requiredAssessments.every(
(assessment) => assessment?.status === "complete"
)
);
});

const someArchetypesAssessed = applicationArchetypes?.some(
(archetype) => !!archetype?.assessments?.length ?? 0 > 0
const hasInProgressOrNotStartedRequiredAssessments = () => {
return (
assessmentsWithArchetypes?.some(
(assessmentsWithArchetype) =>
!assessmentsWithArchetype.archetype.assessed &&
assessmentsWithArchetype.assessments.some(
(assessment) =>
assessment?.required === true &&
(assessment.status === "empty" ||
assessment.status === "started" ||
assessment.status === "complete")
)
) ?? false
);
};
console.log(
"has in progress",
hasInProgressOrNotStartedRequiredAssessments(),
{ assessmentsWithArchetypes }
);
const areAllArchetypesAssessed =
applicationArchetypes?.every(
(archetype) => archetype?.assessments?.length ?? 0 > 0
) ?? false;

const {
assessments,
Expand All @@ -47,27 +92,28 @@ export const ApplicationAssessmentStatus: React.FC<
let statusPreset: IconedStatusPreset = "NotStarted"; // Default status
let tooltipCount: number = 0;

const assessedArchetypeCount =
applicationArchetypes?.filter(
(archetype) => archetype?.assessments?.length ?? 0 > 0
).length || 0;

const isDirectlyAssessed =
application.assessed && (application.assessments?.length ?? 0) > 0;

console.log("app assessment data", {
allArchetypesAssessed,
assessmentsWithArchetypes,
application,
archetypes,
assessments,
assessedArchetypesWithARequiredAssessment,
});

if (isDirectlyAssessed) {
statusPreset = "Completed";
} else if (areAllArchetypesAssessed) {
} else if (allArchetypesAssessed) {
statusPreset = "InheritedAssessments";
tooltipCount = assessedArchetypeCount;
} else if (someArchetypesAssessed) {
tooltipCount = assessedArchetypesWithARequiredAssessment?.length ?? 0;
} else if (hasInProgressOrNotStartedRequiredAssessments()) {
statusPreset = "InProgressInheritedAssessments";
tooltipCount = assessedArchetypeCount;
tooltipCount = assessedArchetypesWithARequiredAssessment?.length ?? 0;
} else if (
assessments?.some(
(assessment) =>
assessment.status === "started" || assessment.status === "complete"
)
assessments?.some((assessment) => assessment.status === "started")
) {
statusPreset = "InProgress";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ export const ApplicationLandscape: React.FC<IApplicationLandscapeProps> = ({
() => aggregateRiskData(filteredAssessments, applications),
[filteredAssessments, applications]
);
console.log("landscapeData", landscapeData);

return (
<ConditionalRender
Expand Down
11 changes: 9 additions & 2 deletions client/src/app/queries/archetypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ export const useFetchArchetypes = (forApplication?: Application | null) => {
queryFn: getArchetypes,
refetchInterval: 5000,
onSuccess: (fetchedArchetypes) => {
if (forApplication && forApplication.archetypes) {
if (!forApplication) {
setFilteredArchetypes(fetchedArchetypes);
} else if (
Array.isArray(forApplication.archetypes) &&
forApplication.archetypes.length > 0
) {
const archetypeIds = forApplication.archetypes.map(
(archetype) => archetype.id
);
Expand All @@ -39,8 +44,10 @@ export const useFetchArchetypes = (forApplication?: Application | null) => {
);
setFilteredArchetypes(filtered);
} else {
setFilteredArchetypes(fetchedArchetypes);
setFilteredArchetypes([]);
}

// Invalidate queries as needed
queryClient.invalidateQueries([reviewsQueryKey]);
queryClient.invalidateQueries([assessmentsQueryKey]);
queryClient.invalidateQueries([assessmentsByItemIdQueryKey]);
Expand Down
Loading