Playground site: airplay serve (ranking, artist history and forecast, what-if on chart situation, backtest summary) at idiotmaxxing.com/airplay via a Traefik file route; daily collection refreshes the forecasts
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Generated
+31
@@ -40,6 +40,7 @@ dependencies = [
|
||||
"reqwest",
|
||||
"rusqlite",
|
||||
"serde_json",
|
||||
"tiny_http",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -57,6 +58,12 @@ version = "1.0.104"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "330a5ed07fa54e4702c9d6c4174f74427fc0ef6e214bbd677ae50a5099946470"
|
||||
|
||||
[[package]]
|
||||
name = "ascii"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d92bec98840b8f03a5ff5413de5293bfcd8bf96467cf5452609f939ec6f5de16"
|
||||
|
||||
[[package]]
|
||||
name = "async-compression"
|
||||
version = "0.4.52"
|
||||
@@ -165,6 +172,12 @@ dependencies = [
|
||||
"phf",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "chunked_transfer"
|
||||
version = "1.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6e4de3bc4ea267985becf712dc6d9eed8b04c953b3fcfb339ebc87acd9804901"
|
||||
|
||||
[[package]]
|
||||
name = "compression-codecs"
|
||||
version = "0.4.47"
|
||||
@@ -388,6 +401,12 @@ version = "1.10.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87"
|
||||
|
||||
[[package]]
|
||||
name = "httpdate"
|
||||
version = "1.0.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9"
|
||||
|
||||
[[package]]
|
||||
name = "hyper"
|
||||
version = "1.12.0"
|
||||
@@ -1144,6 +1163,18 @@ dependencies = [
|
||||
"syn 3.0.10",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tiny_http"
|
||||
version = "0.12.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "389915df6413a2e74fb181895f933386023c71110878cd0825588928e64cdc82"
|
||||
dependencies = [
|
||||
"ascii",
|
||||
"chunked_transfer",
|
||||
"httpdate",
|
||||
"log",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tinystr"
|
||||
version = "0.8.5"
|
||||
|
||||
@@ -17,6 +17,7 @@ regex = "1"
|
||||
reqwest = { version = "0.12", default-features = false, features = ["blocking", "rustls-tls", "gzip", "json"] }
|
||||
rusqlite = { version = "0.32", features = ["bundled"] }
|
||||
serde_json = "1"
|
||||
tiny_http = "0.12"
|
||||
|
||||
[profile.release]
|
||||
opt-level = 3
|
||||
|
||||
@@ -44,3 +44,14 @@ systemctl daemon-reload && systemctl enable --now airplay-collect.timer
|
||||
## Be polite
|
||||
|
||||
One request at a time, at least 0.4 s apart, with a User-Agent that links here. Programmes already read are not fetched again.
|
||||
|
||||
## Playground site
|
||||
|
||||
`airplay serve` serves the ranking, artist pages and a what-if at https://idiotmaxxing.com/airplay/ (rankings from the `forecasts` table; the what-if models are fitted at start and again when a newer forecast run appears).
|
||||
|
||||
```
|
||||
cp deploy/airplay-web.service /etc/systemd/system/ && systemctl daemon-reload && systemctl enable --now airplay-web
|
||||
cp deploy/traefik-airplay.yaml /data/coolify/proxy/dynamic/airplay.yaml # Coolify's Traefik picks it up live
|
||||
```
|
||||
|
||||
After `cargo build --release`, run `systemctl restart airplay-web`. The daily collection runs `airplay model rank` afterwards, so the site refreshes by itself.
|
||||
|
||||
@@ -8,7 +8,9 @@ Wants=network-online.target
|
||||
Type=oneshot
|
||||
WorkingDirectory=/root/dev/Airplay-Predictor
|
||||
ExecStart=/root/dev/Airplay-Predictor/target/release/airplay collect --db /root/dev/Airplay-Predictor/data/airplay.db
|
||||
# Fresh forecasts for the playground after every collection.
|
||||
ExecStartPost=/root/dev/Airplay-Predictor/target/release/airplay model rank --db /root/dev/Airplay-Predictor/data/airplay.db --top 0
|
||||
Nice=10
|
||||
IOSchedulingClass=idle
|
||||
MemoryMax=512M
|
||||
MemoryMax=1500M
|
||||
TimeoutStartSec=3h
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
[Unit]
|
||||
Description=Airplay Predictor playground (idiotmaxxing.com/airplay)
|
||||
# Listens on the Docker bridge address so Coolify's Traefik (host.docker.internal) can reach it.
|
||||
After=docker.service network-online.target
|
||||
Wants=network-online.target
|
||||
|
||||
[Service]
|
||||
WorkingDirectory=/root/dev/Airplay-Predictor
|
||||
ExecStart=/root/dev/Airplay-Predictor/target/release/airplay serve --addr 10.0.0.1:8790 --prefix /airplay
|
||||
Restart=always
|
||||
RestartSec=5
|
||||
MemoryMax=1500M
|
||||
Nice=5
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
@@ -0,0 +1,23 @@
|
||||
# Coolify's Traefik reads every file in /data/coolify/proxy/dynamic/ (watch=true), so copying
|
||||
# this there routes idiotmaxxing.com/airplay to the airplay-web service on the host.
|
||||
http:
|
||||
routers:
|
||||
airplay-http:
|
||||
entryPoints: [http]
|
||||
rule: '(Host(`idiotmaxxing.com`) || Host(`www.idiotmaxxing.com`)) && PathPrefix(`/airplay`)'
|
||||
priority: 1000
|
||||
middlewares: [redirect-to-https]
|
||||
service: airplay
|
||||
airplay-https:
|
||||
entryPoints: [https]
|
||||
rule: '(Host(`idiotmaxxing.com`) || Host(`www.idiotmaxxing.com`)) && PathPrefix(`/airplay`)'
|
||||
priority: 1000
|
||||
middlewares: [gzip]
|
||||
service: airplay
|
||||
tls:
|
||||
certresolver: letsencrypt
|
||||
services:
|
||||
airplay:
|
||||
loadBalancer:
|
||||
servers:
|
||||
- url: 'http://host.docker.internal:8790'
|
||||
+4
-1
@@ -4,6 +4,7 @@
|
||||
//! airplay stats [--db data/airplay.db]
|
||||
//! airplay check [--db data/airplay.db] gaps, duplicates, DR cross-check, chart completeness
|
||||
//! airplay model panel | backtest | rank the first models (src/model)
|
||||
//! airplay serve [--addr 127.0.0.1:8790] [--prefix /airplay] the playground site (src/web.rs)
|
||||
//!
|
||||
//! --backfill fetches all history at once (every DR artist page, the whole Hitlisten archive);
|
||||
//! without it a run takes a bounded slice, which is what the daily timer does.
|
||||
@@ -19,6 +20,7 @@ mod model;
|
||||
mod musicbrainz;
|
||||
mod playlists;
|
||||
mod spotify;
|
||||
mod web;
|
||||
|
||||
use anyhow::Result;
|
||||
use chrono::Utc;
|
||||
@@ -40,8 +42,9 @@ fn main() -> Result<()> {
|
||||
Some("stats") => stats(&db),
|
||||
Some("check") => check::run(&db),
|
||||
Some("model") => model::run(&db, &args),
|
||||
Some("serve") => web::serve(&db_path, &args),
|
||||
_ => {
|
||||
eprintln!("usage: airplay collect [--db FILE] [--days 7] [--all-bauer] [--only dr|bauer|hitlisten|deezer|musicbrainz|spotify|playlists|nmf] [--backfill]\n airplay stats [--db FILE]\n airplay check [--db FILE]\n airplay model panel|backtest|rank");
|
||||
eprintln!("usage: airplay collect [--db FILE] [--days 7] [--all-bauer] [--only dr|bauer|hitlisten|deezer|musicbrainz|spotify|playlists|nmf] [--backfill]\n airplay stats [--db FILE]\n airplay check [--db FILE]\n airplay model panel|backtest|rank\n airplay serve [--addr HOST:PORT] [--prefix /airplay]");
|
||||
std::process::exit(2);
|
||||
}
|
||||
}
|
||||
|
||||
+333
@@ -0,0 +1,333 @@
|
||||
//! The playground site: `airplay serve [--addr 127.0.0.1:8790] [--prefix /airplay]`.
|
||||
//!
|
||||
//! {prefix}/ the page (src/web/index.html)
|
||||
//! {prefix}/api/meta latest forecast run, horizons, channels, backtest scores
|
||||
//! {prefix}/api/rank?h=4 every artist's forecast for one horizon
|
||||
//! {prefix}/api/artist?id= one artist: profile, 51 weeks of plays per channel, forecasts, chart runs
|
||||
//! {prefix}/api/whatif?id=&h=&entry=&t40=&air=
|
||||
//! re-run the model for one artist with a changed chart situation
|
||||
//!
|
||||
//! Rankings are read from the forecasts table (`airplay model rank`); the what-if models are
|
||||
//! fitted in the background at start and again whenever a newer forecast run appears.
|
||||
|
||||
use crate::model::common::{thresholds, Direct, Rng};
|
||||
use crate::model::features::{self, P};
|
||||
use crate::model::glm;
|
||||
use crate::model::panel::{Panel, C, CHANNELS};
|
||||
use crate::model::rank::VERSION;
|
||||
use anyhow::Result;
|
||||
use rusqlite::{params, Connection, OpenFlags, OptionalExtension};
|
||||
use serde_json::{json, Value};
|
||||
use std::collections::HashMap;
|
||||
use std::sync::{Arc, RwLock};
|
||||
use tiny_http::{Header, Method, Response, Server};
|
||||
|
||||
const PAGE: &str = include_str!("web/index.html");
|
||||
const MAX_H: usize = 12;
|
||||
|
||||
struct Models {
|
||||
made_at: String,
|
||||
panel: Panel,
|
||||
tau: [u32; C],
|
||||
direct: Vec<Direct>, // index h - 1
|
||||
index: HashMap<String, usize>,
|
||||
}
|
||||
|
||||
struct State {
|
||||
db_path: String,
|
||||
prefix: String,
|
||||
models: RwLock<Option<Arc<Models>>>,
|
||||
rank_cache: RwLock<HashMap<(String, usize), Arc<String>>>,
|
||||
}
|
||||
|
||||
fn ro(path: &str) -> Result<Connection> {
|
||||
let c = Connection::open_with_flags(path, OpenFlags::SQLITE_OPEN_READ_ONLY | OpenFlags::SQLITE_OPEN_NO_MUTEX)?;
|
||||
c.busy_timeout(std::time::Duration::from_secs(10))?;
|
||||
Ok(c)
|
||||
}
|
||||
|
||||
fn latest_run(c: &Connection) -> Result<Option<String>> {
|
||||
Ok(c.query_row("SELECT max(made_at) FROM forecasts WHERE model_version = ?1", [VERSION], |r| r.get(0)).optional()?.flatten())
|
||||
}
|
||||
|
||||
pub fn serve(db_path: &str, args: &[String]) -> Result<()> {
|
||||
let arg = |n: &str| args.iter().position(|a| a == n).and_then(|i| args.get(i + 1)).cloned();
|
||||
let addr = arg("--addr").unwrap_or_else(|| "127.0.0.1:8790".into());
|
||||
let prefix = arg("--prefix").unwrap_or_else(|| "/airplay".into()).trim_end_matches('/').to_string();
|
||||
let state = Arc::new(State { db_path: db_path.into(), prefix, models: RwLock::new(None), rank_cache: RwLock::new(HashMap::new()) });
|
||||
|
||||
// Keep the what-if models in step with the latest forecast run.
|
||||
{
|
||||
let state = state.clone();
|
||||
std::thread::spawn(move || loop {
|
||||
let current = state.models.read().unwrap().as_ref().map(|m| m.made_at.clone());
|
||||
let latest = ro(&state.db_path).ok().and_then(|c| latest_run(&c).ok().flatten());
|
||||
if latest.is_some() && latest != current {
|
||||
match fit_models(&state.db_path, latest.clone().unwrap()) {
|
||||
Ok(m) => { *state.models.write().unwrap() = Some(Arc::new(m)); eprintln!("what-if models ready ({})", latest.unwrap()); }
|
||||
Err(e) => eprintln!("what-if models: {e}"),
|
||||
}
|
||||
}
|
||||
std::thread::sleep(std::time::Duration::from_secs(600));
|
||||
});
|
||||
}
|
||||
|
||||
let server = Arc::new(Server::http(&addr).map_err(|e| anyhow::anyhow!("{addr}: {e}"))?);
|
||||
eprintln!("serving on http://{addr}{}/", state.prefix);
|
||||
let workers: Vec<_> = (0..4).map(|_| {
|
||||
let (server, state) = (server.clone(), state.clone());
|
||||
std::thread::spawn(move || {
|
||||
for req in server.incoming_requests() {
|
||||
let (status, ctype, body) = match handle(&state, req.method(), req.url()) {
|
||||
Ok(r) => r,
|
||||
Err(e) => (500, "application/json", Arc::new(json!({"error": e.to_string()}).to_string())),
|
||||
};
|
||||
let mut resp = Response::from_data(body.as_bytes().to_vec()).with_status_code(status)
|
||||
.with_header(Header::from_bytes("Content-Type", ctype).unwrap())
|
||||
.with_header(Header::from_bytes("X-Robots-Tag", "noindex").unwrap());
|
||||
if status == 301 {
|
||||
resp = resp.with_header(Header::from_bytes("Location", format!("{}/", state.prefix)).unwrap());
|
||||
}
|
||||
let _ = req.respond(resp);
|
||||
}
|
||||
})
|
||||
}).collect();
|
||||
for w in workers { let _ = w.join(); }
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn fit_models(db_path: &str, made_at: String) -> Result<Models> {
|
||||
let db = crate::db::Db::open(db_path)?;
|
||||
let panel = Panel::load(&db)?;
|
||||
let tau = thresholds(&panel, panel.w());
|
||||
let direct = (1..=MAX_H).map(|h| Direct::fit(&panel, h, panel.w() - 1, &tau, true)).collect();
|
||||
let index = panel.artists.iter().enumerate().map(|(i, a)| (a.id.clone(), i)).collect();
|
||||
Ok(Models { made_at, panel, tau, direct, index })
|
||||
}
|
||||
|
||||
fn query(url: &str) -> HashMap<String, String> {
|
||||
let q = url.split_once('?').map(|x| x.1).unwrap_or("");
|
||||
q.split('&').filter_map(|kv| {
|
||||
let (k, v) = kv.split_once('=').unwrap_or((kv, ""));
|
||||
Some((decode(k), decode(v)))
|
||||
}).collect()
|
||||
}
|
||||
|
||||
fn decode(s: &str) -> String {
|
||||
let b = s.as_bytes();
|
||||
let mut out = Vec::with_capacity(b.len());
|
||||
let mut i = 0;
|
||||
while i < b.len() {
|
||||
match b[i] {
|
||||
b'+' => out.push(b' '),
|
||||
b'%' if i + 2 < b.len() => {
|
||||
if let Ok(v) = u8::from_str_radix(std::str::from_utf8(&b[i + 1..i + 3]).unwrap_or("zz"), 16) { out.push(v); i += 2; } else { out.push(b'%'); }
|
||||
}
|
||||
c => out.push(c),
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
String::from_utf8_lossy(&out).into_owned()
|
||||
}
|
||||
|
||||
type Reply = (u16, &'static str, Arc<String>);
|
||||
|
||||
fn ok(v: Value) -> Result<Reply> { Ok((200, "application/json", Arc::new(v.to_string()))) }
|
||||
|
||||
fn handle(st: &State, method: &Method, url: &str) -> Result<Reply> {
|
||||
if *method != Method::Get && *method != Method::Head { return Ok((405, "text/plain", Arc::new("GET only".into()))); }
|
||||
let path = url.split('?').next().unwrap_or("");
|
||||
let Some(rest) = path.strip_prefix(&st.prefix) else { return Ok((404, "text/plain", Arc::new("not found".into()))) };
|
||||
let q = query(url);
|
||||
match rest {
|
||||
"" => Ok((301, "text/plain", Arc::new(String::new()))),
|
||||
"/" | "/index.html" => Ok((200, "text/html; charset=utf-8", Arc::new(PAGE.to_string()))),
|
||||
"/api/meta" => meta(st),
|
||||
"/api/rank" => rank(st, q.get("h").and_then(|h| h.parse().ok()).unwrap_or(4).clamp(1, MAX_H)),
|
||||
"/api/artist" => artist(st, q.get("id").map(String::as_str).unwrap_or("")),
|
||||
"/api/whatif" => whatif(st, &q),
|
||||
_ => Ok((404, "text/plain", Arc::new("not found".into()))),
|
||||
}
|
||||
}
|
||||
|
||||
fn meta(st: &State) -> Result<Reply> {
|
||||
let c = ro(&st.db_path)?;
|
||||
let Some(made) = latest_run(&c)? else { return ok(json!({"ready": false})) };
|
||||
let (origin,): (String,) = c.query_row("SELECT origin_week FROM forecasts WHERE made_at = ?1 LIMIT 1", [&made], |r| Ok((r.get(0)?,)))?;
|
||||
let horizons: Vec<Value> = c.prepare("SELECT DISTINCT horizon, target_week FROM forecasts WHERE made_at = ?1 ORDER BY horizon")?
|
||||
.query_map([&made], |r| Ok(json!({"h": r.get::<_, i64>(0)?, "week": r.get::<_, String>(1)?})))?.collect::<Result<_, _>>()?;
|
||||
let run: Option<String> = c.query_row("SELECT max(run_at) FROM backtest_scores", [], |r| r.get(0)).optional()?.flatten();
|
||||
let scores: Vec<Value> = match &run {
|
||||
Some(run) => c.prepare("SELECT model, horizon, metric, value FROM backtest_scores WHERE run_at = ?1")?
|
||||
.query_map([run], |r| Ok(json!({"model": r.get::<_, String>(0)?, "h": r.get::<_, i64>(1)?, "metric": r.get::<_, String>(2)?, "value": r.get::<_, Option<f64>>(3)?})))?
|
||||
.collect::<Result<_, _>>()?,
|
||||
None => Vec::new(),
|
||||
};
|
||||
let models = st.models.read().unwrap();
|
||||
let tau = models.as_ref().map(|m| m.tau.to_vec());
|
||||
ok(json!({"ready": true, "made_at": made, "origin_week": origin, "model": VERSION, "horizons": horizons,
|
||||
"channels": CHANNELS, "tau": tau, "whatif_ready": models.is_some(), "backtest_run": run, "backtest": scores}))
|
||||
}
|
||||
|
||||
fn rank(st: &State, h: usize) -> Result<Reply> {
|
||||
let c = ro(&st.db_path)?;
|
||||
let Some(made) = latest_run(&c)? else { return ok(json!({"rows": []})) };
|
||||
if let Some(hit) = st.rank_cache.read().unwrap().get(&(made.clone(), h)) { return Ok((200, "application/json", hit.clone())); }
|
||||
let mut names: HashMap<String, (String, bool, bool)> = HashMap::new();
|
||||
for r in c.prepare("SELECT artist_id, name, json_extract(raw, '$.artist.country'), json_extract(raw, '$.artist.type') FROM dr_artists")?
|
||||
.query_map([], |r| Ok((r.get::<_, String>(0)?, r.get::<_, String>(1)?, r.get::<_, Option<String>>(2)?, r.get::<_, Option<String>>(3)?)))? {
|
||||
let (id, n, country, kind) = r?;
|
||||
names.insert(id, (n, country.as_deref() == Some("Danmark"), kind.is_some_and(|k| k != "person")));
|
||||
}
|
||||
let mut rows: HashMap<String, Value> = HashMap::new();
|
||||
let mut st_ = c.prepare("SELECT key, target, prob, mean, lo, hi FROM forecasts WHERE made_at = ?1 AND model_version = ?2 AND horizon = ?3")?;
|
||||
let it = st_.query_map(params![made, VERSION, h as i64], |r| Ok((r.get::<_, String>(0)?, r.get::<_, String>(1)?, r.get::<_, Option<f64>>(2)?,
|
||||
r.get::<_, Option<f64>>(3)?, r.get::<_, Option<f64>>(4)?, r.get::<_, Option<f64>>(5)?)))?;
|
||||
for r in it {
|
||||
let (key, target, prob, mean, lo, hi) = r?;
|
||||
let Some((id, ch)) = key.split_once(':') else { continue };
|
||||
let e = rows.entry(id.to_string()).or_insert_with(|| {
|
||||
let (n, dk, group) = names.get(id).cloned().unwrap_or_default();
|
||||
json!({"id": id, "name": n, "danish": dk, "group": group, "p": {}, "plays": {}})
|
||||
});
|
||||
if target == "any" {
|
||||
e["p_any"] = json!(prob); e["mean"] = json!(mean); e["lo"] = json!(lo); e["hi"] = json!(hi);
|
||||
} else {
|
||||
e["p"][ch] = json!(prob.map(|p| (p * 1000.0).round() / 1000.0));
|
||||
e["plays"][ch] = json!(mean.map(|m| (m * 10.0).round() / 10.0));
|
||||
}
|
||||
}
|
||||
let mut out: Vec<Value> = rows.into_values().collect();
|
||||
out.sort_by(|a, b| b["mean"].as_f64().unwrap_or(0.0).total_cmp(&a["mean"].as_f64().unwrap_or(0.0)));
|
||||
let body = Arc::new(json!({"made_at": made, "h": h, "rows": out}).to_string());
|
||||
let mut cache = st.rank_cache.write().unwrap();
|
||||
cache.retain(|k, _| k.0 == made);
|
||||
cache.insert((made, h), body.clone());
|
||||
Ok((200, "application/json", body))
|
||||
}
|
||||
|
||||
fn artist(st: &State, id: &str) -> Result<Reply> {
|
||||
let c = ro(&st.db_path)?;
|
||||
let Some((name, raw)) = c.query_row("SELECT name, raw FROM dr_artists WHERE artist_id = ?1", [id], |r| Ok((r.get::<_, String>(0)?, r.get::<_, String>(1)?))).optional()?
|
||||
else { return Ok((404, "application/json", Arc::new(json!({"error": "unknown artist"}).to_string()))) };
|
||||
let v: Value = serde_json::from_str(&raw).unwrap_or(Value::Null);
|
||||
let a = &v["artist"];
|
||||
let image = a["images"][0]["sizes"].as_array().and_then(|s| s.iter().find(|x| x["size"] == "medium")).and_then(|x| x["source"].as_str()).map(str::to_string);
|
||||
let top: Vec<Value> = v["mostPlayed"].as_array().into_iter().flatten().take(8)
|
||||
.map(|t| json!({"title": t["title"], "total": t["total"]})).collect();
|
||||
|
||||
// Weekly plays per channel, in the panel's weeks when the models are loaded.
|
||||
// Complete weeks only (as in the panel): ended before the first artist page was read.
|
||||
let mut weeks: Vec<String> = c.prepare(
|
||||
"SELECT DISTINCT week_start FROM dr_artist_weeks
|
||||
WHERE date(week_start, '+7 days') <= (SELECT substr(min(fetched_at), 1, 10) FROM dr_artists) ORDER BY week_start")?
|
||||
.query_map([], |r| r.get(0))?.collect::<Result<_, _>>()?;
|
||||
if let Some(m) = st.models.read().unwrap().as_ref() { weeks = m.panel.weeks.iter().map(|d| d.to_string()).collect(); }
|
||||
let widx: HashMap<&str, usize> = weeks.iter().enumerate().map(|(i, w)| (w.as_str(), i)).collect();
|
||||
let mut plays: HashMap<&str, Vec<i64>> = CHANNELS.iter().map(|c| (*c, vec![0; weeks.len()])).collect();
|
||||
for r in c.prepare("SELECT week_start, channel, plays FROM dr_artist_weeks WHERE artist_id = ?1")?
|
||||
.query_map([id], |r| Ok((r.get::<_, String>(0)?, r.get::<_, String>(1)?, r.get::<_, i64>(2)?)))? {
|
||||
let (w, ch, n) = r?;
|
||||
if let (Some(&i), Some(v)) = (widx.get(w.as_str()), plays.get_mut(ch.as_str())) { v[i] = n; }
|
||||
}
|
||||
|
||||
let mut fc: Vec<Value> = Vec::new();
|
||||
if let Some(made) = latest_run(&c)? {
|
||||
let mut byh: HashMap<i64, Value> = HashMap::new();
|
||||
for r in c.prepare("SELECT horizon, target_week, key, prob, mean, lo, hi FROM forecasts WHERE made_at = ?1 AND model_version = ?2 AND key LIKE ?3")?
|
||||
.query_map(params![made, VERSION, format!("{id}:%")], |r| Ok((r.get::<_, i64>(0)?, r.get::<_, String>(1)?, r.get::<_, String>(2)?,
|
||||
r.get::<_, Option<f64>>(3)?, r.get::<_, Option<f64>>(4)?, r.get::<_, Option<f64>>(5)?, r.get::<_, Option<f64>>(6)?)))? {
|
||||
let (h, week, key, prob, mean, lo, hi) = r?;
|
||||
let e = byh.entry(h).or_insert_with(|| json!({"h": h, "week": week, "p": {}, "plays": {}}));
|
||||
let ch = key.split_once(':').map(|x| x.1).unwrap_or("");
|
||||
if ch == "any" { e["p_any"] = json!(prob); e["mean"] = json!(mean); e["lo"] = json!(lo); e["hi"] = json!(hi); }
|
||||
else { e["p"][ch] = json!(prob); e["plays"][ch] = json!(mean); }
|
||||
}
|
||||
let mut v: Vec<(i64, Value)> = byh.into_iter().collect();
|
||||
v.sort_by_key(|x| x.0);
|
||||
fc = v.into_iter().map(|x| x.1).collect();
|
||||
}
|
||||
|
||||
// Chart runs: rows whose artist field names this artist (same matching as the panel, roughly).
|
||||
let key = crate::model::panel::key(&name);
|
||||
let mut charts: Vec<Value> = Vec::new();
|
||||
if key.chars().count() >= 3 {
|
||||
let like = format!("%{}%", name.to_uppercase());
|
||||
for r in c.prepare("SELECT chart, year, week, position, artist, title FROM hitlisten_charts
|
||||
WHERE chart IN ('t40', 'airplay') AND upper(artist) LIKE ?1 ORDER BY year DESC, week DESC LIMIT 400")?
|
||||
.query_map([like], |r| Ok((r.get::<_, String>(0)?, r.get::<_, i64>(1)?, r.get::<_, i64>(2)?, r.get::<_, i64>(3)?, r.get::<_, String>(4)?, r.get::<_, String>(5)?)))? {
|
||||
let (chart, y, w, pos, art, title) = r?;
|
||||
let names: Vec<String> = art.to_lowercase().split(&[',', '&'][..]).flat_map(|p| p.split(" feat").map(str::to_string).collect::<Vec<_>>())
|
||||
.flat_map(|p| p.split(" x ").map(crate::model::panel::key).collect::<Vec<_>>()).collect();
|
||||
if !names.iter().any(|n| *n == key) && crate::model::panel::key(&art) != key { continue; }
|
||||
charts.push(json!({"chart": chart, "year": y, "week": w, "position": pos, "title": title}));
|
||||
}
|
||||
}
|
||||
|
||||
ok(json!({
|
||||
"id": id, "name": name, "country": a["country"], "gender": a["gender"], "description": a["description"],
|
||||
"debut": a["lifespan"]["beginYear"], "type": a["type"], "image": image, "top_tracks": top,
|
||||
"weeks": weeks, "plays": plays, "forecasts": fc, "charts": charts,
|
||||
}))
|
||||
}
|
||||
|
||||
/// One artist's forecast under a changed chart situation. Overrides (all optional):
|
||||
/// entry weeks since a new chart entry (a release that charted), "none" = never
|
||||
/// t40 position on the latest Track Top-40 (0 = not on it)
|
||||
/// air position on the latest Airplay Top-20 (0 = not on it)
|
||||
fn whatif(st: &State, q: &HashMap<String, String>) -> Result<Reply> {
|
||||
let guard = st.models.read().unwrap();
|
||||
let Some(m) = guard.as_ref() else { return Ok((503, "application/json", Arc::new(json!({"loading": true}).to_string()))) };
|
||||
let Some(&a) = m.index.get(q.get("id").map(String::as_str).unwrap_or("")) else {
|
||||
return Ok((404, "application/json", Arc::new(json!({"error": "artist not in the model"}).to_string())));
|
||||
};
|
||||
let h: usize = q.get("h").and_then(|v| v.parse().ok()).unwrap_or(4).clamp(1, MAX_H);
|
||||
let d = &m.direct[h - 1];
|
||||
let p = &m.panel;
|
||||
let t0 = p.w() - 1;
|
||||
let chart = p.chart(a, t0);
|
||||
let num = |k: &str| q.get(k).filter(|v| !v.is_empty()).and_then(|v| v.parse::<f64>().ok());
|
||||
let entry: Option<Option<f64>> = q.get("entry").filter(|v| !v.is_empty()).map(|v| if v == "none" { None } else { v.parse().ok() });
|
||||
let t40 = num("t40");
|
||||
let air = num("air");
|
||||
|
||||
let run = |overrides: bool| -> Value {
|
||||
let mut x = vec![0.0; P];
|
||||
let (mut mu, mut p_nb, mut p_lg) = ([0.0; C], [0.0; C], [0.0; C]);
|
||||
for c in 0..C {
|
||||
features::row(p, a, c, t0, t0 + h, &mut x);
|
||||
if overrides {
|
||||
if let Some(e) = entry {
|
||||
let (r6, r26) = match e { Some(w) => ((-w / 6.0).exp(), (-w / 26.0).exp()), None => (0.0, 0.0) };
|
||||
x[12] = r6; x[13] = r26;
|
||||
}
|
||||
if let Some(r) = t40 { x[10] = if r >= 1.0 && r <= 40.0 { (41.0 - r) / 40.0 } else { 0.0 }; }
|
||||
if let Some(r) = air { x[11] = if r >= 1.0 && r <= 20.0 { (21.0 - r) / 20.0 } else { 0.0 }; }
|
||||
}
|
||||
mu[c] = glm::dot(&d.nb, &x).clamp(-30.0, 12.0).exp();
|
||||
p_nb[c] = glm::nb_tail(mu[c], d.alpha, m.tau[c]);
|
||||
p_lg[c] = glm::sigmoid(glm::dot(&d.logit, &x));
|
||||
}
|
||||
let sims = 4000;
|
||||
let shape = 1.0 / d.alpha;
|
||||
let mut rng = Rng(0xa1 ^ a as u64);
|
||||
let (mut any, mut tot) = (0usize, Vec::with_capacity(sims));
|
||||
for _ in 0..sims {
|
||||
let g = rng.gamma(shape) / shape;
|
||||
let (mut s, mut hit) = (0.0, false);
|
||||
for c in 0..C { let y = rng.poisson(mu[c] * g); s += y; hit |= y >= m.tau[c] as f64; }
|
||||
any += hit as usize; tot.push(s);
|
||||
}
|
||||
tot.sort_by(f64::total_cmp);
|
||||
let ch = |v: &[f64; C]| CHANNELS.iter().zip(v).map(|(c, p)| (c.to_string(), json!(p))).collect::<serde_json::Map<_, _>>();
|
||||
json!({"p_any": any as f64 / sims as f64, "mean": mu.iter().sum::<f64>(), "lo": tot[sims / 10], "hi": tot[sims * 9 / 10],
|
||||
"p": ch(&p_nb), "p_logit": ch(&p_lg), "plays": ch(&mu)})
|
||||
};
|
||||
ok(json!({
|
||||
"h": h, "week": (p.weeks[t0] + chrono::Duration::weeks(h as i64)).to_string(),
|
||||
"current": {"t40": chart.t40_rank, "air": chart.air_rank,
|
||||
"entry": if chart.weeks_since_entry == u16::MAX { Value::Null } else { json!(chart.weeks_since_entry) },
|
||||
"t40_hits": chart.t40_hits, "air_hits": chart.air_hits},
|
||||
"base": run(false), "scenario": run(true),
|
||||
}))
|
||||
}
|
||||
@@ -0,0 +1,384 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<meta name="robots" content="noindex">
|
||||
<title>Airplay Predictor</title>
|
||||
<style>
|
||||
:root {
|
||||
--bg: #f7f6f2; --panel: #ffffff; --ink: #1d1d1b; --muted: #6b6a64; --line: #e4e2da;
|
||||
--accent: #c2410c; --accent-soft: #fde7d9; --good: #15803d; --bad: #b91c1c;
|
||||
--p3: #2563eb; --p4: #c2410c; --p5: #7c3aed; --p6: #0f766e; --p8: #a16207;
|
||||
--shadow: 0 1px 2px rgba(0,0,0,.05), 0 8px 24px rgba(0,0,0,.06);
|
||||
}
|
||||
@media (prefers-color-scheme: dark) {
|
||||
:root {
|
||||
--bg: #141412; --panel: #1d1d1a; --ink: #ecebe6; --muted: #9b9a92; --line: #2f2e2a;
|
||||
--accent: #fb923c; --accent-soft: #3a2316; --good: #4ade80; --bad: #f87171;
|
||||
--p3: #60a5fa; --p4: #fb923c; --p5: #a78bfa; --p6: #2dd4bf; --p8: #facc15;
|
||||
--shadow: 0 1px 2px rgba(0,0,0,.3), 0 8px 24px rgba(0,0,0,.3);
|
||||
}
|
||||
}
|
||||
* { box-sizing: border-box; }
|
||||
body { margin: 0; background: var(--bg); color: var(--ink); font: 15px/1.45 ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
|
||||
header { padding: 28px 24px 8px; max-width: 1240px; margin: 0 auto; }
|
||||
h1 { margin: 0; font-size: 26px; letter-spacing: -.02em; }
|
||||
h1 span { color: var(--accent); }
|
||||
.sub { color: var(--muted); margin-top: 4px; font-size: 14px; }
|
||||
nav { display: flex; gap: 4px; margin-top: 18px; border-bottom: 1px solid var(--line); }
|
||||
nav button { background: none; border: 0; padding: 10px 14px; font: inherit; color: var(--muted); cursor: pointer; border-bottom: 2px solid transparent; margin-bottom: -1px; }
|
||||
nav button.on { color: var(--ink); border-color: var(--accent); font-weight: 600; }
|
||||
main { max-width: 1240px; margin: 0 auto; padding: 16px 24px 60px; }
|
||||
.controls { display: flex; flex-wrap: wrap; gap: 14px 22px; align-items: end; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 14px 16px; box-shadow: var(--shadow); }
|
||||
.ctl { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
|
||||
.ctl b { color: var(--ink); text-transform: none; letter-spacing: 0; font-size: 14px; }
|
||||
input[type=range] { width: 220px; accent-color: var(--accent); }
|
||||
input[type=search], select { font: inherit; padding: 7px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--bg); color: var(--ink); min-width: 160px; }
|
||||
label.chk { display: flex; gap: 6px; align-items: center; text-transform: none; letter-spacing: 0; font-size: 14px; color: var(--ink); }
|
||||
.tablewrap { margin-top: 16px; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; overflow-x: auto; box-shadow: var(--shadow); }
|
||||
table { border-collapse: collapse; width: 100%; font-variant-numeric: tabular-nums; }
|
||||
th, td { padding: 9px 12px; text-align: right; border-bottom: 1px solid var(--line); white-space: nowrap; }
|
||||
th { font-size: 12px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .04em; cursor: pointer; user-select: none; position: sticky; top: 0; background: var(--panel); }
|
||||
th.l, td.l { text-align: left; }
|
||||
tbody tr { cursor: pointer; }
|
||||
tbody tr:hover { background: var(--accent-soft); }
|
||||
td.name { font-weight: 600; }
|
||||
td.name small { color: var(--muted); font-weight: 400; margin-left: 6px; }
|
||||
.bar { display: inline-flex; align-items: center; gap: 8px; }
|
||||
.bar i { display: inline-block; height: 8px; border-radius: 4px; background: var(--accent); }
|
||||
.cell { display: inline-block; min-width: 44px; padding: 2px 6px; border-radius: 6px; text-align: center; font-size: 13px; }
|
||||
.more { display: block; margin: 12px auto; font: inherit; padding: 8px 16px; border-radius: 8px; border: 1px solid var(--line); background: var(--panel); color: var(--ink); cursor: pointer; }
|
||||
.empty { padding: 40px; text-align: center; color: var(--muted); }
|
||||
/* drawer */
|
||||
#shade { position: fixed; inset: 0; background: rgba(0,0,0,.35); opacity: 0; pointer-events: none; transition: opacity .2s; }
|
||||
#shade.on { opacity: 1; pointer-events: auto; }
|
||||
#drawer { position: fixed; top: 0; right: 0; bottom: 0; width: min(760px, 100vw); background: var(--panel); transform: translateX(100%); transition: transform .25s ease; overflow-y: auto; box-shadow: -10px 0 40px rgba(0,0,0,.2); }
|
||||
#drawer.on { transform: none; }
|
||||
.dhead { display: flex; gap: 16px; align-items: center; padding: 20px 22px; border-bottom: 1px solid var(--line); position: sticky; top: 0; background: var(--panel); z-index: 1; }
|
||||
.dhead img { width: 64px; height: 64px; border-radius: 50%; object-fit: cover; background: var(--line); }
|
||||
.dhead h2 { margin: 0; font-size: 22px; }
|
||||
.dhead .x { margin-left: auto; font-size: 22px; background: none; border: 0; color: var(--muted); cursor: pointer; }
|
||||
.sec { padding: 16px 22px; border-bottom: 1px solid var(--line); }
|
||||
.sec h3 { margin: 0 0 10px; font-size: 13px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); }
|
||||
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 10px; }
|
||||
.kpi { background: var(--bg); border-radius: 10px; padding: 10px 12px; }
|
||||
.kpi div { font-size: 12px; color: var(--muted); }
|
||||
.kpi b { font-size: 22px; }
|
||||
.kpi .d { font-size: 13px; margin-left: 6px; }
|
||||
.up { color: var(--good); } .down { color: var(--bad); }
|
||||
.legend { display: flex; flex-wrap: wrap; gap: 12px; font-size: 12px; color: var(--muted); margin-top: 6px; }
|
||||
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 4px; vertical-align: -1px; }
|
||||
svg text { fill: var(--muted); font-size: 11px; }
|
||||
.wi { display: grid; grid-template-columns: 1fr; gap: 12px; }
|
||||
.wi .row { display: grid; grid-template-columns: 210px 1fr 60px; align-items: center; gap: 10px; font-size: 14px; }
|
||||
.wi input[type=range] { width: 100%; }
|
||||
.wtable td, .wtable th { padding: 6px 8px; }
|
||||
.note { color: var(--muted); font-size: 13px; }
|
||||
.tracks { columns: 2; font-size: 14px; } .tracks div { break-inside: avoid; padding: 2px 0; }
|
||||
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
|
||||
.chip { background: var(--bg); border-radius: 999px; padding: 3px 10px; font-size: 13px; }
|
||||
/* model tab */
|
||||
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 16px; }
|
||||
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 18px; box-shadow: var(--shadow); }
|
||||
.card h3 { margin-top: 0; }
|
||||
.card p { color: var(--muted); }
|
||||
.prose { max-width: 760px; }
|
||||
.prose li { margin: 6px 0; }
|
||||
@media (max-width: 640px) {
|
||||
header, main { padding-left: 16px; padding-right: 16px; }
|
||||
input[type=range] { width: 100%; }
|
||||
.ctl { width: 100%; }
|
||||
.wi .row { grid-template-columns: 1fr 60px; } .wi .row label { grid-column: 1 / -1; }
|
||||
.tracks { columns: 1; }
|
||||
.hide-sm { display: none; }
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<header>
|
||||
<h1>Airplay <span>Predictor</span></h1>
|
||||
<div class="sub" id="sub">Loading…</div>
|
||||
<nav>
|
||||
<button class="on" data-tab="rank">Ranking</button>
|
||||
<button data-tab="model">How good is it?</button>
|
||||
</nav>
|
||||
</header>
|
||||
<main>
|
||||
<section id="tab-rank">
|
||||
<div class="controls">
|
||||
<div class="ctl">Week <b id="hlabel">—</b><input type="range" id="h" min="1" max="12" value="4"></div>
|
||||
<div class="ctl">Station
|
||||
<select id="ch">
|
||||
<option value="any">Any DR station</option>
|
||||
<option value="p3">P3</option>
|
||||
<option value="p4">P4 (best region)</option>
|
||||
<option value="p5">P5</option>
|
||||
<option value="p6beat">P6 Beat</option>
|
||||
<option value="p8jazz">P8 Jazz</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="ctl">Sort by
|
||||
<select id="sort">
|
||||
<option value="p">Chance of rotation</option>
|
||||
<option value="plays">Expected plays</option>
|
||||
<option value="up">Biggest climbers</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="ctl">Artist <input type="search" id="q" placeholder="Search…"></div>
|
||||
<div class="ctl"><label class="chk"><input type="checkbox" id="dk"> Danish acts only</label></div>
|
||||
</div>
|
||||
<div class="tablewrap">
|
||||
<table>
|
||||
<thead><tr>
|
||||
<th class="l">#</th><th class="l">Artist</th><th>Chance</th>
|
||||
<th class="hide-sm">P3</th><th class="hide-sm">P4</th><th class="hide-sm">P5</th><th class="hide-sm">P6</th><th class="hide-sm">P8</th>
|
||||
<th>Plays / wk (80%)</th>
|
||||
</tr></thead>
|
||||
<tbody id="rows"><tr><td colspan="9" class="empty">Loading…</td></tr></tbody>
|
||||
</table>
|
||||
</div>
|
||||
<button class="more" id="more" hidden>Show more</button>
|
||||
<p class="note">Chance = probability the artist is in rotation (plays at or above the station's weekly threshold) in the chosen week. "Biggest climbers" ranks by expected plays in that week versus the latest week. Click an artist for history and a what-if.</p>
|
||||
</section>
|
||||
|
||||
<section id="tab-model" hidden>
|
||||
<div class="prose">
|
||||
<h2>What the model does</h2>
|
||||
<p>For every artist DR has played, it forecasts each DR music station's weekly plays 1–12 weeks ahead, and the chance that the artist is "in rotation" (at or above the station's weekly play threshold). It learns from 51 weeks of DR's per-artist weekly play counts plus the artist's IFPI chart history (Track Top-40, Airplay Top-20).</p>
|
||||
<p id="taus" class="note"></p>
|
||||
</div>
|
||||
<h2>Backtest</h2>
|
||||
<p class="note prose">Walk-forward: for each week from week 27, the models are fitted only on what was known at the time and forecast 1, 4 and 8 weeks ahead. "Adds" are rows where the artist was not in rotation at the time of the forecast, which is the hard and interesting part. Skill above 0 means better than the baseline.</p>
|
||||
<div class="cards" id="cards"></div>
|
||||
<div class="prose">
|
||||
<h2>Known limits</h2>
|
||||
<ul>
|
||||
<li><b>The artist pool is selected.</b> It holds only artists DR played in mid-November 2026, so every artist had airplay at the end of the window. Scores for the last weeks are flattered.</li>
|
||||
<li><b>Not yet "if they release a song on date X".</b> The ranking is expected airplay given current form and chart situation. Conditioning on a release needs release dates (being collected).</li>
|
||||
<li><b>Rotation thresholds are estimates</b> until DR's weekly rotation lists are parsed.</li>
|
||||
<li><b>Christmas</b> is only seen once, as history. Forecasts for weeks 48–52 don't know about Christmas songs yet.</li>
|
||||
</ul>
|
||||
</div>
|
||||
</section>
|
||||
</main>
|
||||
|
||||
<div id="shade"></div>
|
||||
<aside id="drawer" aria-hidden="true"></aside>
|
||||
|
||||
<script>
|
||||
const BASE = location.pathname.replace(/\/(index\.html)?$/, '');
|
||||
const API = p => fetch(BASE + '/api/' + p).then(r => r.json());
|
||||
const GROUPS = [['p3', 'P3'], ['p4', 'P4'], ['p5', 'P5'], ['p6beat', 'P6'], ['p8jazz', 'P8']];
|
||||
const P4S = ['p4kbh','p4sjaelland','p4fyn','p4syd','p4trekanten','p4vest','p4aarhus','p4nord','p4bornholm','p4esbjerg'];
|
||||
const COLORS = { p3: 'var(--p3)', p4: 'var(--p4)', p5: 'var(--p5)', p6beat: 'var(--p6)', p8jazz: 'var(--p8)' };
|
||||
let meta = null, data = { 1: null }, shown = 100, current = [];
|
||||
const $ = id => document.getElementById(id);
|
||||
const esc = s => String(s ?? '').replace(/[&<>"']/g, c => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[c]));
|
||||
const pct = p => p == null ? '—' : (p >= 0.995 ? '>99' : p < 0.005 ? '<1' : Math.round(p * 100)) + '%';
|
||||
const fmtDate = s => new Date(s + 'T00:00:00Z').toLocaleDateString('en-GB', { day: 'numeric', month: 'short', timeZone: 'UTC' });
|
||||
|
||||
function chanP(r, g) {
|
||||
if (g === 'any') return r.p_any;
|
||||
if (g === 'p4') return Math.max(...P4S.map(c => r.p[c] ?? 0));
|
||||
return r.p[g];
|
||||
}
|
||||
function chanPlays(r, g) {
|
||||
if (g === 'any') return r.mean;
|
||||
if (g === 'p4') return P4S.reduce((s, c) => s + (r.plays[c] ?? 0), 0) / P4S.length;
|
||||
return r.plays[g];
|
||||
}
|
||||
function heat(p) {
|
||||
if (p == null) return '';
|
||||
const a = Math.min(1, Math.max(0, p));
|
||||
return `background: color-mix(in srgb, var(--accent) ${Math.round(a * 85)}%, transparent); color: ${a > .55 ? '#fff' : 'inherit'}`;
|
||||
}
|
||||
|
||||
async function loadMeta() {
|
||||
meta = await API('meta');
|
||||
if (!meta.ready) { $('sub').textContent = 'No forecasts yet: run `airplay model rank`.'; return; }
|
||||
$('h').max = meta.horizons.length;
|
||||
$('sub').textContent = `DR plays through the week of ${fmtDate(meta.origin_week)} · forecasts made ${meta.made_at.slice(0, 10)} · model ${meta.model}`;
|
||||
if (meta.tau) $('taus').textContent = 'Rotation thresholds (plays per week): ' + meta.channels.map((c, i) => `${c} ${meta.tau[i]}`).join(', ') + '.';
|
||||
renderModel();
|
||||
}
|
||||
|
||||
async function loadRank() {
|
||||
const h = +$('h').value;
|
||||
const hz = meta.horizons.find(x => x.h === h);
|
||||
$('hlabel').textContent = hz ? `${fmtDate(hz.week)} (+${h} wk)` : `+${h} wk`;
|
||||
if (!data[h]) data[h] = (await API('rank?h=' + h)).rows;
|
||||
if (!data[1]) data[1] = (await API('rank?h=1')).rows;
|
||||
render();
|
||||
}
|
||||
|
||||
function render() {
|
||||
const h = +$('h').value, g = $('ch').value, sort = $('sort').value, q = $('q').value.trim().toLowerCase(), dk = $('dk').checked;
|
||||
const now = Object.fromEntries((data[1] || []).map(r => [r.id, r]));
|
||||
let rows = data[h].filter(r => (!q || r.name.toLowerCase().includes(q)) && (!dk || r.danish));
|
||||
const key = sort === 'p' ? r => [chanP(r, g) ?? 0, chanPlays(r, g) ?? 0]
|
||||
: sort === 'plays' ? r => [chanPlays(r, g) ?? 0, 0]
|
||||
: r => [(chanPlays(r, g) ?? 0) - (chanPlays(now[r.id] || r, g) ?? 0), 0];
|
||||
rows = rows.map(r => [key(r), r]).sort((a, b) => b[0][0] - a[0][0] || b[0][1] - a[0][1]).map(x => x[1]);
|
||||
current = rows;
|
||||
const body = rows.slice(0, shown).map((r, i) => {
|
||||
const p = chanP(r, g), pl = chanPlays(r, g);
|
||||
const range = g === 'any' ? ` <small class="note">(${Math.round(r.lo)}–${Math.round(r.hi)})</small>` : '';
|
||||
return `<tr data-id="${esc(r.id)}">
|
||||
<td class="l">${i + 1}</td>
|
||||
<td class="l name">${esc(r.name)}${r.danish ? '<small>DK</small>' : ''}</td>
|
||||
<td><span class="bar"><i style="width:${Math.round((p ?? 0) * 70)}px"></i>${pct(p)}</span></td>
|
||||
${GROUPS.map(([c]) => `<td class="hide-sm"><span class="cell" style="${heat(chanP(r, c))}">${pct(chanP(r, c))}</span></td>`).join('')}
|
||||
<td>${pl == null ? '—' : pl.toFixed(pl < 10 ? 1 : 0)}${range}</td>
|
||||
</tr>`;
|
||||
}).join('');
|
||||
$('rows').innerHTML = body || '<tr><td colspan="9" class="empty">No artists match.</td></tr>';
|
||||
$('more').hidden = rows.length <= shown;
|
||||
}
|
||||
|
||||
// ---------- artist drawer ----------
|
||||
function openDrawer(html) { $('drawer').innerHTML = html; $('drawer').classList.add('on'); $('shade').classList.add('on'); $('drawer').setAttribute('aria-hidden', 'false'); }
|
||||
function closeDrawer() { $('drawer').classList.remove('on'); $('shade').classList.remove('on'); $('drawer').setAttribute('aria-hidden', 'true'); }
|
||||
|
||||
function historyChart(a) {
|
||||
// Weekly plays: P3, P4 (mean of regions), P5, P6, P8 as lines, then the forecast of total plays as a band.
|
||||
const W = 700, H = 220, L = 34, R = 10, T = 10, B = 26;
|
||||
const n = a.weeks.length, f = a.forecasts.length;
|
||||
const series = GROUPS.map(([g]) => g === 'p4'
|
||||
? a.weeks.map((_, i) => P4S.reduce((s, c) => s + (a.plays[c]?.[i] ?? 0), 0) / P4S.length)
|
||||
: (a.plays[g] || []).slice());
|
||||
const total = a.weeks.map((_, i) => Object.values(a.plays).reduce((s, v) => s + (v[i] ?? 0), 0));
|
||||
const maxY = Math.max(5, ...series.flat(), ...a.forecasts.map(x => (x.hi ?? 0) / 4), ...total.map(t => t / 4));
|
||||
const X = i => L + (W - L - R) * i / (n + f - 1);
|
||||
const Y = v => T + (H - T - B) * (1 - v / maxY);
|
||||
let s = `<svg viewBox="0 0 ${W} ${H}" width="100%" role="img" aria-label="Weekly plays per station">`;
|
||||
for (let k = 0; k <= 4; k++) {
|
||||
const v = maxY * k / 4;
|
||||
s += `<line x1="${L}" x2="${W - R}" y1="${Y(v)}" y2="${Y(v)}" stroke="var(--line)"/><text x="${L - 6}" y="${Y(v) + 4}" text-anchor="end">${Math.round(v)}</text>`;
|
||||
}
|
||||
s += `<rect x="${X(n - 0.5)}" y="${T}" width="${X(n + f - 1) - X(n - 0.5)}" height="${H - T - B}" fill="var(--accent-soft)" opacity=".6"/>`;
|
||||
s += `<text x="${X(n - 0.3)}" y="${T + 12}">forecast</text>`;
|
||||
// forecast band of total plays scaled to "per station" (÷ 4 so it shares the axis), labelled
|
||||
if (f) {
|
||||
const up = a.forecasts.map((x, i) => `${X(n + i)},${Y((x.hi ?? 0) / 4)}`).join(' ');
|
||||
const dn = a.forecasts.map((x, i) => `${X(n + i)},${Y((x.lo ?? 0) / 4)}`).reverse().join(' ');
|
||||
s += `<polygon points="${up} ${dn}" fill="var(--muted)" opacity=".18"/>`;
|
||||
s += `<polyline points="${X(n - 1)},${Y(total[n - 1] / 4)} ${a.forecasts.map((x, i) => `${X(n + i)},${Y((x.mean ?? 0) / 4)}`).join(' ')}" fill="none" stroke="var(--muted)" stroke-width="2" stroke-dasharray="4 3"/>`;
|
||||
s += `<polyline points="${total.map((t, i) => `${X(i)},${Y(t / 4)}`).join(' ')}" fill="none" stroke="var(--muted)" stroke-width="1.5" opacity=".6"/>`;
|
||||
}
|
||||
GROUPS.forEach(([g], k) => {
|
||||
const v = series[k];
|
||||
if (!v.some(x => x > 0)) return;
|
||||
s += `<polyline points="${v.map((y, i) => `${X(i)},${Y(y)}`).join(' ')}" fill="none" stroke="${COLORS[g]}" stroke-width="2"/>`;
|
||||
});
|
||||
[0, Math.floor(n / 2), n - 1].forEach(i => s += `<text x="${X(i)}" y="${H - 8}" text-anchor="middle">${fmtDate(a.weeks[i])}</text>`);
|
||||
if (f) s += `<text x="${X(n + f - 1)}" y="${H - 8}" text-anchor="end">${fmtDate(a.forecasts[f - 1].week)}</text>`;
|
||||
s += '</svg>';
|
||||
s += `<div class="legend">${GROUPS.map(([g, l]) => `<span><i style="background:${COLORS[g]}"></i>${l}${g === 'p4' ? ' (avg region)' : ''}</span>`).join('')}<span><i style="background:var(--muted)"></i>All DR ÷ 4, forecast with 80% band</span></div>`;
|
||||
return s;
|
||||
}
|
||||
|
||||
async function showArtist(id) {
|
||||
openDrawer('<div class="sec">Loading…</div>');
|
||||
const a = await API('artist?id=' + encodeURIComponent(id));
|
||||
if (a.error) { openDrawer(`<div class="sec">${esc(a.error)}</div>`); return; }
|
||||
const h = +$('h').value;
|
||||
const fc = a.forecasts.find(x => x.h === h) || a.forecasts[0] || {};
|
||||
const facts = [a.country, a.description, a.debut ? `active since ${a.debut}` : null].filter(Boolean).map(esc).join(' · ');
|
||||
const runs = {};
|
||||
a.charts.forEach(c => { const k = c.chart + '|' + c.title; (runs[k] ??= { ...c, weeks: 0, best: 99, first: c }); runs[k].weeks++; runs[k].best = Math.min(runs[k].best, c.position); runs[k].first = c; });
|
||||
const runList = Object.values(runs).slice(0, 12);
|
||||
openDrawer(`
|
||||
<div class="dhead">
|
||||
${a.image ? `<img src="${esc(a.image)}" alt="">` : '<img alt="">'}
|
||||
<div><h2>${esc(a.name)}</h2><div class="note">${facts}</div></div>
|
||||
<button class="x" aria-label="Close" onclick="closeDrawer()">×</button>
|
||||
</div>
|
||||
<div class="sec">
|
||||
<h3>Week of ${fc.week ? fmtDate(fc.week) : '—'} (+${fc.h ?? h} weeks)</h3>
|
||||
<div class="kpis">
|
||||
<div class="kpi"><div>Any DR station</div><b>${pct(fc.p_any)}</b></div>
|
||||
${GROUPS.map(([g, l]) => `<div class="kpi"><div>${l}</div><b>${pct(chanP(fc, g))}</b></div>`).join('')}
|
||||
<div class="kpi"><div>Plays that week</div><b>${Math.round(fc.mean ?? 0)}</b> <span class="note">${Math.round(fc.lo ?? 0)}–${Math.round(fc.hi ?? 0)}</span></div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="sec"><h3>Weekly plays, last 51 weeks and next 12</h3>${historyChart(a)}</div>
|
||||
<div class="sec"><h3>What if…</h3><div id="wi" class="note">Loading the model…</div></div>
|
||||
${a.top_tracks.length ? `<div class="sec"><h3>Most played on DR (all time)</h3><div class="tracks">${a.top_tracks.map(t => `<div>${esc(t.title)} <span class="note">${t.total}</span></div>`).join('')}</div></div>` : ''}
|
||||
${runList.length ? `<div class="sec"><h3>Chart runs (IFPI)</h3><div class="chips">${runList.map(r => `<span class="chip">${r.chart === 't40' ? 'Track Top-40' : 'Airplay Top-20'}: ${esc(r.title)}, peak #${r.best}, ${r.weeks} wk, ${r.first.year}</span>`).join('')}</div></div>` : ''}
|
||||
`);
|
||||
whatIfUI(a, h);
|
||||
}
|
||||
|
||||
function whatIfUI(a, h) {
|
||||
const box = $('wi');
|
||||
box.innerHTML = `<div class="wi">
|
||||
<div class="row"><label for="wi-h">Weeks ahead</label><input type="range" id="wi-h" min="1" max="${meta.horizons.length}" value="${h}"><b id="wi-hv"></b></div>
|
||||
<div class="row"><label for="wi-e">New chart entry, weeks ago</label><input type="range" id="wi-e" min="-1" max="26" value="-1"><b id="wi-ev"></b></div>
|
||||
<div class="row"><label for="wi-t">Track Top-40 position now</label><input type="range" id="wi-t" min="0" max="40" value="0"><b id="wi-tv"></b></div>
|
||||
<div class="row"><label for="wi-a">Airplay Top-20 position now</label><input type="range" id="wi-a" min="0" max="20" value="0"><b id="wi-av"></b></div>
|
||||
<div id="wi-out"></div>
|
||||
<p class="note">Change the artist's chart situation (as if it were true this week) and see how the model's forecast moves. "New chart entry" stands in for a release that charted, until release dates are in the model.</p>
|
||||
</div>`;
|
||||
let init = false, timer = null;
|
||||
const label = () => {
|
||||
$('wi-hv').textContent = '+' + $('wi-h').value;
|
||||
$('wi-ev').textContent = $('wi-e').value < 0 ? 'none' : $('wi-e').value;
|
||||
$('wi-tv').textContent = +$('wi-t').value ? '#' + $('wi-t').value : 'off';
|
||||
$('wi-av').textContent = +$('wi-a').value ? '#' + $('wi-a').value : 'off';
|
||||
};
|
||||
const run = async () => {
|
||||
label();
|
||||
let qs = `id=${encodeURIComponent(a.id)}&h=${$('wi-h').value}`;
|
||||
if (init) qs += `&entry=${$('wi-e').value < 0 ? 'none' : $('wi-e').value}&t40=${$('wi-t').value}&air=${$('wi-a').value}`;
|
||||
const r = await fetch(BASE + '/api/whatif?' + qs);
|
||||
if (r.status === 503) { $('wi-out').innerHTML = '<p class="note">The model is still loading on the server; try again in a minute.</p>'; return; }
|
||||
const w = await r.json();
|
||||
if (w.error) { $('wi-out').innerHTML = `<p class="note">${esc(w.error)}</p>`; return; }
|
||||
if (!init) {
|
||||
init = true;
|
||||
$('wi-e').value = w.current.entry == null ? -1 : Math.min(26, w.current.entry);
|
||||
$('wi-t').value = w.current.t40; $('wi-a').value = w.current.air;
|
||||
label();
|
||||
}
|
||||
const d = (x, y, f) => { const v = y - x; if (Math.abs(v) < 0.005 * (f === 'p' ? 1 : 100)) return ''; return `<span class="d ${v > 0 ? 'up' : 'down'}">${v > 0 ? '+' : ''}${f === 'p' ? Math.round(v * 100) + ' pts' : Math.round(v)}</span>`; };
|
||||
const rows = [['Any DR station', w.base.p_any, w.scenario.p_any]].concat(GROUPS.map(([g, l]) => [l, chanP(w.base, g), chanP(w.scenario, g)]));
|
||||
$('wi-out').innerHTML = `<p class="note">Week of ${fmtDate(w.week)}. Now: ${w.current.t40 ? 'Track Top-40 #' + w.current.t40 : 'not on the Track Top-40'}, ${w.current.air ? 'Airplay Top-20 #' + w.current.air : 'not on the Airplay Top-20'}, ${w.current.entry == null ? 'no chart entry on record' : 'latest new chart entry ' + w.current.entry + ' weeks ago'}.</p>
|
||||
<div class="kpis">${rows.map(([l, b, s]) => `<div class="kpi"><div>${l}</div><b>${pct(s)}</b>${d(b, s, 'p')}<div>was ${pct(b)}</div></div>`).join('')}
|
||||
<div class="kpi"><div>Plays that week</div><b>${Math.round(w.scenario.mean)}</b>${d(w.base.mean, w.scenario.mean, 'n')}<div>was ${Math.round(w.base.mean)}</div></div></div>`;
|
||||
};
|
||||
['wi-h', 'wi-e', 'wi-t', 'wi-a'].forEach(id => $(id).addEventListener('input', () => { label(); clearTimeout(timer); timer = setTimeout(run, 120); }));
|
||||
run();
|
||||
}
|
||||
|
||||
// ---------- model tab ----------
|
||||
function renderModel() {
|
||||
const sc = meta.backtest || [];
|
||||
const get = (m, h, k) => sc.find(x => x.model === m && x.h === h && x.metric === k)?.value;
|
||||
const hs = [...new Set(sc.map(x => x.h))].sort((a, b) => a - b);
|
||||
const names = { persistence: 'Same as now', popularity: 'Last 12 weeks', kalman: 'Kalman form', nb: 'Play-count model', logit: 'Rotation model' };
|
||||
$('cards').innerHTML = hs.map(h => {
|
||||
const ms = ['persistence', 'popularity', 'kalman', 'nb', 'logit'];
|
||||
return `<div class="card"><h3>${h} week${h > 1 ? 's' : ''} ahead</h3>
|
||||
<table class="wtable"><thead><tr><th class="l">Model</th><th>Skill on adds<br>vs last 12 wks</th><th>Ranking<br>NDCG@50</th></tr></thead><tbody>
|
||||
${ms.map(m => { const s = get(m, h, 'skill_add_vs_popularity'); return `<tr><td class="l">${names[m]}</td><td class="${s > 0 ? 'up' : s < 0 ? 'down' : ''}">${s == null ? 'baseline' : (s > 0 ? '+' : '') + s.toFixed(3)}</td><td>${(get(m, h, 'ndcg50') ?? 0).toFixed(3)}</td></tr>`; }).join('')}
|
||||
</tbody></table></div>`;
|
||||
}).join('') || '<p class="note">No backtest stored yet (`airplay model backtest`).</p>';
|
||||
}
|
||||
|
||||
// ---------- wiring ----------
|
||||
document.querySelectorAll('nav button').forEach(b => b.addEventListener('click', () => {
|
||||
document.querySelectorAll('nav button').forEach(x => x.classList.toggle('on', x === b));
|
||||
$('tab-rank').hidden = b.dataset.tab !== 'rank'; $('tab-model').hidden = b.dataset.tab !== 'model';
|
||||
}));
|
||||
$('h').addEventListener('input', () => { shown = 100; loadRank(); });
|
||||
['ch', 'sort', 'dk'].forEach(id => $(id).addEventListener('change', () => { shown = 100; render(); }));
|
||||
$('q').addEventListener('input', () => { shown = 100; render(); });
|
||||
$('more').addEventListener('click', () => { shown += 100; render(); });
|
||||
$('rows').addEventListener('click', e => { const tr = e.target.closest('tr[data-id]'); if (tr) showArtist(tr.dataset.id); });
|
||||
$('shade').addEventListener('click', closeDrawer);
|
||||
document.addEventListener('keydown', e => { if (e.key === 'Escape') closeDrawer(); });
|
||||
loadMeta().then(() => meta.ready && loadRank());
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user