Skip to content

Commit

Permalink
refactor: review
Browse files Browse the repository at this point in the history
  • Loading branch information
victor committed Jun 10, 2024
1 parent aa99502 commit 8ee2b9c
Show file tree
Hide file tree
Showing 13 changed files with 169 additions and 106 deletions.
18 changes: 14 additions & 4 deletions targets/export-elasticsearch/src/ingester/cdtnDocuments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ import {
import { splitArticle } from "./fichesTravailSplitter";
import { createGlossaryTransform } from "./glossary";
import { getVersions } from "./versions";
import { generateContributions } from "./contributions";
import {
fetchContributionDocumentToPublish,
generateContributions,
} from "./contributions";
import { generateAgreements } from "./agreements";
import { getGlossary } from "./common/fetchGlossary";
import { fetchThemes } from "./themes/fetchThemes";
Expand Down Expand Up @@ -56,7 +59,8 @@ export async function getDuplicateSlugs(allDocuments: any) {
}

export async function cdtnDocumentsGen(
updateDocs: (source: string, documents: unknown[]) => Promise<void>
updateDocs: (source: string, documents: unknown[]) => Promise<void>,
isProd: boolean
) {
let documentsCount: Partial<ExportEsStatus["documentsCount"]> = {};

Expand Down Expand Up @@ -190,12 +194,16 @@ export async function cdtnDocumentsGen(
};
}),
}));
logger.info(`Mapped ${fichesMTWithGlossary.length} fiches travail with glossary`);
logger.info(
`Mapped ${fichesMTWithGlossary.length} fiches travail with glossary`
);
documentsCount = {
...documentsCount,
[SOURCES.SHEET_MT_PAGE]: fichesMTWithGlossary.length,
};

const contributionsToPublish = await fetchContributionDocumentToPublish();

await updateDocs(SOURCES.SHEET_MT_PAGE, fichesMTWithGlossary);

logger.info("=== Fiche MT ===");
Expand Down Expand Up @@ -332,7 +340,9 @@ export async function cdtnDocumentsGen(
},
]);

await updateExportStatuses();
if (isProd && contributionsToPublish) {
await updateExportStatuses(contributionsToPublish);
}

logger.info("=== Save the documents length ===");
documentsCount = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { ContributionDocumentJson, DocumentElasticWithSource } from "@socialgouv/cdtn-types";
import {
ContributionDocumentJson,
DocumentElasticWithSource,
} from "@socialgouv/cdtn-types";
import { context } from "../context";
import { gqlClient } from "@shared/utils";

export const fetchContributionDocumentsQuery = `
query fetchContributions {
documents(where: {source: {_eq: "contributions"}}) {
cdtn_id
cdtnId: cdtn_id
export {
createdAt: created_at
}
Expand All @@ -22,30 +25,40 @@ query fetchContributions {
`;

interface HasuraReturn {
documents: [DocumentElasticWithSource<ContributionDocumentJson>] | undefined
documents: [DocumentElasticWithSource<ContributionDocumentJson>] | undefined;
}

