diff --git a/main.go b/main.go index 51f9c1c..fb7ab0f 100644 --- a/main.go +++ b/main.go @@ -5,7 +5,9 @@ import ( "flag" "fmt" "log" + "os" "strings" + "text/tabwriter" "time" units "github.com/docker/go-units" @@ -19,11 +21,13 @@ func main() { var ( orgName, userName, token string since int + punchCard bool ) flag.StringVar(&orgName, "org", "", "Organization name") flag.StringVar(&userName, "user", "", "User name") flag.StringVar(&token, "token", "", "GitHub token") + flag.BoolVar(&punchCard, "punch-card", false, "Show punch card with breakdown of builds per day") flag.IntVar(&since, "since", 30, "Since when to fetch the data (in days)") flag.Parse() @@ -52,9 +56,20 @@ func main() { longestBuild time.Duration actors map[string]bool conclusion map[string]int + buildsPerDay map[string]int ) actors = make(map[string]bool) + buildsPerDay = map[string]int{ + "Monday": 0, + "Tuesday": 0, + "Wednesday": 0, + "Thursday": 0, + "Friday": 0, + "Saturday": 0, + "Sunday": 0, + } + conclusion = map[string]int{ "success": 0, "failure": 0, @@ -73,7 +88,6 @@ func main() { log.Printf("Fetching repos %s page %d", orgName, page) if orgName != "" { opts := &github.RepositoryListByOrgOptions{ListOptions: github.ListOptions{Page: page, PerPage: 100}, Type: "all"} - log.Printf("Fetching repos %s page %d", orgName, page) repos, res, err = client.Repositories.ListByOrg(ctx, orgName, opts) } @@ -215,6 +229,10 @@ func main() { job.GetID(), job.GetStartedAt().Format("2006-01-02 15:04:05"), job.GetCompletedAt().Format("2006-01-02 15:04:05"), humanDuration(dur), job.GetConclusion()) + + dayOfWeek := job.GetStartedAt().Time.Weekday().String() + + buildsPerDay[dayOfWeek]++ } } } @@ -225,6 +243,7 @@ func main() { entity = userName } + days := []string{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"} fmt.Printf("\nGenerated by: https://github.com/self-actuated/actions-usage\nReport for %s - last: %d days.\n\n", entity, since) fmt.Printf("Total repos: %d\n", len(allRepos)) fmt.Printf("Total private repos: %d\n", totalPrivate) @@ -246,6 +265,20 @@ func main() { fmt.Println() fmt.Printf("Longest build: %s\n", longestBuild.Round(time.Second)) fmt.Printf("Average build time: %s\n", (allUsage / time.Duration(totalJobs)).Round(time.Second)) + + fmt.Println() + + if punchCard { + w := tabwriter.NewWriter(os.Stdout, 15, 4, 1, ' ', tabwriter.TabIndent) + fmt.Fprintln(w, "Day\tBuilds") + for _, day := range days { + fmt.Fprintf(w, "%s\t%d\n", day, buildsPerDay[day]) + } + fmt.Fprintf(w, "%s\t%d\n", "Total", totalJobs) + + w.Flush() + } + } fmt.Println()