import Base import ./json.bend as Json import ./store/seq.bend as SSeq # Switch, part 3: choosing the target and reading usage headroom (pure). # One rotation candidate. `headroom` is 100 minus the busiest window, in # hundredths of a percent (None when no usage is known). type Cand is Data: Cand{slot: String, disabled: Bool, switchable: Bool, headroom: Maybe<&2, U32>} # The first candidate to switch to, with a warning for each one skipped on the way. type Sel is Data: SelOk{slot: String, warns: List<&2, String>} SelNone{warns: List<&2, String>, exhausted: Bool} type Pick is Data: Pick{slot: String, h: U32, found: Bool} type BestOut is Data: BPick{slot: String} BNote{note: String} def skip_with(+w: String, +ex: Bool, +r: Sel) -> Sel: match r: case SelOk{s, ws}: SelOk{s, w <> ws} case SelNone{ws, e}: SelNone{w <> ws, e || ex} def at_limit(+h: Maybe<&2, U32>) -> Bool: match h: case Some{x}: U32.is_eq(x, 0) case None{}: False{} def sel_step2(+dis: Bool, +sw: Bool, +limit: Bool, +slot: String, +r: Sel) -> Sel: match dis sw limit: case True{} _ _: skip_with("Skipped Account-" ++ slot ++ " (disabled)", False{}, r) case False{} False{} _: skip_with("Skipped Account-" ++ slot ++ " (no stored credentials/config)", False{}, r) case False{} True{} True{}: skip_with("Skipped Account-" ++ slot ++ " (at 5h/7d limit)", True{}, r) case False{} True{} False{}: SelOk{slot, []} def sel_step(+avail: Bool, +c: Cand, +r: Sel) -> Sel: match c: case Cand{slot, dis, sw, hr}: sel_step2(dis, sw, avail && at_limit(hr), slot, r) # Walk `cs` (already in rotation order, current slot excluded) as switch() does. # `avail` is next-available: skip accounts with no headroom. def select_next(+cs: List<&2, Cand>, +avail: Bool) -> Sel: match cs: case Nil{}: SelNone{[], False{}} case Con{c, rest}: sel_step(avail, c, select_next(rest, avail)) def sel_n(+c: Bool, +x: Nat, +y: Nat) -> Nat: match c: case True{}: x case False{}: y def index_of(+xs: List<&2, String>, +x: String, +i: Nat) -> Nat: match xs: case Nil{}: 0n case Con{h, t}: sel_n(String.eq(h, x), i, index_of(t, x, 1n+i)) def rotated2(+xs: List<&2, String>, +idx: Nat) -> List<&2, String>: List.append(&2, String, List.drop(&2, String, xs, 1n+idx), List.take(&2, String, xs, idx)) # The other slots in rotation order after `anchor`: offsets 1..n-1 of the # cyclic sequence. An unknown anchor counts as index 0, as in the reference. def rotated(+xs: List<&2, String>, +anchor: Maybe<&2, String>) -> List<&2, String>: rotated2(xs, index_of(xs, SSeq.str_or(anchor, ""), 0n)) def better(+found: Bool, +h: U32, +cur: Pick) -> Bool: match cur: case Pick{s, ch, f}: Bool.not(f) || U32.is_gt(h, ch) def sel_pick(+c: Bool, +x: Pick, +y: Pick) -> Pick: match c: case True{}: x case False{}: y def best_known(+cs: List<&2, Cand>, +cur: Pick) -> Pick: match cs: case Nil{}: cur case Con{Cand{slot, d, sw, Some{h}}, t}: best_known(t, sel_pick(better(True{}, h, cur), Pick{slot, h, True{}}, cur)) case Con{c, t}: best_known(t, cur) def any_unknown(+cs: List<&2, Cand>) -> Bool: match cs: case Nil{}: False{} case Con{Cand{s, d, sw, None{}}, t}: True{} case Con{c, t}: any_unknown(t) def best4(+wins: Bool, +s: String, +unknown: Bool, +exhausted: Bool) -> BestOut: match wins unknown exhausted: case True{} _ _: BPick{s} case False{} True{} _: BNote{"incomplete-comparison"} case False{} False{} True{}: BNote{"exhausted"} case False{} False{} False{}: BNote{"stay"} def best3(+p: Pick, +cur: U32, +unknown: Bool) -> BestOut: match p: case Pick{s, h, f}: best4(U32.is_gt(h, cur), s, unknown, U32.is_eq(cur, 0)) def best2(+cur: Maybe<&2, U32>, +others: List<&2, Cand>, +p: Pick) -> BestOut: match cur p: case None{} _: BNote{"current-unavailable"} case Some{c} Pick{s, h, False{}}: BNote{"no-comparison"} case Some{c} Pick{s, h, True{}}: best3(Pick{s, h, True{}}, c, any_unknown(others)) # `_select_best_switchable`: `others` are the switchable, enabled slots but the # current one. Switch only to strictly more headroom. def select_best(+cur: Maybe<&2, U32>, +others: List<&2, Cand>) -> BestOut: match others: case Nil{}: BNote{"none"} case Con{o, t}: best2(cur, Con{o, t}, best_known(Con{o, t}, Pick{"", 0, False{}})) def pct2(+parts: List<&2, String>) -> U32: match parts: case Con{i, Con{f, Nil{}}}: (SSeq.str_u32(i) * 100 + SSeq.str_u32(String.take(f ++ "00", 2n)) : U32) case Con{i, Nil{}}: (SSeq.str_u32(i) * 100 : U32) case _: 0 # "90.0" -> 9000, "12.34" -> 1234 (hundredths of a percent). def pct_hundredths(+t: String) -> U32: pct2(String.split(t, '.')) def pct_of2(+m: Maybe<&2, Json.Json>) -> Maybe<&2, U32>: match m: case Some{Json.JNum{t}}: Some{pct_hundredths(t)} case _: None{} def pct_of(+w: Json.Json) -> Maybe<&2, U32>: pct_of2(Json.get("pct", w)) def push_pct(+m: Maybe<&2, U32>, +rest: List<&2, U32>) -> List<&2, U32>: match m: case Some{p}: p <> rest case None{}: rest def lower_all(+xs: List<&2, String>) -> List<&2, String>: match xs: case Nil{}: [] case Con{h, t}: String.to_lower(h) <> lower_all(t) def mem(+x: String, +xs: List<&2, String>) -> Bool: match xs: case Nil{}: False{} case Con{h, t}: String.eq(h, x) || mem(x, t) def scoped_pick(+hit: Bool, +m: Maybe<&2, U32>, +rest: List<&2, U32>) -> List<&2, U32>: match hit: case True{}: push_pct(m, rest) case False{}: rest def scoped_pcts(+items: Json.Json, +wanted: List<&2, String>) -> List<&2, U32>: match items: case Json.JCons{s, t}: scoped_pick(mem("all", wanted) || mem(String.to_lower(SSeq.field_str(s, "name")), wanted), pct_of(s), scoped_pcts(t, wanted)) case _: [] def window_pct2(+m: Maybe<&2, Json.Json>) -> Maybe<&2, U32>: match m: case Some{w}: pct_of(w) case None{}: None{} def window_pct(+row: Json.Json, +key: String) -> Maybe<&2, U32>: window_pct2(Json.get(key, row)) def sel_l(+c: Bool, +x: List<&2, U32>, +y: List<&2, U32>) -> List<&2, U32>: match c: case True{}: x case False{}: y # The percentages `relevant_windows` looks at: 5h, 7d and, with models, the # scoped windows named by them (or all of them for "all"). def pcts(+row: Json.Json, +models: List<&2, String>) -> List<&2, U32>: push_pct(window_pct(row, "five_hour"), push_pct(window_pct(row, "seven_day"), sel_l(List.is_empty(&2, String, models), [], scoped_pcts(SSeq.unwrap_arr(SSeq.get_or("scoped", row, Json.JNull{})), lower_all(models))))) def max_pct(+xs: List<&2, U32>, +acc: U32) -> U32: match xs: case Nil{}: acc case Con{h, t}: max_pct(t, SSeq.max_u32(acc, h)) def min_u32(+a: U32, +b: U32) -> U32: SSeq.sel.u(U32.is_lt(a, b), a, b) def headroom2(+xs: List<&2, U32>) -> Maybe<&2, U32>: match xs: case Nil{}: None{} case Con{h, t}: Some{(10000 - min_u32(10000, max_pct(Con{h, t}, 0)) : U32)} # 100 - the busiest relevant window, from a usage row's `lastGood`. def headroom(+row: Json.Json, +models: List<&2, String>) -> Maybe<&2, U32>: headroom2(pcts(row, models)) # usage.json -> the lastGood object of a slot. def usage_row(+usage: Json.Json, +slot: String) -> Json.Json: SSeq.get_or("lastGood", SSeq.get_or(slot, SSeq.accounts_of(usage), Json.JNull{}), Json.JNull{}) def sel_lc(+c: Bool, +x: List<&2, Cand>, +y: List<&2, Cand>) -> List<&2, Cand>: match c: case True{}: x case False{}: y