Skip to content

Commit

Permalink
Integrated statistic printing into timed-cli
Browse files Browse the repository at this point in the history
  • Loading branch information
corka committed May 7, 2019
1 parent e872829 commit 2ac32de
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
3 changes: 3 additions & 0 deletions lib/persister.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ defmodule Timed.Persister do

require Logger

alias Timed.Reporter

@doc """
Returns the path to the timed.csv-File.
"""
Expand All @@ -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
Expand Down
33 changes: 26 additions & 7 deletions lib/reporter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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 """
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit 2ac32de

Please sign in to comment.