Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calculate total workflow run usage which has over 100 jobs #15

Merged
merged 2 commits into from
Dec 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 4 additions & 11 deletions cmd/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ type WorkflowRunResult struct {
Error error
}

type UsageResult struct {
Usage github.Usage
Error error
}

func Run(repo string, startDate string, endDate string, token string) (github.Usage, error) {
targetRange, err := github.NewRange(startDate, endDate)
if err != nil {
Expand Down Expand Up @@ -74,15 +69,15 @@ func Run(repo string, startDate string, endDate string, token string) (github.Us
}
}

uc := make(chan UsageResult)
uc := make(chan github.UsageResult)
for _, w := range allWorkflowRuns {
go func(w github.WorkflowRun) {
u, err := w.Usage(client)
if err != nil {
uc <- UsageResult{Usage: github.Usage{}, Error: err}
uc <- github.UsageResult{Usage: github.Usage{}, Error: err}
return
}
uc <- UsageResult{Usage: u, Error: nil}
uc <- github.UsageResult{Usage: u, Error: nil}
}(w)
}

Expand All @@ -93,9 +88,7 @@ func Run(repo string, startDate string, endDate string, token string) (github.Us
return github.Usage{}, u.Error
}

usage.Linux += u.Usage.Linux
usage.Windows += u.Usage.Windows
usage.Mac += u.Usage.Mac
usage = usage.Plus(u.Usage)

fmt.Printf("Complete fetch job (%d/%d)\n", k+1, workflowRuns.TotalCount)
}
Expand Down
20 changes: 20 additions & 0 deletions github/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package github

import (
"fmt"
"ghausage/config"
"math"
"strings"
"time"
)
Expand Down Expand Up @@ -47,6 +49,11 @@ type Usage struct {
SelfHosted int64
}

type UsageResult struct {
Usage Usage
Error error
}

func (u Usage) HumanReadable() (HumanReadableUsage, error) {
linux, err := ToString(u.Linux)
if err != nil {
Expand Down Expand Up @@ -109,6 +116,10 @@ func (j JobRuns) Usage() Usage {
return u
}

func (j JobRuns) TotalPage() int {
return int(math.Ceil(float64(j.TotalCount) / float64(config.PerPage)))
}

func (j Job) Usage() int64 {
if j.CompletedAt.IsZero() {
stepUsage := j.StepUsage()
Expand Down Expand Up @@ -151,6 +162,15 @@ func (j Job) RunnerType() RunnerType {
return SelfHosted
}

func (u Usage) Plus(addend Usage) Usage {
return Usage{
Linux: u.Linux + addend.Linux,
Windows: u.Windows + addend.Windows,
Mac: u.Mac + addend.Mac,
SelfHosted: u.SelfHosted + addend.SelfHosted,
}
}

func IsLinuxRunner(label string) bool {
if label == "ubuntu-latest" {
return true
Expand Down
36 changes: 32 additions & 4 deletions github/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ type WorkflowRun struct {
JobsUrl string `json:"jobs_url"`
}

func (w WorkflowRun) JobRuns(client Client) (JobRuns, error) {
func (w WorkflowRun) JobRuns(client Client, page int) (JobRuns, error) {
body, err := client.Get(fmt.Sprintf(
"%s?per_page=%d",
"%s?per_page=%d&page=%d",
w.JobsUrl,
// Maybe, job per workflow is under 100
config.PerPage,
page,
))
if err != nil {
return JobRuns{}, err
Expand All @@ -35,12 +36,39 @@ func (w WorkflowRun) JobRuns(client Client) (JobRuns, error) {
}

func (w WorkflowRun) Usage(client Client) (Usage, error) {
jobRuns, err := w.JobRuns(client)
jobRuns, err := w.JobRuns(client, 1)
if err != nil {
return Usage{}, err
}

return jobRuns.Usage(), nil
u := jobRuns.Usage()

totalPage := jobRuns.TotalPage()

if totalPage > 1 {
uc := make(chan UsageResult)
for i := 2; i <= totalPage; i++ {
go func(page int) {
jobRuns, err := w.JobRuns(client, page)
if err != nil {
uc <- UsageResult{Usage: Usage{}, Error: err}
return
}
uc <- UsageResult{Usage: jobRuns.Usage(), Error: nil}
}(i)
}

for j := 2; j <= totalPage; j++ {
result := <-uc
if result.Error != nil {
return Usage{}, result.Error
}

u = u.Plus(result.Usage)
}
}

return u, nil
}

func FetchWorkflowRuns(repo string, client Client, targetRange Range, perPage int, page int) (WorkflowRuns, error) {
Expand Down
10 changes: 10 additions & 0 deletions github/workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,16 @@ func TestWorkflowRunUsage(t *testing.T) {
SelfHosted: 0,
},
},
{
name: "job per workflow is over 100",
jobsUrl: "https://api.github.com/repos/phpstan/phpstan-src/actions/runs/3281861062/jobs",
expectedUsage: Usage{
Linux: 7135,
Windows: 61,
Mac: 0,
SelfHosted: 0,
},
},
}

for _, tt := range tests {
Expand Down