Skip to content

Commit 9c5db85

Browse files
author
Lauri Piispanen
committed
output total number of users
1 parent 2da1ee8 commit 9c5db85

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

github/github.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,15 @@ func (client HTTPGithubClient) User(login string) (User, error) {
6060
return user, nil
6161
}
6262

63-
func (client HTTPGithubClient) SearchUsers(query UserSearchQuery) ([]User, error) {
63+
func (client HTTPGithubClient) SearchUsers(query UserSearchQuery) (GithubSearchResults, error) {
6464
users := []User{}
6565
userLogins := map[string]bool{}
6666

6767
totalCount := 0
6868
minFollowerCount := -1
6969
maxPerQuery := 1000
7070
perPage := 5
71+
totalUsersCount := 0
7172

7273
Pages:
7374
for totalCount < query.MaxUsers {
@@ -83,6 +84,7 @@ Pages:
8384
}
8485
graphQlString := fmt.Sprintf(`{ "query": "query {
8586
search(type: USER, query:\"%s%s sort:%s-%s\", first: %d%s) {
87+
userCount
8688
edges {
8789
node {
8890
__typename
@@ -130,6 +132,7 @@ Pages:
130132
}
131133
dataNode := rootNode["data"].(map[string]interface{})
132134
searchNode := dataNode["search"].(map[string]interface{})
135+
totalUsersCount = int(searchNode["userCount"].(float64))
133136
edgeNodes := searchNode["edges"].([]interface{})
134137

135138
if len(edgeNodes) == 0 {
@@ -184,7 +187,10 @@ Pages:
184187
}
185188
}
186189

187-
return users, nil
190+
return GithubSearchResults{
191+
Users: users,
192+
MinimumFollowerCount: minFollowerCount,
193+
TotalUserCount: totalUsersCount}, nil
188194
}
189195

190196
func strPropOrEmpty(obj map[string]interface{}, prop string) string {
@@ -245,3 +251,9 @@ type UserSearchQuery struct {
245251
Order string
246252
MaxUsers int
247253
}
254+
255+
type GithubSearchResults struct {
256+
Users []User
257+
MinimumFollowerCount int
258+
TotalUserCount int
259+
}

output/output.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ import (
1515
"github.com/lauripiispanen/most-active-github-users-counter/top"
1616
)
1717

18-
type Format func(users GithubUserList, writer io.Writer, options top.Options) error
18+
type Format func(results github.GithubSearchResults, writer io.Writer, options top.Options) error
1919

20-
func PlainOutput(users GithubUserList, writer io.Writer, options top.Options) error {
20+
func PlainOutput(results github.GithubSearchResults, writer io.Writer, options top.Options) error {
21+
users := GithubUserList(results.Users)
2122
fmt.Fprintln(writer, "USERS\n--------")
2223
for i, user := range users {
2324
fmt.Fprintf(writer, "#%+v: %+v (%+v):%+v (%+v) %+v\n", i+1, user.Name, user.Login, user.ContributionCount, user.Company, strings.Join(user.Organizations, ","))
@@ -29,7 +30,8 @@ func PlainOutput(users GithubUserList, writer io.Writer, options top.Options) er
2930
return nil
3031
}
3132

32-
func CsvOutput(users GithubUserList, writer io.Writer, options top.Options) error {
33+
func CsvOutput(results github.GithubSearchResults, writer io.Writer, options top.Options) error {
34+
users := GithubUserList(results.Users)
3335
w := csv.NewWriter(writer)
3436
if err := w.Write([]string{"rank", "name", "login", "contributions", "company", "organizations"}); err != nil {
3537
return err
@@ -49,7 +51,8 @@ func CsvOutput(users GithubUserList, writer io.Writer, options top.Options) erro
4951
return nil
5052
}
5153

52-
func YamlOutput(users GithubUserList, writer io.Writer, options top.Options) error {
54+
func YamlOutput(results github.GithubSearchResults, writer io.Writer, options top.Options) error {
55+
users := GithubUserList(results.Users)
5356
outputUsers := func(user []github.User, public_only bool) {
5457
for i, u := range user {
5558
contributionCount := u.ContributionCount
@@ -106,7 +109,8 @@ func YamlOutput(users GithubUserList, writer io.Writer, options top.Options) err
106109
outputOrganizations(topPrivate.TopOrgs(10))
107110

108111
fmt.Fprintf(writer, "generated: %+v\n", time.Now())
109-
fmt.Fprintf(writer, "min_followers_required: %+v\n", users.MinFollowers())
112+
fmt.Fprintf(writer, "min_followers_required: %+v\n", results.MinimumFollowerCount)
113+
fmt.Fprintf(writer, "total_user_count: %+v\n", results.TotalUserCount)
110114

111115
return nil
112116
}

top/top.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,21 @@ import (
88
"github.com/lauripiispanen/most-active-github-users-counter/net"
99
)
1010

11-
func GithubTop(options Options) ([]github.User, error) {
11+
func GithubTop(options Options) (github.GithubSearchResults, error) {
1212
var token = options.Token
1313
if token == "" {
14-
return []github.User{}, errors.New("Missing GITHUB token")
14+
return github.GithubSearchResults{}, errors.New("Missing GITHUB token")
1515
}
1616

17-
query := "repos:>1 type:user"
17+
query := "type:user"
1818
for _, location := range options.Locations {
1919
query = fmt.Sprintf("%s location:%s", query, location)
2020
}
2121

2222
var client = github.NewGithubClient(net.TokenAuth(token))
2323
users, err := client.SearchUsers(github.UserSearchQuery{Q: query, Sort: "followers", Order: "desc", MaxUsers: options.ConsiderNum})
2424
if err != nil {
25-
return []github.User{}, err
25+
return github.GithubSearchResults{}, err
2626
}
2727
return users, nil
2828
}

0 commit comments

Comments
 (0)