Skip to content

Commit

Permalink
fix(networks): citations count
Browse files Browse the repository at this point in the history
  • Loading branch information
ahonestla committed Oct 21, 2024
1 parent 8082bf3 commit 47a4f49
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 16 deletions.
29 changes: 19 additions & 10 deletions client/src/api/networks/network/communities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, number>): number =>
citationsByYear ? Object.values(citationsByYear).reduce((acc: number, value: number) => acc + value, 0) : 0

const nodeGetCitationsRecent = (citationsByYear: Record<string, number>): number =>
citationsByYear ? (citationsByYear?.[CURRENT_YEAR - 1] || 0) + (citationsByYear?.[CURRENT_YEAR] || 0) : 0

const communityGetAttribute = (graph: Graph, community: number, name: string): Array<string> | Array<number> =>
graph.reduceNodes(
(acc, _, attr) => (attr.community === community && attr?.[name] ? (acc = arrayPush(acc, attr[name])) : acc),
Expand Down Expand Up @@ -75,35 +81,38 @@ const communityGetOaPercent = (aggs: ElasticAggregations): number => {
return (isOa / (isOa + isNotOa || 1)) * 100
}

const communityGetPublications = (hits: ElasticHits): Array<Record<string, string>> =>
const communityGetPublications = (hits: ElasticHits): Array<Record<string, string | number>> =>
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
const id = nodeGetId(key)
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<string, number>): number =>
citationsByYear ? Object.values(citationsByYear).reduce((acc: number, value: number) => acc + value, 0) : 0

const nodeGetCitationsRecent = (citationsByYear: Record<string, number>): number =>
citationsByYear ? (citationsByYear?.[CURRENT_YEAR - 1] || 0) + (citationsByYear?.[CURRENT_YEAR] || 0) : 0

export default async function communitiesCreate(graph: Graph, computeClusters: boolean): Promise<NetworkCommunities> {
const query: string = graph.getAttribute("query")
const model: string = graph.getAttribute("model")
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions client/src/api/networks/network/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,6 @@ export default async function networkCreate(
clusters: communities,
}

console.log("network", network)
return network
}
20 changes: 15 additions & 5 deletions client/src/pages/networks/hooks/useExportData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" })
}

Expand Down
3 changes: 2 additions & 1 deletion client/src/types/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export type NetworkCommunity = {
maxYear?: number
publicationsByYear?: Record<string, number>
publicationsCount?: number
publications?: Array<Record<string, string>>
publications?: Array<Record<string, string | number>>
citationsByYear?: Record<string, number>
citationsCount?: number
citationsRecent?: number
Expand Down Expand Up @@ -118,6 +118,7 @@ export type ElasticHits = Array<ElasticHit>
export type ElasticHit = {
id: string
title?: LangField
cited_by_counts_by_year?: Record<string, number>
}
export type ElasticAggregation = {
buckets?: ElasticBuckets
Expand Down

0 comments on commit 47a4f49

Please sign in to comment.