Rework stats display

This commit is contained in:
Cadence Ember 2025-04-01 11:44:40 +13:00
parent 9951842594
commit cd2827791f
3 changed files with 86 additions and 24 deletions

View file

@ -102,22 +102,25 @@ router.get("/", defineEventHandler({
const locals = {
items: prepared.all(params),
query,
count: [
["total", db.prepare("SELECT count(*) FROM item").pluck().get()],
["runtime", db.prepare("SELECT iif(sum(duration) > 86400, cast(total(duration)/86400 AS INTEGER) || 'd ' || cast(total(duration)/3600%24 AS INTEGER) || 'h', cast(total(duration)/3600 AS INTEGER) || 'h') FROM track").pluck().get()],
["albums", db.prepare("SELECT count(*) FROM item WHERE item_type = 'album'").pluck().get()],
["singles", db.prepare("SELECT count(*) FROM item WHERE item_type = 'track'").pluck().get()],
["free", db.prepare("SELECT count(*) FROM item WHERE price = 0").pluck().get()],
["paid", db.prepare("SELECT count(*) FROM item WHERE price > 0").pluck().get()],
["tracks", db.prepare("SELECT count(*) FROM track").pluck().get()],
["avg tracks", Math.round(db.prepare("SELECT avg(count) FROM (SELECT count(*) AS count FROM track INNER JOIN item USING (item_id) WHERE item_type = 'album' GROUP BY item_id)").pluck().get()*10)/10],
["tags", db.prepare("SELECT count(*) FROM item_tag").pluck().get()],
["avg tags", Math.round(db.prepare("SELECT avg(count) FROM (SELECT count(*) AS count FROM item_tag GROUP BY item_id)").pluck().get()*10)/10],
["lonely tags", db.prepare("SELECT count(*) FROM (SELECT tag FROM item_tag GROUP BY tag HAVING count(*) = 1)").pluck().get()],
["value", displayCurrencySymbol + Math.round(select("item", ["currency", "price"]).all().map(c => {
count: {
total: db.prepare("SELECT count(*) FROM item").pluck().get(),
runtime: db.prepare("SELECT iif(sum(duration) > 86400, cast(total(duration)/86400 AS INTEGER) || 'd ' || cast(total(duration)/3600%24 AS INTEGER) || 'h', cast(total(duration)/3600 AS INTEGER) || 'h') FROM track").pluck().get(),
albums: db.prepare("SELECT count(*) FROM item WHERE item_type = 'album'").pluck().get(),
singles: db.prepare("SELECT count(*) FROM item WHERE item_type = 'track'").pluck().get(),
free: db.prepare("SELECT count(*) FROM item WHERE price = 0").pluck().get(),
paid: db.prepare("SELECT count(*) FROM item WHERE price > 0").pluck().get(),
tracks: db.prepare("SELECT count(*) FROM track").pluck().get(),
avgTracks: Math.round(db.prepare("SELECT avg(count) FROM (SELECT count(*) AS count FROM track INNER JOIN item USING (item_id) WHERE item_type = 'album' GROUP BY item_id)").pluck().get()*10)/10,
tags: db.prepare("SELECT count(*) FROM item_tag").pluck().get(),
avgTags: Math.round(db.prepare("SELECT avg(count) FROM (SELECT count(*) AS count FROM item_tag GROUP BY item_id)").pluck().get()*10)/10,
lonelyTags: db.prepare("SELECT count(*) FROM (SELECT tag FROM item_tag GROUP BY tag HAVING count(*) = 1)").pluck().get(),
value: Math.round(select("item", ["currency", "price"]).all().map(c => {
return (currencyExchange.get(c.currency) || 0.6) * c.price / (currencyExchange.get(displayCurrency) || 1) / 10
}).reduce((a, c) => a + c, 0)) * 10 + " " + displayCurrency]
]
}).reduce((a, c) => a + c, 0)) * 10,
displayCurrency,
displayCurrencySymbol,
taste: db.prepare("with popularity (a) as (select avg(also_collected_count) from item group by band_url) select sum(iif(a >= 0 and a < 20, 1, 0)) as cold, sum(iif(a >= 20 and a < 200, 1, 0)) as warm, sum(iif(a >= 200 and a < 2000, 1, 0)) as hot, sum(iif(a >= 2000, 1, 0)) as supernova from popularity").raw().get()
}
}
if (mode === "artist_grid") {
loadPreviews(locals, "band_name", 4, whereClause, filter_field, filter)