Skip to content

Commit 8cebeea

Browse files
authored
Merge pull request #25 from nbonfils/refs-label
Refs label
2 parents 25b358e + 34a9b38 commit 8cebeea

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

src/lib/fetch.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@ export const fetchWorks = async (params, maxWorks) => {
7979
return {count, works};
8080
};
8181

82-
export const fetchRefsLabels = async (openalexIds) => {
82+
export const fetchRefsLabels = async (openalexIds, maxRefs = 500) => {
83+
// We'll only fetch the labels for the first maxRefs refs
8384
if (openalexIds.length === 0) return [];
8485

8586
const refsPerPage = 50;
86-
const maxRefs = 500; // We'll only fetch the labels for the first maxRefs refs
8787
const ids = [...openalexIds.slice(0, maxRefs)]; // Copy the array to prevent its destruction
8888
const numReq = Math.ceil(ids.length / refsPerPage);
8989

@@ -95,7 +95,7 @@ export const fetchRefsLabels = async (openalexIds) => {
9595
const response = await fetch(
9696
"https://api.openalex.org/works?" + new URLSearchParams({
9797
filter: `openalex:${idsStr}`,
98-
select: "id,display_name",
98+
select: "id,title,authorships,publication_year",
9999
mailto: `****@****.com`,
100100
"per-page": perPage,
101101
page: 1,

src/lib/graph.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ export const generateGraph = (data) => {
2727
// Step 1: Create the map background (refs)
2828

2929
console.time('add refs nodes');
30-
Object.entries(data['refs']).forEach(([id, {count, label}]) => {
30+
Object.entries(data['refs']).forEach(([id, {count, label, title}]) => {
3131
graph.addNode(id, {
3232
label,
33+
title,
3334
size: Math.sqrt(maxRefNodeSize * count / data.maxCounts.refs),
3435
color: fieldColors['refs'],
3536
count,
@@ -92,7 +93,7 @@ export const generateGraph = (data) => {
9293
}
9394

9495
for (const field of metadataFields) {
95-
for (const [id, {count, label}] of Object.entries(data[field])) {
96+
for (const [id, {count, label, title}] of Object.entries(data[field])) {
9697
graph.addNode(id, {
9798
label,
9899
size: Math.sqrt(maxMetadataNodeSize * count / data.maxCounts[field]),
@@ -101,6 +102,10 @@ export const generateGraph = (data) => {
101102
dataType: field,
102103
});
103104

105+
if (title) {
106+
graph.mergeNodeAttributes(id, {title});
107+
}
108+
104109
// Get the list of works containing that metadata
105110
const works = Object.entries(data.sets[field]).filter(([workId, fieldSet]) => fieldSet.has(id)).map(([workId,]) => workId);
106111

src/lib/processing.js

+16-6
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,16 @@ export const processWorks = (works) => {
2121
data.sets[field][work.id] = new Set();
2222
}
2323

24-
data.works[`work-${work.id}`] = {count: work.cited_by_count, label: work.title};
24+
let label = work.authorships.slice(0, 3).map((authorship) => authorship.author.display_name).join(', ');
25+
if (work.authorships.length > 3) {
26+
label += ' et al.';
27+
}
28+
label += `, ${work.publication_year}`;
29+
data.works[`work-${work.id}`] = {count: work.cited_by_count, title: work.title, label};
2530
data.sets.works[work.id].add(`work-${work.id}`);
2631

2732
work.referenced_works.forEach((ref) => {
2833
incOrCreate(data.refs, ref, 'count');
29-
data.refs[ref].label = ref;
3034
});
3135
data.sets.refs[work.id] = new Set(work.referenced_works);
3236

@@ -161,11 +165,17 @@ export const filterData = async (data, filters) => {
161165

162166
filteredData.maxCounts.refs = filteredRefs.reduce((acc, [, {count}]) => Math.max(acc, count), 0);
163167

164-
// Get the refs labels
168+
// Create the refs labels
165169
console.time('label refs');
166-
const refsLabels = await fetchRefsLabels(filteredRefs.map(([id,]) => id));
167-
for (const {id, display_name} of refsLabels) {
168-
filteredData.refs[id].label = display_name;
170+
const refsLabels = await fetchRefsLabels(filteredRefs.map(([id,]) => id), 50);
171+
for (const {id, title, authorships, publication_year} of refsLabels) {
172+
let label = authorships.slice(0, 3).map((authorship) => authorship.author.display_name).join(', ');
173+
if (authorships.length > 3) {
174+
label += ' et al.';
175+
}
176+
label += `, ${publication_year}`;
177+
filteredData.refs[id].label = label;
178+
filteredData.refs[id].title = title;
169179
}
170180
console.timeEnd('label refs');
171181

0 commit comments

Comments
 (0)