Skip to content

Commit

Permalink
Add counts to topics
Browse files Browse the repository at this point in the history
Previously, on list-topics, only a list of unqiue topics would be
displayed. With this change, each topic will now have an appearance
count next to it. The list is now also sorted by topic appearance counts
in descending order.
  • Loading branch information
gkze committed Sep 28, 2018
1 parent 6864f5d commit 8c86aec
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ func main() {
Usage: "list all topics of starred projects",
Action: func(c *cli.Context) error {
sm.SaveIfEmpty()
for _, t := range sm.GetTopics() {
fmt.Println(t)
for _, pair := range sm.GetTopics() {
fmt.Println(pair.Key, pair.Value)
}

return nil
Expand Down
41 changes: 23 additions & 18 deletions starmanager/starmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package starmanager
import (
"context"
"log"
"math/rand"
"net/url"
"os"
"os/user"
Expand All @@ -11,7 +12,6 @@ import (
"strings"
"sync"
"time"
"math/rand"

"golang.org/x/oauth2"

Expand Down Expand Up @@ -142,7 +142,7 @@ func (s *StarManager) SaveStarredPage(pageno int, responses chan *github.Respons
)
if err != nil {
log.Printf(
"An error occurred while attempting to fetch page %d of %s's GitHub stars!",
"An error occurred while atresultsting to fetch page %d of %s's GitHub stars!",
pageno,
s.Username,
)
Expand All @@ -154,7 +154,7 @@ func (s *StarManager) SaveStarredPage(pageno int, responses chan *github.Respons
responses <- response
}

log.Printf("Attempting to save starred projects on page %d...\n", pageno)
log.Printf("Atresultsting to save starred projects on page %d...\n", pageno)
for _, r := range firstPage {
go s.SaveStarredRepository(r.Repository, wg)
}
Expand All @@ -168,11 +168,11 @@ func (s *StarManager) SaveAllStars() (bool, error) {
responses := make(chan *github.Response, 1)

// Fetch the first page to determine the last page number from the response "Link" header
log.Printf("Attempting to save first page...")
log.Printf("Atresultsting to save first page...")
go s.SaveStarredPage(1, responses, &wg)
firstPageResponse := <-responses

log.Printf("Attempting to save the rest of the pages...")
log.Printf("Atresultsting to save the rest of the pages...")
for i := 2; i <= firstPageResponse.LastPage; i++ {
go s.SaveStarredPage(i, nil, &wg)
}
Expand All @@ -189,32 +189,37 @@ func (s *StarManager) SaveIfEmpty() {
}
}

// KV is a generic struct that maintains a string key - int value pair ( :( ).
type KV struct {
Key string
Value int
}

// GetTopics returns topics for a repository, otherwise if no repository is passed, returns
// a list of all topics
func (s *StarManager) GetTopics() []string {
func (s *StarManager) GetTopics() []KV {
stars := []Star{}
allTopics := []string{}
uniqueTopics := []string{}
keys := map[string]bool{}
topicCounts := map[string]int{}

s.DB.All(&stars)

for _, star := range stars {
allTopics = append(allTopics, star.Topics...)
for _, topic := range star.Topics {
topicCounts[topic]++
}
}

for _, topic := range allTopics {
if _, value := keys[topic]; !value {
keys[topic] = true
uniqueTopics = append(uniqueTopics, topic)
}
results := []KV{}

for topic, count := range topicCounts {
results = append(results, KV{topic, count})
}

sort.Slice(uniqueTopics, func(i, j int) bool {
return uniqueTopics[i] < uniqueTopics[j]
sort.Slice(results, func(i, j int) bool {
return results[i].Value > results[j].Value
})

return uniqueTopics
return results
}

// GetRandomProjects returns random projects given a project count to return, and an optional
Expand Down

0 comments on commit 8c86aec

Please sign in to comment.