浏览代码

Improve the granularity selection

Vadim Markovtsev 8 年之前
父节点
当前提交
fc9ceecb6d
共有 2 个文件被更改,包括 40 次插入27 次删除
  1. 24 11
      analyser.go
  2. 16 16
      cmd/hercules/main.go

+ 24 - 11
analyser.go

@@ -15,6 +15,7 @@ import (
 
 type Analyser struct {
 	Repository *git.Repository
+	Granularity int
 	OnProgress func(int)
 }
 
@@ -170,7 +171,25 @@ func (analyser *Analyser) commits() []*git.Commit {
 	return result
 }
 
-func (analyser *Analyser) Analyse() ([]map[int]int64, int) {
+func (analyser *Analyser) groupStatus(status map[int]int64, day int) []int64 {
+  granularity := analyser.Granularity
+  result := make([]int64, day / granularity)
+  var group int64
+  for i := 0; i < day; i++ {
+	  group += status[i]
+    if i % granularity == (granularity - 1) {
+	    result[i / granularity] = group
+	    group = 0
+    }
+  }
+	return result
+}
+
+func (analyser *Analyser) Analyse() [][]int64 {
+	granularity := analyser.Granularity
+	if granularity == 0 {
+		granularity = 1
+	}
 	onProgress := analyser.OnProgress
 	if onProgress == nil {
 		onProgress = func(int) {}
@@ -180,7 +199,7 @@ func (analyser *Analyser) Analyse() ([]map[int]int64, int) {
 	// beginning of the history
 	status := map[int]int64{}
 	// weekly snapshots of status
-	statuses := []map[int]int64{}
+	statuses := [][]int64{}
 	// mapping <file path> -> hercules.File
 	files := map[string]*File{}
 	// list of commits belonging to the default branch, from oldest to newest
@@ -218,16 +237,10 @@ func (analyser *Analyser) Analyse() ([]map[int]int64, int) {
 			}()
 		} else {
 			day := int(commit.Author.When.Sub(day0).Hours() / 24)
-			delta := (day / 7) - (prev_day / 7)
+			delta := (day / granularity) - (prev_day / granularity)
 			if delta > 0 {
 				prev_day = day
-				status_copy := map[int]int64{}
-				for k, v := range status {
-					status_copy[k] = v
-				}
-				for i := 0; i < delta; i++ {
-					statuses = append(statuses, status_copy)
-				}
+				statuses = append(statuses, analyser.groupStatus(status, day))
 			}
 			tree_diff, err := git.DiffTree(prev_tree, tree)
 			if err != nil {
@@ -248,5 +261,5 @@ func (analyser *Analyser) Analyse() ([]map[int]int64, int) {
 		}
 		prev_tree = tree
 	}
-	return statuses, prev_day
+	return statuses
 }

+ 16 - 16
cmd/hercules/main.go

@@ -14,8 +14,14 @@ import (
 
 func main() {
 	var profile bool
+	var granularity int
 	flag.BoolVar(&profile, "profile", false, "Collect the profile to hercules.pprof.")
+	flag.IntVar(&granularity, "granularity", 30, "Report granularity in days.")
 	flag.Parse()
+	if (granularity <= 0) {
+		fmt.Fprint(os.Stderr, "Warning: adjusted the granularity to 1 day\n")
+		granularity = 1
+	}
 	if profile {
 		prof, _ := os.Create("profile")
 		pprof.StartCPUProfile(prof)
@@ -61,8 +67,9 @@ func main() {
 		OnProgress: func(commit int) {
 		  fmt.Fprintf(os.Stderr, "%d\r", commit)
 	  },
+		Granularity: granularity,
 	}
-	statuses, last := analyser.Analyse()
+	statuses := analyser.Analyse()
 	fmt.Fprint(os.Stderr, "        \r")
 	if len(statuses) == 0 {
 		return
@@ -70,29 +77,22 @@ func main() {
 	// determine the maximum length of each value
 	var maxnum int64
 	for _, status := range statuses {
-		var week int64
-		for i := 0; i < last; i++ {
-			val, _ := status[i]
-			week += val
-			if i % 7 == 6 {
-				if week > maxnum {
-					maxnum = week;
-				}
-				week = 0
+		for _, val := range status {
+			if val > maxnum {
+				maxnum = val
 			}
 		}
 	}
 	width := len(strconv.FormatInt(maxnum, 10))
+	last := len(statuses[len(statuses) - 1])
 	// print the resulting triangle matrix
 	for _, status := range statuses {
-		var week int64
 		for i := 0; i < last; i++ {
-			val, _ := status[i]
-			week += val
-			if i % 7 == 6 {
-				fmt.Printf("%[1]*[2]d ", width, week)
-				week = 0
+			var val int64
+			if i < len(status) {
+				val = status[i]
 			}
+			fmt.Printf("%[1]*[2]d ", width, val)
 		}
 		println()
 	}