/
statistic.go
66 lines (54 loc) · 1.48 KB
/
statistic.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package model
import "fmt"
// Statistic count events we needed
type Statistic struct {
PROpened uint
PRClosed uint
IssueOpened uint
IssueClosed uint
}
type Statistics []Statistic
func (s Statistic) String() string {
return fmt.Sprintf("PR open: %d, close: %d; Issue open: %d, close: %d", s.PROpened, s.PRClosed, s.IssueOpened, s.IssueClosed)
}
// FormatPrint format statistic as print needed
func (s Statistic) FormatPrint() string {
return fmt.Sprintf(`
## Weekly Stats
| | Opened this week | Closed this week |
| ---- | ---- | ---- |
| Issues | %d | %d |
| PR's | %d | %d |
`, s.IssueOpened, s.IssueClosed, s.PROpened, s.PRClosed)
}
// CountPROpen add PROpened counter
func (s *Statistic) CountPROpen() {
s.PROpened++
}
// CountPRClose add PRClosed counter
func (s *Statistic) CountPRClose() {
s.PRClosed++
}
// CountIssueOpen add IssueOpened counter
func (s *Statistic) CountIssueOpen() {
s.IssueOpened++
}
// CountIssueClose add IssueClosed counter
func (s *Statistic) CountIssueClose() {
s.IssueClosed++
}
// IsBlank check whether the statistic is blank (each field equals 0)
func (s *Statistic) IsBlank() bool {
return s.PROpened+s.PRClosed+s.IssueOpened+s.IssueClosed == 0
}
// Sum all statistics and return the final result as a Statistic
func (s Statistics) Sum() Statistic {
res := Statistic{}
for _, stat := range s {
res.PROpened += stat.PROpened
res.PRClosed += stat.PRClosed
res.IssueOpened += stat.IssueOpened
res.IssueClosed += stat.IssueClosed
}
return res
}