export function filterContributionDocumentsToPublish(contributionDocs: DocumentElasticWithSource<any>[] | undefined): DocumentElasticWithSource<any>[] | undefined {
return contributionDocs?.filter((doc) => {
const exportDate = doc.export?.createdAt ? new Date(doc.export?.createdAt).getTime() : 0;
const statusDate = doc.contribution?.statuses.length ? new Date(doc.contribution?.statuses[0].createdAt).getTime() : 0;
return statusDate > exportDate;
})
export function filterContributionDocumentsToPublish(
contributionDocs:
| DocumentElasticWithSource<ContributionDocumentJson>[]
| undefined
): DocumentElasticWithSource<ContributionDocumentJson>[] | undefined {
return contributionDocs?.filter((doc) => {
const exportDate = doc.export?.createdAt
? new Date(doc.export.createdAt).getTime()
: 0;
const statusDate = doc.contribution?.statuses?.length
? new Date(doc.contribution.statuses[0].createdAt).getTime()
: 0;
return statusDate > exportDate;
});
}

export async function fetchContributionDocumentToPublish(): Promise<DocumentElasticWithSource<any>[] | undefined> {
const HASURA_GRAPHQL_ENDPOINT =
context.get("cdtnAdminEndpoint") || "http://localhost:8080/v1/graphql";
const HASURA_GRAPHQL_ENDPOINT_SECRET =
context.get("cdtnAdminEndpointSecret") || "admin1";
const res = await gqlClient({
graphqlEndpoint: HASURA_GRAPHQL_ENDPOINT,
adminSecret: HASURA_GRAPHQL_ENDPOINT_SECRET,
})
.query<HasuraReturn>(fetchContributionDocumentsQuery, {})
.toPromise();
if (res.error) {
throw res.error;
}
return filterContributionDocumentsToPublish(res.data?.documents);
export async function fetchContributionDocumentToPublish(): Promise<
DocumentElasticWithSource<ContributionDocumentJson>[] | undefined
> {
const HASURA_GRAPHQL_ENDPOINT =
context.get("cdtnAdminEndpoint") || "http://localhost:8080/v1/graphql";
const HASURA_GRAPHQL_ENDPOINT_SECRET =
context.get("cdtnAdminEndpointSecret") || "admin1";
const res = await gqlClient({
graphqlEndpoint: HASURA_GRAPHQL_ENDPOINT,
adminSecret: HASURA_GRAPHQL_ENDPOINT_SECRET,
})
.query<HasuraReturn>(fetchContributionDocumentsQuery, {})
.toPromise();
if (res.error) {
throw res.error;
}
return filterContributionDocumentsToPublish(res.data?.documents);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import {fetchContributionDocumentToPublish} from "../contributions";
import {fetchLastExportStatus} from "./fetchLastExportStatus";
import { fetchLastExportStatus } from "./fetchLastExportStatus";
import { gqlClient } from "@shared/utils";
import { context } from "../context";
import {
ContributionDocumentJson,
DocumentElasticWithSource,
} from "@socialgouv/cdtn-types";

export const updateToLastExportStatusMutation = `mutation updateToLastExportStatus($cdtnIds: [String!], $exportId: uuid) {
updateDocuments: update_documents(
Expand All @@ -16,28 +19,32 @@ export const updateToLastExportStatusMutation = `mutation updateToLastExportStat
}
}`;

export async function updateExportStatuses() {
const HASURA_GRAPHQL_ENDPOINT = context.get("cdtnAdminEndpoint") || "http://localhost:8080/v1/graphql";
const HASURA_GRAPHQL_ENDPOINT_SECRET = context.get("cdtnAdminEndpointSecret") || "admin1";
export async function updateExportStatuses(
contributionsToPublish: DocumentElasticWithSource<ContributionDocumentJson>[]
) {
const HASURA_GRAPHQL_ENDPOINT =
context.get("cdtnAdminEndpoint") || "http://localhost:8080/v1/graphql";
const HASURA_GRAPHQL_ENDPOINT_SECRET =
context.get("cdtnAdminEndpointSecret") || "admin1";
const exportStatus = await fetchLastExportStatus();
const contributionsToPublish = await fetchContributionDocumentToPublish();

if (!exportStatus?.id) {
return;
}
const cdtnIds = contributionsToPublish?.map((contribution) => contribution.cdtn_id);
const {id: exportId} = exportStatus;
const cdtnIds = contributionsToPublish.map(
(contribution) => contribution.cdtnId
);
const { id: exportId } = exportStatus;

if (cdtnIds?.length) {
if (cdtnIds.length) {
const res = await gqlClient({
graphqlEndpoint: HASURA_GRAPHQL_ENDPOINT,
adminSecret: HASURA_GRAPHQL_ENDPOINT_SECRET,
})
.mutation(updateToLastExportStatusMutation, { cdtnIds, exportId })
.toPromise();
.mutation(updateToLastExportStatusMutation, { cdtnIds, exportId })
.toPromise();
if (res.error) {
throw res.error;
throw res.error;
}
}
}

12 changes: 8 additions & 4 deletions targets/export-elasticsearch/src/ingester/ingest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { cdtnDocumentsGen } from "./cdtnDocuments";
import { context } from "./context";
import { addCovisits, buildCovisitMap } from "./monolog";
import { populateSuggestions } from "./suggestion";
import { Environment } from "@socialgouv/cdtn-types";

async function addVector(data: any) {
const NLP_URL = context.get("nlpUrl");
Expand Down Expand Up @@ -64,7 +65,8 @@ export async function ingest(
suggestIndexName: string | undefined,
bufferSize: number | undefined,
suggestFile: string | undefined,
disableGlossary: boolean | undefined
disableGlossary: boolean | undefined,
isProd = false
) {
context.provide();
process.env.NLP_URL = nlpUrl; //pour setter la variable d'environment du package elasticsearch...
Expand All @@ -80,7 +82,8 @@ export async function ingest(
suggestIndexName,
bufferSize,
suggestFile,
disableGlossary
disableGlossary,
isProd
);
}

Expand All @@ -96,7 +99,8 @@ async function runIngester(
suggestIndexName: string | undefined,
bufferSize: number | undefined,
suggestFile: string | undefined,
disableGlossary: boolean | undefined
disableGlossary: boolean | undefined,
isProd: boolean
) {
const ES_INDEX_PREFIX = esIndexPrefix || "cdtn";

Expand Down Expand Up @@ -167,7 +171,7 @@ async function runIngester(
size: 800,
});
};
await cdtnDocumentsGen(updateDocs);
await cdtnDocumentsGen(updateDocs, isProd);

logger.info(`done in ${(Date.now() - t0) / 1000} s`);

Expand Down
3 changes: 2 additions & 1 deletion targets/export-elasticsearch/src/workers/ingester-prod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ const ingester = async (): Promise<string> => {
undefined,
undefined,
undefined,
false
false,
true
);
resolve("Export elasticsearch completed successfully");
} catch (error: unknown) {
Expand Down
15 changes: 5 additions & 10 deletions targets/frontend/src/components/contributions/answers/Answer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { fr } from "@codegouvfr/react-dsfr";
import { usePublishContributionMutation } from "./usePublishAnswer";
import { useGenericContributionAnswerQuery } from "./answerGeneric.query";
import { getLastPublicationDate } from "../publication";
import { StatusPublicationContainer } from "../status/StatusPublication";

export type ContributionsAnswerProps = {
id: string;
Expand Down Expand Up @@ -127,20 +128,14 @@ export const ContributionsAnswer = ({
</BreadcrumbLink>
</Breadcrumb>
{answer?.status && (
<div>
<StatusContainer
status={answer.status.status}
<Stack>
<StatusContainer status={answer.status.status} displayText />
<StatusPublicationContainer
statusDate={answer.status.createdAt}
exportDate={answer.publication?.export.createdAt}
displayText
/>
{answer.publication
? `dernière publication: ${getLastPublicationDate({
status: answer.status,
exportStatus: answer.publication?.export,
})}`
: ""}
</div>
</Stack>
)}
</Stack>
<Stack direction="row">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export const Comment = ({ comment, onDelete }: Props) => {
sx={{
border: "1px solid",
borderColor: "grey.300",
minWidth: "300px",
}}
mt={1}
mb={1}
Expand Down Expand Up @@ -81,7 +82,9 @@ export const Comment = ({ comment, onDelete }: Props) => {
}}
>
{" "}
{statusesMapping[comment.status].text === "Publié" ? "À Publier" : statusesMapping[comment.status].text}
{statusesMapping[comment.status].text === "Publié"
? "À Publier"
: statusesMapping[comment.status].text}
</Typography>
</Stack>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,7 @@ export const QuestionList = (): JSX.Element => {
<TableHead>
<TableRow>
<TableCell>Questions ({rows.length})</TableCell>
<TableCell
key={statusesMapping.PUBLISHED.text}
style={{ color: statusesMapping.PUBLISHED.color }}
align="center"
>
<Tooltip title={statusesMapping.PUBLISHED.text}>
{statusesMapping.PUBLISHED.icon}
</Tooltip>
</TableCell>
<TableCell align="center">Publiées</TableCell>
</TableRow>
</TableHead>
<TableBody>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { StatusContainer } from "../status";
import { useRouter } from "next/router";
import { fr } from "@codegouvfr/react-dsfr";
import { getLastPublicationDate } from "../publication";
import { StatusPublicationContainer } from "../status/StatusPublication";

type EditQuestionAnswerListProps = {
answers: Answer[];
Expand Down Expand Up @@ -201,29 +202,18 @@ export const QuestionAnswerList = ({
onClick={() => redirectToAnswer(answer.id)}
>
{answer.status && (
<StatusContainer
status={answer.status.status}
statusDate={answer.status.createdAt}
exportDate={answer.publication?.export.createdAt}
/>
<StatusContainer status={answer.status.status} />
)}
</TableCell>
<TableCell
scope="row"
align="center"
onClick={() => redirectToAnswer(answer.id)}
>
{publicationDate ? (
<StatusContainer
status={"PUBLISHED"}
exportDate={answer.publication?.export.createdAt}
/>
) : (
<StatusContainer
status={"TODO"}
exportDate={answer.publication?.export.createdAt}
/>
)}
<StatusPublicationContainer
exportDate={answer.publication?.export.createdAt}
statusDate={answer.status.createdAt}
/>
</TableCell>
</TableRow>
);
Expand Down
16 changes: 1 addition & 15 deletions targets/frontend/src/components/contributions/status/Status.tsx
Original file line number Diff line number Diff line change
@@ -1,32 +1,18 @@
import { format, parseISO } from "date-fns";
import { Box, Stack, Tooltip } from "@mui/material";

import { Status } from "../type";
import { statusesMapping } from "./data";
import { isPublished } from "../publication";

export const StatusContainer = ({
status,
statusDate,
exportDate,
displayText = false,
dataTestid,
}: {
status: Status;
statusDate?: string;
exportDate?: string;
displayText?: boolean;
dataTestid?: string;
}) => {
let tooltipDate = exportDate
? format(parseISO(exportDate), "dd/MM/yyyy HH:mm:ss")
: "";
const tooltipTitle = !statusDate ? tooltipDate : statusesMapping[status].text;
if (statusDate && exportDate && status === "PUBLISHED") {
status = isPublished({ statusDate, exportDate })
? "PUBLISHED"
: "TO_PUBLISH";
}
const tooltipTitle = statusesMapping[status].text;
return (
<Tooltip title={tooltipTitle}>
<Stack
Expand Down
Loading

0 comments on commit 8ee2b9c

Please sign in to comment.