Skip to content

Commit

Permalink
add json formatting for the output
Browse files Browse the repository at this point in the history
  • Loading branch information
nimakaviani committed Nov 25, 2020
1 parent e7bf85e commit e25e592
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 88 deletions.
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ module github.com/nimakaviani/github-contributors
go 1.15

require (
github.com/cheggaaa/pb v1.0.29
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/cheggaaa/pb/v3 v3.0.5
github.com/maxbrunsfeld/counterfeiter/v6 v6.3.0
github.com/olekukonko/tablewriter v0.0.5-0.20201029120751-42e21c7531a3
github.com/spf13/cobra v1.1.1
Expand Down
149 changes: 109 additions & 40 deletions pkg/analyzer/activities.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package analyzer

import (
"encoding/json"
"fmt"
"os"
"sort"
"strconv"

"github.com/cheggaaa/pb"
"github.com/cheggaaa/pb/v3"
"github.com/nimakaviani/github-contributors/pkg/models"
"github.com/nimakaviani/github-contributors/pkg/scraper"
"github.com/nimakaviani/github-contributors/pkg/utils"
Expand Down Expand Up @@ -53,7 +54,7 @@ func (i *activities) Process(repo string) error {
return err
}

fmt.Printf("Analyzig the last %d %ss on %s\n", len(activities), i.activityName, repo)
fmt.Fprintf(os.Stderr, "Analyzig the last %d %ss on %s\n", len(activities), i.activityName, repo)
bar := pb.StartNew(len(activities))

utils.Log("> reviewing activities ...")
Expand All @@ -73,41 +74,7 @@ func (i *activities) Process(repo string) error {
return nil
}

func (i *activities) Write(expand bool) {
table := tablewriter.NewWriter(os.Stdout)
if expand {
table.SetHeader([]string{
"Org",
"GitHubId",
"Email",
"Issue / PR",
"Association",
"State",
})
table.SetHeaderColor(
tablewriter.Colors{tablewriter.Bold},
tablewriter.Colors{tablewriter.Bold},
tablewriter.Colors{tablewriter.Bold},
tablewriter.Colors{tablewriter.Bold},
tablewriter.Colors{tablewriter.Bold},
tablewriter.Colors{tablewriter.Bold},
)
} else {
table.SetHeader([]string{
"Org",
"GitHubId",
"Association",
})
table.SetHeaderColor(
tablewriter.Colors{tablewriter.Bold},
tablewriter.Colors{tablewriter.Bold},
tablewriter.Colors{tablewriter.Bold},
)
}
table.SetAutoMergeCells(true)
table.SetBorder(true)
table.SetRowLine(true)

func (i *activities) Write(expand bool, format string) error {
activities := i.activities
sort.Slice(activities, func(i, j int) bool {
if activities[i].userDetails.org < activities[j].userDetails.org {
Expand All @@ -121,18 +88,60 @@ func (i *activities) Write(expand bool) {
return false
})

type issue struct {
Issue string `json:"issue"`
State string `json:"state"`
}

type contributor struct {
Login string `json:"login"`
Email string `json:"email,omitempty"`
Association string `json:"association,omitempty"`
Activities []issue `json:"activities,omitempty"`
}

data := make([][]string, 0)

contributors := make(map[string]*contributor)
output := make(map[string][]*contributor)

for _, activity := range activities {
if _, ok := output[activity.userDetails.org]; !ok {
output[activity.userDetails.org] = []*contributor{}
}

var (
c *contributor
ok bool
)

if c, ok = contributors[activity.User.Login]; !ok {
c = &contributor{
Login: activity.User.Login,
Email: activity.userDetails.email,
Association: activity.userDetails.association,
}
contributors[activity.User.Login] = c
output[activity.userDetails.org] = append(output[activity.userDetails.org], c)
}

var row []string
if expand {
issueString := fmt.Sprintf("%s(%s) : %s \n\n %s", i.activityName, strconv.Itoa(activity.Number), activity.Title, activity.Url)
row = []string{
activity.userDetails.org,
activity.User.Login,
activity.userDetails.email,
fmt.Sprintf("%s(%s) : %s \n\n %s", i.activityName, strconv.Itoa(activity.Number), activity.Title, activity.Url),
issueString,
activity.userDetails.association,
activity.State,
}

c.Activities = append(c.Activities, issue{
State: activity.State,
Issue: issueString,
})

} else {
row = []string{
activity.userDetails.org,
Expand All @@ -143,6 +152,66 @@ func (i *activities) Write(expand bool) {
data = append(data, row)
}

table.AppendBulk(data)
table.Render()
if format != JsonFormat {
table := tablewriter.NewWriter(os.Stdout)
if expand {
table.SetHeader([]string{
"Org",
"GitHubId",
"Email",
"Issue / PR",
"Association",
"State",
})
table.SetHeaderColor(
tablewriter.Colors{tablewriter.Bold},
tablewriter.Colors{tablewriter.Bold},
tablewriter.Colors{tablewriter.Bold},
tablewriter.Colors{tablewriter.Bold},
tablewriter.Colors{tablewriter.Bold},
tablewriter.Colors{tablewriter.Bold},
)
} else {
table.SetHeader([]string{
"Org",
"GitHubId",
"Association",
})
table.SetHeaderColor(
tablewriter.Colors{tablewriter.Bold},
tablewriter.Colors{tablewriter.Bold},
tablewriter.Colors{tablewriter.Bold},
)
}
table.SetAutoMergeCells(true)
table.SetBorder(true)
table.SetRowLine(true)

table.AppendBulk(data)
table.Render()
return nil
}

type jsonOutput struct {
Org string `json:"org"`
Percentage string `json:"percentage"`
Contributors []*contributor `json:"contributors"`
}

jsonStruct := []jsonOutput{}
for org, contribs := range output {
percentage := float64(float64(len(contribs))/float64(i.count)) * 100.0
jsonStruct = append(jsonStruct, jsonOutput{
Org: org,
Contributors: contribs,
Percentage: fmt.Sprintf("%.1f%%", percentage),
})
}

jsonData, err := json.Marshal(jsonStruct)
if err != nil {
return err
}
fmt.Println(string(jsonData))
return nil
}
127 changes: 85 additions & 42 deletions pkg/analyzer/charter.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package analyzer

import (
"encoding/json"
"fmt"
"math"
"os"
"regexp"
"strconv"

"github.com/cheggaaa/pb"
"github.com/cheggaaa/pb/v3"
"github.com/nimakaviani/github-contributors/pkg/scraper"
"github.com/nimakaviani/github-contributors/pkg/utils"
"github.com/olekukonko/tablewriter"
)

const (
Unknown = "unknown"
Unknown = "unknown"
JsonFormat = "json"
)

type Details struct {
Expand Down Expand Up @@ -50,8 +52,13 @@ func (c *charter) Process(repo string, count int) error {
}

c.total = math.Max(float64(len(users)), float64(count))
fmt.Printf("Analyzig the top %d contributors on %s\n", int(c.total), repo)
fmt.Fprintf(os.Stderr, "Analyzig the top %d contributors on %s\n", int(c.total), repo)

bar := pb.StartNew(len(users))
defer bar.Finish()
defer utils.Log("> done")
defer utils.Log(">> RESULTS")

utils.Log("> building charter ...")
for _, user := range users {
err := c.build(repo, user.Login)
Expand All @@ -61,9 +68,6 @@ func (c *charter) Process(repo string, count int) error {
bar.Increment()
}

bar.Finish()
utils.Log("> done")
utils.Log(">> RESULTS")
return nil
}

Expand Down Expand Up @@ -120,64 +124,103 @@ func (c *charter) parse(login, email string) error {
return nil
}

func (c *charter) Write(expand bool) {
table := tablewriter.NewWriter(os.Stdout)

if expand {
table.SetHeader([]string{
"Org",
"GitHubId",
"Email",
})
table.SetHeaderColor(
tablewriter.Colors{tablewriter.Bold},
tablewriter.Colors{tablewriter.Bold},
tablewriter.Colors{tablewriter.Bold},
)
table.SetAutoMergeCells(true)
} else {
table.SetHeader([]string{
"Org",
"Count",
"Percentage",
})
table.SetHeaderColor(
tablewriter.Colors{tablewriter.Bold},
tablewriter.Colors{tablewriter.Bold},
tablewriter.Colors{tablewriter.Bold},
)
func (c *charter) Write(expand bool, format string) error {
type contributor struct {
Login string `json:"login"`
Email string `json:"email"`
}

table.SetRowLine(true)
table.SetBorder(true)
type jsonOutput struct {
OrgName string `json:"org"`
Percentage string `json:"percentage"`
Contributors []contributor `json:"contributors,omitempty"`
}

output := []jsonOutput{}
data := make([][]string, 0)

for org, users := range c.charterMap {
count := len(users.(map[string]*Details))
percentage := float64(float64(count)/c.total) * 100.0

outputItem := jsonOutput{
OrgName: org,
Percentage: fmt.Sprintf("%.1f%%", percentage),
}

if expand {
for login, details := range users.(map[string]*Details) {
data = append(data, []string{
fmt.Sprintf("%s \n %.1f%% ", org, (float64(count)/c.total)*100.0),
fmt.Sprintf("%s \n %.1f%% ", org, percentage),
login,
details.email,
})

outputItem.Contributors = append(
outputItem.Contributors, contributor{
Login: login,
Email: details.email,
})
}
} else {
if count == 0 {
continue
}
}

if count == 0 {
continue
}

if !expand {
data = append(data, []string{
org,
strconv.Itoa(count),
fmt.Sprintf("%.1f%%", float64(float64(count)/c.total)*100.0),
fmt.Sprintf("%.1f%% ", percentage),
})
}

output = append(output, outputItem)
}

table.AppendBulk(data)
table.Render()
if format != JsonFormat {
table := tablewriter.NewWriter(os.Stdout)

if expand {
table.SetHeader([]string{
"Org",
"GitHubId",
"Email",
})
table.SetHeaderColor(
tablewriter.Colors{tablewriter.Bold},
tablewriter.Colors{tablewriter.Bold},
tablewriter.Colors{tablewriter.Bold},
)
table.SetAutoMergeCells(true)
} else {
table.SetHeader([]string{
"Org",
"Count",
"Percentage",
})
table.SetHeaderColor(
tablewriter.Colors{tablewriter.Bold},
tablewriter.Colors{tablewriter.Bold},
tablewriter.Colors{tablewriter.Bold},
)
}

table.SetRowLine(true)
table.SetBorder(true)
table.AppendBulk(data)
table.Render()
return nil
}

jsonData, err := json.Marshal(output)
if err != nil {
return err
}

fmt.Println(string(jsonData))
return nil
}

func extract(login, email string) (Details, error) {
Expand Down
Loading

0 comments on commit e25e592

Please sign in to comment.