Skip to content

Commit

Permalink
distri chart: open browser, move file to tmp, print $ATOM distri
Browse files Browse the repository at this point in the history
  • Loading branch information
tbruyelle committed Mar 27, 2024
1 parent 2be565f commit cc8cf34
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 55 deletions.
123 changes: 68 additions & 55 deletions distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down

0 comments on commit cc8cf34

Please sign in to comment.