Implement WeatherNews.fix_conflict(db, schedule)

- Compare a schedule against what's in the database.
- If schedule changes have been made,
  + record the cancellation
  + update the schedule to reflect the new reality
This commit is contained in:
gg 2023-08-30 18:58:58 -07:00
parent a20200d575
commit 8c4a437687
2 changed files with 70 additions and 17 deletions

View File

@ -26,7 +26,11 @@ TITLES = Dict(
function massage_fn(zone, leading_zero)
td = Date(now(tz"Asia/Tokyo"))
zero_adjustment = if leading_zero 0 else 1 end
zero_adjustment = if leading_zero
0
else
1
end
ymd = yearmonthday(td)
zn = ZonedDateTime(now(), localzone())
return function massage(item)
@ -86,4 +90,34 @@ function update_schedule_with_live_video(db)
)
end
"""
TODO - Given a schedule, check the database for conflicting entries and fix them.
This usually happens when a caster has to be rescheduled due to unforseen circumstances.
"""
function fix_conflict(db, schedule)
casters = DB.find_all_casters(db)
cid = eachrow(casters) |> rows -> map(x -> (x[:n] => x[:id]), rows) |> Dict
cname = eachrow(casters) |> rows -> map(x -> (x[:id] => x[:n]), rows) |> Dict
# iterate through schedule
for s in schedule
# find a db schedule row with the same jst
dbs = DB.find_schedule_by_jst(db, string(s[:t]))
if ismissing(dbs)
continue
end
# check if the caster matches
if cid[s[:caster]] != dbs[:caster_id]
# if the caster doesn't match,
# add an entry to the cancellation table
# update the schedule row with a new caster
# the new caster may be NULL in rare cases
@info ("$(dbs[:id]) $(cname[dbs[:caster_id]]):$(dbs[:caster_id]) replaced by $(s[:caster]):$(cid[s[:caster]])")
DB.cancel_schedule(db, dbs[:id], cid[s[:caster]])
else
@debug "$(dbs[:id]) $(s[:caster]) matches"
end
end
end
end

View File

@ -17,6 +17,14 @@ function find_or_create_caster(db, n)
end
end
"""
Find all casters
"""
function find_all_casters(db)
sql_select = "SELECT * FROM CASTER"
return DBInterface.execute(db, sql_select) |> DataFrame
end
"""
Find a segment by short name `n`.
"""
@ -52,26 +60,16 @@ function insert_schedule(db, row)
end
end
"""
TODO - Given a schedule, check the database for conflicting entries and fix them.
This usually happens when a caster has to be rescheduled due to unforseen circumstances.
"""
function fix_conflict(db, schedule)
# iterate through schedule
# find a db schedule row with the same jst
# check if the caster matches
# if the caster doesn't match,
# add an entry to the cancellation table
# update the schedule row with a new caster
# the new caster may be NULL in rare cases
end
"""
Return a row from the schedule based on its `jst` time.
"""
function find_schedule_by_jst(db, jst)
return DBInterface.execute(db, "SELECT * FROM schedule WHERE jst = ? LIMIT 1", [jst]) |> DataFrame
df = DBInterface.execute(db, "SELECT * FROM schedule WHERE jst = ? LIMIT 1", [jst]) |> DataFrame
if size(df, 1) == 0
return missing
else
return eachrow(df)[1]
end
end
"""
@ -87,3 +85,24 @@ function update_schedule_with_video(db, jst, video_id)
return false
end
end
"""
Cancel a schedule, and replace the entry with a new caster (or none at all).
"""
function cancel_schedule(db, schedule_id, new_caster_id)
sql_insert = "INSERT INTO cancellation SELECT id, caster_id, segment_id, jst FROM schedule WHERE id = ?"
sql_update = "UPDATE schedule SET caster_id = ? WHERE id = ?"
try
DBInterface.transaction(
function()
DBInterface.execute(db, sql_insert, [schedule_id])
DBInterface.execute(db, sql_update, [new_caster_id, schedule_id])
end,
db
)
catch e
@debug e
return false
end
return true
end