Skip to content

Commit

Permalink
add fork metric #30
Browse files Browse the repository at this point in the history
  • Loading branch information
mchmarny committed May 31, 2022
1 parent fd2b80c commit 7e567ec
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 17 deletions.
16 changes: 11 additions & 5 deletions cmd/cli/assets/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ const colors = [
'rgb(176, 216, 148)',
'rgb(250, 222, 137)',
'rgb(253, 186, 187)',
'rgb(114, 90, 76)',
'rgb(177, 148, 87)',
'rgb(114, 90, 76)',
'rgb(137, 137, 250)',
'rgb(187, 137, 253)',
'rgb(76, 114, 90)',
Expand Down Expand Up @@ -370,29 +370,35 @@ function loadTimeSeriesChart(url, fn) {
borderWidth: 1,
order: 2
}, {
label: 'PR-Comments',
label: 'PR-Comment',
data: data.pr_comment,
backgroundColor: colors[1],
borderWidth: 1,
order: 3
}, {
label: 'Issues',
label: 'Issue',
data: data.issue,
backgroundColor: colors[2],
borderWidth: 1,
order: 4
}, {
label: 'Issue-Comments',
label: 'Issue-Comment',
data: data.issue_comment,
backgroundColor: colors[3],
borderWidth: 1,
order: 5
}, {
label: 'Fork',
data: data.fork,
backgroundColor: colors[4],
borderWidth: 1,
order: 6
},{
label: 'Mean',
type: 'line',
fill: false,
data: data.avg,
borderColor: colors[4],
borderColor: colors[5],
order: 1,
borderWidth: 5,
showLine: true,
Expand Down
8 changes: 5 additions & 3 deletions cmd/cli/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,14 @@ func eventSearchHandler(c *gin.Context) {
switch eType {
case "PR":
eType = data.EventTypePR
case "PR-Comments":
case "PR-Comment":
eType = data.EventTypePRComment
case "Issues":
case "Issue":
eType = data.EventTypeIssue
case "Issue-Comments":
case "Issue-Comment":
eType = data.EventTypeIssueComment
case "Fork":
eType = data.EventTypeFork
default:
eType = ""
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ var (

eventTypeFlag = &cli.StringFlag{
Name: "type",
Usage: fmt.Sprintf("Event type (%s, %s, %s, %s)",
data.EventTypePR, data.EventTypeIssue, data.EventTypePRComment, data.EventTypeIssueComment),
Usage: fmt.Sprintf("Event type (%s, %s, %s, %s, %s)",
data.EventTypePR, data.EventTypeIssue, data.EventTypePRComment, data.EventTypeIssueComment, data.EventTypeFork),
Required: false,
}

Expand Down
46 changes: 46 additions & 0 deletions pkg/data/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const (
EventTypePRComment string = "pr_comment"
EventTypeIssue string = "issue"
EventTypeIssueComment string = "issue_comment"
EventTypeFork string = "fork"

pageSizeDefault = 100
importBatchSize = 500
Expand All @@ -29,6 +30,7 @@ const (

sortField string = "created"
sortCommentField string = "updated"
sortForkField string = "newest"
sortDirection string = "desc"

insertEventSQL = `INSERT INTO event (
Expand All @@ -46,6 +48,7 @@ var (
EventTypeIssue,
EventTypeIssueComment,
EventTypePRComment,
EventTypeFork,
}
)

Expand Down Expand Up @@ -125,6 +128,7 @@ func ImportEvents(dbPath, token, owner, repo string, months int) (map[string]int
imp.importIssueEvents,
imp.importIssueCommentEvents,
imp.importPRCommentEvents,
imp.importForkEvents,
}

if err := imp.loadState(); err != nil {
Expand Down Expand Up @@ -523,6 +527,48 @@ func (e *EventImporter) importPRCommentEvents(ctx context.Context) error {
return nil
}

func (e *EventImporter) importForkEvents(ctx context.Context) error {
log.Debugf("starting fork event import on page %d since %s", e.state[EventTypeFork].Page, e.state[EventTypeFork].Since.Format("2006-01-02"))

opt := &github.RepositoryListForksOptions{
Sort: sortForkField,
ListOptions: github.ListOptions{
PerPage: pageSizeDefault,
Page: e.state[EventTypeFork].Page,
},
}

for {
items, resp, err := e.client.Repositories.ListForks(ctx, e.owner, e.repo, opt)
if err != nil || resp.StatusCode != http.StatusOK {
net.PrintHTTPResponse(resp.Response)
return errors.Wrapf(err, "error listing forks, rate: %s", rateInfo(&resp.Rate))
}
log.Debugf("fork - found:%d, page:%d/%d, %s", len(items), resp.NextPage, resp.LastPage, rateInfo(&resp.Rate))

if len(items) == 0 {
break
}

for i := range items {
u := items[i].UpdatedAt
if err := e.add(*items[i].ID, EventTypeFork, *items[i].HTMLURL, items[i].Owner, &u.Time, parseUsers(nil), nil); err != nil {
return errors.Wrapf(err, "error adding fork event: %s/%s", e.owner, e.repo)
}
}

e.state[EventTypeFork].Page = opt.ListOptions.Page

if resp.NextPage == 0 {
break
}

opt.Page = resp.NextPage
}

return nil
}

func unique(slice []string) []string {
keys := make(map[string]bool)
list := []string{}
Expand Down
17 changes: 11 additions & 6 deletions pkg/data/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ const (
SUM(prs) as prs,
SUM(pr_comments) as pr_comments,
SUM(issues) as issues,
SUM(issue_comments) as issue_comments
SUM(issue_comments) as issue_comments,
SUM(forks) as forks
FROM (
WITH RECURSIVE dates(date) AS (
VALUES(?)
Expand All @@ -29,7 +30,8 @@ const (
CASE WHEN e.event_type = ? THEN 1 ELSE 0 END as prs,
CASE WHEN e.event_type = ? THEN 1 ELSE 0 END as pr_comments,
CASE WHEN e.event_type = ? THEN 1 ELSE 0 END as issues,
CASE WHEN e.event_type = ? THEN 1 ELSE 0 END as issue_comments
CASE WHEN e.event_type = ? THEN 1 ELSE 0 END as issue_comments,
CASE WHEN e.event_type = ? THEN 1 ELSE 0 END as forks
FROM dates
LEFT JOIN event e ON dates.date = e.event_date
JOIN developer d ON e.username = d.username
Expand Down Expand Up @@ -81,6 +83,7 @@ type EventTypeSeries struct {
PRComments []int `json:"pr_comment"`
Issues []int `json:"issue"`
IssueComments []int `json:"issue_comment"`
Forks []int `json:"fork"`
Avg []float32 `json:"avg"`
}

Expand Down Expand Up @@ -180,7 +183,7 @@ func GetEventTypeSeries(db *sql.DB, org, repo, entity *string, months int) (*Eve
to := time.Now().UTC().Format("2006-01-02")

rows, err := stmt.Query(since, to,
EventTypePR, EventTypePRComment, EventTypeIssue, EventTypeIssueComment,
EventTypePR, EventTypePRComment, EventTypeIssue, EventTypeIssueComment, EventTypeFork,
org, repo, entity)
if err != nil && err != sql.ErrNoRows {
return nil, errors.Wrap(err, "failed to execute series select statement")
Expand All @@ -193,26 +196,28 @@ func GetEventTypeSeries(db *sql.DB, org, repo, entity *string, months int) (*Eve
PRComments: make([]int, 0),
Issues: make([]int, 0),
IssueComments: make([]int, 0),
Forks: make([]int, 0),
Avg: make([]float32, 0),
}

var runSum float32 = 0
var runCount int = 0
for rows.Next() {
var date string
var prs, prComments, issues, issueComments int
if err := rows.Scan(&date, &prs, &prComments, &issues, &issueComments); err != nil {
var prs, prComments, issues, issueComments, forks int
if err := rows.Scan(&date, &prs, &prComments, &issues, &issueComments, &forks); err != nil {
return nil, errors.Wrapf(err, "failed to scan row")
}
series.Dates = append(series.Dates, date)
series.PRs = append(series.PRs, prs)
series.PRComments = append(series.PRComments, prComments)
series.Issues = append(series.Issues, issues)
series.IssueComments = append(series.IssueComments, issueComments)
series.Forks = append(series.Forks, forks)

// avg
runCount++
runSum += float32(prs + prComments + issues + issueComments)
runSum += float32(prs + prComments + issues + issueComments + forks)
series.Avg = append(series.Avg, runSum/float32(len(event_types)*runCount))
}

Expand Down
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v0.3.14
v0.3.15

0 comments on commit 7e567ec

Please sign in to comment.