diff --git a/distribution.go b/distribution.go index 2cfa391..e672f99 100644 --- a/distribution.go +++ b/distribution.go @@ -8,6 +8,7 @@ import ( "github.com/go-echarts/go-echarts/v2/charts" "github.com/go-echarts/go-echarts/v2/opts" "github.com/olekukonko/tablewriter" + "github.com/pkg/browser" sdk "github.com/cosmos/cosmos-sdk/types" govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" @@ -218,63 +219,17 @@ func (m voteMap) add(v govtypes.VoteOption, d sdk.Dec) { func printAirdropsStats(chartMode bool, airdrops []airdrop) error { if chartMode { - f, _ := os.Create("bar.html") + f, err := os.CreateTemp("", "chart*.html") + if err != nil { + return err + } + defer f.Close() + renderChart(f, "$ATOM distribution", airdrops[0].atom) for _, airdrop := range airdrops { - pie := charts.NewPie() - pie.SetGlobalOptions( - charts.WithTitleOpts(opts.Title{ - Title: fmt.Sprintf("$ATONE Distribution %s", airdrop.params), - }), - charts.WithLegendOpts(opts.Legend{ - Show: false, - }), - ) - var ( - data = make([]opts.PieData, 6) - votePercs = airdrop.atone.votePercentages() - oneHundred = sdk.NewDec(100) - ) - data[0] = opts.PieData{ - Name: "Yes", - ItemStyle: &opts.ItemStyle{Color: "#ff6f69"}, - Value: votePercs[govtypes.OptionYes].Mul(oneHundred).RoundInt64(), - } - data[1] = opts.PieData{ - Name: "No", - ItemStyle: &opts.ItemStyle{Color: "#96ceb4"}, - Value: votePercs[govtypes.OptionNo].Mul(oneHundred).RoundInt64(), - } - data[2] = opts.PieData{ - Name: "NWV", - ItemStyle: &opts.ItemStyle{Color: "#87b9a2"}, - Value: votePercs[govtypes.OptionNoWithVeto].Mul(oneHundred).RoundInt64(), - } - data[3] = opts.PieData{ - Name: "Abstain", - ItemStyle: &opts.ItemStyle{Color: "#ffcc5c"}, - Value: votePercs[govtypes.OptionAbstain].Mul(oneHundred).RoundInt64(), - } - data[4] = opts.PieData{ - Name: "DNV", - ItemStyle: &opts.ItemStyle{Color: "#ffeead"}, - Value: votePercs[govtypes.OptionEmpty].Mul(oneHundred).RoundInt64(), - } - data[5] = opts.PieData{ - Name: "Unstaked", - ItemStyle: &opts.ItemStyle{Color: "#fff8de"}, - Value: airdrop.atone.unstaked.Quo(airdrop.atone.supply).Mul(oneHundred).RoundInt64(), - } - pie.AddSeries("pie", data). - SetSeriesOptions(charts.WithLabelOpts( - opts.Label{ - Show: true, - Formatter: "{b}: {c}%", - }), - ) - if err := pie.Render(f); err != nil { - return err - } + renderChart(f, fmt.Sprintf("$ATONE distribution %s", airdrop.params), airdrop.atone) } + fmt.Printf("Charts rendered in %s\n", f.Name()) + browser.OpenFile(f.Name()) return nil } @@ -320,3 +275,61 @@ func printAirdropsStats(chartMode bool, airdrops []airdrop) error { } return nil } + +func renderChart(f *os.File, title string, d distrib) error { + pie := charts.NewPie() + pie.SetGlobalOptions( + charts.WithTitleOpts(opts.Title{ + Title: title, + }), + charts.WithLegendOpts(opts.Legend{ + Show: false, + }), + ) + var ( + data = make([]opts.PieData, 6) + votePercs = d.votePercentages() + oneHundred = sdk.NewDec(100) + ) + data[0] = opts.PieData{ + Name: "Yes", + ItemStyle: &opts.ItemStyle{Color: "#ff6f69"}, + Value: votePercs[govtypes.OptionYes].Mul(oneHundred).RoundInt64(), + } + data[1] = opts.PieData{ + Name: "No", + ItemStyle: &opts.ItemStyle{Color: "#96ceb4"}, + Value: votePercs[govtypes.OptionNo].Mul(oneHundred).RoundInt64(), + } + data[2] = opts.PieData{ + Name: "NWV", + ItemStyle: &opts.ItemStyle{Color: "#87b9a2"}, + Value: votePercs[govtypes.OptionNoWithVeto].Mul(oneHundred).RoundInt64(), + } + data[3] = opts.PieData{ + Name: "Abstain", + ItemStyle: &opts.ItemStyle{Color: "#ffcc5c"}, + Value: votePercs[govtypes.OptionAbstain].Mul(oneHundred).RoundInt64(), + } + data[4] = opts.PieData{ + Name: "DNV", + ItemStyle: &opts.ItemStyle{Color: "#ffeead"}, + Value: votePercs[govtypes.OptionEmpty].Mul(oneHundred).RoundInt64(), + } + data[5] = opts.PieData{ + Name: "Unstaked", + ItemStyle: &opts.ItemStyle{Color: "#fff8de"}, + Value: d.unstaked.Quo(d.supply).Mul(oneHundred).RoundInt64(), + } + pie.AddSeries("pie", data). + SetSeriesOptions(charts.WithLabelOpts( + opts.Label{ + Show: true, + Formatter: "{b}: {c}%", + }), + ) + if err := pie.Render(f); err != nil { + return err + } + return nil +} diff --git a/go.mod b/go.mod index 4e743be..d36bbff 100644 --- a/go.mod +++ b/go.mod @@ -10,6 +10,7 @@ require ( github.com/gogo/protobuf v1.3.3 github.com/olekukonko/tablewriter v0.0.5 github.com/peterbourgon/ff/v3 v3.4.0 + github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c github.com/stretchr/testify v1.8.1 github.com/tendermint/tendermint v0.34.27 ) diff --git a/go.sum b/go.sum index 7afea04..0631782 100644 --- a/go.sum +++ b/go.sum @@ -545,6 +545,8 @@ github.com/petermattis/goid v0.0.0-20180202154549-b0b1615b78e5 h1:q2e307iGHPdTGp github.com/petermattis/goid v0.0.0-20180202154549-b0b1615b78e5/go.mod h1:jvVRKCrJTQWu0XVbaOlby/2lO20uSCHEMzzplHXte1o= github.com/pingcap/errors v0.11.4 h1:lFuQV/oaUMGcD2tqt+01ROSmJs75VG1ToEOkZIZ4nE4= github.com/pingcap/errors v0.11.4/go.mod h1:Oi8TUi2kEtXXLMJk9l1cGmz20kV3TaQ0usTwv5KuLY8= +github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c h1:+mdjkGKdHQG3305AYmdv1U2eRNDiU2ErMBj1gwrq8eQ= +github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c/go.mod h1:7rwL4CYBLnjLxUqIJNnCWiEdr3bn6IUYi15bNlnbCCU= github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= @@ -899,6 +901,7 @@ golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220209214540-3681064d5158/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=