This commit is contained in:
Benedetta 2024-04-30 12:01:58 +02:00
parent d747aa1cca
commit 552c7b1296
4 changed files with 48 additions and 15 deletions

View file

@ -13,23 +13,21 @@ let _HTTP_CLIENT = "http_client"
let http_client (repos: Config.repo_data list) = let http_client (repos: Config.repo_data list) =
let http_actor = Httpclient.init repos in let http_actor = Httpclient.init repos in
let now = Ptime_clock.now () in
let _ = print_endline "Initialized http client" in let _ = print_endline "Initialized http client" in
in
let rec loop () = let rec loop () =
let _ = let _ =
match Riot.receive () with match Riot.receive_any () with
| ListIssues -> | ListIssues ->
Httpclient.make_get_request http_actor let issues = Httpclient.make_get_request http_actor
(* |> Result.map Pam.Datatypes.forgejo_issues *) in
|> Result.bind (fun issue -> (* Result.bind issues pair_to_alert_time *) TODO: usa Pam.Issue_parser.should_alert
match to_datetime issue with |> Result.map (List.filter_map filter_issue_by_time)
| Error e -> Error e |> Option.some |> Result.map_error internal_failure
| Ok None -> None |> Result.fold ~ok:forgejo_issues ~error:List.singleton
| Ok (Some times) -> failwith "dunno"
)
|> Result.map_error Pam.Datatypes.internal_failure
|> Result.fold ~ok:identity ~error:(fun e -> [e])
|> List.map (Riot.send_by_name ~name:_MQ_CLIENT) |> List.map (Riot.send_by_name ~name:_MQ_CLIENT)
@ -53,7 +51,7 @@ let mq_client (mq_url, mq_user, mq_password) =
let%lwt _ = Lwt_unix.sleep 1.0 in let%lwt _ = Lwt_unix.sleep 1.0 in
let _ = let _ =
try%lwt try%lwt
match Riot.receive ~after:one_second () with match Riot.receive_any ~after:one_second () with
| InternalFailure err -> | InternalFailure err ->
let _ = Pamlog.error [%string "Got error from Forgejo: %{err}"] in let _ = Pamlog.error [%string "Got error from Forgejo: %{err}"] in
Mq.mq_publish mq err Mq.mq_publish mq err

View file

@ -17,7 +17,7 @@ type forgejo_issue_data = { (* from an issue in forgejo get a reminder *)
type Riot.Message.t += type Riot.Message.t +=
| RegisterClient of (client_id * Riot.Pid.t) | RegisterClient of (client_id * Riot.Pid.t)
| LookupClient of client_id | LookupClient of client_id
| ListIssues | ListIssues
| Reminder of forgejo_issue_data | Reminder of forgejo_issue_data
| InternalFailure of string | InternalFailure of string

View file

@ -95,3 +95,16 @@ let to_datetime: Datatypes.forgejo_issue_data -> (Ptime.t list option, string) r
List.map (fun ap -> ap |> convert |> to_exact_date offset) alerts List.map (fun ap -> ap |> convert |> to_exact_date offset) alerts
|> all' [] |> all' []
|> Result.ok |> Result.ok
let should_alert alerts now =
let is_noon_in_seconds hour tz_offset minute =
let h = hour * 60 * 60 in
let m = minute * 60 in
let total = tz_offset + h + m in
let _ = total |> string_of_int |> print_endline in
(12 * 60 * 60) = total
in
let is_noon t = Ptime.to_date_time t |> function (_date, ((h, m, _s), tz_offset)) -> is_noon_in_seconds h tz_offset m in
(List.exists (Ptime.equal now) alerts) ||
(List.for_all (Ptime.is_earlier ~than:now) alerts && is_noon now)

View file

@ -31,8 +31,30 @@ let test_parse () =
| Error e -> Alcotest.fail [%string "Got Error %{e}"] | Error e -> Alcotest.fail [%string "Got Error %{e}"]
| Ok (Some got) -> Alcotest.check (list ptime_testable) "Alert times" expected got | Ok (Some got) -> Alcotest.check (list ptime_testable) "Alert times" expected got
let test_fire () =
let should_alert (lst, now) = Pam.Issue_parser.should_alert lst now in
let of_rfc x = x |> Ptime.of_rfc3339 |> Result.get_ok |> function | t, _, _ -> t in
let transform (lst, clock) = (List.map of_rfc lst, of_rfc clock) in
let alerts0 = (["2024-04-19T12:38:00+02:00"], "2024-04-19T06:38:00-04:00") |> transform in
let alerts1 = (["2024-04-20T10:02:00+02:00"], "2024-04-20T10:08:00+00:00") |> transform in
let alerts2 = (["2024-04-20T10:02:00+02:00"], "2024-04-20T10:08:00+04:00") |> transform in
let alerts3 = (["2024-04-20T10:02:00+02:00"], "2024-04-22T12:00:00+00:00") |> transform in
let alerts4 = (["2024-04-20T10:02:00+02:00"], "2024-04-22T12:00:00+02:00") |> transform in
let alerts5 = (["2024-04-20T10:02:00+02:00" ; "2024-05-20T10:02:00+02:00"], "2024-04-22T12:00:00+02:00") |> transform in
let alerts6 = (["2024-04-20T10:02:00+02:00" ; "2024-05-20T10:02:00+02:00"], "2024-04-22T12:00:00+00:00") |> transform in
(
Alcotest.check (bool) "exact moment" true ( should_alert alerts0 ) ;
Alcotest.check (bool) "alert is in the future" false ( should_alert alerts1 ) ;
Alcotest.check (bool) "alert is in the past" false ( should_alert alerts2 ) ;
Alcotest.check (bool) "alert is in the past but it is noon" true ( should_alert alerts3 ) ;
Alcotest.check (bool) "alert is in the past but it is non-utc noon" false ( should_alert alerts4 ) ;
Alcotest.check (bool) "one alert in the past, one in the future" false ( should_alert alerts5 ) ;
Alcotest.check (bool) "one alert in the past, one in the future, it is noon" false ( should_alert alerts6 ) ;
)
let () = let () =
Alcotest.run "test1" [ Alcotest.run "test1" [
"alert_times", [test_case "Test Alert Times" `Quick test_parse] "alert_times", [test_case "Test Alert Times" `Quick test_parse] ;
"send_alerts", [test_case "Test sending alerts" `Quick test_fire]
] ]