Skip to content

Commit

Permalink
Merge pull request #6 from bassam/pr-fix-logging
Browse files Browse the repository at this point in the history
fix logging and compile issues
  • Loading branch information
spencerkimball committed Oct 24, 2017
2 parents 58a01a0 + 2991505 commit aa0a1d3
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 77 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#*
*~
stargazers
stargazer_cache/
65 changes: 32 additions & 33 deletions analyze/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ package analyze
import (
"encoding/csv"
"fmt"
"log"
"os"
"path/filepath"
"sort"
"strconv"
"time"

"github.com/cockroachdb/cockroach/util"
"github.com/cockroachdb/cockroach/util/log"
"github.com/spencerkimball/stargazers/fetch"
)

Expand Down Expand Up @@ -111,17 +110,17 @@ func RunAll(c *fetch.Context, sg []*fetch.Stargazer, rs map[string]*fetch.Repo)
// RunCumulativeStars creates a table of date and cumulative
// star count for the provided stargazers.
func RunCumulativeStars(c *fetch.Context, sg []*fetch.Stargazer) error {
log.Infof("running cumulative stars analysis")
log.Printf("running cumulative stars analysis")

// Open file and prepare.
f, err := createFile(c, "cumulative_stars.csv")
if err != nil {
return util.Errorf("failed to create file: %s", err)
return fmt.Errorf("failed to create file: %s", err)
}
defer f.Close()
w := csv.NewWriter(f)
if err := w.Write([]string{"Date", "New", "Cumulative"}); err != nil {
return util.Errorf("failed to write to CSV: %s", err)
return fmt.Errorf("failed to write to CSV: %s", err)
}

// Sort the stargazers.
Expand All @@ -142,7 +141,7 @@ func RunCumulativeStars(c *fetch.Context, sg []*fetch.Stargazer) error {
if count > 0 {
t := time.Unix(lastDay*60*60*24, 0)
if err := w.Write([]string{t.Format("01/02/2006"), strconv.Itoa(count), strconv.Itoa(total)}); err != nil {
return util.Errorf("failed to write to CSV: %s", err)
return fmt.Errorf("failed to write to CSV: %s", err)
}
}
lastDay = day
Expand All @@ -155,29 +154,29 @@ func RunCumulativeStars(c *fetch.Context, sg []*fetch.Stargazer) error {
if count > 0 {
t := time.Unix(lastDay*60*60*24, 0)
if err := w.Write([]string{t.Format("01/02/2006"), strconv.Itoa(count), strconv.Itoa(total)}); err != nil {
return util.Errorf("failed to write to CSV: %s", err)
return fmt.Errorf("failed to write to CSV: %s", err)
}
}
w.Flush()
log.Infof("wrote cumulative stars analysis to %s", f.Name())
log.Printf("wrote cumulative stars analysis to %s", f.Name())

return nil
}

// RunCorrelatedRepos creates a map from repo name to count of
// repos for repo lists of each stargazer.
func RunCorrelatedRepos(c *fetch.Context, listType string, sg []*fetch.Stargazer, rs map[string]*fetch.Repo) error {
log.Infof("running correlated starred repos analysis")
log.Printf("running correlated starred repos analysis")

// Open file and prepare.
f, err := createFile(c, fmt.Sprintf("correlated_%s_repos.csv", listType))
if err != nil {
return util.Errorf("failed to create file: %s", err)
return fmt.Errorf("failed to create file: %s", err)
}
defer f.Close()
w := csv.NewWriter(f)
if err := w.Write([]string{"Repository", "URL", "Count", "Committers", "Commits", "Additions", "Deletions"}); err != nil {
return util.Errorf("failed to write to CSV: %s", err)
return fmt.Errorf("failed to write to CSV: %s", err)
}
// Compute counts.
counts := map[string]int{}
Expand Down Expand Up @@ -205,29 +204,29 @@ func RunCorrelatedRepos(c *fetch.Context, listType string, sg []*fetch.Stargazer
url := fmt.Sprintf("https://github.com/%s", rs[r.name].FullName)
if err := w.Write([]string{r.name, url, strconv.Itoa(r.count), strconv.Itoa(len(rs[r.name].Statistics)),
strconv.Itoa(c), strconv.Itoa(a), strconv.Itoa(d)}); err != nil {
return util.Errorf("failed to write to CSV: %s", err)
return fmt.Errorf("failed to write to CSV: %s", err)
}
}
w.Flush()
log.Infof("wrote correlated %s repos analysis to %s", listType, f.Name())
log.Printf("wrote correlated %s repos analysis to %s", listType, f.Name())

// Open histogram file.
fHist, err := createFile(c, fmt.Sprintf("correlated_%s_repos_hist.csv", listType))
if err != nil {
return util.Errorf("failed to create file: %s", err)
return fmt.Errorf("failed to create file: %s", err)
}
defer fHist.Close()
wHist := csv.NewWriter(fHist)
if err := wHist.Write([]string{"Correlation", "Count"}); err != nil {
return util.Errorf("failed to write to CSV: %s", err)
return fmt.Errorf("failed to write to CSV: %s", err)
}
lastCorrelation := 0
count := 0
for _, r := range repos {
if lastCorrelation != r.count {
if count > 0 {
if err := wHist.Write([]string{strconv.Itoa(lastCorrelation), strconv.Itoa(count)}); err != nil {
return util.Errorf("failed to write to CSV: %s", err)
return fmt.Errorf("failed to write to CSV: %s", err)
}
}
lastCorrelation = r.count
Expand All @@ -238,29 +237,29 @@ func RunCorrelatedRepos(c *fetch.Context, listType string, sg []*fetch.Stargazer
}
if count > 0 {
if err := wHist.Write([]string{strconv.Itoa(lastCorrelation), strconv.Itoa(count)}); err != nil {
return util.Errorf("failed to write to CSV: %s", err)
return fmt.Errorf("failed to write to CSV: %s", err)
}
}
wHist.Flush()
log.Infof("wrote correlated %s repos histogram to %s", listType, fHist.Name())
log.Printf("wrote correlated %s repos histogram to %s", listType, fHist.Name())

return nil
}

// RunFollowers computes the size of follower networks, as well as
// the count of shared followers.
func RunFollowers(c *fetch.Context, sg []*fetch.Stargazer) error {
log.Infof("running followers analysis")
log.Printf("running followers analysis")

// Open file and prepare.
f, err := createFile(c, "followers.csv")
if err != nil {
return util.Errorf("failed to create file: %s", err)
return fmt.Errorf("failed to create file: %s", err)
}
defer f.Close()
w := csv.NewWriter(f)
if err := w.Write([]string{"Name", "Login", "URL", "Avatar URL", "Company", "Location", "Followers", "Shared Followers"}); err != nil {
return util.Errorf("failed to write to CSV: %s", err)
return fmt.Errorf("failed to write to CSV: %s", err)
}

shared := map[string]int{}
Expand All @@ -281,29 +280,29 @@ func RunFollowers(c *fetch.Context, sg []*fetch.Stargazer) error {
}
url := fmt.Sprintf("https://github.com/%s", s.Login)
if err := w.Write([]string{s.Name, s.Login, url, s.AvatarURL, s.Company, s.Location, strconv.Itoa(s.User.Followers), strconv.Itoa(sharedCount)}); err != nil {
return util.Errorf("failed to write to CSV: %s", err)
return fmt.Errorf("failed to write to CSV: %s", err)
}
}
w.Flush()
log.Infof("wrote followers analysis to %s", f.Name())
log.Printf("wrote followers analysis to %s", f.Name())

return nil
}

// RunCommitters lists stargazers by commits to subscribed repos, from
// most prolific committer to least.
func RunCommitters(c *fetch.Context, sg []*fetch.Stargazer, rs map[string]*fetch.Repo) error {
log.Infof("running committers analysis")
log.Printf("running committers analysis")

// Open file and prepare.
f, err := createFile(c, "committers.csv")
if err != nil {
return util.Errorf("failed to create file: %s", err)
return fmt.Errorf("failed to create file: %s", err)
}
defer f.Close()
w := csv.NewWriter(f)
if err := w.Write([]string{"Login", "Email", "Commits", "Additions", "Deletions"}); err != nil {
return util.Errorf("failed to write to CSV: %s", err)
return fmt.Errorf("failed to write to CSV: %s", err)
}

// Sort the stargazers.
Expand All @@ -317,29 +316,29 @@ func RunCommitters(c *fetch.Context, sg []*fetch.Stargazer, rs map[string]*fetch
break
}
if err := w.Write([]string{s.Login, s.Email, strconv.Itoa(c), strconv.Itoa(a), strconv.Itoa(d)}); err != nil {
return util.Errorf("failed to write to CSV: %s", err)
return fmt.Errorf("failed to write to CSV: %s", err)
}
}
w.Flush()
log.Infof("wrote committers analysis to %s", f.Name())
log.Printf("wrote committers analysis to %s", f.Name())

return nil
}

// RunCumulativeStars creates a table of date and cumulative
// star count for the provided stargazers.
func RunAttributesByTime(c *fetch.Context, sg []*fetch.Stargazer, rs map[string]*fetch.Repo) error {
log.Infof("running stargazer attributes by time analysis")
log.Printf("running stargazer attributes by time analysis")

// Open file and prepare.
f, err := createFile(c, "attributes_by_time.csv")
if err != nil {
return util.Errorf("failed to create file: %s", err)
return fmt.Errorf("failed to create file: %s", err)
}
defer f.Close()
w := csv.NewWriter(f)
if err := w.Write([]string{"Date", "New Stars", "Avg Age", "Avg Followers", "Avg Commits"}); err != nil {
return util.Errorf("failed to write to CSV: %s", err)
return fmt.Errorf("failed to write to CSV: %s", err)
}

output := func(day int64, count, age, followers, commits int) error {
Expand All @@ -348,7 +347,7 @@ func RunAttributesByTime(c *fetch.Context, sg []*fetch.Stargazer, rs map[string]
avgFollowers := fmt.Sprintf("%.2f", float64(followers)/float64(count))
avgCommits := fmt.Sprintf("%.2f", float64(commits)/float64(count))
if err := w.Write([]string{t.Format("01/02/2006"), strconv.Itoa(count), avgAge, avgFollowers, avgCommits}); err != nil {
return util.Errorf("failed to write to CSV: %s", err)
return fmt.Errorf("failed to write to CSV: %s", err)
}
return nil
}
Expand Down Expand Up @@ -400,7 +399,7 @@ func RunAttributesByTime(c *fetch.Context, sg []*fetch.Stargazer, rs map[string]
}
}
w.Flush()
log.Infof("wrote stargazer attributes by time analysis to %s", f.Name())
log.Printf("wrote stargazer attributes by time analysis to %s", f.Name())

return nil
}
Expand Down
10 changes: 5 additions & 5 deletions cmd/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ package cmd

import (
"errors"
"log"

"github.com/cockroachdb/cockroach/util/log"
"github.com/spencerkimball/stargazers/analyze"
"github.com/spencerkimball/stargazers/fetch"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -51,19 +51,19 @@ func RunAnalyze(cmd *cobra.Command, args []string) error {
if len(Repo) == 0 {
return errors.New("repository not specified; use --repo=:owner/:repo")
}
log.Infof("fetching saved GitHub stargazer data for repository %s", Repo)
log.Printf("fetching saved GitHub stargazer data for repository %s", Repo)
fetchCtx := &fetch.Context{
Repo: Repo,
CacheDir: CacheDir,
}
sg, rs, err := fetch.LoadState(fetchCtx)
if err != nil {
log.Errorf("failed to load saved stargazer data: %s", err)
log.Printf("failed to load saved stargazer data: %s", err)
return nil
}
log.Infof("analyzing GitHub data for repository %s", Repo)
log.Printf("analyzing GitHub data for repository %s", Repo)
if err := analyze.RunAll(fetchCtx, sg, rs); err != nil {
log.Errorf("failed to query stargazer data: %s", err)
log.Printf("failed to query stargazer data: %s", err)
return nil
}
return nil
Expand Down
6 changes: 3 additions & 3 deletions cmd/clear.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ package cmd

import (
"errors"
"log"

"github.com/cockroachdb/cockroach/util/log"
"github.com/spencerkimball/stargazers/fetch"
"github.com/spf13/cobra"
)
Expand All @@ -41,13 +41,13 @@ func RunClear(cmd *cobra.Command, args []string) error {
if len(Repo) == 0 {
return errors.New("repository not specified; use --repo=:owner/:repo")
}
log.Infof("clearing GitHub API response cache for repository %s", Repo)
log.Printf("clearing GitHub API response cache for repository %s", Repo)
fetchCtx := &fetch.Context{
Repo: Repo,
CacheDir: CacheDir,
}
if err := fetch.Clear(fetchCtx); err != nil {
log.Errorf("failed to clear cached responses: %s", err)
log.Printf("failed to clear cached responses: %s", err)
return nil
}
return nil
Expand Down
6 changes: 3 additions & 3 deletions cmd/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ package cmd

import (
"errors"
"log"

"github.com/cockroachdb/cockroach/util/log"
"github.com/spencerkimball/stargazers/fetch"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -50,14 +50,14 @@ func RunFetch(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
log.Infof("fetching GitHub data for repository %s", Repo)
log.Printf("fetching GitHub data for repository %s", Repo)
fetchCtx := &fetch.Context{
Repo: Repo,
Token: token,
CacheDir: CacheDir,
}
if err := fetch.QueryAll(fetchCtx); err != nil {
log.Errorf("failed to query stargazer data: %s", err)
log.Printf("failed to query stargazer data: %s", err)
return nil
}
return nil
Expand Down
13 changes: 5 additions & 8 deletions fetch/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ import (
"bytes"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"path/filepath"
"strings"

"github.com/cockroachdb/cockroach/util/log"
"github.com/kennygrant/sanitize"
)

Expand All @@ -47,9 +47,8 @@ func getCache(c *Context, req *http.Request) (*http.Response, error) {
}
return nil, err
}
if log.V(1) {
log.Infof("found %q in response cache", req.URL.String())
}
log.Printf("found %q in response cache", req.URL.String())

return resp, err
}

Expand All @@ -74,14 +73,12 @@ func putCache(c *Context, req *http.Request, resp *http.Response) error {
return err
}
f.Close()
if log.V(1) {
log.Infof("wrote %q to response cache", req.URL.String())
}
log.Printf("wrote %q to response cache", req.URL.String())

// TODO(spencer): this sucks, but we must re-read the response as
// the body is closed during the call to resp.Write().
if readResp, err := readCachedResponse(filename, req); err != nil {
log.Errorf("failed reading cached response: %s", err)
log.Printf("failed reading cached response: %s", err)
return err
} else {
resp.Body = readResp.Body
Expand Down
Loading

0 comments on commit aa0a1d3

Please sign in to comment.