Skip to content

Commit

Permalink
feat(analyze): output as csv,tsv,json. close #4
Browse files Browse the repository at this point in the history
  • Loading branch information
crispgm committed Feb 19, 2024
1 parent a35f42b commit 268103c
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 68 deletions.
23 changes: 15 additions & 8 deletions cmd/kicker-cli/cmd/event_analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ import (
)

var (
rankGameMode string
rankMinPlayed int
rankHead int
rankTail int
rankSortBy string
rankPlayerName string
rankGameMode string
rankMinPlayed int
rankHead int
rankTail int
rankSortBy string
rankPlayerName string
rankOutputFormat string
)

func init() {
Expand All @@ -30,6 +31,7 @@ func init() {
analyzeCmd.Flags().IntVarP(&rankHead, "head", "", 0, "display the head part of rank")
analyzeCmd.Flags().IntVarP(&rankTail, "tail", "", 0, "display the last part of rank")
analyzeCmd.Flags().StringVarP(&rankPlayerName, "player", "", "", "Player name for detail only modes")
analyzeCmd.Flags().StringVarP(&rankOutputFormat, "output-format", "f", "default", "Output formats: default, csv, & tsv. Notice: supported output formats may vary according to different operators.")
_ = analyzeCmd.MarkFlagRequired("mode")
analyzeCmd.MarkFlagsMutuallyExclusive("head", "tail")
eventCmd.AddCommand(analyzeCmd)
Expand Down Expand Up @@ -130,8 +132,13 @@ var analyzeCmd = &cobra.Command{
Head: rankHead,
Tail: rankTail,
PlayerName: rankPlayerName,
WithHeader: !globalNoHeaders,
WithBoxes: !globalNoBoxes,

OutputFormat: strings.ToLower(rankOutputFormat),
WithHeader: !globalNoHeaders,
WithBoxes: !globalNoBoxes,
}
if options.OutputFormat != "default" {
pterm.DisableColor()
}
op.Input(eTournaments, instance.Conf.Players, options)
op.Output()
Expand Down
5 changes: 3 additions & 2 deletions internal/operator/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Option struct {
Tail int
PlayerName string

WithHeader bool
WithBoxes bool
WithHeader bool
WithBoxes bool
OutputFormat string
}
27 changes: 13 additions & 14 deletions internal/operator/double_player_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ func (o *DoublePlayerHistory) Output() {
found := false
header := []string{"#", "Event", "Team1", "Team2", "Result", "WR%", "ELO", "KRP", "ATSA", "ITSF"}
table := [][]string{}
if o.options.WithHeader {
table = append(table, header)
}
eloChart := []float64{}
winRateChart := []float64{}
data := make(map[string]entity.Player)
Expand Down Expand Up @@ -221,18 +218,20 @@ func (o *DoublePlayerHistory) Output() {
}

if found {
_ = pterm.DefaultTable.WithHasHeader(o.options.WithHeader).WithData(table).WithBoxed(o.options.WithBoxes).Render()
}
output(o.options, header, table)

if len(eloChart) > 0 {
fmt.Println()
eloGraph := asciigraph.Plot(eloChart, asciigraph.Caption("ELO"), asciigraph.Height(20), asciigraph.Width(80))
fmt.Println(eloGraph)
}
if len(winRateChart) > 0 {
fmt.Println()
winRateGraph := asciigraph.Plot(winRateChart, asciigraph.Caption("Win Rate"), asciigraph.Height(20), asciigraph.Width(80))
fmt.Println(winRateGraph)
if o.options.OutputFormat == "default" {
if len(eloChart) > 0 {
fmt.Println()
eloGraph := asciigraph.Plot(eloChart, asciigraph.Caption("ELO"), asciigraph.Height(20), asciigraph.Width(80))
fmt.Println(eloGraph)
}
if len(winRateChart) > 0 {
fmt.Println()
winRateGraph := asciigraph.Plot(winRateChart, asciigraph.Caption("Win Rate"), asciigraph.Height(20), asciigraph.Width(80))
fmt.Println(winRateGraph)
}
}
}
}

Expand Down
8 changes: 2 additions & 6 deletions internal/operator/double_player_rank.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"fmt"
"sort"

"github.com/pterm/pterm"

"github.com/crispgm/kicker-cli/internal/entity"
"github.com/crispgm/kicker-cli/pkg/ktool/model"
"github.com/crispgm/kicker-cli/pkg/rating"
Expand Down Expand Up @@ -232,9 +230,6 @@ func (o *DoublePlayerRank) Output() {

header := []string{"#", "Name", "Events", "Games", "Win", "Loss", "Draw", "WR%", "ELO", "KRP", "ATSA", "ITSF"}
table := [][]string{}
if o.options.WithHeader {
table = append(table, header)
}
for i, d := range sliceData {
item := []string{
fmt.Sprintf("%d", i+1),
Expand All @@ -252,6 +247,7 @@ func (o *DoublePlayerRank) Output() {
}
table = append(table, item)
}
_ = pterm.DefaultTable.WithHasHeader(o.options.WithHeader).WithData(table).WithBoxed(o.options.WithBoxes).Render()
// }}}

output(o.options, header, table)
}
8 changes: 2 additions & 6 deletions internal/operator/double_team_rank.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"fmt"
"sort"

"github.com/pterm/pterm"

"github.com/crispgm/kicker-cli/internal/entity"
"github.com/crispgm/kicker-cli/pkg/ktool/model"
)
Expand Down Expand Up @@ -117,9 +115,6 @@ func (o *DoubleTeamRank) Output() {

header := []string{"#", "Name", "Num", "Win", "Loss", "Draw", "WR%"}
table := [][]string{}
if o.options.WithHeader {
table = append(table, header)
}
for i, d := range sliceData {
if d.Played == 0 {
continue
Expand All @@ -136,5 +131,6 @@ func (o *DoubleTeamRank) Output() {
}
table = append(table, item)
}
_ = pterm.DefaultTable.WithHasHeader(o.options.WithHeader).WithData(table).WithBoxed(o.options.WithBoxes).Render()

output(o.options, header, table)
}
7 changes: 1 addition & 6 deletions internal/operator/double_team_rival.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"fmt"
"sort"

"github.com/pterm/pterm"

"github.com/crispgm/kicker-cli/internal/entity"
"github.com/crispgm/kicker-cli/pkg/ktool/model"
)
Expand Down Expand Up @@ -135,9 +133,6 @@ func (o *DoubleTeamRival) Output() {

header := []string{"#", "Team1", "Team2", "Num", "Win", "Loss", "Draw"}
table := [][]string{}
if o.options.WithHeader {
table = append(table, header)
}
for i, d := range sliceData {
if d.Played == 0 {
continue
Expand All @@ -154,5 +149,5 @@ func (o *DoubleTeamRival) Output() {
table = append(table, item)
}

_ = pterm.DefaultTable.WithHasHeader(o.options.WithHeader).WithData(table).WithBoxed(o.options.WithBoxes).Render()
output(o.options, header, table)
}
27 changes: 13 additions & 14 deletions internal/operator/single_player_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ func (o *SinglePlayerHistory) Output() {
found := false
header := []string{"#", "Event", "Player 1", "Player 2", "Result", "WR%", "ELO", "KRP", "ATSA", "ITSF"}
table := [][]string{}
if o.options.WithHeader {
table = append(table, header)
}
eloChart := []float64{}
winRateChart := []float64{}
data := make(map[string]entity.Player)
Expand Down Expand Up @@ -187,18 +184,20 @@ func (o *SinglePlayerHistory) Output() {
}

if found {
_ = pterm.DefaultTable.WithHasHeader(o.options.WithHeader).WithData(table).WithBoxed(o.options.WithBoxes).Render()
}
output(o.options, header, table)

if len(eloChart) > 0 {
fmt.Println()
eloGraph := asciigraph.Plot(eloChart, asciigraph.Caption("ELO"), asciigraph.Height(20), asciigraph.Width(80))
fmt.Println(eloGraph)
}
if len(winRateChart) > 0 {
fmt.Println()
winRateGraph := asciigraph.Plot(winRateChart, asciigraph.Caption("Win Rate"), asciigraph.Height(20), asciigraph.Width(80))
fmt.Println(winRateGraph)
if o.options.OutputFormat == "default" {
if len(eloChart) > 0 {
fmt.Println()
eloGraph := asciigraph.Plot(eloChart, asciigraph.Caption("ELO"), asciigraph.Height(20), asciigraph.Width(80))
fmt.Println(eloGraph)
}
if len(winRateChart) > 0 {
fmt.Println()
winRateGraph := asciigraph.Plot(winRateChart, asciigraph.Caption("Win Rate"), asciigraph.Height(20), asciigraph.Width(80))
fmt.Println(winRateGraph)
}
}
}
}

Expand Down
8 changes: 2 additions & 6 deletions internal/operator/single_player_rank.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"fmt"
"sort"

"github.com/pterm/pterm"

"github.com/crispgm/kicker-cli/internal/entity"
"github.com/crispgm/kicker-cli/pkg/ktool/model"
"github.com/crispgm/kicker-cli/pkg/rating"
Expand Down Expand Up @@ -194,9 +192,6 @@ func (o *SinglePlayerRank) Output() {

header := []string{"#", "Name", "Events", "Games", "Win", "Loss", "Draw", "WR%", "ELO", "KRP", "ATSA", "ITSF"}
table := [][]string{}
if o.options.WithHeader {
table = append(table, header)
}
for i, d := range sliceData {
item := []string{
fmt.Sprintf("%d", i+1),
Expand All @@ -214,6 +209,7 @@ func (o *SinglePlayerRank) Output() {
}
table = append(table, item)
}
_ = pterm.DefaultTable.WithHasHeader(o.options.WithHeader).WithData(table).WithBoxed(o.options.WithBoxes).Render()
// }}}

output(o.options, header, table)
}
8 changes: 2 additions & 6 deletions internal/operator/single_player_rival.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"fmt"
"sort"

"github.com/pterm/pterm"

"github.com/crispgm/kicker-cli/internal/entity"
"github.com/crispgm/kicker-cli/pkg/ktool/model"
)
Expand Down Expand Up @@ -122,9 +120,6 @@ func (o *SinglePlayerRival) Output() {

header := []string{"#", "Team1", "Team2", "Num", "Win", "Loss", "Draw"}
table := [][]string{}
if o.options.WithHeader {
table = append(table, header)
}
for i, d := range sliceData {
if d.Played == 0 {
continue
Expand All @@ -140,5 +135,6 @@ func (o *SinglePlayerRival) Output() {
}
table = append(table, item)
}
_ = pterm.DefaultTable.WithHasHeader(o.options.WithHeader).WithData(table).WithBoxed(o.options.WithBoxes).Render()

output(o.options, header, table)
}
48 changes: 48 additions & 0 deletions internal/operator/util.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package operator

import (
"encoding/csv"
"encoding/json"
"os"

"github.com/pterm/pterm"

"github.com/crispgm/kicker-cli/pkg/ktool/model"
"github.com/crispgm/kicker-cli/pkg/rating"
)
Expand Down Expand Up @@ -39,3 +45,45 @@ func openDoubleTournament(trn *model.Tournament) bool {

return false
}

func output(opt Option, header []string, body [][]string) {
var table [][]string
if opt.WithHeader {
table = append(table, header)
table = append(table, body...)
}

if opt.OutputFormat == "default" {
_ = pterm.DefaultTable.WithHasHeader(opt.WithHeader).WithData(table).WithBoxed(opt.WithBoxes).Render()
} else if opt.OutputFormat == "csv" {
csvwriter := csv.NewWriter(os.Stdout)
for _, row := range table {
_ = csvwriter.Write(row)
}
csvwriter.Flush()
} else if opt.OutputFormat == "tsv" {
csvwriter := csv.NewWriter(os.Stdout)
csvwriter.Comma = '\t'
for _, row := range table {
_ = csvwriter.Write(row)
}
csvwriter.Flush()
} else if opt.OutputFormat == "json" {
var jsonData []map[string]string
for _, row := range body {
jsonItem := make(map[string]string)
for i, item := range row {
key := header[i]
if key == "#" {
key = "Index"
} else if key == "WR%" {
key = "WinRate"
}
jsonItem[key] = item
}
jsonData = append(jsonData, jsonItem)
}
encoder := json.NewEncoder(os.Stdout)
_ = encoder.Encode(jsonData)
}
}

0 comments on commit 268103c

Please sign in to comment.