View raw

1 type timestamp = int 2 3 let timestamp_of_unix_seconds s = s 4 let timestamp_to_unix_seconds t = t 5 6 type duration = int 7 8 let hours n = n * 3600 9 let days n = n * 86_400 10 let duration_to_seconds d = d 11 let elapsed ~since ~now = Int.max 0 (now - since) 12 13 let pp_duration ppf d = 14 if d mod 86_400 = 0 && d <> 0 then Format.fprintf ppf "%dd" (d / 86_400) 15 else if d mod 3600 = 0 && d <> 0 then Format.fprintf ppf "%dh" (d / 3600) 16 else Format.fprintf ppf "%ds" d 17 18 type readiness = 19 | Ready 20 | Recovering of { rested : duration; recommended : duration } 21 22 let evaluate_readiness ~elapsed ~recommended = 23 if elapsed >= recommended then Ready 24 else Recovering { rested = elapsed; recommended } 25 26 let is_ready = function Ready -> true | Recovering _ -> false 27 28 let pp_readiness ppf = function 29 | Ready -> Format.pp_print_string ppf "ready" 30 | Recovering { rested; recommended } -> 31 Format.fprintf ppf "recovering (%a of %a)" pp_duration rested pp_duration 32 recommended 33 34 type basis = 35 | Recovered 36 | Overridden of { rested : duration; recommended : duration } 37 38 type clearance = basis 39 40 let clear = function Ready -> Some Recovered | Recovering _ -> None 41 42 let override readiness = 43 match readiness with 44 | Ready -> Recovered 45 | Recovering { rested; recommended } -> Overridden { rested; recommended } 46 47 let basis c = c 48