Skip to content

Commit c5d6bd4

Browse files
committed
chore(patents): use organisation request instead of patents for bubble graph
1 parent 5d93162 commit c5d6bd4

File tree

6 files changed

+49
-112
lines changed

6 files changed

+49
-112
lines changed

client/src/api/organizations/[id]/index.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ async function getStructurePublicationsById(id: string): Promise<any> {
208208
}
209209
}).filter(el => el) || [];
210210

211-
return { publicationsCount, byYear, byType, bySource, byAuthors, byWiki, byOpenAlexFields, byInfrastructureName, byGrantsFunder, bySupportEntity } || {}
211+
return { publicationsCount, byYear, byType, bySource, byAuthors, byWiki, byOpenAlexFields, byInfrastructureName, byGrantsFunder, bySupportEntity }
212212
}
213213

214214
async function getStructureProjectsById(id: string): Promise<any> {
@@ -290,7 +290,21 @@ async function getStructurePatentsById(id: string): Promise<any> {
290290
size: 25,
291291

292292
}
293-
}
293+
},
294+
byCpc: {
295+
terms: {
296+
field: "cpc.classe.code.keyword",
297+
size: 10000,
298+
},
299+
aggs: {
300+
bySectionLabel: {
301+
terms: {
302+
field: "cpc.section.label.keyword",
303+
size: 1,
304+
},
305+
},
306+
},
307+
},
294308
}
295309
}
296310
const res = await fetch(
@@ -309,5 +323,15 @@ async function getStructurePatentsById(id: string): Promise<any> {
309323
normalizedCount: element.doc_count * 100 / _100Year,
310324
}
311325
}).sort((a, b) => a.label - b.label).reduce(fillWithMissingYears, []) || [];
312-
return { byYear, patentsCount }
326+
const byCpc =
327+
data?.byCpc?.buckets
328+
?.map((element) => {
329+
return {
330+
value: element.key.split("###")?.[0],
331+
label: element.key.split("###")?.[1],
332+
count: element.doc_count,
333+
};
334+
})
335+
.filter((el) => el) || [];
336+
return { byYear, byCpc, patentsCount }
313337
}

client/src/api/patents/[id]/index.ts

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -32,71 +32,3 @@ export async function getPatentById(id: string): Promise<Patent> {
3232
if (!patent) throw new Error("404");
3333
return { ...patent, _id: data?.hits?.hits?.[0]._id };
3434
}
35-
36-
export async function getCpcAggregation(value: string): Promise<Patent> {
37-
const body: any = {
38-
size: 10000,
39-
query: {
40-
bool: {
41-
must: [
42-
{
43-
term: {
44-
"applicants.ids.id.keyword": value,
45-
},
46-
},
47-
],
48-
},
49-
},
50-
aggs: {
51-
byCpc: {
52-
terms: {
53-
field: "cpc.classe.code.keyword",
54-
size: 10000,
55-
},
56-
aggs: {
57-
bySectionLabel: {
58-
terms: {
59-
field: "cpc.section.label.keyword",
60-
size: 1,
61-
},
62-
},
63-
},
64-
},
65-
},
66-
};
67-
68-
const res = await fetch(`${patentsIndex}/_search`, {
69-
method: "POST",
70-
body: JSON.stringify(body),
71-
headers: postHeaders,
72-
});
73-
74-
const data = await res.json();
75-
76-
const buckets = data?.aggregations?.byCpc?.buckets;
77-
const hits = data?.hits?.hits;
78-
79-
const labelsByCode = hits.reduce((acc: any, hit: any) => {
80-
const cpcGroups = hit._source.cpc?.classe ?? [];
81-
cpcGroups.forEach((cpc: any) => {
82-
if (!acc[cpc.code]) {
83-
acc[cpc.code] = cpc.label;
84-
}
85-
});
86-
return acc;
87-
}, {});
88-
89-
const patent = buckets.map((bucket: any) => {
90-
const sectionLabel =
91-
bucket.bySectionLabel?.buckets?.[0]?.key || "Label de section non trouvé";
92-
93-
return {
94-
code: bucket.key,
95-
doc_count: bucket.doc_count,
96-
label: labelsByCode[bucket.key] || "Label non trouvé",
97-
sectionLabel,
98-
};
99-
});
100-
101-
return patent;
102-
}

client/src/components/patent-chart/index.tsx

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,12 @@ import Highcharts from "highcharts";
33
import HighchartsReact from "highcharts-react-official";
44
import HighchartsMore from "highcharts/highcharts-more";
55
import { useIntl } from "react-intl";
6+
import { Aggregation } from "../../types/commons";
67

78
HighchartsMore(Highcharts);
89

910
type CpcChartProps = {
10-
data: {
11-
label: string;
12-
doc_count: number;
13-
code: string;
14-
}[];
11+
data: Aggregation[];
1512
};
1613

