diff --git a/package-lock.json b/package-lock.json index 542bd414..9b3c8e87 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "coryd.dev", - "version": "21.6.3", + "version": "21.6.4", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "coryd.dev", - "version": "21.6.3", + "version": "21.6.4", "license": "MIT", "dependencies": { "@cdransf/api-text": "^1.4.0", @@ -813,9 +813,9 @@ } }, "node_modules/caniuse-lite": { - "version": "1.0.30001650", - "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001650.tgz", - "integrity": "sha512-fgEc7hP/LB7iicdXHUI9VsBsMZmUmlVJeQP2qqQW+3lkqVhbmjEU8zp+h5stWeilX+G7uXuIUIIlWlDw9jdt8g==", + "version": "1.0.30001651", + "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001651.tgz", + "integrity": "sha512-9Cf+Xv1jJNe1xPZLGuUXLNkE1BoDkqRqYyFJ9TDYSqhduqA4hu4oR9HluGoWYQC/aj8WHjsGVV+bwkh0+tegRg==", "dev": true, "funding": [ { diff --git a/package.json b/package.json index 672e9ea8..f10d2c0b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "coryd.dev", - "version": "21.6.3", + "version": "21.6.4", "description": "The source for my personal site. Built using 11ty (and other tools).", "type": "module", "scripts": { @@ -47,4 +47,4 @@ "sanitize-html": "^2.13.0", "slugify": "^1.6.6" } -} +} \ No newline at end of file diff --git a/src/data/music.js b/src/data/music.js index d8809ea7..c6536653 100644 --- a/src/data/music.js +++ b/src/data/music.js @@ -1,29 +1,29 @@ import { createClient } from '@supabase/supabase-js' -import { DateTime } from 'luxon' import { sanitizeMediaString, parseCountryField } from '../../config/utilities/index.js' const SUPABASE_URL = process.env.SUPABASE_URL const SUPABASE_KEY = process.env.SUPABASE_KEY const supabase = createClient(SUPABASE_URL, SUPABASE_KEY) -const PAGE_SIZE = 10000 +const PAGE_SIZE = 1000 -const fetchDataForPeriod = async (startPeriod, fields, table) => { +const fetchDataFromView = async (viewName, fields) => { let rows = [] let rangeStart = 0 while (true) { const { data, error } = await supabase - .from(table) + .from(viewName) .select(fields) .order('listened_at', { ascending: false }) - .gte('listened_at', startPeriod.toSeconds()) .range(rangeStart, rangeStart + PAGE_SIZE - 1) if (error) { - console.error(`Error fetching data from ${table}:`, error) + console.error(`Error fetching data from view ${viewName}:`, error) break } + if (data.length === 0) break + rows = rows.concat(data) if (data.length < PAGE_SIZE) break @@ -50,7 +50,6 @@ const fetchGenreMapping = async () => { const aggregateData = (data, groupByField, groupByType, genreMapping) => { const aggregation = {} - data.forEach(item => { const key = item[groupByField] if (!aggregation[key]) { @@ -94,12 +93,6 @@ const aggregateGenres = (data, genreMapping) => { } export default async function () { - const periods = { - week: DateTime.now().minus({ days: 7 }).startOf('day'), - month: DateTime.now().minus({ days: 30 }).startOf('day'), - threeMonth: DateTime.now().minus({ months: 3 }).startOf('day') - } - const selectFields = ` listened_at, track_name, @@ -117,24 +110,36 @@ export default async function () { try { const genreMapping = await fetchGenreMapping() - const results = await Promise.all(Object.entries(periods).map(async ([period, startPeriod]) => { - const periodData = await fetchDataForPeriod(startPeriod, selectFields, 'optimized_listens') - return { - [period]: { - artists: aggregateData(periodData, 'artist_name', 'artist_art', genreMapping), - albums: aggregateData(periodData, 'album_name', 'album_art', genreMapping), - tracks: aggregateData(periodData, 'track_name', 'track', genreMapping), - genres: aggregateGenres(periodData, genreMapping), - totalTracks: periodData.length.toLocaleString('en-US') - } + const [recentTracks, monthTracks, threeMonthTracks] = await Promise.all([ + fetchDataFromView('recent_tracks', selectFields), + fetchDataFromView('month_tracks', selectFields), + fetchDataFromView('three_month_tracks', selectFields) + ]) + + return { + recent: buildRecents(recentTracks), + week: { + artists: aggregateData(recentTracks, 'artist_name', 'artist_art', genreMapping), + albums: aggregateData(recentTracks, 'album_name', 'album_art', genreMapping), + tracks: aggregateData(recentTracks, 'track_name', 'track', genreMapping), + genres: aggregateGenres(recentTracks, genreMapping), + totalTracks: recentTracks.length.toLocaleString('en-US') + }, + month: { + artists: aggregateData(monthTracks, 'artist_name', 'artist_art', genreMapping), + albums: aggregateData(monthTracks, 'album_name', 'album_art', genreMapping), + tracks: aggregateData(monthTracks, 'track_name', 'track', genreMapping), + genres: aggregateGenres(monthTracks, genreMapping), + totalTracks: monthTracks.length.toLocaleString('en-US') + }, + threeMonth: { + artists: aggregateData(threeMonthTracks, 'artist_name', 'artist_art', genreMapping), + albums: aggregateData(threeMonthTracks, 'album_name', 'album_art', genreMapping), + tracks: aggregateData(threeMonthTracks, 'track_name', 'track', genreMapping), + genres: aggregateGenres(threeMonthTracks, genreMapping), + totalTracks: threeMonthTracks.length.toLocaleString('en-US') } - })) - - const recentData = await fetchDataForPeriod(DateTime.now().minus({ days: 7 }), selectFields, 'optimized_listens') - - results.push({ recent: buildRecents(recentData) }) - - return Object.assign({}, ...results) + } } catch (error) { console.error('Error in fetching and processing music data:', error) return {}