Skip to content

Commit

Permalink
Merge pull request #91 from SocialGouv/fix/categories-associate-filte…
Browse files Browse the repository at this point in the history
…r-response

fix: filter categories associate xAgg response
  • Loading branch information
ClementNumericite authored Mar 1, 2024
2 parents f58d8ea + d56b2e4 commit d6bba73
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 28 deletions.
1 change: 0 additions & 1 deletion webapp-next/components/charts/map/Map.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<MapConfig | null>(null);
const context = useContext(Cm2dContext);

Expand Down
2 changes: 0 additions & 2 deletions webapp-next/components/charts/map/MapDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ export type Props = {
};

export function MapDetails({ mapConfig }: Props) {
console.log(mapConfig);

function extractDetailsValues(
input: string
): { label: string; value: number }[] {
Expand Down
25 changes: 14 additions & 11 deletions webapp-next/components/charts/table/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -246,16 +247,18 @@ export function ChartTableHeader() {
Colonnes : <Text as="b">{getLabelFromElkField(aggregateY)}</Text>
</MenuButton>
<MenuList>
{availableFields.map(field => (
<MenuItem
key={field.value}
onClick={() => handleYAxisChange(field.value)}
>
<Text as={aggregateY === field.value ? 'b' : 'span'}>
{field.label}
</Text>
</MenuItem>
))}
{availableFields
.filter(field => field.value !== 'categories_associate')
.map(field => (
<MenuItem
key={field.value}
onClick={() => handleYAxisChange(field.value)}
>
<Text as={aggregateY === field.value ? 'b' : 'span'}>
{field.label}
</Text>
</MenuItem>
))}
</MenuList>
</Menu>
</Flex>
Expand Down
65 changes: 56 additions & 9 deletions webapp-next/pages/api/elk/data.ts
Original file line number Diff line number Diff line change
@@ -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<unknown, Record<string, AggregationsAggregate>>;
};

// HANDLE ASSOCIATE CAUSES FILTER RESULT
const transformResult = (
result: TransportResult<
SearchResponse<unknown, Record<string, AggregationsAggregate>>,
unknown
>,
aggregations: any,
filters: any
): TransportResult<
SearchResponse<unknown, Record<string, AggregationsAggregate>>,
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<Data>
Expand All @@ -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)
Expand All @@ -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 });
}
5 changes: 1 addition & 4 deletions webapp-next/pages/bo/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
1 change: 0 additions & 1 deletion webapp-next/utils/cm2d-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ export const baseAggregation = {

export function Cm2dProvider({ children }: Cm2dProviderProps) {
const [filters, setFilters] = useState<Filters>(baseFilters);
console.log(filters);
const [selectedFiltersPile, setSelectedFiltersPile] = useState<string[]>([]);
const [aggregations, setAggregations] = useState<any>(baseAggregation);

Expand Down
1 change: 1 addition & 0 deletions webapp-next/utils/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down

0 comments on commit d6bba73

Please sign in to comment.