Skip to content

Commit

Permalink
fix #67: improve suggest command to support edge case with the last S…
Browse files Browse the repository at this point in the history
…unday
  • Loading branch information
kamilsk committed Jul 6, 2021
1 parent a33aefc commit 294366c
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 45 deletions.
3 changes: 1 addition & 2 deletions internal/command/github/contribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,6 @@ func Contribution(cnf *config.Tool) *cobra.Command {
date, err = time.Parse(time.RFC3339Month, input)
case len(time.RFC3339Day):
date, err = time.Parse(time.RFC3339Day, input)
date = time.TruncateToWeek(date)
default:
err = fmt.Errorf("unsupported format")
}
Expand All @@ -370,7 +369,7 @@ func Contribution(cnf *config.Tool) *cobra.Command {
}

// data provisioning
start := time.TruncateToWeek(date) // Monday
start := time.TruncateToWeek(date)
scope := time.NewRange(
start.Add(-2*time.Week-time.Day), // buffer from left side with Sunday
time.Now().UTC(),
Expand Down
34 changes: 4 additions & 30 deletions internal/command/github/view/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,43 +27,17 @@ func Diff(
{Align: simpletable.AlignLeft, Text: "Day / Week"},
},
}
for i, week := range data {
if shiftIsNeeded(i, week.Report) {
continue
}
for _, week := range data {
table.Header.Cells = append(table.Header.Cells, &simpletable.Cell{
Align: simpletable.AlignCenter,
Text: fmt.Sprintf("#%d", week.Number),
})
}

// shift Sunday to the right
row := make([]*simpletable.Cell, 0, 4)
row = append(row, &simpletable.Cell{Text: time.Sunday.String()})
for i := range data {
if shiftIsNeeded(i, data[i].Report) {
continue
}
// TODO:unclear explain
if i == 0 {
row = append(row, &simpletable.Cell{Align: simpletable.AlignCenter, Text: "-"})
continue
}
txt := "-"
if count := data[i-1].Report[time.Sunday]; count != 0 {
txt = fmt.Sprintf("%+d", count)
}
row = append(row, &simpletable.Cell{Align: simpletable.AlignCenter, Text: txt})
}
table.Body.Cells = append(table.Body.Cells, row)

for i := time.Monday; i <= time.Saturday; i++ {
row = make([]*simpletable.Cell, 0, 4)
for i := time.Sunday; i <= time.Saturday; i++ {
row := make([]*simpletable.Cell, 0, len(data)+1)
row = append(row, &simpletable.Cell{Text: i.String()})
for j, week := range data {
if shiftIsNeeded(j, week.Report) {
continue
}
for _, week := range data {
txt := "-"
if count := week.Report[i]; count != 0 {
txt = fmt.Sprintf("%+d", count)
Expand Down
34 changes: 23 additions & 11 deletions internal/command/github/view/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,28 @@ func prepare(heatmap contribution.HeatMap) []WeekReport {
report = append(report, row)
}

return report
}
// shift Sunday to the right and cleanup empty weeks
last := len(report) - 1
if last > -1 {
_, week := heatmap.To().Add(time.Week).ISOWeek()
report = append(report, WeekReport{
Number: week,
Report: make(map[time.Weekday]int),
})
for i := last + 1; i > 0; i-- {
if count, present := report[i-1].Report[time.Sunday]; present {
report[i].Report[time.Sunday] = count
delete(report[i-1].Report, time.Sunday)
}
}
}
cleaned := make([]WeekReport, 0, len(report))
for _, row := range report {
if len(row.Report) > 0 {
cleaned = append(cleaned, row)
}
}
report = cleaned

// If it's a first-week report with a single entry for Sunday,
// we skip it completely.
//
// It's because GitHub shows the contribution chart started on Sunday
// of the previous week. For that reason we have to shift it to the right
// and compensate `.Shift(-time.Day)` call for the scope.
func shiftIsNeeded(idx int, report map[time.Weekday]int) bool {
_, is := report[time.Sunday]
return idx == 0 && len(report) == 1 && is
return report
}
2 changes: 1 addition & 1 deletion internal/command/github/view/suggest.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func Suggest(
suggestion = fmt.Sprintf("%dd", option.Suggest.Day.Sub(now)/xtime.Day)
} else {
day := xtime.CopyClock(now, option.Suggest.Day).In(time.Local)
suggestion = fmt.Sprintf("%s", day.Format(time.RFC3339))
suggestion = day.Format(time.RFC3339)
}
if option.Short {
printer.Println(suggestion)
Expand Down
6 changes: 5 additions & 1 deletion internal/pkg/time/range.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,11 @@ func TruncateToDay(t time.Time) time.Time {
}

func TruncateToWeek(t time.Time) time.Time {
return TruncateToDay(t).Add(Day * time.Duration(time.Monday-t.Weekday()))
day := t.Weekday()
if day == time.Sunday {
day = 7
}
return TruncateToDay(t).Add(Day * time.Duration(time.Monday-day))
}

func TruncateToMonth(t time.Time) time.Time {
Expand Down
53 changes: 53 additions & 0 deletions internal/pkg/time/range_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package time_test

import (
"testing"
"time"

"github.com/stretchr/testify/assert"

. "go.octolab.org/toolset/maintainer/internal/pkg/time"
)

func TestTruncateToWeek(t *testing.T) {
tests := map[string]struct {
day time.Time
want time.Time
}{
time.Monday.String(): {
time.Date(2022, 07, 4, 0, 0, 0, 0, time.UTC),
time.Date(2022, 07, 4, 0, 0, 0, 0, time.UTC),
},
time.Tuesday.String(): {
time.Date(2022, 7, 5, 0, 0, 0, 0, time.UTC),
time.Date(2022, 7, 4, 0, 0, 0, 0, time.UTC),
},
time.Wednesday.String(): {
time.Date(2022, 7, 6, 0, 0, 0, 0, time.UTC),
time.Date(2022, 7, 4, 0, 0, 0, 0, time.UTC),
},
time.Thursday.String(): {
time.Date(2022, 7, 7, 0, 0, 0, 0, time.UTC),
time.Date(2022, 7, 4, 0, 0, 0, 0, time.UTC),
},
time.Friday.String(): {
time.Date(2022, 7, 5, 0, 0, 0, 0, time.UTC),
time.Date(2022, 7, 4, 0, 0, 0, 0, time.UTC),
},
time.Saturday.String(): {
time.Date(2022, 7, 9, 0, 0, 0, 0, time.UTC),
time.Date(2022, 7, 4, 0, 0, 0, 0, time.UTC),
},
time.Sunday.String(): {
time.Date(2022, 7, 10, 0, 0, 0, 0, time.UTC),
time.Date(2022, 7, 4, 0, 0, 0, 0, time.UTC),
},
}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
got := TruncateToWeek(test.day)
assert.Equal(t, test.want, got)
})
}
}

0 comments on commit 294366c

Please sign in to comment.