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

Commit 0c24790

Browse files
committed
update user analysis
1 parent b59ae99 commit 0c24790

3 files changed

Lines changed: 70 additions & 26 deletions

File tree

api.go

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,45 @@ func GetRepoExist(username string, repo string) bool {
4242
return !(ok && val == "Not Found")
4343
}
4444

45+
func iterRepos(username string) (data []interface{}, err error) {
46+
repos, err := GetRepos(username)
47+
if err != nil {
48+
logger.Error(err)
49+
return nil, err
50+
}
51+
52+
var res []interface{}
53+
for _, repo := range repos {
54+
repo := repo.(map[string]interface{})
55+
if !repo["fork"].(bool) {
56+
res = append(res, repo)
57+
}
58+
}
59+
return res, nil
60+
}
61+
4562
func CollectLanguages(username string, repos []string) (data map[string]int, err error) {
4663
data = make(map[string]int)
64+
channel := make(chan map[string]interface{}, len(repos))
65+
4766
for _, repo := range repos {
48-
languages, err := GetLanguages(username, repo)
49-
if err != nil {
50-
return data, err
51-
}
52-
for k, v := range languages {
53-
data[k] += v.(int)
67+
go func(username string, repo string) {
68+
languages, err := GetLanguages(username, repo)
69+
if err != nil {
70+
return
71+
}
72+
channel <- languages
73+
}(username, repo)
74+
}
75+
76+
for range repos {
77+
select {
78+
case languages := <-channel:
79+
for k, v := range languages {
80+
data[k] += v.(int)
81+
}
5482
}
5583
}
84+
5685
return data, nil
5786
}

main.go

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
package main
22

33
import (
4-
"fmt"
54
"github.com/kataras/iris/v12"
65
"github.com/sirupsen/logrus"
76
)
87

98
func main() {
10-
app := iris.New()
11-
route := app.Party("/api")
9+
app := iris.Default()
1210
{
13-
route.Use(iris.Compression)
14-
route.Get("/user/{username:string}", AnalyseUser)
15-
route.Get("/repo/{username:string}/{repo:string}", AnalyseRepo)
11+
app.Get("/user/{username:string}", AnalyseUser)
12+
app.Get("/repo/{username:string}/{repo:string}", AnalyseRepo)
1613
}
14+
1715
logger.SetLevel(logrus.DebugLevel)
1816
logger.SetFormatter(&Formatter{})
1917

@@ -28,19 +26,28 @@ func AnalyseUser(ctx iris.Context) {
2826
if GetUserExist(username) {
2927
res, err := GetUser(username)
3028
if err != nil {
31-
ctx.StatusCode(iris.StatusInternalServerError)
32-
ctx.JSON(iris.Map{
33-
"message": err.Error(),
34-
})
29+
ThrowError(ctx, iris.StatusInternalServerError, err.Error())
30+
return
31+
}
32+
repo, err := iterRepos(username)
33+
if err != nil {
34+
ThrowError(ctx, iris.StatusInternalServerError, err.Error())
3535
return
3636
}
37-
fmt.Println(res)
38-
ctx.JSON(iris.Map{})
37+
ctx.JSON(iris.Map{
38+
"username": username,
39+
"location": res["location"],
40+
"org": res["type"] != "User",
41+
"repos": res["public_repos"],
42+
"follower": ScaleConvert(res["followers"].(float64), true),
43+
"stars": ScaleConvert(Sum(repo, "stargazers_count"), true),
44+
"forks": ScaleConvert(Sum(repo, "forks_count"), true),
45+
"issues": ScaleConvert(Sum(repo, "open_issues_count"), true),
46+
"watchers": ScaleConvert(Sum(repo, "watchers_count"), true),
47+
})
48+
return
3949
}
40-
ctx.StatusCode(iris.StatusNotFound)
41-
ctx.JSON(iris.Map{
42-
"message": "User not found.",
43-
})
50+
ThrowError(ctx, iris.StatusNotFound, "User not found.")
4451
}
4552

4653
func AnalyseRepo(ctx iris.Context) {

utils.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@ package main
33
import (
44
"encoding/json"
55
"fmt"
6+
"github.com/kataras/iris/v12"
67
"github.com/sirupsen/logrus"
78
"io"
89
"net/http"
910
)
1011

11-
func Sum(array []int) int {
12-
total := 0
13-
for _, v := range array {
14-
total += v
12+
func Sum(m []interface{}, k string) float64 {
13+
total := 0.
14+
for _, v := range m {
15+
total += v.(map[string]interface{})[k].(float64)
1516
}
1617
return total
1718
}
@@ -73,3 +74,10 @@ func Get(uri string, ptr interface{}) (err error) {
7374
}
7475
return nil
7576
}
77+
78+
func ThrowError(ctx iris.Context, code int, message string) {
79+
ctx.StatusCode(code)
80+
ctx.JSON(iris.Map{
81+
"message": message,
82+
})
83+
}

0 commit comments

Comments
 (0)