chore: better month based filtering

This commit is contained in:
Cory Dransfeldt 2024-04-08 06:40:34 -07:00
parent a979a27268
commit af8d65dc74
No known key found for this signature in database
2 changed files with 13 additions and 3 deletions

View file

@ -1,6 +1,6 @@
{
"name": "coryd.dev",
"version": "9.8.2",
"version": "9.8.3",
"description": "The source for my personal site. Built using 11ty.",
"type": "module",
"scripts": {

View file

@ -18,6 +18,16 @@ const getKeys = (months = 1) => {
return keys
}
const filterOldScrobbles = (scrobbles, months = 1) => {
const currentDate = DateTime.now()
const weeks = Math.floor((currentDate.daysInMonth * months) / 7)
const windowEnd = DateTime.now().minus({ weeks });
return scrobbles.filter(scrobble => {
const timestamp = DateTime.fromISO(scrobble.timestamp);
return timestamp >= windowEnd;
});
}
export const onPreBuild = async ({ constants }) => {
setEnvironmentContext({
siteID: constants.SITE_ID,
@ -50,6 +60,6 @@ export const onPreBuild = async ({ constants }) => {
fs.writeFileSync('./src/_data/json/artists-map.json', JSON.stringify(artistsMap))
fs.writeFileSync('./src/_data/json/albums-map.json', JSON.stringify(albumsMap))
fs.writeFileSync('./src/_data/json/now-playing.json', JSON.stringify(nowPlaying))
fs.writeFileSync('./src/_data/json/scrobbles-month-chart.json', JSON.stringify(monthChartData))
fs.writeFileSync('./src/_data/json/scrobbles-three-month-chart.json', JSON.stringify(threeMonthChartData))
fs.writeFileSync('./src/_data/json/scrobbles-month-chart.json', JSON.stringify({ data: filterOldScrobbles(monthChartData.data) }))
fs.writeFileSync('./src/_data/json/scrobbles-three-month-chart.json', JSON.stringify({ data: filterOldScrobbles(threeMonthChartData.data, 3) }))
}