|
|
|
@@ -2,6 +2,7 @@ package tv.orbit.classic
|
|
|
|
|
|
|
|
|
|
import androidx.compose.foundation.background
|
|
|
|
|
import androidx.compose.foundation.border
|
|
|
|
|
import androidx.compose.foundation.clickable
|
|
|
|
|
import androidx.compose.foundation.focusable
|
|
|
|
|
import androidx.compose.foundation.layout.*
|
|
|
|
|
import androidx.compose.foundation.shape.RoundedCornerShape
|
|
|
|
@@ -37,12 +38,12 @@ internal class DeskPick(val title: String, val channel: String, val tag: String,
|
|
|
|
|
internal class DeskAsk(val title: String, val first: String, val second: String)
|
|
|
|
|
|
|
|
|
|
internal class DeskReport(val newlyReleased: Int, val newToLibrary: Int, val scanned: Int, val unread: Int, val strongByChannel: List<Pair<String, Int>>, val strong: Int,
|
|
|
|
|
val picks: List<DeskPick>, val asks: List<DeskAsk>, val collections: Int, val catalogueComplete: Boolean, val computedMs: Long, val tookMs: Long) {
|
|
|
|
|
val picks: List<DeskPick>, val asks: List<DeskAsk>, val collections: Int, val catalogueComplete: Boolean, val franchises: Int, val computedMs: Long, val tookMs: Long) {
|
|
|
|
|
fun toJson(): JSONObject = JSONObject().put("newlyReleased", newlyReleased).put("newToLibrary", newToLibrary).put("scanned", scanned).put("unread", unread)
|
|
|
|
|
.put("strong", strong).put("strongByChannel", JSONArray(strongByChannel.map { JSONObject().put("c", it.first).put("n", it.second) }))
|
|
|
|
|
.put("picks", JSONArray(picks.map { JSONObject().put("t", it.title).put("c", it.channel).put("g", it.tag).put("l", it.line) }))
|
|
|
|
|
.put("asks", JSONArray(asks.map { JSONObject().put("t", it.title).put("a", it.first).put("b", it.second) }))
|
|
|
|
|
.put("collections", collections).put("catalogueComplete", catalogueComplete).put("computedMs", computedMs).put("tookMs", tookMs)
|
|
|
|
|
.put("collections", collections).put("franchises", franchises).put("catalogueComplete", catalogueComplete).put("computedMs", computedMs).put("tookMs", tookMs)
|
|
|
|
|
companion object {
|
|
|
|
|
fun of(o: JSONObject): DeskReport {
|
|
|
|
|
fun list(key: String) = o.optJSONArray(key)?.let { a -> (0 until a.length()).map { a.getJSONObject(it) } }.orEmpty()
|
|
|
|
@@ -50,7 +51,7 @@ internal class DeskReport(val newlyReleased: Int, val newToLibrary: Int, val sca
|
|
|
|
|
list("strongByChannel").map { it.optString("c") to it.optInt("n") }, o.optInt("strong"),
|
|
|
|
|
list("picks").map { DeskPick(it.optString("t"), it.optString("c"), it.optString("g"), it.optString("l")) },
|
|
|
|
|
list("asks").map { DeskAsk(it.optString("t"), it.optString("a"), it.optString("b")) },
|
|
|
|
|
o.optInt("collections"), o.optBoolean("catalogueComplete"), o.optLong("computedMs"), o.optLong("tookMs"))
|
|
|
|
|
o.optInt("collections"), o.optBoolean("catalogueComplete"), o.optInt("franchises"), o.optLong("computedMs"), o.optLong("tookMs"))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@@ -58,21 +59,29 @@ internal class DeskReport(val newlyReleased: Int, val newToLibrary: Int, val sca
|
|
|
|
|
/** Paul's standing editorial calls so far, shown so the director's reasoning is visible. Decisions on the Chair itself come later. */
|
|
|
|
|
internal val DirectorRules = listOf("Made-for-TV romances: not for Orbit", "Tales from the Crypt: After Dark strand", "Black Mirror: Paradox from 21:00")
|
|
|
|
|
|
|
|
|
|
/** Emby collections as franchise links: each member's collection, and the channels its fellow members are on. */
|
|
|
|
|
internal fun franchiseLinks(file: File, pools: Map<Int, List<String>>): Pair<FranchiseLinks, Int> {
|
|
|
|
|
if (!file.exists()) return FranchiseLinks.Unknown to 0
|
|
|
|
|
/**
|
|
|
|
|
* Emby collections as evidence: franchise links only from TMDB-style "... Collection" sets (each member's collection, and the
|
|
|
|
|
* channels its fellow members are on), and British origin from the broadcasters' lists. Curated lists link nothing.
|
|
|
|
|
* Returns the links, how many collections were read, and how many of them are franchises.
|
|
|
|
|
*/
|
|
|
|
|
internal fun franchiseLinks(file: File, pools: Map<Int, List<String>>): Triple<FranchiseLinks, Int, Int> {
|
|
|
|
|
if (!file.exists()) return Triple(FranchiseLinks.Unknown, 0, 0)
|
|
|
|
|
val onChannel = HashMap<String, MutableSet<Int>>().apply { pools.forEach { (ch, ids) -> ids.forEach { getOrPut(it) { HashSet() } += ch } } }
|
|
|
|
|
val collectionOf = HashMap<String, String>(); val channelsOf = HashMap<String, Set<Int>>()
|
|
|
|
|
var count = 0
|
|
|
|
|
val collectionOf = HashMap<String, String>(); val channelsOf = HashMap<String, Set<Int>>(); val british = HashMap<String, String>()
|
|
|
|
|
var count = 0; var franchises = 0
|
|
|
|
|
file.forEachLine { line ->
|
|
|
|
|
val o = runCatching { JSONObject(line) }.getOrNull() ?: return@forEachLine
|
|
|
|
|
count++
|
|
|
|
|
val members = o.optJSONArray("members")?.let { a -> (0 until a.length()).map { a.getString(it) } }.orEmpty()
|
|
|
|
|
val name = o.optString("name")
|
|
|
|
|
channelsOf[name] = members.flatMap { onChannel[it].orEmpty() }.toSet()
|
|
|
|
|
members.forEach { collectionOf[it] = name }
|
|
|
|
|
if (FranchiseCollection.containsMatchIn(name)) {
|
|
|
|
|
franchises++
|
|
|
|
|
channelsOf[name] = members.flatMap { onChannel[it].orEmpty() }.toSet()
|
|
|
|
|
members.forEach { collectionOf[it] = name }
|
|
|
|
|
}
|
|
|
|
|
if (BritishLists.containsMatchIn(name)) members.forEach { british.putIfAbsent(it, name) }
|
|
|
|
|
}
|
|
|
|
|
return FranchiseLinks(channelsOf, collectionOf) to count
|
|
|
|
|
return Triple(FranchiseLinks(channelsOf, collectionOf, british), count, franchises)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@@ -80,12 +89,17 @@ internal fun franchiseLinks(file: File, pools: Map<Int, List<String>>): Pair<Fra
|
|
|
|
|
* Titles already on a channel are left out (by work, whichever copy). [mode] sets how many discoveries and how far they reach.
|
|
|
|
|
*/
|
|
|
|
|
internal fun buildDeskReport(evidenceFile: File, collectionsFile: File, pools: Map<Int, List<String>>, released: List<Arrival>, carriedWorks: Set<String>,
|
|
|
|
|
originals: Map<String, String>, newToLibrary: Int, catalogueComplete: Boolean, mode: DirectorMode, nowMs: Long, zone: ZoneId = ZoneId.systemDefault()): DeskReport {
|
|
|
|
|
originals: Map<String, String>, newToLibrary: Int, catalogueComplete: Boolean, mode: DirectorMode, nowMs: Long, zone: ZoneId = ZoneId.systemDefault()): DeskReport =
|
|
|
|
|
buildDeskReports(evidenceFile, collectionsFile, pools, released, carriedWorks, originals, newToLibrary, catalogueComplete, nowMs, zone).getValue(mode)
|
|
|
|
|
|
|
|
|
|
/** All three modes' reports from one scoring pass: the modes only differ in how many discoveries they show and how far they reach. */
|
|
|
|
|
internal fun buildDeskReports(evidenceFile: File, collectionsFile: File, pools: Map<Int, List<String>>, released: List<Arrival>, carriedWorks: Set<String>,
|
|
|
|
|
originals: Map<String, String>, newToLibrary: Int, catalogueComplete: Boolean, nowMs: Long, zone: ZoneId = ZoneId.systemDefault()): Map<DirectorMode, DeskReport> {
|
|
|
|
|
val started = System.nanoTime()
|
|
|
|
|
val evidence = if (evidenceFile.exists()) evidenceFile.readLines().mapNotNull { runCatching { JSONObject(it) }.getOrNull() }
|
|
|
|
|
.associate { it.optString("id") to TitleEvidence.of(it, originals[it.optString("id")]) } else emptyMap()
|
|
|
|
|
val text = ChannelText(pools, evidence)
|
|
|
|
|
val (links, collections) = franchiseLinks(collectionsFile, pools)
|
|
|
|
|
val (links, collections, franchises) = franchiseLinks(collectionsFile, pools)
|
|
|
|
|
val carriedIds = pools.values.flatten().toSet()
|
|
|
|
|
val candidates = released.filter { it.id !in carriedIds && it.work !in carriedWorks }.distinctBy { it.work }
|
|
|
|
|
val read = candidates.mapNotNull { evidence[it.id] }
|
|
|
|
@@ -95,16 +109,45 @@ internal fun buildDeskReport(evidenceFile: File, collectionsFile: File, pools: M
|
|
|
|
|
val today = Instant.ofEpochMilli(nowMs).atZone(zone).toLocalDate()
|
|
|
|
|
val premiered = released.associate { it.id to it.premiere }
|
|
|
|
|
fun recent(id: String) = premiered[id]?.takeIf { it.length == 10 }?.let { runCatching { LocalDate.parse(it) }.getOrNull() }?.let { !it.isBefore(today.minusDays(14)) } == true
|
|
|
|
|
// Discoveries: the strongest strong matches, one per channel; Surprise Me reaches into the possibles for channels the strong list missed.
|
|
|
|
|
val pool = strong.sortedByDescending { it.s1 }.distinctBy { it.top.number }.toMutableList()
|
|
|
|
|
if (mode == DirectorMode.Surprise) pool += placed.filter { it.tier == FitTier.Possible && pool.none { p -> p.top.number == it.top.number } }.sortedByDescending { it.s1 }.take(2)
|
|
|
|
|
val picks = pool.take(mode.picks).map { p ->
|
|
|
|
|
DeskPick(p.evidence.name, p.top.name, when { p.tier == FitTier.Possible -> "SOMETHING DIFFERENT"; recent(p.evidence.id) -> "JUST RELEASED"; else -> "STRONG MATCH" },
|
|
|
|
|
p.evidence.overview?.let { sentence(it) } ?: "No description in the library yet")
|
|
|
|
|
}
|
|
|
|
|
val asks = placed.filter { it.tier == FitTier.NeedsEye }.sortedByDescending { it.s1 }.take(3).map { DeskAsk(it.evidence.name, it.top.name, it.second.name) }
|
|
|
|
|
return DeskReport(candidates.size, newToLibrary, read.size, candidates.size - read.size, byChannel, strong.size, picks, asks, collections, catalogueComplete, nowMs,
|
|
|
|
|
(System.nanoTime() - started) / 1_000_000)
|
|
|
|
|
val tookMs = (System.nanoTime() - started) / 1_000_000
|
|
|
|
|
return DirectorMode.values().associateWith { mode ->
|
|
|
|
|
// Discoveries: the strongest strong matches, one per channel; Surprise Me reaches into the possibles for channels the strong list missed.
|
|
|
|
|
val pool = strong.sortedByDescending { it.s1 }.distinctBy { it.top.number }.toMutableList()
|
|
|
|
|
if (mode == DirectorMode.Surprise) pool += placed.filter { it.tier == FitTier.Possible && pool.none { p -> p.top.number == it.top.number } }.sortedByDescending { it.s1 }.take(2)
|
|
|
|
|
val picks = pool.take(mode.picks).map { p ->
|
|
|
|
|
DeskPick(p.evidence.name, p.top.name, when { p.tier == FitTier.Possible -> "SOMETHING DIFFERENT"; recent(p.evidence.id) -> "JUST RELEASED"; else -> "STRONG MATCH" },
|
|
|
|
|
p.evidence.overview?.let { sentence(it) } ?: "No description in the library yet")
|
|
|
|
|
}
|
|
|
|
|
DeskReport(candidates.size, newToLibrary, read.size, candidates.size - read.size, byChannel, strong.size, picks, asks, collections, catalogueComplete, franchises, nowMs, tookMs)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private val DirectorLock = Any()
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The Chair's reports for all three modes, from the saved copy when nothing it reads has changed (the evidence, the
|
|
|
|
|
* collections, the channels' pools and the catalogue) and it's under a day old; otherwise worked out once and saved. One at
|
|
|
|
|
* a time: a second caller waits for the first and then reads its copy. Read only.
|
|
|
|
|
*/
|
|
|
|
|
internal fun directorReports(store: ScheduleStore, catalogue: CatalogueStore, userId: String, nowMs: Long = System.currentTimeMillis()): Map<DirectorMode, DeskReport>? = synchronized(DirectorLock) {
|
|
|
|
|
val evidence = store.evidenceFile(); val collections = store.collectionsFile(); val cache = store.directorFile()
|
|
|
|
|
val key = listOf(evidence.lastModified(), collections.lastModified(), store.poolsStamp(), catalogue.summary().total ?: 0L).joinToString(":")
|
|
|
|
|
runCatching { JSONObject(cache.readText()) }.getOrNull()?.takeIf { it.optString("key") == key && nowMs - it.optLong("savedMs") < 24 * 60 * 60_000L }?.let { saved ->
|
|
|
|
|
val reports = saved.optJSONObject("reports")
|
|
|
|
|
if (reports != null) return@synchronized DirectorMode.values().associateWith { DeskReport.of(reports.getJSONObject(it.name)) }
|
|
|
|
|
}
|
|
|
|
|
val today = Instant.ofEpochMilli(nowMs).atZone(ZoneId.systemDefault()).toLocalDate()
|
|
|
|
|
val released = catalogue.releasedBetween(today.minusDays(NEWLY_RELEASED_DAYS).toString(), today.toString(), 2_000)
|
|
|
|
|
val ids = SmartChannels.associate { p -> p.number to store.loadPool(userId, p.number)?.series.orEmpty().map { it.id } }.filterValues { it.isNotEmpty() }
|
|
|
|
|
val carriedWorks = catalogue.worksOf(ids.values.flatten()).values.toSet()
|
|
|
|
|
val (arrivals, _) = store.loadArrivals(userId)
|
|
|
|
|
val newToLibrary = arrivals.count { nowMs - it.firstSeenMs < NEW_TO_LIBRARY_DAYS * 24 * 60 * 60_000L }
|
|
|
|
|
val reports = buildDeskReports(evidence, collections, ids, released, carriedWorks, catalogue.originalTitles(released.map { it.id }), newToLibrary, catalogue.complete(), nowMs)
|
|
|
|
|
runCatching { cache.writeText(JSONObject().put("key", key).put("savedMs", nowMs).put("reports", JSONObject().apply { reports.forEach { (m, r) -> put(m.name, r.toJson()) } }).toString()) }
|
|
|
|
|
val b = reports.getValue(DirectorMode.Balanced)
|
|
|
|
|
android.util.Log.i("OrbitSchedule", "director newlyReleased=${b.newlyReleased} read=${b.scanned} strong=${b.strong} asks=${b.asks.size} collections=${b.collections} tookMs=${b.tookMs}")
|
|
|
|
|
reports
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** The first sentence of a description, kept short for a card. */
|
|
|
|
@@ -127,7 +170,8 @@ internal fun DirectorChair(load: (DirectorMode) -> DeskReport?, first: FocusRequ
|
|
|
|
|
Spacer(Modifier.width(10.dp))
|
|
|
|
|
DirectorMode.values().forEachIndexed { i, m ->
|
|
|
|
|
var focused by remember { mutableStateOf(false) }
|
|
|
|
|
Text(m.label, Modifier.then(if (i == 1) Modifier.focusRequester(first) else Modifier).onFocusChanged { focused = it.isFocused; if (it.isFocused) mode = m }.focusable()
|
|
|
|
|
Text(m.label, Modifier.then(if (i == 1) Modifier.focusRequester(first) else Modifier).onFocusChanged { focused = it.isFocused }
|
|
|
|
|
.clickable { mode = m }
|
|
|
|
|
.background(if (m == mode) ChairGold else ChairPanel, RoundedCornerShape(12.dp)).border(if (focused) 2.dp else 0.dp, Color.White, RoundedCornerShape(12.dp))
|
|
|
|
|
.padding(horizontal = 10.dp, vertical = 3.dp), color = if (m == mode) ChairNavy else Color.White, fontSize = 11.sp, fontWeight = FontWeight.SemiBold)
|
|
|
|
|
}
|
|
|
|
@@ -135,7 +179,7 @@ internal fun DirectorChair(load: (DirectorMode) -> DeskReport?, first: FocusRequ
|
|
|
|
|
Text("Auto-approval: OFF", color = ChairMint, fontSize = 11.sp, fontWeight = FontWeight.SemiBold)
|
|
|
|
|
}
|
|
|
|
|
val r = report
|
|
|
|
|
if (r == null) { Text("Reading the library's evidence…", color = Muted, fontSize = 12.sp); return@Column }
|
|
|
|
|
if (r == null) { Text("Working out this week's report. The first time takes a little while; after that the Chair opens straight away.", color = Muted, fontSize = 12.sp); return@Column }
|
|
|
|
|
Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.spacedBy(10.dp)) {
|
|
|
|
|
Column(Modifier.weight(.9f).background(ChairPanel, RoundedCornerShape(8.dp)).padding(10.dp), verticalArrangement = Arrangement.spacedBy(3.dp)) {
|
|
|
|
|
Text("THIS WEEK", color = Color.White, fontFamily = Anton, fontSize = 14.sp)
|
|
|
|
@@ -148,8 +192,9 @@ internal fun DirectorChair(load: (DirectorMode) -> DeskReport?, first: FocusRequ
|
|
|
|
|
Text("Held as proposals. Nothing is booked.", color = Muted, fontSize = 10.sp)
|
|
|
|
|
// Scan status, always visible: what hasn't been read yet is said, not hidden.
|
|
|
|
|
val gaps = listOfNotNull("${r.unread} not read yet".takeIf { r.unread > 0 }, "no collections read, so franchises unknown".takeIf { r.collections == 0 },
|
|
|
|
|
"franchises unknown: none of the library's ${r.collections} collections is a franchise".takeIf { r.collections > 0 && r.franchises == 0 },
|
|
|
|
|
"library list still importing".takeIf { !r.catalogueComplete })
|
|
|
|
|
Text(if (gaps.isEmpty()) "Scan complete: ${r.scanned} read, ${r.collections} collections" else "Scan: " + gaps.joinToString("; "), color = if (gaps.isEmpty()) Muted else ChairGold, fontSize = 10.sp)
|
|
|
|
|
Text(if (gaps.isEmpty()) "Scan complete: ${r.scanned} read, ${r.franchises} franchise collections" else "Scan: " + gaps.joinToString("; "), color = if (gaps.isEmpty()) Muted else ChairGold, fontSize = 10.sp)
|
|
|
|
|
}
|
|
|
|
|
Column(Modifier.weight(1.3f), verticalArrangement = Arrangement.spacedBy(5.dp)) {
|
|
|
|
|
Text("WORTH A LOOK", color = Color.White, fontFamily = Anton, fontSize = 14.sp)
|
|
|
|
|