Skip to content
This repository was archived by the owner on May 11, 2025. It is now read-only.

Commit 1b096e1

Browse files
committed
add language analysis
1 parent e226312 commit 1b096e1

2 files changed

Lines changed: 22 additions & 10 deletions

File tree

api.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func GetRepo(username string, repo string) (data map[string]interface{}, err err
2121
return data, err
2222
}
2323

24-
func GetLanguages(username string, repo string) (data map[string]interface{}, err error) {
24+
func GetLanguages(username string, repo string) (data map[string]float64, err error) {
2525
err = Get(fmt.Sprintf("repos/%s/%s/languages", username, repo), &data)
2626
return data, err
2727
}
@@ -70,7 +70,7 @@ func iterRepos(username string) (data []interface{}, err error) {
7070

7171
func CollectLanguages(username string, repos []interface{}) (data map[string]float64, err error) {
7272
data = make(map[string]float64)
73-
channel := make(chan map[string]interface{}, len(repos))
73+
channel := make(chan map[string]float64, len(repos))
7474

7575
for _, repo := range repos {
7676
go func(username string, repo string) {
@@ -86,7 +86,7 @@ func CollectLanguages(username string, repos []interface{}) (data map[string]flo
8686
select {
8787
case languages := <-channel:
8888
for k, v := range languages {
89-
data[k] += v.(float64)
89+
data[k] += v
9090
}
9191
}
9292
}
@@ -118,7 +118,7 @@ func AnalysisUser(username string) (data iris.Map, err error, code int) {
118118
"forks": ScaleConvert(Sum(repos, "forks_count"), true),
119119
"issues": ScaleConvert(Sum(repos, "open_issues_count"), true),
120120
"watchers": ScaleConvert(Sum(repos, "watchers_count"), true),
121-
"languages": languages,
121+
"languages": CountLanguages(languages),
122122
}, nil, iris.StatusOK
123123
}
124124
return nil, errors.New("user not found"), iris.StatusNotFound
@@ -144,7 +144,7 @@ func AnalysisRepo(username string, repo string) (data iris.Map, err error, code
144144
"issues": ScaleConvert(res["open_issues_count"].(float64), false),
145145
"color": GetColor(res["language"]),
146146
"license": getLicense(res["license"]),
147-
"languages": languages,
147+
"languages": CountLanguages(languages),
148148
}, nil, iris.StatusOK
149149
}
150150
return nil, errors.New("repo not found"), iris.StatusNotFound

utils.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ func ScaleConvert(n float64, useSmallScale bool) string {
6262
return fmt.Sprintf("%.1f%s", n, scaleUnits[idx])
6363
}
6464

65+
func ThrowError(ctx iris.Context, message string, code int) {
66+
ctx.StatusCode(code)
67+
ctx.JSON(iris.Map{
68+
"message": message,
69+
})
70+
}
71+
6572
func Get(uri string, ptr interface{}) (err error) {
6673
req, err := http.NewRequest("GET", "https://api.github.com/"+uri, nil)
6774
if err != nil {
@@ -89,9 +96,14 @@ func Get(uri string, ptr interface{}) (err error) {
8996
return nil
9097
}
9198

92-
func ThrowError(ctx iris.Context, message string, code int) {
93-
ctx.StatusCode(code)
94-
ctx.JSON(iris.Map{
95-
"message": message,
96-
})
99+
func CountLanguages(languages map[string]float64) map[string]string {
100+
total := 0.
101+
res := map[string]string{}
102+
for _, v := range languages {
103+
total += v
104+
}
105+
for k, v := range languages {
106+
res[k] = fmt.Sprintf("%.0f%% (%s)", v/total*100, ScaleConvert(v, false))
107+
}
108+
return res
97109
}

0 commit comments

Comments
 (0)