Coordinated cutover plan and simulation on box copies (nothing applied)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
package tv.orbit.classic
|
||||
|
||||
import org.json.JSONObject
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assume.assumeTrue
|
||||
import org.junit.Test
|
||||
import java.io.File
|
||||
|
||||
/**
|
||||
* The coordinated cutover, simulated on copies of the box's files (4 November 2026). Nothing touches the box:
|
||||
* ORBIT_REPLAY_DIR=<copies> ORBIT_CUTOVER_AT=<epoch ms, optional> ORBIT_CUTOVER_OUT=<empty folder> ./gradlew :app:testDebugUnitTest --tests '*CutoverSimulationTest*'
|
||||
*
|
||||
* At the cutover moment T, for every scheduled channel:
|
||||
* 1. Keep everything that has started by T, including the programme on air, so nothing playing is interrupted.
|
||||
* 2. Drop the bookings that haven't started. They never aired, so they must not count: the channel's place is rebuilt
|
||||
* from what has actually gone out (its saved aired cursor, brought up to T with the kept programmes), never from the
|
||||
* old plan's tail, which had already moved past the unaired bookings.
|
||||
* 3. Rebook seven days from the end of the programme on air, with today's rules, every channel in turn avoiding the
|
||||
* others (all cut back first, so nothing avoids a booking that is about to disappear).
|
||||
* The Drive-In, its tickets, suggestion choices, failed files and pools are left exactly as they are.
|
||||
* Writes the new plan and progress files to ORBIT_CUTOVER_OUT and cutover-simulation.md to the replay folder.
|
||||
*/
|
||||
class CutoverSimulationTest {
|
||||
private val dir = System.getenv("ORBIT_REPLAY_DIR")?.let(::File)
|
||||
private val zone = java.time.ZoneId.of("Europe/London")
|
||||
private val day = 24 * 60 * 60_000L
|
||||
|
||||
@Test fun cutover() {
|
||||
assumeTrue(dir != null && dir.isDirectory)
|
||||
val source = ScheduleStore(dir!!)
|
||||
val user = JSONObject(File(dir, "schedule-101.json").readText()).getString("user")
|
||||
val channels = SmartChannels.filter { source.loadPlan(user, it.number) != null && source.loadPool(user, it.number) != null }.sortedBy { it.number }
|
||||
val at = System.getenv("ORBIT_CUTOVER_AT")?.toLong() ?: channels.mapNotNull { source.loadPlan(user, it.number)?.entries?.firstOrNull()?.startMs }.max() + SCHEDULE_KEEP_PAST_MS + 60 * 60_000L
|
||||
val pools = channels.associate { it.number to withoutFailed(source.loadPool(user, it.number)!!.series, source.loadFailed(user)) }
|
||||
val old = channels.associate { it.number to source.loadPlan(user, it.number)!! }
|
||||
val progress = channels.associate { it.number to source.loadProgress(user, it.number) }
|
||||
val driveIn = source.loadPlan(user, DRIVE_IN_NUMBER)
|
||||
|
||||
// 1 and 2: cut every plan back to what has started by T, and rebuild its place from what has gone out.
|
||||
val kept = HashMap<Int, ChannelPlan>()
|
||||
val corrected = HashMap<Int, Int>()
|
||||
for (p in channels) {
|
||||
val plan = old.getValue(p.number); val pool = pools.getValue(p.number)
|
||||
val keep = plan.entries.filter { it.startMs <= at }
|
||||
val base = progress[p.number]?.aired ?: ScheduleCursor()
|
||||
val aired = airedCursor(base, keep, at)
|
||||
// The last show in each block, and when each series last started its run, from the kept programmes only.
|
||||
val blockLast = aired.blockLast.toMutableMap()
|
||||
val runStarted = HashMap<String, Long>()
|
||||
keep.forEach { e ->
|
||||
blockLast[dayPartFor(pool, p.dayParts, e.startMs, zone).second] = e.seriesId
|
||||
pool.firstOrNull { it.id == e.seriesId }?.let { s ->
|
||||
val running = if (p.shuffleEpisodes || p.mixes(s)) shuffledEpisodes(s) else s.episodes
|
||||
if (running.firstOrNull()?.mark == e.mark) runStarted[s.id] = e.startMs
|
||||
}
|
||||
}
|
||||
// Tallies start level: they only matter relative to each other, and the old ones counted bookings that never aired.
|
||||
val cursor = aired.copy(blockLast = blockLast, tallies = emptyMap(), runStarted = runStarted)
|
||||
corrected[p.number] = plan.tail.marks.count { (id, mark) -> cursor.marks[id] != mark }
|
||||
kept[p.number] = ChannelPlan(keep, cursor)
|
||||
}
|
||||
// 3: rebook every channel in turn, each avoiding the others as they now stand.
|
||||
val rebooked = HashMap<Int, ChannelPlan>(kept)
|
||||
for (p in channels) {
|
||||
val others = clashesWith(rebooked.filterKeys { it != p.number }.values.toList() + listOfNotNull(driveIn))
|
||||
val k = kept.getValue(p.number)
|
||||
rebooked[p.number] = topUpPlan(k, k.tail, pools.getValue(p.number), at, p.filmRestMs, p.finishTogether, p.dayParts, zone, others,
|
||||
shuffle = p.shuffleEpisodes, mixed = p::mixes, weightOf = p.tierWeights?.let { p::weightOf }, adaptiveRest = p.adaptiveFilmRest) ?: k
|
||||
}
|
||||
|
||||
val md = StringBuilder("# Coordinated cutover: simulation on box copies\n\nCutover moment T = ${java.time.Instant.ofEpochMilli(at).atZone(zone).toLocalDateTime().withNano(0)} (London).\n\n")
|
||||
md.appendLine("| Ch | Channel | Kept (started by T) | On air at T kept | Unaired bookings dropped | New bookings | New plan ends | Seam | Places corrected |")
|
||||
md.appendLine("|---|---|---|---|---|---|---|---|---|")
|
||||
var problems = 0
|
||||
val outDir = System.getenv("ORBIT_CUTOVER_OUT")?.let(::File)?.also { it.mkdirs() }
|
||||
val out = outDir?.let(::ScheduleStore)
|
||||
for (p in channels) {
|
||||
val before = old.getValue(p.number); val k = kept.getValue(p.number); val after = rebooked.getValue(p.number)
|
||||
val onAir = before.entries.firstOrNull { it.startMs <= at && at < it.endMs }
|
||||
val keptInAfter = after.entries.take(after.entries.indexOfFirst { it.startMs > at }.let { if (it < 0) after.entries.size else it })
|
||||
val keptOk = k.entries.dropWhile { it.endMs < at - SCHEDULE_KEEP_PAST_MS } == keptInAfter
|
||||
val new = after.entries.filter { it.startMs > at }
|
||||
val seamOk = new.isEmpty() || k.entries.isEmpty() || new.first().startMs == k.entries.last().endMs
|
||||
val gaps = after.entries.zipWithNext().count { (a, b) -> a.endMs != b.startMs }
|
||||
if (!keptOk || !seamOk || gaps > 0) problems++
|
||||
md.appendLine("| ${p.number} | ${p.name} | ${k.entries.size} | ${if (onAir != null) "yes" else "none on air"} | ${before.entries.count { it.startMs > at }} | ${new.size} | " +
|
||||
"${java.time.Instant.ofEpochMilli(after.endMs).atZone(zone).toLocalDate()} | ${if (seamOk && gaps == 0) "no gap" else "GAP $gaps"} | ${corrected[p.number]} |")
|
||||
out?.savePlan(user, p.number, after)
|
||||
out?.saveProgress(user, p.number, ScheduleStore.SavedProgress(progress[p.number]?.enabled ?: true, airedCursor(progress[p.number]?.aired ?: ScheduleCursor(), k.entries, at)))
|
||||
}
|
||||
// The rebooked network, checked together.
|
||||
val newOnly = rebooked.mapValues { (_, plan) -> plan.entries.filter { it.startMs > at } }
|
||||
val byTitle = newOnly.flatMap { (n, es) -> es.map { n to it } }.groupBy { seriesKey(it.second.media.item.title.substringBefore(" / ")) }
|
||||
val clashes = byTitle.values.sumOf { list -> list.indices.sumOf { i -> list.drop(i + 1).count { b -> b.first != list[i].first && list[i].second.startMs < b.second.endMs && b.second.startMs < list[i].second.endMs } } }
|
||||
val keyed = newOnly.values.sumOf { es -> es.count { it.presentation.isNotEmpty() } }
|
||||
md.appendLine().appendLine("- New bookings clashing with another channel: $clashes.")
|
||||
md.appendLine("- New bookings carrying a presentation key (special idents): $keyed.")
|
||||
md.appendLine("- \"Places corrected\" counts shows whose old plan tail had moved past episodes that were only booked, not aired; after the cutover their place is what actually aired.")
|
||||
md.appendLine("- Drive-In plan, tickets, choices, failed files and pools: not touched.")
|
||||
File(dir, "cutover-simulation.md").writeText(md.toString())
|
||||
println(md)
|
||||
assertEquals(0, problems); assertEquals(0, clashes)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user