Release-week calendar: adjustable window (2-12 weeks, one release-event model each), time-since features moved to the release week, 'too soon' instead of special cases, season index (IFPI 2002-24) in the window; site: Best week tab, live ranking for any release week and window, single new-song view

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-15 12:24:01 +00:00
co-authored by Claude Opus 5.5
parent 347680bc9e
commit 3699944692
6 changed files with 369 additions and 154 deletions
+88 -14
View File
@@ -25,6 +25,26 @@ use tiny_http::{Header, Method, Response, Server};
const PAGE: &str = include_str!("web/index.html");
const MAX_H: usize = 12;
/// Window lengths offered (weeks after the release within which DR must add the song).
const WINDOWS: [usize; 6] = [2, 4, 6, 8, 10, 12];
/// Release weeks shown in the best-week calendar.
const CALENDAR: usize = 26;
/// "If the artist releases a new song in week t0+h+1, will DR add it within `win` weeks?":
/// (per channel, any station, any station if placed on NMF Denmark, P(NMF)), blended over the
/// new song's NMF placement by how often the artist's releases were placed. None = too soon after
/// the artist's last release (the model only learned from releases with a 6-week gap).
fn new_song(m: &Models, a: usize, h: usize, win: usize, nmf: Option<bool>) -> Option<([f64; C], f64, f64, f64)> {
use crate::model::adds::{p_nmf, too_soon, Scenario};
let p = &m.panel;
let t0 = p.w() - 1;
if too_soon(p, a, t0, h) { return None; }
let ev = m.events_w.get(&win).unwrap_or(&m.events);
let q = match nmf { Some(true) => 1.0, Some(false) => 0.0, None => p_nmf(p, a, t0) };
let (pcn, pan) = ev.predict_at(p, &m.rot, a, t0, h, Scenario::new(true, true).at(h), None);
let (pcr, par) = ev.predict_at(p, &m.rot, a, t0, h, Scenario::new(true, false).at(h), None);
Some((std::array::from_fn(|c| q * pcn[c] + (1.0 - q) * pcr[c]), q * pan + (1.0 - q) * par, pan, q))
}
struct Models {
made_at: String,
@@ -33,6 +53,8 @@ struct Models {
direct: Vec<Direct>, // index h - 1
adds: Vec<AddModel>, // index h
events: AddModel,
/// The release-event model per window length (weeks the add must come within).
events_w: HashMap<usize, AddModel>,
rot: Rot,
index: HashMap<String, usize>,
}
@@ -41,7 +63,7 @@ struct State {
db_path: String,
prefix: String,
models: RwLock<Option<Arc<Models>>>,
rank_cache: RwLock<HashMap<(String, usize), Arc<String>>>,
rank_cache: RwLock<HashMap<(String, usize, usize, bool), Arc<String>>>,
}
fn ro(path: &str) -> Result<Connection> {
@@ -108,8 +130,9 @@ fn fit_models(db_path: &str, made_at: String) -> Result<Models> {
let rot = Rot::new(&panel, &tau);
let adds = (0..=MAX_H).map(|h| AddModel::fit(&panel, &rot, h, panel.w() - 1)).collect();
let events = AddModel::fit_events(&panel, &rot, panel.w() - 1);
let events_w = WINDOWS.iter().map(|&w| (w, AddModel::fit_events_w(&panel, &rot, panel.w() - 1, w))).collect();
let index = panel.artists.iter().enumerate().map(|(i, a)| (a.id.clone(), i)).collect();
Ok(Models { made_at, panel, tau, direct, adds, events, rot, index })
Ok(Models { made_at, panel, tau, direct, adds, events, events_w, rot, index })
}
fn query(url: &str) -> HashMap<String, String> {
@@ -150,7 +173,9 @@ fn handle(st: &State, method: &Method, url: &str) -> Result<Reply> {
"" => 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(0).min(MAX_H)),
"/api/rank" => rank(st, q.get("h").and_then(|h| h.parse().ok()).unwrap_or(0).min(CALENDAR - 1),
q.get("w").and_then(|w| w.parse().ok()).filter(|w| WINDOWS.contains(w)).unwrap_or(8)),
"/api/bestweek" => bestweek(st, &q),
"/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()))),
@@ -191,7 +216,7 @@ fn fold(e: &mut Value, target: &str, ch: &str, prob: Option<f64>, mean: Option<f
"add_any_rel" => { e["add_any_rel"] = r3(prob); }
"add_any_nmf" => { e["add_any_nmf"] = r3(prob); }
"p_nmf" => { e["p_nmf"] = r3(prob); }
"recent_release" => { e["recent_release"] = json!(true); }
"too_soon" => { e["too_soon"] = json!(true); }
"add_rel" => { if !e["add_rel"].is_object() { e["add_rel"] = json!({}); } e["add_rel"][ch] = r3(prob); }
"p_release" => { e["p_release"] = r3(prob); }
"now" => { e["now"][ch] = json!({"on": prob.unwrap_or(0.0) > 0.5, "plays": mean}); }
@@ -227,10 +252,13 @@ fn bauer_week(c: &Connection) -> Result<HashMap<String, HashMap<String, (i64, Ve
Ok(out)
}
fn rank(st: &State, h: usize) -> Result<Reply> {
fn rank(st: &State, h: usize, win: 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 live = st.models.read().unwrap().clone();
let key = (made.clone(), h, win, live.is_some());
if let Some(hit) = st.rank_cache.read().unwrap().get(&key) { return Ok((200, "application/json", hit.clone())); }
let h_db = h.min(MAX_H);
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)?)))? {
@@ -239,8 +267,8 @@ fn rank(st: &State, h: usize) -> Result<Reply> {
}
let mut rows: HashMap<String, Value> = HashMap::new();
let mut st_ = c.prepare("SELECT key, target, prob, mean, lo, hi, target_week FROM forecasts WHERE made_at = ?1 AND model_version = ?2
AND ((target IN ('rotation', 'any') AND horizon = ?3) OR (target IN ('add', 'add_any', 'add_rel', 'add_any_rel', 'add_any_nmf', 'p_nmf', 'p_release', 'recent_release') AND horizon = ?4) OR target = 'now')")?;
let it = st_.query_map(params![made, VERSION, h.max(1) as i64, h as i64], |r| Ok((r.get::<_, String>(0)?, r.get::<_, String>(1)?, r.get::<_, Option<f64>>(2)?,
AND ((target IN ('rotation', 'any') AND horizon = ?3) OR (target IN ('add', 'add_any', 'add_rel', 'add_any_rel', 'add_any_nmf', 'p_nmf', 'p_release', 'too_soon') AND horizon = ?4) OR target = 'now')")?;
let it = st_.query_map(params![made, VERSION, h_db.max(1) as i64, h_db 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)?, r.get::<_, String>(6)?)))?;
let mut window = String::new();
for r in it {
@@ -258,15 +286,61 @@ fn rank(st: &State, h: usize) -> Result<Reply> {
let k = crate::model::panel::key(e["name"].as_str().unwrap_or(""));
if let Some(b) = bauer.get(&k) { e["bauer"] = json!(b.iter().map(|(s, v)| (s.clone(), v.0)).collect::<HashMap<_, _>>()); }
}
// The new-song numbers: live from the models for any release week and window, else the stored ones (8 weeks).
let mut window = window;
if let Some(m) = live.as_ref() {
window = (m.panel.weeks[m.panel.w() - 1] + chrono::Duration::weeks(h as i64 + 1)).to_string();
for a in 0..m.panel.a() {
let Some(e) = rows.get_mut(&m.panel.artists[a].id) else { continue };
match new_song(m, a, h, win, None) {
None => { e["too_soon"] = json!(true); e["add_any_rel"] = Value::Null; e["add_rel"] = json!({}); }
Some((pc, pa, pan, q)) => {
let r3 = |v: f64| (v * 1000.0).round() / 1000.0;
e["too_soon"] = json!(false);
e["add_any_rel"] = json!(r3(pa));
e["add_any_nmf"] = json!(r3(pan));
e["p_nmf"] = json!(r3(q));
e["add_rel"] = json!(CHANNELS.iter().zip(pc).map(|(c, v)| (c.to_string(), json!(r3(v)))).collect::<serde_json::Map<_, _>>());
}
}
}
}
let mut out: Vec<Value> = rows.into_values().collect();
out.sort_by(|a, b| b["add_any"].as_f64().unwrap_or(0.0).total_cmp(&a["add_any"].as_f64().unwrap_or(0.0)));
let body = Arc::new(json!({"made_at": made, "h": h, "window_start": window, "rows": out}).to_string());
out.sort_by(|a, b| b["add_any_rel"].as_f64().unwrap_or(-1.0).total_cmp(&a["add_any_rel"].as_f64().unwrap_or(-1.0)));
let body = Arc::new(json!({"made_at": made, "h": h, "w": win, "live": live.is_some(), "window_start": window, "rows": out}).to_string());
let mut cache = st.rank_cache.write().unwrap();
cache.retain(|k, _| k.0 == made);
cache.insert((made, h), body.clone());
cache.insert(key, body.clone());
Ok((200, "application/json", body))
}
/// The best week for an artist to release a new song: the next CALENDAR release weeks with the
/// chance DR adds it within `w` weeks (any station and per station group), like a fare calendar.
fn bestweek(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 win = q.get("w").and_then(|w| w.parse().ok()).filter(|w| WINDOWS.contains(w)).unwrap_or(8);
let nmf = match q.get("nmf").map(String::as_str) { Some("yes") => Some(true), Some("no") => Some(false), _ => None };
let p = &m.panel;
let t0 = p.w() - 1;
let groups = [("P3", vec![0usize]), ("P4", (0..C).filter(|&c| crate::model::panel::is_p4(c)).collect()), ("P5", vec![11]), ("P6", vec![12]), ("P8", vec![13])];
let weeks: Vec<Value> = (0..CALENDAR).map(|h| {
let start = p.weeks[t0] + chrono::Duration::weeks(h as i64 + 1);
let xmas = crate::model::features::christmas(p, t0 + h + 1);
match new_song(m, a, h, win, nmf) {
None => json!({"h": h, "week": start.to_string(), "too_soon": true, "christmas": xmas}),
Some((pc, pa, _, _)) => json!({"h": h, "week": start.to_string(), "too_soon": false, "christmas": xmas, "p": pa,
"groups": groups.iter().map(|(n, cs)| (n.to_string(), json!(cs.iter().map(|&c| pc[c]).fold(0.0, f64::max)))).collect::<serde_json::Map<_, _>>()}),
}
}).collect();
let last_release = p.releases[a].as_ref().and_then(|d| d.iter().rev().find(|d| **d <= p.weeks[t0] + chrono::Duration::days(6)).map(|d| d.to_string()));
ok(json!({"id": p.artists[a].id, "name": p.artists[a].name, "w": win, "nmf": q.get("nmf"), "p_nmf": crate::model::adds::p_nmf(p, a, t0),
"last_release": last_release, "weeks": weeks}))
}
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()?
@@ -304,7 +378,7 @@ fn artist(st: &State, id: &str) -> Result<Reply> {
let ch = key.split_once(':').map(|x| x.1).unwrap_or("");
if target == "now" { fold(&mut now, "now", ch, prob, mean, lo, hi); continue; }
let e = byh.entry(h).or_insert_with(|| json!({"h": h, "p": {}, "plays": {}, "add": {}}));
if target.starts_with("add") || target.starts_with("p_") || target == "recent_release" { e["window_start"] = json!(week); } else { e["week"] = json!(week); }
if target.starts_with("add") || target.starts_with("p_") || target == "too_soon" { e["window_start"] = json!(week); } else { e["week"] = json!(week); }
fold(e, &target, ch, prob, mean, lo, hi);
}
now_out = now["now"].clone();
@@ -369,7 +443,7 @@ fn whatif(st: &State, q: &HashMap<String, String>) -> Result<Reply> {
let run = |overrides: bool| -> Value {
let f: Option<&dyn Fn(&mut [f64], [usize; 4])> = if overrides { Some(&over) } else { None };
// A new release: the release-event model; no release: the window model (current songs only).
let (add_c, add_any) = if release { m.events.predict(p, &m.rot, a, t0, crate::model::adds::Scenario::new(true, nmf), f) }
let (add_c, add_any) = if release { m.events.predict_at(p, &m.rot, a, t0, h, crate::model::adds::Scenario::new(true, nmf).at(h), f) }
else { am.predict(p, &m.rot, a, t0, crate::model::adds::Scenario::new(false, false), f) };
let mut x = vec![0.0; P];
let mut mu = [0.0; C];
@@ -392,7 +466,7 @@ fn whatif(st: &State, q: &HashMap<String, String>) -> Result<Reply> {
};
let now: serde_json::Map<String, Value> = (0..C).filter(|&c| m.rot.on(a, c, t0)).map(|c| (CHANNELS[c].to_string(), json!(p.y(a, c, t0)))).collect();
ok(json!({
"h": h, "release": release, "nmf": nmf, "p_release": crate::model::adds::p_release(p, a, t0), "p_nmf": crate::model::adds::p_nmf(p, a, t0), "recent_release": crate::model::adds::recent_release(p, a, t0),
"h": h, "release": release, "nmf": nmf, "p_release": crate::model::adds::p_release(p, a, t0), "p_nmf": crate::model::adds::p_nmf(p, a, t0), "too_soon": crate::model::adds::too_soon(p, a, t0, h),
"nmf_history": p.nmf_dk[a].iter().rev().take(8).map(|(d, pos)| json!({"friday": d.to_string(), "position": pos})).collect::<Vec<_>>(),
"window_start": (p.weeks[t0] + chrono::Duration::weeks(h as i64 + 1)).to_string(),
"current": {"t40": chart.t40_rank, "air": chart.air_rank,