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

Poll classification results for datasets #2123

Merged
merged 6 commits into from
Jan 5, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ The types of changes are:

* Added the connection key to the execution log [#2100](https://github.com/ethyca/fides/pull/2100)
* Added endpoints to retrieve DSR `Rule`s and `Rule Target`s [#2116](https://github.com/ethyca/fides/pull/2116)
* Dataset classification UI now polls for results [#2123](https://github.com/ethyca/fides/pull/2123)

### Changed

Expand Down
48 changes: 48 additions & 0 deletions clients/admin-ui/src/features/common/classifications/helpers.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { useEffect, useState } from "react";

import { useGetAllClassifyInstancesQuery } from "~/features/plus/plus.slice";
import { ClassificationStatus, GenerateTypes } from "~/types/api";

const POLL_INTERVAL_SECONDS = 3;

/**
* Poll for updates to classification until all classifications are finished
*/
export const usePollForClassifications = ({
resourceType,
fidesKeys,
skip,
}: {
resourceType: GenerateTypes;
fidesKeys?: string[];
skip?: boolean;
}) => {
const [shouldPoll, setShouldPoll] = useState(true);
const result = useGetAllClassifyInstancesQuery(
{
resource_type: resourceType,
fides_keys: fidesKeys,
},
{
skip,
pollingInterval: shouldPoll ? POLL_INTERVAL_SECONDS * 1000 : undefined,
}
);

const isClassificationFinished = result.data
? result.data.every(
allisonking marked this conversation as resolved.
Show resolved Hide resolved
(c) =>
c.status === ClassificationStatus.COMPLETE ||
c.status === ClassificationStatus.FAILED ||
c.status === ClassificationStatus.REVIEWED
)
: false;

useEffect(() => {
if (isClassificationFinished) {
setShouldPoll(false);
}
}, [isClassificationFinished]);

return { ...result, isClassificationFinished };
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./helpers";
16 changes: 6 additions & 10 deletions clients/admin-ui/src/features/dataset/DatasetTable.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { Table, Tbody, Td, Th, Thead, Tr } from "@fidesui/react";
import { useDispatch, useSelector } from "react-redux";

import { usePollForClassifications } from "~/features/common/classifications";
import { useFeatures } from "~/features/common/features";
import ClassificationStatusBadge from "~/features/plus/ClassificationStatusBadge";
import {
selectDatasetClassifyInstanceMap,
useGetAllClassifyInstancesQuery,
} from "~/features/plus/plus.slice";
import { selectDatasetClassifyInstanceMap } from "~/features/plus/plus.slice";
import { Dataset, GenerateTypes } from "~/types/api";

import {
Expand All @@ -21,12 +19,10 @@ const DatasetsTable = () => {

const { data: datasets } = useGetAllDatasetsQuery();
const features = useFeatures();
useGetAllClassifyInstancesQuery(
{ resource_type: GenerateTypes.DATASETS },
{
skip: !features.plus,
}
);
usePollForClassifications({
resourceType: GenerateTypes.DATASETS,
skip: !features.plus,
});
const classifyInstanceMap = useSelector(selectDatasetClassifyInstanceMap);

const handleRowClick = (dataset: Dataset) => {
Expand Down
49 changes: 13 additions & 36 deletions clients/admin-ui/src/pages/classify-systems/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,21 @@ import { Button, Heading, HStack, Spinner, Stack, Text } from "@fidesui/react";
import type { NextPage } from "next";
import NextLink from "next/link";
import { useRouter } from "next/router";
import { ReactNode, useEffect, useState } from "react";
import { ReactNode, useEffect } from "react";
import { useDispatch } from "react-redux";

import { useAppSelector } from "~/app/hooks";
import { usePollForClassifications } from "~/features/common/classifications";
import { useInterzoneNav } from "~/features/common/hooks/useInterzoneNav";
import Layout from "~/features/common/Layout";
import {
useGetAllClassifyInstancesQuery,
useGetHealthQuery,
} from "~/features/plus/plus.slice";
import { useGetHealthQuery } from "~/features/plus/plus.slice";
import {
selectSystemsToClassify,
setSystemsToClassify,
useGetAllSystemsQuery,
} from "~/features/system";
import ClassifySystemsTable from "~/features/system/ClassifySystemsTable";
import { ClassificationStatus, GenerateTypes } from "~/types/api";

const POLL_INTERVAL_SECONDS = 3;
import { GenerateTypes } from "~/types/api";

const ClassifySystemsLayout = ({ children }: { children: ReactNode }) => (
<Layout title="Classify Systems">
Expand Down Expand Up @@ -58,19 +54,15 @@ const ClassifySystems: NextPage = () => {
}
}, [dispatch, allSystems]);

// Poll for updates to classification until all classifications are finished
const [shouldPoll, setShouldPoll] = useState(true);
const { isLoading: isLoadingClassifications, data: classifications } =
useGetAllClassifyInstancesQuery(
{
resource_type: GenerateTypes.SYSTEMS,
fides_keys: systems?.map((s) => s.fides_key),
},
{
skip: !hasPlus,
pollingInterval: shouldPoll ? POLL_INTERVAL_SECONDS * 1000 : undefined,
}
);
const {
isLoading: isLoadingClassifications,
data: classifications,
isClassificationFinished,
} = usePollForClassifications({
resourceType: GenerateTypes.SYSTEMS,
fidesKeys: systems?.map((s) => s.fides_key),
skip: !hasPlus,
});

useEffect(() => {
if (!isLoadingPlus && !hasPlus) {
Expand All @@ -80,21 +72,6 @@ const ClassifySystems: NextPage = () => {

const isLoading = isLoadingSystems || isLoadingClassifications;

const isClassificationFinished = classifications
? classifications.every(
(c) =>
c.status === ClassificationStatus.COMPLETE ||
c.status === ClassificationStatus.FAILED ||
c.status === ClassificationStatus.REVIEWED
)
: false;

useEffect(() => {
if (isClassificationFinished) {
setShouldPoll(false);
}
}, [isClassificationFinished]);

if (isLoading) {
return (
<ClassifySystemsLayout>
Expand Down