Skip to content

Commit

Permalink
Merge pull request #388 from mission-apprentissage/ui-server/run-job-…
Browse files Browse the repository at this point in the history
…only-on-anonymmized

[UI - server] Anonymisé que les verbatims déjà anonymisé
  • Loading branch information
yohanngab authored Dec 18, 2024
2 parents faa4e16 + 473a062 commit 2c9ba0e
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 9 deletions.
7 changes: 6 additions & 1 deletion server/src/controllers/jobs.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import tryCatch from "../utils/tryCatch.utils";
export const startJob = tryCatch(async (req: any, res: any) => {
const jobId = uuidv4();
const jobType = req.body.jobType;
const jobOnlyAnonymized = req.body.onlyAnonymized;
const status = JOB_STATUS.IN_PROGRESS;
let worker;

Expand All @@ -17,7 +18,11 @@ export const startJob = tryCatch(async (req: any, res: any) => {
});
} else if (jobType === JOB_TYPES.VERBATIMS_THEMES_EXTRACTION) {
worker = new Worker("./dist/workers/verbatimsExpositionPreparation.js", {
workerData: { jobId, processAll: true },
workerData: {
jobId,
processAll: jobOnlyAnonymized ? false : true,
type: jobOnlyAnonymized ? "onlyAnonymized" : null,
},
});
}

Expand Down
1 change: 1 addition & 0 deletions server/src/validators/jobs.validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export const startJobSchema = Joi.object({
jobType: Joi.string()
.valid(...Object.values(JOB_TYPES))
.required(),
onlyAnonymized: Joi.boolean(),
});
10 changes: 6 additions & 4 deletions server/src/workers/verbatimsExpositionPreparation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,17 +117,19 @@ const cancellationMonitor = async (jobId: string) => {
};

(async () => {
const { jobId, processAll } = workerData;
const { jobId, processAll, type } = workerData;

try {
await connectToPgDb(config.psql.uri);

cancellationMonitor(jobId);

const verbatimsQuery = getKbdClient().selectFrom("verbatims").selectAll();
let verbatimsQuery = getKbdClient().selectFrom("verbatims").selectAll();

if (!processAll) {
verbatimsQuery.where("deleted_at", "is", null).where("themes", "is", null);
if (!processAll && !type) {
verbatimsQuery = verbatimsQuery.where("deleted_at", "is", null).where("themes", "is", null);
} else if (!processAll && type === "onlyAnonymized") {
verbatimsQuery = verbatimsQuery.where("deleted_at", "is", null).where("is_anonymized", "=", true);
}

const verbatims = await verbatimsQuery.execute();
Expand Down
2 changes: 1 addition & 1 deletion ui/src/hooks/useStartJob.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const useStartJob = () => {
const [userContext] = useContext(UserContext);

const { mutate, data, isSuccess, isError, isLoading } = useMutation({
mutationFn: (jobType) => startJob({ jobType, token: userContext.token }),
mutationFn: ({ jobType, onlyAnonymized }) => startJob({ jobType, onlyAnonymized, token: userContext.token }),
mutationKey: "start-job",
onSuccess: () => {
queryClient.invalidateQueries(["jobs"]);
Expand Down
18 changes: 17 additions & 1 deletion ui/src/jobs/ManageJobsPage.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { fr } from "@codegouvfr/react-dsfr";
import { Alert } from "@codegouvfr/react-dsfr/Alert";
import { Button } from "@codegouvfr/react-dsfr/Button";
import { Checkbox } from "@codegouvfr/react-dsfr/Checkbox";
import { Select } from "@codegouvfr/react-dsfr/SelectNext";
import { Table } from "@codegouvfr/react-dsfr/Table";
import { useQueryClient } from "@tanstack/react-query";
Expand Down Expand Up @@ -57,6 +58,7 @@ const ManageJobsPage = () => {

const [selectedJob, setSelectedJob] = useState(null);
const [runningJob, setRunningJob] = useState(null);
const [onlyAlreadyAnonymizedVerbatims, setOnlyAlreadyAnonymizedVerbatims] = useState(false);
const { jobs, isError, isLoading, isSuccess } = useFetchJobs();
const { mutate: startJob, startedJob } = useStartJob();
const { mutate: stopJob, stoppedJob } = useStopJob();
Expand All @@ -65,7 +67,7 @@ const ManageJobsPage = () => {

const handleRunningJob = () => {
if (!selectedJob) return;
startJob(selectedJob);
startJob({ jobType: selectedJob, onlyAnonymized: onlyAlreadyAnonymizedVerbatims });
};

const handleJobCancelation = () => {
Expand Down Expand Up @@ -129,6 +131,20 @@ const ManageJobsPage = () => {
options={jobsOption}
onChange={(event) => setSelectedJob(event.target.value)}
/>
{selectedJob === JOB_TYPES.VERBATIMS_THEMES_EXTRACTION && (
<Checkbox
options={[
{
label: "Uniquement sur les verbatims déjà anonymisés",
nativeInputProps: {
name: `selectOnlyAlreadyAnonymized`,
checked: onlyAlreadyAnonymizedVerbatims,
onChange: () => setOnlyAlreadyAnonymizedVerbatims(!onlyAlreadyAnonymizedVerbatims),
},
},
]}
/>
)}
<Button disabled={!selectedJob || !!runningJob} onClick={handleRunningJob}>
Lancer le job
</Button>
Expand Down
4 changes: 2 additions & 2 deletions ui/src/queries/jobs.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ export const fetchJobs = async ({ token }) => {
return response;
};

export const startJob = async ({ jobType, token }) => {
export const startJob = async ({ jobType, onlyAnonymized, token }) => {
const url = `/jobs/start`;

const response = await apiPost(url, {
body: { jobType },
body: { jobType, onlyAnonymized },
headers: {
Authorization: `Bearer ${token}`,
},
Expand Down

0 comments on commit 2c9ba0e

Please sign in to comment.