Skip to content

Commit

Permalink
Output json format
Browse files Browse the repository at this point in the history
  • Loading branch information
nao1215 committed Mar 5, 2023
1 parent f33ce30 commit 1bbcf35
Showing 1 changed file with 50 additions and 8 deletions.
58 changes: 50 additions & 8 deletions cmd/stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package cmd

import (
"context"
"encoding/json"
"fmt"
"image/color"
"io"
"os"

"github.com/nao1215/leadtime/di"
"github.com/nao1215/leadtime/domain/usecase"
Expand Down Expand Up @@ -133,7 +136,12 @@ func stat(cmd *cobra.Command, args []string) error { //nolint
outputMarkdown(output.LeadTime)
return nil
}
outputDefault(output.LeadTime)

lts := newLeadTimeStat(output.LeadTime)
if err := lts.json(os.Stdout); err != nil {
return err
}
// outputDefault(output.LeadTime)

return nil
}
Expand Down Expand Up @@ -194,14 +202,16 @@ func outputMarkdown(lt *usecase.LeadTime) {
}

func outputDefault(lt *usecase.LeadTime) {
fmt.Printf("PR\tAuthor\tBot\tLeadTime[min]\tTitle\n")
for _, v := range lt.PRs {
if v.User.Bot {
fmt.Printf("#%d\t%s\t%s\t%d\t%s\n", v.Number, pointer.StringValue(v.User.Name), "yes", v.MergeTimeMinutes, v.Title)
continue
/*
fmt.Printf("PR\tAuthor\tBot\tLeadTime[min]\tTitle\n")
for _, v := range lt.PRs {
if v.User.Bot {
fmt.Printf("#%d\t%s\t%s\t%d\t%s\n", v.Number, pointer.StringValue(v.User.Name), "yes", v.MergeTimeMinutes, v.Title)
continue
}
fmt.Printf("#%d\t%s\t%s\t%d\t%s\n", v.Number, pointer.StringValue(v.User.Name), "no", v.MergeTimeMinutes, v.Title)
}
fmt.Printf("#%d\t%s\t%s\t%d\t%s\n", v.Number, pointer.StringValue(v.User.Name), "no", v.MergeTimeMinutes, v.Title)
}
*/

fmt.Println("")
fmt.Println("[statistics]")
Expand All @@ -212,3 +222,35 @@ func outputDefault(lt *usecase.LeadTime) {
fmt.Printf(" Lead Time(Ave) = %.2f[min]\n", lt.Average())
fmt.Printf(" Lead Time(Median) = %.2f[min]\n", lt.Median())
}

// LeadTimeStat is Lead time statistics.
type LeadTimeStat struct {
TotalPR int `json:"total_pr,omitempty"`
LeadTimeMaximum int `json:"lead_time_maximum,omitempty"`
LeadTimeMinimum int `json:"lead_time_minimum,omitempty"`
LeadTimeSummation int `json:"lead_time_summation,omitempty"`
LeadTimeAverage float64 `json:"lead_time_average,omitempty"`
LeadTimeMedian float64 `json:"lead_time_median,omitempty"`
}

func newLeadTimeStat(lt *usecase.LeadTime) *LeadTimeStat {
return &LeadTimeStat{
TotalPR: len(lt.PRs),
LeadTimeMaximum: lt.Max(),
LeadTimeMinimum: lt.Min(),
LeadTimeSummation: lt.Sum(),
LeadTimeAverage: lt.Average(),
LeadTimeMedian: lt.Median(),
}
}

func (lts *LeadTimeStat) json(w io.Writer) error {
bytes, err := json.Marshal(lts)
if err != nil {
return err
}

fmt.Fprintln(w, string(bytes))

return nil
}

0 comments on commit 1bbcf35

Please # to comment.