Trends: fill in empty tag histories
Fixes https://gitlab.com/soapbox-pub/ditto/-/issues/29
This commit is contained in:
parent
89b98ae77a
commit
f8d46cae58
|
@ -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 };
|
||||
|
|
Loading…
Reference in New Issue