-
Notifications
You must be signed in to change notification settings - Fork 1
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
feat: 1406 contrib affichage des status sur ladmin #1425
Changes from 21 commits
f81e68f
f4cdea4
735c7bb
74b922f
cbd9751
d6e391c
40bfa47
ab7e46e
aa99502
8ee2b9c
053b906
101c285
6c28b9c
800c8d1
e269a0f
7356762
2cc2dda
18eee26
0837d9f
0e4f7cf
0a65902
641e499
2b9da39
c554821
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { filterContributionDocumentsToPublish } from "../fetchContributionDocumentsToPublish"; | ||
|
||
describe("filterContributionDocumentsToPublish", () => { | ||
it("should send the list of contributions which have been updated after last export", () => { | ||
const result = filterContributionDocumentsToPublish([ | ||
{ | ||
cdtn_id: "388f8d7860", | ||
//@ts-ignore | ||
export: { | ||
createdAt: "2024-06-03T08:10:03.697249+00:00", | ||
}, | ||
//@ts-ignore | ||
contribution: { | ||
id: "d61a3e21-9bf1-4199-b9d7-944c2382750c", | ||
updatedAt: "2024-03-25T17:25:33.002376+00:00", | ||
statuses: [ | ||
{ | ||
status: "TO_PUBLISH", | ||
createdAt: "2024-03-27T14:26:28.387898+00:00", | ||
}, | ||
], | ||
}, | ||
}, | ||
]); | ||
expect(result?.length).toBe(0); | ||
}); | ||
it("should send the list of contributions which have been updated after last export2", () => { | ||
const result = filterContributionDocumentsToPublish([ | ||
{ | ||
cdtn_id: "388f8d7860", | ||
//@ts-ignore | ||
export: { | ||
createdAt: "2024-06-03T08:10:03.697249+00:00", | ||
}, | ||
//@ts-ignore | ||
contribution: { | ||
id: "d61a3e21-9bf1-4199-b9d7-944c2382750c", | ||
updatedAt: "2024-03-25T17:25:33.002376+00:00", | ||
statuses: [ | ||
{ | ||
status: "TO_PUBLISH", | ||
createdAt: "2024-06-27T14:26:28.387898+00:00", | ||
}, | ||
], | ||
}, | ||
}, | ||
]); | ||
expect(result?.length).toBe(1); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
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"}}) { | ||
cdtnId: cdtn_id | ||
export { | ||
createdAt: created_at | ||
} | ||
contribution { | ||
id | ||
updatedAt: updated_at | ||
statuses(where:{status: {_eq: "TO_PUBLISH"}}, order_by: {created_at: desc}, limit: 1) { | ||
status | ||
createdAt: created_at | ||
} | ||
} | ||
} | ||
} | ||
`; | ||
|
||
interface HasuraReturn { | ||
documents: [DocumentElasticWithSource<ContributionDocumentJson>] | undefined; | ||
} | ||
|
||
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<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,2 +1,3 @@ | ||
export * from "./helpers"; | ||
export * from "./generate"; | ||
export * from "./fetchContributionDocumentsToPublish"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { Export } from "@socialgouv/cdtn-types"; | ||
import { context } from "../context"; | ||
import { gqlClient } from "@shared/utils"; | ||
|
||
export const fetchLastExportStatusQuery = ` | ||
query lastExportStatus { | ||
exportEsStatus: export_es_status(order_by: {created_at: desc}, limit: 1) { | ||
id | ||
} | ||
} | ||
`; | ||
|
||
interface HasuraReturn { | ||
exportEsStatus: [Export] | undefined | ||
} | ||
|
||
|
||
export async function fetchLastExportStatus(): Promise<Export | 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>(fetchLastExportStatusQuery, {}) | ||
.toPromise(); | ||
if (res.error) { | ||
throw res.error; | ||
} | ||
return res.data?.exportEsStatus?.[0]; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
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( | ||
where: {cdtn_id: {_in: $cdtnIds}}, | ||
_set: { | ||
exportId: $exportId | ||
} | ||
) { | ||
returning { | ||
cdtn_id | ||
} | ||
} | ||
}`; | ||
|
||
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(); | ||
|
||
if (!exportStatus?.id) { | ||
return; | ||
} | ||
const cdtnIds = contributionsToPublish.map( | ||
(contribution) => contribution.cdtnId | ||
); | ||
const { id: exportId } = exportStatus; | ||
|
||
if (cdtnIds.length) { | ||
const res = await gqlClient({ | ||
graphqlEndpoint: HASURA_GRAPHQL_ENDPOINT, | ||
adminSecret: HASURA_GRAPHQL_ENDPOINT_SECRET, | ||
}) | ||
.mutation(updateToLastExportStatusMutation, { cdtnIds, exportId }) | ||
.toPromise(); | ||
if (res.error) { | ||
throw res.error; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ import pMap from "p-map"; | |
import { cdtnDocumentsGen } from "./cdtnDocuments"; | ||
import { context } from "./context"; | ||
import { populateSuggestions } from "./suggestion"; | ||
import { Environment } from "@socialgouv/cdtn-types"; | ||
|
||
async function addVector(data: any) { | ||
const NLP_URL = context.get("nlpUrl"); | ||
|
@@ -61,7 +62,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... | ||
|
@@ -75,7 +77,8 @@ export async function ingest( | |
suggestIndexName, | ||
bufferSize, | ||
suggestFile, | ||
disableGlossary | ||
disableGlossary, | ||
isProd | ||
); | ||
} | ||
|
||
|
@@ -89,7 +92,8 @@ async function runIngester( | |
suggestIndexName: string | undefined, | ||
bufferSize: number | undefined, | ||
suggestFile: string | undefined, | ||
disableGlossary: boolean | undefined | ||
disableGlossary: boolean | undefined, | ||
isProd: boolean | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pour toutes ces variables, on utilise des contexte globaux pour éviter de faire du props forwarding, c'est dommage de changer cette approche There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ça revient à deux approches différentes dans le même code There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Faudra que tu me montre, j'avais compris qu'il fallait faire avec du props forwarding de mon côté |
||
) { | ||
const ES_INDEX_PREFIX = esIndexPrefix ?? "cdtn"; | ||
|
||
|
@@ -159,7 +163,7 @@ async function runIngester( | |
size: 800, | ||
}); | ||
}; | ||
await cdtnDocumentsGen(updateDocs); | ||
await cdtnDocumentsGen(updateDocs, isProd); | ||
|
||
logger.info(`done in ${(Date.now() - t0) / 1000} s`); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
C'est dommage qu'on soit obligé de fetch deux fois les contributions, on aurait pu éviter cela en vérifiant une propriété des contributions, non ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On ne fetch pas toutes les contribs là je crois, on ne récupère que celle qui ont été modifiées : https://github.com/SocialGouv/cdtn-admin/pull/1425/files#diff-046031753264baf1637cef36c0bc23cd316e32a73e7250a81133410e2c0eed4fR8
Je pense que le nommage peut être amélioré ici pour éviter cette confusion :