Skip to content

Commit

Permalink
REPORTING: improve cli report and added unit tests (#7)
Browse files Browse the repository at this point in the history
* add reporting option to show culmative month

* add images to readme page

* small change to cli report output

* make images in readme smaller

* add test for cli markdown report
  • Loading branch information
jeff-knurek authored Sep 26, 2020
1 parent 3fdac91 commit 1a2fb14
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 3 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ Developed specifically for ubuntu 18.04 _(soon to be tested on 20.01)_. Focused

Config values can be passed by cli args, and/or provided via `~/.hours-worked/config.toml`. If that files doesn't exist on first run, a default one will be created.

### Examples

#### Menu icon

<img src="./images/example-menu.png" width="10%" alt="Example of menu icon" />

#### CLI Text Report

<img src="./images/example-cli-report.png" width="55%" alt="Example cli text report" />


## Install

In order to show the menu icon, `github.com/getlantern/systray` requires a few libraries: _(some may already be installed)_
Expand Down
1 change: 1 addition & 0 deletions cmd/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func runReport(format string) {
filename := viper.GetString("tracking_file")

fmt.Printf("hours worked this week: %.1f \n", reporting.HoursWorkedThisWeek(filename, user.Username))
fmt.Printf("hours worked this month: %.1f \n", reporting.HoursWorkedThisMonth(filename, user.Username))
fmt.Println("-------------")

fmt.Println(reporting.TextCalendar(time.Now(), filename, user.Username))
Expand Down
Binary file added images/example-cli-report.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/example-menu.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 7 additions & 3 deletions pkg/reporting/calendar.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,23 @@ import (
func TextCalendar(t time.Time, filename, user string) string {
trackedData := getTrackedData(filename)
uData := trackedData[user]
return textCalendar(uData, t)
}

func textCalendar(data Years, t time.Time) string {
weekLabels := []string{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}
y := strconv.Itoa(t.Year())
if uData[y] == nil || uData[y][t.Month().String()] == nil {
if data[y] == nil || data[y][t.Month().String()] == nil {
return "--no data tracked for this month--"
}
trackedDays := uData[y][t.Month().String()]
trackedDays := data[y][t.Month().String()]

wc := len(weekLabels)
wd := (int(t.Weekday()) - (t.Day() - 1) + wc*30) % wc
lastTracked := t.Day()
last := t.AddDate(0, 1, -t.Day()).Day()

s := t.Month().String() + " \n\n"
s := t.Month().String() + "\n\n"
s += "| " + strings.Join(weekLabels, " | ") + " |\n"
s += "|" + strings.Repeat("----:|", wc) + "\n"
s += "|" + strings.Repeat(" |", wd)
Expand Down
62 changes: 62 additions & 0 deletions pkg/reporting/calendar_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package reporting

import (
"strings"
"testing"
"time"
)

func TestTextCalendar(t *testing.T) {
dec, _ := time.Parse("2006-01-02 15:04", "2020-12-31 9:59")
jan, _ := time.Parse("2006-01-02 15:04", "2021-01-02 9:59")
tests := []struct {
name string
data Years
t time.Time
want string
}{
{
name: "one month",
data: Years{"2020": {"December": {"27": 420, "30": 120, "31": 120}}},
t: dec,
want: strings.Replace(`December
| Sun | Mon | Tue | Wed | Thu | Fri | Sat |
|----:|----:|----:|----:|----:|----:|----:|
| | | 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 7.0 | 0 | 0 | 2.0 | 2.0 | | |`, "\t", "", -1),
},
{
name: "no data this month",
data: Years{"2020": {"October": {"27": 420, "30": 120, "31": 120}}},
t: dec,
want: "--no data tracked for this month--",
},
{
name: "two months",
data: Years{"2020": {"December": {"27": 420, "30": 120}}, "2021": {"January": {"1": 120, "2": 120}}},
t: jan,
want: strings.Replace(`January
| Sun | Mon | Tue | Wed | Thu | Fri | Sat |
|----:|----:|----:|----:|----:|----:|----:|
| | | | | | 2.0 | 2.0 |
| . | . | . | . | . | . | . |
| . | . | . | . | . | . | . |
| . | . | . | . | . | . | . |
| . | . | . | . | . | . | . |
| . | | | | | | |`, "\t", "", -1),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := textCalendar(tt.data, tt.t)
if got != tt.want {
t.Errorf("textCalendar() \nGot:\n%v,\nwant:\n%v\n", got, tt.want)
}
})
}
}
21 changes: 21 additions & 0 deletions pkg/reporting/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,24 @@ func sumThisWeek(data Years, t time.Time) float64 {
}
return float64(sum) / 60
}

// HoursWorkedThisMonth returns the total hours documented per day for the current month
func HoursWorkedThisMonth(filename, user string) float64 {
data := getTrackedData(filename)[user]
now := time.Now()

return sumThisMonth(data, now)
}

func sumThisMonth(data Years, t time.Time) float64 {
thisDay := t
y := strconv.Itoa(thisDay.Year())
m := thisDay.Month().String() // uses month name - might "break" if user switches locales
days := data[y][m]

sum := 0
for _, d := range days {
sum += d
}
return float64(sum) / 60
}
55 changes: 55 additions & 0 deletions pkg/reporting/simple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ func Test_sumThisWeek(t *testing.T) {
t: jan,
want: 480 / 60,
},
{
name: "same days two years",
data: Years{"2020": {"December": {"27": 420, "30": 120, "31": 120}}, "2021": {"December": {"27": 420, "30": 120, "31": 120}}},
t: dec,
want: 240 / 60,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -41,3 +47,52 @@ func Test_sumThisWeek(t *testing.T) {
})
}
}

func Test_sumThisMonth(t *testing.T) {
dec, _ := time.Parse("2006-01-02 15:04", "2020-12-31 9:59")
jan, _ := time.Parse("2006-01-02 15:04", "2021-01-02 9:59")
tests := []struct {
name string
data Years
t time.Time
want float64
}{
{
name: "just one day an hour and a half",
data: Years{"2020": {"December": {"31": 90}}},
t: dec,
want: float64(90) / 60,
},
{
name: "two days, lots of hours",
data: Years{"2020": {"December": {"30": 1440, "31": 1443}}},
t: dec,
want: 2883.0 / 60,
},
{
name: "no time recorded yet",
data: Years{"2020": {"October": {"30": 1440}}},
t: dec,
want: float64(0) / 60,
},
{
name: "four days spanning one year",
data: Years{"2020": {"December": {"27": 420, "30": 120, "31": 120}}, "2021": {"January": {"1": 120, "2": 120}}},
t: jan,
want: 240 / 60,
},
{
name: "same days two years",
data: Years{"2020": {"December": {"27": 420, "30": 120, "31": 120}}, "2021": {"December": {"27": 120, "30": 60, "31": 120}}},
t: dec,
want: 660 / 60,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := sumThisMonth(tt.data, tt.t); got != tt.want {
t.Errorf("sumThisMonth() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 1a2fb14

Please sign in to comment.