From 95a76cda554221df7f8a65d315ffd12a617144c0 Mon Sep 17 00:00:00 2001 From: Guillaume Petiot Date: Mon, 26 Feb 2024 11:59:37 +0000 Subject: [PATCH] Create Period file --- bin/main.ml | 45 ++++----------------------------------------- lib/period.ml | 44 ++++++++++++++++++++++++++++++++++++++++++++ lib/period.mli | 12 ++++++++++++ 3 files changed, 60 insertions(+), 41 deletions(-) create mode 100644 lib/period.ml create mode 100644 lib/period.mli diff --git a/bin/main.ml b/bin/main.ml index 7ef8cab..0c7743b 100644 --- a/bin/main.ml +++ b/bin/main.ml @@ -8,8 +8,6 @@ let or_die = function Fmt.epr "%s@." m; exit 1 -let one_week = 60. *. 60. *. 24. *. 7. - let home = match Sys.getenv_opt "HOME" with | None -> Fmt.failwith "$HOME is not set!" @@ -32,44 +30,9 @@ let mtime path = | info -> Some info.Unix.st_mtime | exception Unix.Unix_error(Unix.ENOENT, _, _) -> None -let set_mtime path time = - if not (Sys.file_exists path) then - close_out @@ open_out_gen [Open_append; Open_creat] 0o600 path; - Unix.utimes path time time - let get_token () = Token.load (home / ".github" / "github-activity-token") -let to_8601 t = - let open Unix in - let t = gmtime t in - Printf.sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ" - (t.tm_year + 1900) - (t.tm_mon + 1) - (t.tm_mday) - (t.tm_hour) - (t.tm_min) - (t.tm_sec) - -(* Run [fn (start, finish)], where [(start, finish)] is the period specified by [period]. - If [period] is [`Since_last_fetch] or [`Last_week] then update the last-fetch timestamp on success. *) -let with_period period fn = - let now = Unix.time () in - let last_week = now -. one_week in - let range = - match period with - | `Since_last_fetch -> - let last_fetch = Option.value ~default:last_week (mtime last_fetch_file) in - (to_8601 last_fetch, to_8601 now) - | `Last_week -> - (to_8601 last_week, to_8601 now) - | `Range r -> r - in - fn range; - match period with - | `Since_last_fetch | `Last_week -> set_mtime last_fetch_file now - | `Range _ -> () - let show ~from json = let contribs = Contributions.of_json ~from json in if Contributions.is_empty contribs then @@ -96,7 +59,7 @@ let last_week = Arg.(value & flag doc) let period = - let f from to_ last_week = + let f from to_ last_week : Period.t = if last_week then `Last_week else match (from, to_) with @@ -111,20 +74,20 @@ let info = Cmd.info "get-activity" let run period : unit = match mode with | `Normal -> - with_period period (fun period -> + Period.with_period period ~last_fetch_file ~f:(fun period -> (* Fmt.pr "period: %a@." Fmt.(pair string string) period; *) let token = get_token () |> or_die in show ~from:(fst period) @@ Contributions.fetch ~period ~token ) | `Save -> - with_period period (fun period -> + Period.with_period period ~last_fetch_file ~f:(fun period -> let token = get_token () |> or_die in Contributions.fetch ~period ~token |> Yojson.Safe.to_file "activity.json" ) | `Load -> (* When testing formatting changes, it is quicker to fetch the data once and then load it again for each test: *) - let from = mtime last_fetch_file |> Option.value ~default:0.0 |> to_8601 in + let from = mtime last_fetch_file |> Option.value ~default:0.0 |> Period.to_8601 in show ~from @@ Yojson.Safe.from_file "activity.json" let term = Term.(const run $ period) diff --git a/lib/period.ml b/lib/period.ml new file mode 100644 index 0000000..3e4efb6 --- /dev/null +++ b/lib/period.ml @@ -0,0 +1,44 @@ +type t = + [ `Last_week + | `Range of string * string + | `Since_last_fetch ] + +let one_week = 60. *. 60. *. 24. *. 7. + +let to_8601 t = + let open Unix in + let t = gmtime t in + Printf.sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ" + (t.tm_year + 1900) + (t.tm_mon + 1) + (t.tm_mday) + (t.tm_hour) + (t.tm_min) + (t.tm_sec) + +let mtime path = + match Unix.stat path with + | info -> Some info.Unix.st_mtime + | exception Unix.Unix_error(Unix.ENOENT, _, _) -> None + +let set_mtime path time = + if not (Sys.file_exists path) then + close_out @@ open_out_gen [Open_append; Open_creat] 0o600 path; + Unix.utimes path time time + +let with_period period ~last_fetch_file ~f = + let now = Unix.time () in + let last_week = now -. one_week in + let range = + match period with + | `Since_last_fetch -> + let last_fetch = Option.value ~default:last_week (mtime last_fetch_file) in + (to_8601 last_fetch, to_8601 now) + | `Last_week -> + (to_8601 last_week, to_8601 now) + | `Range r -> r + in + f range; + match period with + | `Since_last_fetch | `Last_week -> set_mtime last_fetch_file now + | `Range _ -> () diff --git a/lib/period.mli b/lib/period.mli new file mode 100644 index 0000000..1c2d083 --- /dev/null +++ b/lib/period.mli @@ -0,0 +1,12 @@ +type t = + [ `Last_week + | `Range of string * string + | `Since_last_fetch ] + +val one_week : float + +val to_8601 : float -> string + +val with_period : t -> last_fetch_file:string -> f:(string * string -> unit) -> unit +(** Run [f (start, finish)], where [(start, finish)] is the period specified by [period]. + If [period] is [`Since_last_fetch] or [`Last_week] then update the last-fetch timestamp on success. *)