Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/api/fetchPageViewsCount.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ const fetchPageViewsCount = async (specification) => {
end = `${todayYear}${todayMonth}${todayDay}`,
} = specification;
try {
const response = await axios.get(
`https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/${project}/${acess}/${agents}/${article}/${dateType}/${start}/${end}`,
);
const encodedArticle = encodeURIComponent((article || '').replace(/\s+/g, '_'));
const url = `https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/${project}/${acess}/${agents}/${encodedArticle}/${dateType}/${start}/${end}`;
const response = await axios.get(url);
return response.data;
} catch (error) {
return;
// On error (404 or network), return an empty items array so callers can
// treat it as "no data" instead of triggering a fetch error UI.
return { items: [] };
}
};

Expand Down
44 changes: 23 additions & 21 deletions src/components/PageViews/ArticleViewsGraph.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,34 @@ export default function ArticleViewsGraph() {
try {
const results = await Promise.all(
pages.map(async (page) => {
const response = await fetchPageViewsCount({
article: page,
project,
acess: platform,
agents: agent,
dateType: dateType?.toLowerCase(),
start: dates.start,
end: dates.end,
});
try {
const response = await fetchPageViewsCount({
article: page,
project,
acess: platform,
agents: agent,
dateType: dateType?.toLowerCase(),
start: dates.start,
end: dates.end,
});

if (!response?.items) {
throw new Error('Invalid response format');
}
const views = Array.isArray(response?.items) ? response.items : [];

const views = response.items;
const dates = views.map((view) => {
const year = view.timestamp.substring(0, 4);
const month = view.timestamp.substring(4, 6);
const day = view.timestamp.substring(6, 8);
return `${year}-${month}-${day}`;
});
const counts = views.map((view) => view.views);
return { article: page, dates, counts };
const viewDates = views.map((view) => {
const year = view.timestamp.substring(0, 4);
const month = view.timestamp.substring(4, 6);
const day = view.timestamp.substring(6, 8);
return `${year}-${month}-${day}`;
});
const counts = views.map((view) => view.views);
return { article: page, dates: viewDates, counts };
} catch (e) {
return { article: page, dates: [], counts: [] };
}
}),
);


const combinedDates = [...new Set(results.flatMap((data) => data.dates))].sort();
const datasets = results.map((data, index) => ({
label: decodeURIComponent(data.article),
Expand Down