diff --git a/config.toml.dist b/config.toml.dist index 67a4a9f..290bbd2 100644 --- a/config.toml.dist +++ b/config.toml.dist @@ -1,7 +1,17 @@ # begin time of statistics period. -# for merged PRs, we only analysis those being merged after begin time. -# for open PRs, we only analysis those being created after begin time. +# for merged PRs, we only analysis those being merged after begin time (begin time included). +# for open PRs, we only analysis those being created after begin time(begin time included). +# if begin time not specified, it defaults to create time(merge time) of the very first PR. +# this usually will result in listing all PRs of specified repository and slow analysis process. statBeginTime = 2016-10-01T00:00:00 + +# end time of statistics period. +# for merged PRs, we only analysis those being merged before end time (end time excluded). +# for open PRs, we only analysis those being created after end time(end time excluded). +# if end time not specified, it defaults to current time. +# if end time is specified, week statistics is disabled. +statEndTime = 2016-10-01T00:00:00 + accessToken = "personal access token" # repositories in which Pull Requests / Commits are analyzed diff --git a/githubstat/load.go b/githubstat/load.go index e941d83..22f6ece 100644 --- a/githubstat/load.go +++ b/githubstat/load.go @@ -1,7 +1,6 @@ package githubstat import ( - "fmt" "time" "github.com/BurntSushi/toml" @@ -23,6 +22,7 @@ type User struct { } type Configuration struct { StatBeginTime time.Time + StatEndTime time.Time AccessToken string Users []User Repos []string @@ -47,12 +47,12 @@ func getWeekFirstDay(t time.Time) time.Time { } // read config file -var _ = func() int { +func init() { if _, err := toml.DecodeFile("./config.toml", &Config); err != nil { panic(err) } + if !Config.StatEndTime.After(Config.StatBeginTime) { + panic("stat end time must be after stat begin time") + } Config.ThisWeekFirstDay = getWeekFirstDay(time.Now()) - fmt.Printf("this week first day is : %v\n", Config.ThisWeekFirstDay) - fmt.Printf("stat begin time is : %v\n", Config.StatBeginTime) - return 0 -}() +} diff --git a/githubstat/metrics_pull_request.go b/githubstat/metrics_pull_request.go index 622c6e1..b7878fa 100644 --- a/githubstat/metrics_pull_request.go +++ b/githubstat/metrics_pull_request.go @@ -33,6 +33,10 @@ func (w *WeekPullRequestMetrics) merge() { } func (w *WeekPullRequestMetrics) Show() { + if !Config.StatEndTime.IsZero() { + fmt.Println("Week statistics is disabled because statEndTime is specified") + return + } w.merge() data := [][]string{} var totalMerged int @@ -55,7 +59,7 @@ func (w *WeekPullRequestMetrics) Show() { } if len(data) != 0 { table := tablewriter.NewWriter(os.Stdout) - fmt.Print("\nStatistics Per Week\n") + fmt.Printf("\nStatistics for this Week ( week first day : %v)\n", Config.ThisWeekFirstDay) table.SetHeader([]string{"User Name", "Merged PRs", "Merged Commits", "LGTM'ed PRs", "NonLGTM'ed PRs", "Created PRs"}) table.AppendBulk(data) @@ -117,7 +121,7 @@ func (m *OverallPullRequestMetrics) Show() { } if len(data) != 0 { table := tablewriter.NewWriter(os.Stdout) - fmt.Print("\nOverall Statistics\n") + fmt.Printf("\nOverall Statistics ( %v ~ %v)\n", Config.StatBeginTime, Config.StatEndTime) mergedCommitsHeader := "Merged Commits(actual/stack)" table.SetHeader([]string{"User Name", "Merged PRs", mergedCommitsHeader, "LGTM'ed PRs", "NonLGTM'ed PRs"}) table.AppendBulk(data) @@ -238,6 +242,9 @@ loop: fmt.Printf("page:%d fin\n", page) for _, pr := range prs { t := pr.CreatedAt + if !Config.StatEndTime.IsZero() && !t.Before(Config.StatEndTime) { + continue + } if !t.Before(Config.StatBeginTime) { allPRs = append(allPRs, pr) } else { @@ -274,17 +281,19 @@ loop: fmt.Printf("page:%d fin\n", page) for _, pr := range prs { + if pr.MergedAt == nil { continue } t := pr.UpdatedAt - /* MergedAt is always before UpdatedAt, so if a PR is updated before stat begin time, this PR is absolutely merged before stat begin time. + WARNING: UpdatedAt is sorted descendingly, but MergedAt is not. so we can break outer loop according to MergedAt */ if !t.Before(Config.StatBeginTime) { - if !pr.MergedAt.Before(Config.StatBeginTime) { + + if !pr.MergedAt.Before(Config.StatBeginTime) && !Config.StatEndTime.IsZero() && pr.MergedAt.Before(Config.StatEndTime) { allPRs = append(allPRs, pr) }