From 47a4f49a4ead2c5f953c5b299653eef0fc8b74ea Mon Sep 17 00:00:00 2001 From: victor barbier Date: Mon, 21 Oct 2024 15:46:07 +0200 Subject: [PATCH] fix(networks): citations count --- .../src/api/networks/network/communities.ts | 29 ++++++++++++------- client/src/api/networks/network/network.ts | 1 + .../src/pages/networks/hooks/useExportData.ts | 20 +++++++++---- client/src/types/network.ts | 3 +- 4 files changed, 37 insertions(+), 16 deletions(-) diff --git a/client/src/api/networks/network/communities.ts b/client/src/api/networks/network/communities.ts index b2b4102d..aa659810 100644 --- a/client/src/api/networks/network/communities.ts +++ b/client/src/api/networks/network/communities.ts @@ -14,6 +14,12 @@ import { nodeGetId } from "./network" const CURRENT_YEAR = new Date().getFullYear() const RECENT_YEARS = [CURRENT_YEAR - 1, CURRENT_YEAR] +const nodeGetCitationsCount = (citationsByYear: Record): number => + citationsByYear ? Object.values(citationsByYear).reduce((acc: number, value: number) => acc + value, 0) : 0 + +const nodeGetCitationsRecent = (citationsByYear: Record): number => + citationsByYear ? (citationsByYear?.[CURRENT_YEAR - 1] || 0) + (citationsByYear?.[CURRENT_YEAR] || 0) : 0 + const communityGetAttribute = (graph: Graph, community: number, name: string): Array | Array => graph.reduceNodes( (acc, _, attr) => (attr.community === community && attr?.[name] ? (acc = arrayPush(acc, attr[name])) : acc), @@ -75,16 +81,18 @@ const communityGetOaPercent = (aggs: ElasticAggregations): number => { return (isOa / (isOa + isNotOa || 1)) * 100 } -const communityGetPublications = (hits: ElasticHits): Array> => +const communityGetPublications = (hits: ElasticHits): Array> => hits.map((hit) => ({ id: hit.id, title: hit.title.default, + citationsCount: nodeGetCitationsCount(hit?.cited_by_counts_by_year), + citationsRecent: nodeGetCitationsRecent(hit?.cited_by_counts_by_year), })) const communityGetNodesInfos = (hits: ElasticHits, model: string): any => hits.reduce((acc, hit) => { const field = CONFIG[model].field.split(".")[0] - const citationsByYear = hit?.["cited_by_counts_by_year"] + const citationsByYear = hit?.cited_by_counts_by_year hit?.[field]?.forEach((node) => { const key = node[CONFIG[model].field.split(".")[1]] if (!key) return @@ -92,18 +100,19 @@ const communityGetNodesInfos = (hits: ElasticHits, model: string): any => acc[id] = { ...acc?.[id], publicationsCount: acc?.[id]?.publicationsCount ? acc[id].publicationsCount + 1 : 1, - citationsByYear: citationsByYear, + citationsByYear: { + ...citationsByYear, + ...(acc?.[id]?.citationsByYear && + Object.entries(acc[id].citationsByYear).reduce( + (obj, [key, value]: [string, number]) => ({ ...obj, [key]: value + (citationsByYear?.[key] || 0) }), + {} + )), + }, } }) return acc }, {}) -const nodeGetCitationsCount = (citationsByYear: Record): number => - citationsByYear ? Object.values(citationsByYear).reduce((acc: number, value: number) => acc + value, 0) : 0 - -const nodeGetCitationsRecent = (citationsByYear: Record): number => - citationsByYear ? (citationsByYear?.[CURRENT_YEAR - 1] || 0) + (citationsByYear?.[CURRENT_YEAR] || 0) : 0 - export default async function communitiesCreate(graph: Graph, computeClusters: boolean): Promise { const query: string = graph.getAttribute("query") const model: string = graph.getAttribute("model") @@ -129,7 +138,7 @@ export default async function communitiesCreate(graph: Graph, computeClusters: b // Add info to nodes if (hits) { const nodesInfos = communityGetNodesInfos(hits, model) - graph.forEachNode((key) => { + communityGetIds(graph, index).forEach((key) => { if (!Object.keys(nodesInfos).includes(key)) return const nodeInfos = nodesInfos[key] const nodeCitationsByYear = nodeInfos?.citationsByYear diff --git a/client/src/api/networks/network/network.ts b/client/src/api/networks/network/network.ts index c309374f..9ecb0feb 100644 --- a/client/src/api/networks/network/network.ts +++ b/client/src/api/networks/network/network.ts @@ -99,5 +99,6 @@ export default async function networkCreate( clusters: communities, } + console.log("network", network) return network } diff --git a/client/src/pages/networks/hooks/useExportData.ts b/client/src/pages/networks/hooks/useExportData.ts index c4a06fdc..0e9944a9 100644 --- a/client/src/pages/networks/hooks/useExportData.ts +++ b/client/src/pages/networks/hooks/useExportData.ts @@ -21,24 +21,34 @@ const XSLXFormatter = (network: any) => { workbook, XLSX.utils.json_to_sheet( network.clusters.map((cluster) => { - const { publications, ...data } = cluster - return data + const _cluster = { ...cluster } + delete _cluster.publications + return _cluster }) ), "Clusters" ) const publicationsList = network.clusters?.reduce((acc, cluster) => { + console.log("cluster", cluster) cluster?.publications.forEach((publication) => { - acc = [...acc, { id: publication.id, title: publication.title, cluster: cluster.cluster, clusterLabel: cluster.label }] + acc = [ + ...acc, + { + id: publication.id, + title: publication.title, + citationsCount: publication?.citationsCount, + citationsRecent: publication?.citationsRecent, + cluster: cluster.id, + clusterLabel: cluster.label, + }, + ] }) return acc }, []) - XLSX.utils.book_append_sheet(workbook, XLSX.utils.json_to_sheet(publicationsList), "Publications") const workbookOutput = XLSX.write(workbook, { type: "binary", bookType: "xlsx" }) - return new Blob([stringToArrayBuffer(workbookOutput)], { type: "application/octet-stream" }) } diff --git a/client/src/types/network.ts b/client/src/types/network.ts index edac3a26..aa1a2117 100644 --- a/client/src/types/network.ts +++ b/client/src/types/network.ts @@ -46,7 +46,7 @@ export type NetworkCommunity = { maxYear?: number publicationsByYear?: Record publicationsCount?: number - publications?: Array> + publications?: Array> citationsByYear?: Record citationsCount?: number citationsRecent?: number @@ -118,6 +118,7 @@ export type ElasticHits = Array export type ElasticHit = { id: string title?: LangField + cited_by_counts_by_year?: Record } export type ElasticAggregation = { buckets?: ElasticBuckets