From 2ac32dee6ff35a6d0a26a910a86956ea7c1ca46f Mon Sep 17 00:00:00 2001 From: corka Date: Tue, 7 May 2019 22:05:48 +0200 Subject: [PATCH] Integrated statistic printing into timed-cli --- lib/persister.ex | 3 +++ lib/reporter.ex | 33 ++++++++++++++++++++++++++------- mix.exs | 2 +- 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/lib/persister.ex b/lib/persister.ex index f1730d6..f49d269 100644 --- a/lib/persister.ex +++ b/lib/persister.ex @@ -2,6 +2,8 @@ defmodule Timed.Persister do require Logger + alias Timed.Reporter + @doc """ Returns the path to the timed.csv-File. """ @@ -22,6 +24,7 @@ defmodule Timed.Persister do case get_db_path() do {:ok, path} -> read_db(path) |> update_entries(new_entry) + |> Reporter.log_statistics() |> save_db(path) {:error, message} -> Logger.error message end diff --git a/lib/reporter.ex b/lib/reporter.ex index 1dc48f2..fce4a6e 100644 --- a/lib/reporter.ex +++ b/lib/reporter.ex @@ -2,22 +2,41 @@ defmodule Timed.Reporter do require Logger - def log_hours_today([]) do nil end + def log_statistics(timed_list) do + timed_list + |> log_hours_today() + |> log_overtime() + end + + @doc """ + Logs to stdout the worked hours for today. + """ + def log_hours_today(timed_list) do + log_hours(timed_list) + timed_list + end + + defp log_hours([]) do nil end - def log_hours_today([head | tail]) do - if NaiveDateTime.to_date(head) == Date.utc_today() do + defp log_hours([head | tail]) do + %{start: start} = head + if NaiveDateTime.to_date(start) == Date.utc_today() do worked_hours = calc_worked_hours(head) - Logger.info("Hours today: #{worked_hours}") + Logger.info("Hours today: #{worked_hours} hrs") else log_hours_today(tail) end end + @doc """ + Logs to stdout the difference between expected sum of hours and sum of actual hours + """ def log_overtime(timed_list) do expected_worked_hours = 8 * length(timed_list) - actual_worked_hours = Enum.reduce(timed_list, fn timed, acc -> calc_worked_hours(timed) + acc end) - overtime = expected_worked_hours - actual_worked_hours - Logger.info("Overtime: #{overtime}") + actual_worked_hours = Enum.reduce(timed_list, 0, fn timed, acc -> calc_worked_hours(timed) + acc end) + overtime = actual_worked_hours - expected_worked_hours + Logger.info("Overtime: #{overtime} hrs") + timed_list end @doc """ diff --git a/mix.exs b/mix.exs index 785cbdd..41e77df 100644 --- a/mix.exs +++ b/mix.exs @@ -4,7 +4,7 @@ defmodule Timed.MixProject do def project do [ app: :timed, - version: "1.0.2", + version: "1.1.0", elixir: "~> 1.8", start_permanent: Mix.env() == :prod, deps: deps(),