fix: artist totals

This commit is contained in:
Cory Dransfeldt 2024-08-08 15:33:39 -07:00
parent 23906da619
commit 5283f27e73
No known key found for this signature in database
3 changed files with 42 additions and 37 deletions

10
package-lock.json generated
View file

@ -1,12 +1,12 @@
{ {
"name": "coryd.dev", "name": "coryd.dev",
"version": "21.6.3", "version": "21.6.4",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "coryd.dev", "name": "coryd.dev",
"version": "21.6.3", "version": "21.6.4",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@cdransf/api-text": "^1.4.0", "@cdransf/api-text": "^1.4.0",
@ -813,9 +813,9 @@
} }
}, },
"node_modules/caniuse-lite": { "node_modules/caniuse-lite": {
"version": "1.0.30001650", "version": "1.0.30001651",
"resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001650.tgz", "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001651.tgz",
"integrity": "sha512-fgEc7hP/LB7iicdXHUI9VsBsMZmUmlVJeQP2qqQW+3lkqVhbmjEU8zp+h5stWeilX+G7uXuIUIIlWlDw9jdt8g==", "integrity": "sha512-9Cf+Xv1jJNe1xPZLGuUXLNkE1BoDkqRqYyFJ9TDYSqhduqA4hu4oR9HluGoWYQC/aj8WHjsGVV+bwkh0+tegRg==",
"dev": true, "dev": true,
"funding": [ "funding": [
{ {

View file

@ -1,6 +1,6 @@
{ {
"name": "coryd.dev", "name": "coryd.dev",
"version": "21.6.3", "version": "21.6.4",
"description": "The source for my personal site. Built using 11ty (and other tools).", "description": "The source for my personal site. Built using 11ty (and other tools).",
"type": "module", "type": "module",
"scripts": { "scripts": {

View file

@ -1,29 +1,29 @@
import { createClient } from '@supabase/supabase-js' import { createClient } from '@supabase/supabase-js'
import { DateTime } from 'luxon'
import { sanitizeMediaString, parseCountryField } from '../../config/utilities/index.js' import { sanitizeMediaString, parseCountryField } from '../../config/utilities/index.js'
const SUPABASE_URL = process.env.SUPABASE_URL const SUPABASE_URL = process.env.SUPABASE_URL
const SUPABASE_KEY = process.env.SUPABASE_KEY const SUPABASE_KEY = process.env.SUPABASE_KEY
const supabase = createClient(SUPABASE_URL, 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 rows = []
let rangeStart = 0 let rangeStart = 0
while (true) { while (true) {
const { data, error } = await supabase const { data, error } = await supabase
.from(table) .from(viewName)
.select(fields) .select(fields)
.order('listened_at', { ascending: false }) .order('listened_at', { ascending: false })
.gte('listened_at', startPeriod.toSeconds())
.range(rangeStart, rangeStart + PAGE_SIZE - 1) .range(rangeStart, rangeStart + PAGE_SIZE - 1)
if (error) { if (error) {
console.error(`Error fetching data from ${table}:`, error) console.error(`Error fetching data from view ${viewName}:`, error)
break break
} }
if (data.length === 0) break
rows = rows.concat(data) rows = rows.concat(data)
if (data.length < PAGE_SIZE) break if (data.length < PAGE_SIZE) break
@ -50,7 +50,6 @@ const fetchGenreMapping = async () => {
const aggregateData = (data, groupByField, groupByType, genreMapping) => { const aggregateData = (data, groupByField, groupByType, genreMapping) => {
const aggregation = {} const aggregation = {}
data.forEach(item => { data.forEach(item => {
const key = item[groupByField] const key = item[groupByField]
if (!aggregation[key]) { if (!aggregation[key]) {
@ -94,12 +93,6 @@ const aggregateGenres = (data, genreMapping) => {
} }
export default async function () { 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 = ` const selectFields = `
listened_at, listened_at,
track_name, track_name,
@ -117,24 +110,36 @@ export default async function () {
try { try {
const genreMapping = await fetchGenreMapping() const genreMapping = await fetchGenreMapping()
const results = await Promise.all(Object.entries(periods).map(async ([period, startPeriod]) => { const [recentTracks, monthTracks, threeMonthTracks] = await Promise.all([
const periodData = await fetchDataForPeriod(startPeriod, selectFields, 'optimized_listens') fetchDataFromView('recent_tracks', selectFields),
fetchDataFromView('month_tracks', selectFields),
fetchDataFromView('three_month_tracks', selectFields)
])
return { return {
[period]: { recent: buildRecents(recentTracks),
artists: aggregateData(periodData, 'artist_name', 'artist_art', genreMapping), week: {
albums: aggregateData(periodData, 'album_name', 'album_art', genreMapping), artists: aggregateData(recentTracks, 'artist_name', 'artist_art', genreMapping),
tracks: aggregateData(periodData, 'track_name', 'track', genreMapping), albums: aggregateData(recentTracks, 'album_name', 'album_art', genreMapping),
genres: aggregateGenres(periodData, genreMapping), tracks: aggregateData(recentTracks, 'track_name', 'track', genreMapping),
totalTracks: periodData.length.toLocaleString('en-US') 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) { } catch (error) {
console.error('Error in fetching and processing music data:', error) console.error('Error in fetching and processing music data:', error)
return {} return {}