Skip to content

Commit

Permalink
Create Period file
Browse files Browse the repository at this point in the history
  • Loading branch information
gpetiot committed Feb 26, 2024
1 parent 912b1ac commit 95a76cd
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 41 deletions.
45 changes: 4 additions & 41 deletions bin/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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!"
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down
44 changes: 44 additions & 0 deletions lib/period.ml
Original file line number Diff line number Diff line change
@@ -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 _ -> ()
12 changes: 12 additions & 0 deletions lib/period.mli
Original file line number Diff line number Diff line change
@@ -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. *)

0 comments on commit 95a76cd

Please sign in to comment.