Skip to content

Commit

Permalink
add stat end time support
Browse files Browse the repository at this point in the history
  • Loading branch information
bruceauyeung committed Dec 28, 2016
1 parent 23b29bd commit b710819
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
14 changes: 12 additions & 2 deletions config.toml.dist
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 6 additions & 6 deletions githubstat/load.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package githubstat

import (
"fmt"
"time"

"github.com/BurntSushi/toml"
Expand All @@ -23,6 +22,7 @@ type User struct {
}
type Configuration struct {
StatBeginTime time.Time
StatEndTime time.Time
AccessToken string
Users []User
Repos []string
Expand All @@ -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
}()
}
17 changes: 13 additions & 4 deletions githubstat/metrics_pull_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}

Expand Down

0 comments on commit b710819

Please sign in to comment.