Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor it all
Browse files Browse the repository at this point in the history
Signed-off-by: Brian Downs <brian.downs@gmail.com>
briandowns committed Dec 17, 2024
1 parent 16ba0e7 commit e3fba3b
Showing 1 changed file with 93 additions and 33 deletions.
126 changes: 93 additions & 33 deletions cmd/release/cmd/stats.go
Original file line number Diff line number Diff line change
@@ -2,9 +2,11 @@ package cmd

import (
"context"
"errors"
"fmt"
"os"
"sort"
"strings"
"time"

"github.com/briandowns/spinner"
@@ -19,30 +21,43 @@ var (
endDate *string
)

const layout = "2006-01-02"

var repoToOwner = map[string]string{
"rke2": "rancher",
"rancher": "rancher",
"k3s": "k3s-io",
}

type monthly struct {
count int
captains []string
tags []string
}

type relStats struct {
count int
monthly map[time.Month]monthly
}

// statsCmd represents the stats command
var statsCmd = &cobra.Command{
Use: "stats",
Short: "Release statistics",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
from, err := time.Parse(layout, *startDate)
Long: `Retrieve release statistics for a time period.`,
RunE: func(cmd *cobra.Command, args []string) error {
from, err := time.Parse(time.DateOnly, *startDate)
if err != nil {
fmt.Fprint(os.Stderr, err)
os.Exit(1)
return err
}
to, err := time.Parse(layout, *endDate)

to, err := time.Parse(time.DateOnly, *endDate)
if err != nil {
fmt.Fprint(os.Stderr, err)
os.Exit(1)
return err
}

if to.Before(from) {
return errors.New("end date before start date")
}

githubToken := os.Getenv("GITHUB_TOKEN")

ctx := context.Background()
@@ -52,8 +67,8 @@ var statsCmd = &cobra.Command{
s.HideCursor = true
s.Start()

var allReleases []*github.RepositoryRelease
monthly := make(map[int]int)
var total int
data := make(map[int]relStats)
captains := make(map[string]int)

lo := github.ListOptions{
@@ -62,20 +77,43 @@ var statsCmd = &cobra.Command{
for {
releases, resp, err := client.Repositories.ListReleases(ctx, repoToOwner[*repo], *repo, &lo)
if err != nil {
fmt.Fprint(os.Stderr, err)
os.Exit(1)
return err
}

for _, release := range releases {
releaseDate := release.GetCreatedAt().Time
if releaseDate.After(from) && releaseDate.Before(to) {
allReleases = append(allReleases, release)

if _, ok := monthly[int(releaseDate.Month())]; !ok {
monthly[int(releaseDate.Month())]++
if releaseDate.After(from) && (releaseDate.Before(to) || releaseDate.Equal(to)) {
total++

if _, ok := data[int(release.CreatedAt.Year())]; !ok {
data[int(release.CreatedAt.Year())] = relStats{
count: 1,
monthly: map[time.Month]monthly{
release.CreatedAt.Month(): {
count: 1,
captains: []string{
*release.Author.Login,
},
tags: []string{
*release.Name,
},
},
},
}
continue
}
monthly[int(releaseDate.Month())]++

rs := data[int(release.CreatedAt.Year())]
rs.count++

mon := rs.monthly[release.CreatedAt.Month()]
mon.count++
mon.captains = append(mon.captains, *release.Author.Login)
mon.tags = append(mon.tags, *release.Name)

rs.monthly[release.CreatedAt.Month()] = mon

data[int(release.CreatedAt.Year())] = rs

if release.Author.Login != nil {
if _, ok := captains[*release.Author.Login]; !ok {
@@ -93,27 +131,49 @@ var statsCmd = &cobra.Command{
lo.Page = resp.NextPage
}

months := make([]int, 0, 12)
for k := range monthly {
months = append(months, int(k))
}
sort.Ints(months)

s.Stop()

fmt.Printf("Total: %d\n", len(allReleases))
for _, month := range months {
fmt.Printf(" %-10s %2d\n", time.Month(month).String(), monthly[int(time.Month(month))])
for year := range data {
fmt.Printf("\n%d:\n", year)

months := make([]int, 0, len(data[year].monthly))
for k := range data[year].monthly {
months = append(months, int(k))
}
sort.Ints(months)

for _, m := range months {
mon := time.Month(m)
tmp := data[year].monthly[mon]
tmp.captains = dedup(tmp.captains)
data[year].monthly[mon] = tmp
captains := strings.Join(data[year].monthly[mon].captains, ", ")
tags := strings.Join(data[year].monthly[mon].tags, ", ")
fmt.Printf(" %-9s\n Count: %3d\n Captains: %s\n Tags: %s\n",
mon, data[year].monthly[mon].count, captains, tags)
}
}

fmt.Println("\nCaptains:")
for captain, count := range captains {
fmt.Printf(" %-15s %2d\n", captain, count)
}
fmt.Printf("\nTotal: %d\n", total)

return nil
},
}

func dedup(slice []string) []string {
seen := make(map[string]struct{})
result := []string{}

for _, val := range slice {
if _, ok := seen[val]; !ok {
seen[val] = struct{}{}
result = append(result, val)
}
}

return result
}

func init() {
rootCmd.AddCommand(statsCmd)

0 comments on commit e3fba3b

Please sign in to comment.