forked from tcnksm/ghr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stat.go
94 lines (73 loc) · 1.65 KB
/
stat.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"fmt"
"io"
)
type Stat struct {
// TagName is relase tag
TagName string
// DownloadCount is download number of its release
DownloadCount int
// Best is marked when it's most downloaded release
Best bool
// MaxLength
MaxLength int
}
// ShowStat displays dowload counts of latest releases.
func ShowStat(outStream io.Writer, apiOpts *GitHubAPIOpts) (err error) {
// Get Statical infomation
stats, err := GetStat(apiOpts)
if err != nil {
return err
}
// Find most donwloaded release
MarkBest(stats)
// Display all stats
for _, s := range stats {
msg := fmt.Sprintf("%-20s: %4d downloads\n", s.TagName, s.DownloadCount)
if s.Best {
fmt.Fprintf(outStream, Hot(msg))
continue
}
fmt.Fprintf(outStream, msg)
}
return
}
// GetStat gets download counts of all releases
func GetStat(apiOpts *GitHubAPIOpts) (stats []*Stat, err error) {
// Create client
client := NewOAuthedClient(apiOpts)
// Get All releases
releases, res, err := client.Repositories.ListReleases(apiOpts.OwnerName, apiOpts.RepoName, nil)
if err != nil {
return stats, err
}
err = CheckStatusOK(res)
if err != nil {
return stats, err
}
for _, r := range releases {
var count int
for _, a := range r.Assets {
count += *a.DownloadCount
}
stats = append(stats, &Stat{TagName: *r.TagName, DownloadCount: count})
}
return stats, nil
}
// MarkBest marks most donloaded release
func MarkBest(stats []*Stat) {
// Find most max download num
var max int
for _, s := range stats {
if s.DownloadCount > max {
max = s.DownloadCount
}
}
// Mark best
for _, s := range stats {
if s.DownloadCount >= max {
s.Best = true
}
}
}