Files
Airplay-Predictor/src/web.rs
T

484 lines
29 KiB
Rust

//! 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::adds::{set_chart, AddModel, Rot, CHART_IDX_CHANNEL};
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;
/// 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,
panel: Panel,
tau: [u32; C],
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>,
}
struct State {
db_path: String,
prefix: String,
models: RwLock<Option<Arc<Models>>>,
rank_cache: RwLock<HashMap<(String, usize, usize, bool), 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, false)).collect();
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_site(&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, events_w, rot, 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(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()))),
}
}
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();
// The latest score of each model, horizon and metric (the airplay and add backtests run separately).
let scores: Vec<Value> = match &run {
Some(_) => c.prepare("SELECT model, horizon, metric, value FROM backtest_scores b WHERE run_at =
(SELECT max(run_at) FROM backtest_scores WHERE model = b.model AND horizon = b.horizon AND metric = b.metric)")?
.query_map([], |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}))
}
/// Puts one forecast row into an artist's JSON: airplay ('rotation', 'any'), new adds ('add',
/// 'add_any') and the origin week ('now').
fn fold(e: &mut Value, target: &str, ch: &str, prob: Option<f64>, mean: Option<f64>, lo: Option<f64>, hi: Option<f64>) {
let r3 = |v: Option<f64>| json!(v.map(|p| (p * 1000.0).round() / 1000.0));
let r1 = |v: Option<f64>| json!(v.map(|m| (m * 10.0).round() / 10.0));
match target {
"any" => { e["p_any"] = r3(prob); e["mean"] = r1(mean); e["lo"] = json!(lo); e["hi"] = json!(hi); }
"rotation" => { e["p"][ch] = r3(prob); e["plays"][ch] = r1(mean); }
"add_any" => { e["add_any"] = r3(prob); }
"add" => { e["add"][ch] = r3(prob); }
"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); }
"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}); }
_ => {}
}
}
/// Bauer plays (NOVA, Radio Soft, The Voice, Pop FM, Radio 100) in the 7 days up to the latest
/// one, per artist name key (each credited artist) and station: (plays, songs).
fn bauer_week(c: &Connection) -> Result<HashMap<String, HashMap<String, (i64, Vec<(String, i64)>)>>> {
let mut out: HashMap<String, HashMap<String, (i64, Vec<(String, i64)>)>> = HashMap::new();
let mut st = c.prepare(
"SELECT station, artist, title, count(*) FROM plays WHERE source = 'bauer'
AND played_at > (SELECT strftime('%Y-%m-%dT%H:%M:%SZ', max(played_at), '-7 days') FROM plays WHERE source = 'bauer')
GROUP BY station, artist, title")?;
let rows = st.query_map([], |r| Ok((r.get::<_, String>(0)?, r.get::<_, String>(1)?, r.get::<_, String>(2)?, r.get::<_, i64>(3)?)))?;
for r in rows {
let (station, artist, title, n) = r?;
let mut parts = vec![artist.to_lowercase()];
for sep in [" feat. ", " feat ", " ft. ", " ft ", " featuring ", " & ", ", ", " x ", " og ", " vs "] {
parts = parts.iter().flat_map(|p| p.split(sep).map(str::to_string).collect::<Vec<_>>()).collect();
}
let mut keys: Vec<String> = parts.iter().map(|p| crate::model::panel::key(p)).collect();
keys.push(crate::model::panel::key(&artist));
keys.sort(); keys.dedup();
for k in keys.into_iter().filter(|k| !k.is_empty()) {
let e = out.entry(k).or_default().entry(station.clone()).or_insert((0, Vec::new()));
e.0 += n;
e.1.push((title.clone(), n));
}
}
Ok(out)
}
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": []})) };
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)?)))? {
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, 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', '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 {
let (key, target, prob, mean, lo, hi, week) = 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": {}, "add": {}, "now": {}})
});
if target == "add_any" && window.is_empty() { window = week; }
fold(e, &target, ch, prob, mean, lo, hi);
}
let bauer = bauer_week(&c)?;
for e in rows.values_mut() {
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_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(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;
// Release weeks between `from` and `to` (default: the next 26), at most 53 weeks, from next week on.
let first = p.weeks[t0] + chrono::Duration::weeks(1);
let date = |k: &str| q.get(k).and_then(|s| chrono::NaiveDate::parse_from_str(s, "%Y-%m-%d").ok());
let wk = |d: chrono::NaiveDate| ((d - first).num_days().div_euclid(7)).max(0) as usize;
let h0 = date("from").map(wk).unwrap_or(0);
let h1 = date("to").map(wk).unwrap_or(h0 + CALENDAR - 1).max(h0).min(h0 + 52);
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> = (h0..=h1).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()?
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();
let mut now_out = Value::Null;
if let Some(made) = latest_run(&c)? {
let mut byh: HashMap<i64, Value> = HashMap::new();
let mut now = json!({});
for r in c.prepare("SELECT horizon, target_week, key, prob, mean, lo, hi, target 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)?, r.get::<_, String>(7)?)))? {
let (h, week, key, prob, mean, lo, hi, target) = r?;
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 == "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();
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}));
}
}
let bauer: Value = bauer_week(&c)?.remove(&key).map(|b| json!(b.into_iter().map(|(s, (n, mut songs))| {
songs.sort_by(|x, y| y.1.cmp(&x.1));
(s, json!({"plays": n, "songs": songs.into_iter().take(5).map(|(t, n)| json!({"title": t, "plays": n})).collect::<Vec<_>>()}))
}).collect::<HashMap<_, _>>())).unwrap_or(Value::Null);
ok(json!({
"id": id, "name": name, "bauer": bauer, "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, "now": now_out, "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)
/// h = 0..12: the add window starts h weeks after the latest week; plays are for week max(h, 1).
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(0).min(MAX_H);
let d = &m.direct[h.max(1) - 1];
let am = &m.adds[h];
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, air) = (num("t40"), num("air"));
// The scenario: a release in the window (default) or none.
let release = q.get("release").map(|v| v != "no").unwrap_or(true);
// ...and whether that release is placed on New Music Friday Denmark (default: no).
let nmf = q.get("nmf").is_some_and(|v| v == "yes");
let over = move |x: &mut [f64], idx: [usize; 4]| set_chart(x, idx, entry, t40, air);
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_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];
for c in 0..C {
features::row(p, a, c, t0, t0 + h.max(1), &mut x);
if overrides { over(&mut x, CHART_IDX_CHANNEL); }
mu[c] = glm::dot(&d.nb, &x).clamp(-30.0, 12.0).exp();
}
let sims = 4000;
let shape = 1.0 / d.alpha;
let mut rng = Rng(0xa1 ^ a as u64);
let mut tot = Vec::with_capacity(sims);
for _ in 0..sims {
let g = rng.gamma(shape) / shape;
tot.push((0..C).map(|c| rng.poisson(mu[c] * g)).sum::<f64>());
}
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!({"add_any": add_any, "add": ch(&add_c), "mean": mu.iter().sum::<f64>(), "lo": tot[sims / 10], "hi": tot[sims * 9 / 10], "plays": ch(&mu)})
};
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), "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,
"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, "in_rotation": now},
"base": run(false), "scenario": run(true),
}))
}