Trends: fill in empty tag histories

Fixes https://gitlab.com/soapbox-pub/ditto/-/issues/29
This commit is contained in:
Alex Gleason 2023-07-26 15:40:29 -05:00
parent 89b98ae77a
commit f8d46cae58
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
1 changed files with 25 additions and 1 deletions

View File

@ -29,7 +29,7 @@ const trendingTagsController: AppController = (c) => {
accounts: String(accounts),
uses: String(uses),
},
...trends.getTagHistory({
...getTagHistoryWithGapsFilled({
tag: name,
since: lastWeek,
until: now,
@ -44,4 +44,28 @@ const trendingTagsController: AppController = (c) => {
})));
};
function generateDateRange(since: Date, until: Date): Date[] {
const dates = [];
const sinceDate = new Date(Date.UTC(since.getUTCFullYear(), since.getUTCMonth(), since.getUTCDate() + 1));
const untilDate = new Date(Date.UTC(until.getUTCFullYear(), until.getUTCMonth(), until.getUTCDate()));
while (sinceDate < untilDate) {
dates.push(new Date(sinceDate));
sinceDate.setUTCDate(sinceDate.getUTCDate() + 1);
}
return dates.reverse();
}
function getTagHistoryWithGapsFilled(params: Parameters<typeof trends.getTagHistory>[0]) {
const history = trends.getTagHistory(params);
const dateRange = generateDateRange(params.since, params.until);
return dateRange.map((day) => {
const data = history.find((item) => item.day.getTime() === day.getTime());
return data || { day, accounts: 0, uses: 0 };
});
}
export { trendingTagsController };