From d56b2e49823323b5c87852ea4f398c52b3ccee5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment?= Date: Fri, 1 Mar 2024 13:56:43 +0100 Subject: [PATCH] fix: filter categories associate xAgg response --- webapp-next/components/charts/map/Map.tsx | 1 - .../components/charts/map/MapDetails.tsx | 2 - .../components/charts/table/Header.tsx | 25 +++---- webapp-next/pages/api/elk/data.ts | 65 ++++++++++++++++--- webapp-next/pages/bo/index.tsx | 5 +- webapp-next/utils/cm2d-provider.tsx | 1 - webapp-next/utils/tools.ts | 1 + 7 files changed, 72 insertions(+), 28 deletions(-) diff --git a/webapp-next/components/charts/map/Map.tsx b/webapp-next/components/charts/map/Map.tsx index eeb1c97..d0442dd 100644 --- a/webapp-next/components/charts/map/Map.tsx +++ b/webapp-next/components/charts/map/Map.tsx @@ -14,7 +14,6 @@ type Props = { export default function MapIframe(props: Props) { const iframeRef = React.useRef(null); const { datasets, id } = props; - console.log(datasets); const [mapConfig, setMapConfig] = useState(null); const context = useContext(Cm2dContext); diff --git a/webapp-next/components/charts/map/MapDetails.tsx b/webapp-next/components/charts/map/MapDetails.tsx index d65c83a..f6bdff6 100644 --- a/webapp-next/components/charts/map/MapDetails.tsx +++ b/webapp-next/components/charts/map/MapDetails.tsx @@ -7,8 +7,6 @@ export type Props = { }; export function MapDetails({ mapConfig }: Props) { - console.log(mapConfig); - function extractDetailsValues( input: string ): { label: string; value: number }[] { diff --git a/webapp-next/components/charts/table/Header.tsx b/webapp-next/components/charts/table/Header.tsx index f711055..29490a9 100644 --- a/webapp-next/components/charts/table/Header.tsx +++ b/webapp-next/components/charts/table/Header.tsx @@ -26,7 +26,8 @@ type Field = | 'home_department' | 'months' | 'categories_level_1' - | 'categories_level_2'; + | 'categories_level_2' + | 'categories_associate'; export function ChartTableHeader() { const context = useContext(Cm2dContext); @@ -246,16 +247,18 @@ export function ChartTableHeader() { Colonnes : {getLabelFromElkField(aggregateY)} - {availableFields.map(field => ( - handleYAxisChange(field.value)} - > - - {field.label} - - - ))} + {availableFields + .filter(field => field.value !== 'categories_associate') + .map(field => ( + handleYAxisChange(field.value)} + > + + {field.label} + + + ))} diff --git a/webapp-next/pages/api/elk/data.ts b/webapp-next/pages/api/elk/data.ts index 1e44008..971e308 100644 --- a/webapp-next/pages/api/elk/data.ts +++ b/webapp-next/pages/api/elk/data.ts @@ -1,17 +1,64 @@ -import { Client } from '@elastic/elasticsearch'; -import fs from 'fs'; -import path from 'path'; +import { ELASTIC_API_KEY_NAME } from '@/utils/tools'; +import { Client, TransportResult } from '@elastic/elasticsearch'; import { AggregationsAggregate, + SearchResponse, SearchResponseBody } from '@elastic/elasticsearch/lib/api/types'; +import fs from 'fs'; import { NextApiRequest, NextApiResponse } from 'next'; -import { ELASTIC_API_KEY_NAME } from '@/utils/tools'; +import path from 'path'; type Data = { result: SearchResponseBody>; }; +// HANDLE ASSOCIATE CAUSES FILTER RESULT +const transformResult = ( + result: TransportResult< + SearchResponse>, + unknown + >, + aggregations: any, + filters: any +): TransportResult< + SearchResponse>, + unknown +> => { + let tmpResult = result; + if (aggregations.aggregated_x?.terms?.field === 'categories_associate') { + filters.forEach((filter: any) => { + const filterCategoriesAssociate = filter.bool?.should + ?.filter( + (should: any) => + !!(should.match && should.match['categories_associate']) + ) + .map((r: any) => r.match['categories_associate']) as any[]; + + if (filterCategoriesAssociate && !!filterCategoriesAssociate.length) { + const responseAggregateX: any = result.body.aggregations?.aggregated_x; + if (responseAggregateX) { + tmpResult = { + ...result, + body: { + ...result.body, + aggregations: { + ...result.body.aggregations, + aggregated_x: { + buckets: responseAggregateX.buckets?.filter((bucket: any) => { + return filterCategoriesAssociate.includes(bucket.key); + }) + } + } + } + }; + } + } + }); + } + return tmpResult; +}; + export default async function handler( req: NextApiRequest, res: NextApiResponse @@ -22,13 +69,11 @@ export default async function handler( apiKey: req.cookies[ELASTIC_API_KEY_NAME] as string }, tls: { - ca: fs.readFileSync( - path.resolve(process.cwd(), './certs/ca/ca.crt') - ), + ca: fs.readFileSync(path.resolve(process.cwd(), './certs/ca/ca.crt')), rejectUnauthorized: false } }); - + // Parse the filters, aggregations and index from the query parameters const filters = req.query.filters ? JSON.parse(req.query.filters as string) @@ -55,5 +100,7 @@ export default async function handler( { meta: true } ); - res.status(200).json({ result: result.body }); + res + .status(200) + .json({ result: transformResult(result, aggregations, filters).body }); } diff --git a/webapp-next/pages/bo/index.tsx b/webapp-next/pages/bo/index.tsx index 7adb1ff..d8213ed 100644 --- a/webapp-next/pages/bo/index.tsx +++ b/webapp-next/pages/bo/index.tsx @@ -30,10 +30,7 @@ export default function Home() { const { filters, aggregations, view, setCSVData } = context; - const { data, dataKind, isLoading } = useData( - filters, - addMissingSizes(aggregations, filters.categories_associate.length) - ); + const { data, dataKind, isLoading } = useData(filters, aggregations); const fetchNewTitle = async () => { if (!filters.categories[0]) setTitle('Nombre de certificats de décès'); diff --git a/webapp-next/utils/cm2d-provider.tsx b/webapp-next/utils/cm2d-provider.tsx index f412cc3..1890ddc 100644 --- a/webapp-next/utils/cm2d-provider.tsx +++ b/webapp-next/utils/cm2d-provider.tsx @@ -82,7 +82,6 @@ export const baseAggregation = { export function Cm2dProvider({ children }: Cm2dProviderProps) { const [filters, setFilters] = useState(baseFilters); - console.log(filters); const [selectedFiltersPile, setSelectedFiltersPile] = useState([]); const [aggregations, setAggregations] = useState(baseAggregation); diff --git a/webapp-next/utils/tools.ts b/webapp-next/utils/tools.ts index b2fd49a..d3ebfbb 100644 --- a/webapp-next/utils/tools.ts +++ b/webapp-next/utils/tools.ts @@ -723,6 +723,7 @@ export function capitalizeString(str: string): string { return str.toString().charAt(0).toUpperCase() + str.toString().substring(1); } +// NOT USED ANYMORE export function addMissingSizes(obj: any, size: number): any { if (typeof obj === 'object') { if (obj.terms && obj.terms.field === 'categories_associate') {