1714
const sectionLabels: Record<string, string> = {
@@ -40,11 +37,11 @@ const CpcChart: React.FC<CpcChartProps> = ({ data }) => {
4037
"#AEA397",
4138
];
4239
const groupedData = data.reduce((acc, item) => {
43-
const firstLetter = item.code.charAt(0).toUpperCase();
40+
const firstLetter = item.value.charAt(0).toUpperCase();
4441
if (!acc[firstLetter]) acc[firstLetter] = [];
4542
acc[firstLetter].push({
46-
name: item.code,
47-
value: item.doc_count,
43+
name: item.value,
44+
value: item.count,
4845
label: item.label,
4946
});
5047
return acc;
@@ -54,9 +51,9 @@ const CpcChart: React.FC<CpcChartProps> = ({ data }) => {
5451
([letter, items], index) => {
5552
const totalCount = items.reduce((sum, item) => sum + item.value, 0);
5653
return {
57-
name:
58-
intl.formatMessage({ id: sectionLabels[letter] }) ||
59-
`Section ${letter}`,
54+
name: sectionLabels[letter]
55+
? intl.formatMessage({ id: sectionLabels[letter] })
56+
: `Section ${letter}`,
6057
color: colorPalette[index % colorPalette.length],
6158
data: items.map((item) => ({
6259
name: item.name,
@@ -105,18 +102,19 @@ const CpcChart: React.FC<CpcChartProps> = ({ data }) => {
105102
);
106103
const totalFamiliesText = intl.formatMessage(
107104
{
108-
id: "organizations.patents.chart.families",
109-
defaultMessage: "{count} famille{plural}",
105+
id: "organizations.patents.chart.totalFamilies",
106+
defaultMessage:
107+
"{count, plural, one {# famille} other {# familles}}",
110108
},
111109
{
112110
count: totalCount,
113-
plural: totalCount > 1 ? "s" : "",
114111
}
115112
);
116113

117-
if (!pointName && !pointLabel) {
118-
return `<b>${sectionName}</b>: ${totalFamiliesText}`;
119-
}
114+
if (!pointName && !pointLabel)
115+
if (!pointName && !pointLabel) {
116+
return `<b>${sectionName}</b>: ${totalFamiliesText}`;
117+
}
120118

121119
return `${pointName} ${
122120
pointLabel ? `- ${pointLabel}` : ""
@@ -126,11 +124,12 @@ const CpcChart: React.FC<CpcChartProps> = ({ data }) => {
126124

127125
plotOptions: {
128126
packedbubble: {
129-
minSize: "45%",
130-
maxSize: "120%",
127+
minSize: "30%",
128+
maxSize: "100%",
131129
zMin: zMin,
132130
zMax: zMax,
133131
layoutAlgorithm: {
132+
enableSimulation: false,
134133
gravitationalConstant: 0.05,
135134
splitSeries: true,
136135
seriesInteraction: false,

client/src/pages/organizations/[id]/components/patents/index.tsx

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import { Button, Row, Col, Text } from "@dataesr/dsfr-plus";
33
import useScreenSize from "../../../../../hooks/useScreenSize";
44
import YearBars from "../../../../../components/year-bars";
55
import { useState } from "react";
6-
import { useQuery } from "@tanstack/react-query";
7-
import { getCpcAggregation } from "../../../../../api/patents/[id]";
86
import { OrganizationPatentsData } from "../../../../../types/organization";
97
import CpcWordCloud from "../../../../../components/patent-chart";
108

@@ -26,24 +24,6 @@ export default function OrganizationPatents({
2624
const searchFilters = {
2725
"applicants.ids.id": { values: [{ value, label }], type: "terms" },
2826
};
29-
30-
const patentId = searchFilters["applicants.ids.id"].values[0].value;
31-
const { data: patentsData = [] } = useQuery({
32-
queryKey: ["patent", patentId],
33-
queryFn: () => getCpcAggregation(patentId),
34-
throwOnError: true,
35-
});
36-
37-
const prepareCpcGraphData = (patentsData) => {
38-
return patentsData.map((item) => ({
39-
code: item.code,
40-
doc_count: item.doc_count,
41-
label: item.label,
42-
}));
43-
};
44-
45-
const graphData = prepareCpcGraphData(patentsData);
46-
4727
const patentsFilterUrl = `/search/patents?filters=${encodeURIComponent(
4828
JSON.stringify(searchFilters)
4929
)}`;
@@ -161,8 +141,8 @@ export default function OrganizationPatents({
161141
/>
162142
)}
163143
<>
164-
{projectGraph === "cpc" && patentsData && (
165-
<CpcWordCloud data={graphData} />
144+
{projectGraph === "cpc" && patents.byCpc && (
145+
<CpcWordCloud data={patents.byCpc} />
166146
)}
167147
</>
168148
</Col>

client/src/pages/organizations/[id]/locales/fr.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"organizations.sectionLabels.other": "Technologies émergentes ou celles qui couvrent plusieurs sections",
5757
"organizations.patents.search": "Voir la liste des brevets",
5858
"organizations.patents.chart.families": "{value} {value, plural, one {famille de brevets} other {familles de brevets}}",
59+
"organizations.patents.chart.totalFamilies": "{count, plural, one {# famille} other {# familles}}",
5960
"organizations.patents.chart.title": "Domaines technogiques identifiés dans les familles de brevets de la structure (par section et classe)",
6061
"organizations.patents.nav.year": "Répartition par année",
6162
"organizations.patents.year-bars.name": "Familles de brevets",

client/src/types/organization.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export type OrganizationPatentsData = {
3030
level: any;
3131
kind: any;
3232
byYear: Aggregation[];
33+
byCpc: Aggregation[];
3334
patentsCount: number;
3435
};
3536

0 commit comments

Comments
 (0)