summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthieu <matthieu.cneude@gmail.com>2019-08-17 21:24:43 +0200
committerMatthieu <matthieu.cneude@gmail.com>2019-08-17 21:24:43 +0200
commit73940cdd85746eb6b88a9217778d96c72cd9cfd2 (patch)
tree657303fdbfe937468294d5c73fbb7c715cb40394
parent48751c9cb05f04fe8f6297a391c1f69bce265e73 (diff)
[github-widgets] Small refactoring
-rw-r--r--cmd/devdash/devdash.go1
-rw-r--r--internal/github_widget.go25
-rw-r--r--internal/monitor_widget.go3
-rw-r--r--internal/plateform/date.go48
-rw-r--r--internal/plateform/github.go45
-rw-r--r--internal/plateform/github_perso.go4
-rw-r--r--internal/plateform/github_test.go11
-rw-r--r--internal/widget.go6
-rw-r--r--totime/totime.go7
9 files changed, 86 insertions, 64 deletions
diff --git a/cmd/devdash/devdash.go b/cmd/devdash/devdash.go
index ca4de71..71b12d3 100644
--- a/cmd/devdash/devdash.go
+++ b/cmd/devdash/devdash.go
@@ -58,7 +58,6 @@ func main() {
tui.Loop()
}
-// TODO To refactor - SRP violated here.
func loadFile(file string) (config, *internal.Tui, error) {
termui, err := plateform.NewTermUI(*debug)
if err != nil {
diff --git a/internal/github_widget.go b/internal/github_widget.go
index 11e1ff7..232d20f 100644
--- a/internal/github_widget.go
+++ b/internal/github_widget.go
@@ -294,7 +294,7 @@ func (g *githubWidget) barViews(widget Widget) (err error) {
return nil
}
-// TODO to refactor - transforming any date statement (weeks_ago, month_ago) into days ago in plateform.date, and plugt it in.
+// TODO to refactor - transforming any date statement (weeks_ago, month_ago) into days weeks_ago in plateform.date, and plugt it in.
func (g *githubWidget) barCommits(widget Widget) (err error) {
var repo string
if _, ok := widget.Options[optionRepository]; ok {
@@ -316,27 +316,26 @@ func (g *githubWidget) barCommits(widget Widget) (err error) {
ed = widget.Options[optionEndDate]
}
- if !strings.Contains(sd, "weeks_ago") {
- return errors.New("the widget bar_commits require you to indicate a week range, ie startDate: 5_weeks_ago, endDate: 0_weeks_ago ")
+ scope := ownerScope
+ if _, ok := widget.Options[optionScope]; ok {
+ scope = widget.Options[optionScope]
}
- t := strings.Split(sd, "_")
- sw, err := strconv.ParseInt(t[0], 0, 0)
- if err != nil {
- return errors.Wrapf(err, "%s is not a valid date", sd)
+ if !strings.Contains(sd, "weeks_ago") || !strings.Contains(ed, "weeks_ago") {
+ return errors.New("The widget github.bar_commits require you to indicate a week range, ie startDate: 5_weeks_ago, endDate: 1_weeks_ago ")
}
- if !strings.Contains(ed, "weeks_ago") {
- return errors.New("the widget bar_commits require you to indicate a week range, ie startDate: 5_weeks_ago, endDate: 0_weeks_ago ")
+ sw, err := plateform.ExtractCountPeriod(sd)
+ if err != nil {
+ return err
}
- t = strings.Split(ed, "_")
- ew, err := strconv.ParseInt(t[0], 0, 0)
+ ew, err := plateform.ExtractCountPeriod(ed)
if err != nil {
- return errors.Wrapf(err, "%s is not a valid date", ed)
+ return err
}
- dim, counts, err := g.client.CommitCounts(repo, sw, ew, time.Now())
+ dim, counts, err := g.client.CommitCounts(repo, scope, sw, ew, time.Now())
if err != nil {
return err
}
diff --git a/internal/monitor_widget.go b/internal/monitor_widget.go
index deacba6..ec9c7d1 100644
--- a/internal/monitor_widget.go
+++ b/internal/monitor_widget.go
@@ -87,8 +87,9 @@ func (m *monitorWidget) availabilityWidget(widget Widget) error {
statusCode = res.StatusCode
}
status = "offline"
+ } else {
+ defer res.Body.Close()
}
- defer res.Body.Close()
title := " Availability "
if _, ok := widget.Options[optionTitle]; ok {
diff --git a/internal/plateform/date.go b/internal/plateform/date.go
index 5608d66..7ce5157 100644
--- a/internal/plateform/date.go
+++ b/internal/plateform/date.go
@@ -56,10 +56,9 @@ func convertStartDate(base time.Time, startDate string) (time.Time, error) {
}
if strings.Contains(startDate, daysAgo) {
- t := strings.Split(startDate, "_")
- days, err := strconv.ParseInt(t[0], 0, 0)
+ days, err := ExtractCountPeriod(startDate)
if err != nil {
- return time.Time{}, errors.Wrapf(err, "%s is not a valid date", startDate)
+ return time.Time{}, err
}
return base.AddDate(0, 0, -int(days)), nil
@@ -71,10 +70,9 @@ func convertStartDate(base time.Time, startDate string) (time.Time, error) {
}
if strings.Contains(startDate, weeksAgo) {
- t := strings.Split(startDate, "_")
- weeks, err := strconv.ParseInt(t[0], 0, 0)
+ weeks, err := ExtractCountPeriod(startDate)
if err != nil {
- return time.Time{}, errors.Wrapf(err, "%s is not a valid date", startDate)
+ return time.Time{}, err
}
startDate, _ := totime.PrevWeeks(base, int(weeks))
@@ -88,10 +86,9 @@ func convertStartDate(base time.Time, startDate string) (time.Time, error) {
}
if strings.Contains(startDate, monthsAgo) {
- t := strings.Split(startDate, "_")
- months, err := strconv.ParseInt(t[0], 0, 0)
+ months, err := ExtractCountPeriod(startDate)
if err != nil {
- return time.Time{}, errors.Wrapf(err, "%s is not a valid date", startDate)
+ return time.Time{}, err
}
startDate, _ := totime.PrevMonths(base, int(months))
@@ -105,8 +102,7 @@ func convertStartDate(base time.Time, startDate string) (time.Time, error) {
}
if strings.Contains(startDate, yearsAgo) {
- t := strings.Split(startDate, "_")
- years, err := strconv.ParseInt(t[0], 0, 0)
+ years, err := ExtractCountPeriod(startDate)
if err != nil {
return time.Time{}, errors.Wrapf(err, "%s is not a valid date", startDate)
}
@@ -125,10 +121,9 @@ func convertEndDate(base time.Time, endDate string) (time.Time, error) {
}
if strings.Contains(endDate, daysAgo) {
- t := strings.Split(endDate, "_")
- days, err := strconv.ParseInt(t[0], 0, 0)
+ days, err := ExtractCountPeriod(endDate)
if err != nil {
- return time.Time{}, errors.Wrapf(err, "%s is not a valid date", endDate)
+ return time.Time{}, err
}
return base.AddDate(0, 0, -int(days)), nil
@@ -140,10 +135,9 @@ func convertEndDate(base time.Time, endDate string) (time.Time, error) {
}
if strings.Contains(endDate, weeksAgo) {
- t := strings.Split(endDate, "_")
- weeks, err := strconv.ParseInt(t[0], 0, 0)
+ weeks, err := ExtractCountPeriod(endDate)
if err != nil {
- return time.Time{}, errors.Wrapf(err, "%s is not a valid date", endDate)
+ return time.Time{}, err
}
_, endDate := totime.PrevWeeks(base, int(weeks))
@@ -157,10 +151,9 @@ func convertEndDate(base time.Time, endDate string) (time.Time, error) {
}
if strings.Contains(endDate, monthsAgo) {
- t := strings.Split(endDate, "_")
- months, err := strconv.ParseInt(t[0], 0, 0)
+ months, err := ExtractCountPeriod(endDate)
if err != nil {
- return time.Time{}, errors.Wrapf(err, "%s is not a valid date", endDate)
+ return time.Time{}, err
}
_, endDate := totime.PrevMonths(base, int(months))
@@ -174,10 +167,9 @@ func convertEndDate(base time.Time, endDate string) (time.Time, error) {
}
if strings.Contains(endDate, yearsAgo) {
- t := strings.Split(endDate, "_")
- years, err := strconv.ParseInt(t[0], 0, 0)
+ years, err := ExtractCountPeriod(endDate)
if err != nil {
- return time.Time{}, errors.Wrapf(err, "%s is not a valid date", endDate)
+ return time.Time{}, err
}
endDate, _ := totime.PrevYears(base, int(years))
@@ -208,6 +200,16 @@ func resolveAlias(date string) string {
return date
}
+func ExtractCountPeriod(period string) (int64, error) {
+ t := strings.Split(period, "_")
+ p, err := strconv.ParseInt(t[0], 0, 0)
+ if err != nil {
+ return 0, errors.Wrapf(err, "%v is from %s not a valid number", t[0], period)
+ }
+
+ return p, nil
+}
+
// missingDays between two dates.
// Example: start 2019-01-01, end 2019-01-03, return 2019-01-02.
func missingDays(start time.Time, end time.Time) []time.Time {
diff --git a/internal/plateform/github.go b/internal/plateform/github.go
index cd8cf0e..e8cdf06 100644
--- a/internal/plateform/github.go
+++ b/internal/plateform/github.go
@@ -20,6 +20,9 @@ const (
githubRepoWatchers = "watchers"
githubRepoForks = "forks"
githubRepoOpenIssues = "open_issues"
+
+ githubScopeOwner = "owner"
+ githubScopeAll = "all"
)
type Github struct {
@@ -275,44 +278,52 @@ func (g *Github) Views(repository string, days int) ([]string, []int, error) {
}
// TODO rename countCommits
-func (g *Github) CommitCounts(repository string, startWeek int64, endWeek int64, startDate time.Time) ([]string, []int, error) {
+func (g *Github) CommitCounts(
+ repository string,
+ scope string,
+ startWeek int64,
+ endWeek int64,
+ startDate time.Time,
+) ([]string, []int, error) {
c, err := g.fetchCommitCount(repository)
if err != nil {
return nil, nil, err
}
- d, co := formatCommitCounts(c, startWeek, endWeek, startDate)
+ cm := c.Owner
+ if scope == githubScopeAll {
+ cm = c.All
+ }
+
+ d, co := formatCountCommits(cm, startWeek, endWeek, startDate)
return d, co, nil
}
-// TODO rename formatCountCommits
-func formatCommitCounts(
- c *github.RepositoryParticipation,
+func formatCountCommits(
+ c []int,
startWeek int64,
endWeek int64,
startDate time.Time,
) ([]string, []int) {
// Reverse the count of commits (from ASC to DESC).
- for i := len(c.Owner)/2 - 1; i >= 0; i-- {
- opp := len(c.Owner) - 1 - i
- c.Owner[i], c.Owner[opp] = c.Owner[opp], c.Owner[i]
+ for i := len(c)/2 - 1; i >= 0; i-- {
+ opp := len(c) - 1 - i
+ c[i], c[opp] = c[opp], c[i]
}
counts := []int{}
- for _, v := range c.Owner {
+ for _, v := range c {
counts = append(counts, v)
}
dimension := []string{}
- for k, _ := range c.Owner {
- // Since the startDate is the end of the week,
- // we need to come back to the first day of it
- // and then go back to the number of week (7 days)
- // specified in start date.
- weekDay := int(startDate.Weekday())
- s := startDate.AddDate(0, 0, (-(weekDay) - (7 * k)))
- if weekDay == 0 && k == 0 {
+ for k, _ := range c {
+ startWeekDay := int(startDate.Weekday())
+ beginningOfWeek := -(startWeekDay)
+ weekBefore := (7 * k)
+ s := startDate.AddDate(0, 0, (beginningOfWeek - weekBefore))
+ if startWeekDay == int(time.Sunday) && k == 0 {
s = startDate
}
dimension = append(dimension, s.Format("01-02"))
diff --git a/internal/plateform/github_perso.go b/internal/plateform/github_perso.go
deleted file mode 100644
index 457c61f..0000000
--- a/internal/plateform/github_perso.go
+++ /dev/null
@@ -1,4 +0,0 @@
-// Implementation of a custom client Github client
-// to get the data not available via the go-github client.
-
-package plateform
diff --git a/internal/plateform/github_test.go b/internal/plateform/github_test.go
index da037a6..9f7f51c 100644
--- a/internal/plateform/github_test.go
+++ b/internal/plateform/github_test.go
@@ -59,6 +59,7 @@ func Test_formatCommitCount(t *testing.T) {
startWeek int64
endWeek int64
startDate time.Time
+ scope string
}{
{
name: "format commit counts 6_weeks_ago to today",
@@ -68,6 +69,7 @@ func Test_formatCommitCount(t *testing.T) {
startWeek: 6, // 6_weeks_ago
endWeek: 0, // today
startDate: time.Date(2019, 06, 30, 00, 00, 00, 00, time.UTC), // Sunday 30 June 2019
+ scope: githubScopeOwner,
},
{
name: "format commit counts 6_weeks_ago to today, beginning by Thursday",
@@ -77,6 +79,7 @@ func Test_formatCommitCount(t *testing.T) {
startWeek: 6, // 6_weeks_ago
endWeek: 0, // today
startDate: time.Date(2019, 06, 27, 00, 00, 00, 00, time.UTC), // Thursday 27 June 2019
+ scope: githubScopeOwner,
},
{
name: "format commit counts 5_weeks_ago to 1_weeks_ago",
@@ -86,6 +89,7 @@ func Test_formatCommitCount(t *testing.T) {
startWeek: 5, // 5_weeks_ago
endWeek: 1, // 1_weeks_ago
startDate: time.Date(2019, 06, 30, 00, 00, 00, 00, time.UTC), // Sunday 30 June 2019
+ scope: githubScopeOwner,
},
}
@@ -98,7 +102,12 @@ func Test_formatCommitCount(t *testing.T) {
t.Error(err)
}
- dim, val := formatCommitCounts(part, tc.startWeek, tc.endWeek, tc.startDate)
+ p := part.Owner
+ if tc.scope == githubScopeAll {
+ p = part.All
+ }
+
+ dim, val := formatCountCommits(p, tc.startWeek, tc.endWeek, tc.startDate)
if !reflect.DeepEqual(dim, tc.expectedDim) {
t.Errorf("Expected %v, actual %v", tc.expectedDim, dim)
diff --git a/internal/widget.go b/internal/widget.go
index 076b27c..c1d4ad1 100644
--- a/internal/widget.go
+++ b/internal/widget.go
@@ -33,6 +33,12 @@ const (
// Repository
optionRepository = "repository"
+
+ // Owner / all
+ optionScope = ownerScope
+
+ ownerScope = "owner"
+ // allScope = "all"
)
type Widget struct {
diff --git a/totime/totime.go b/totime/totime.go
index e84ab0a..7245f5c 100644
--- a/totime/totime.go
+++ b/totime/totime.go
@@ -15,10 +15,9 @@ import (
func ThisWeek(base time.Time) (startDate time.Time, endDate time.Time) {
startDate = time.Time{}
- // 1 = Monday
weekDay := int(base.Weekday())
startDate = base.AddDate(0, 0, -(weekDay - 1))
- if weekDay == 0 {
+ if weekDay == int(time.Sunday) {
startDate = base.AddDate(0, 0, -(weekDay + 1))
}
@@ -33,7 +32,7 @@ func PrevWeeks(base time.Time, count int) (startDate time.Time, endDate time.Tim
// 1 = Monday
weekDay := int(base.Weekday())
startDate = base.AddDate(0, 0, (-(weekDay - 1) - (7 * count)))
- if weekDay == 0 {
+ if weekDay == int(time.Sunday) {
startDate = base.AddDate(0, 0, (-(weekDay + 1) - (7 * count)))
}
@@ -46,7 +45,7 @@ func NextWeeks(base time.Time, count int) (startDate time.Time, endDate time.Tim
// 1 = Monday
weekDay := int(base.Weekday())
startDate = base.AddDate(0, 0, (-(weekDay - 1) + (7 * count)))
- if weekDay == 0 {
+ if weekDay == int(time.Sunday) {
startDate = base.AddDate(0, 0, (-(weekDay + 1) + (7 * count)))
}