Skip to content

Commit

Permalink
Return each proposal voting status
Browse files Browse the repository at this point in the history
  • Loading branch information
maiquanghiep committed Nov 15, 2022
1 parent c3bd782 commit fdc1e37
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 29 deletions.
52 changes: 26 additions & 26 deletions collector/active_proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package collector
import (
"context"
"log"
"strconv"
"sync"

govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
Expand All @@ -27,35 +28,34 @@ func (collector *CosmosSDKCollector) CollectActiveProposal() {
countProposalType := make(map[string]float64)
for _, proposal := range govRes.Proposals {
countProposalType[proposal.Content.TypeUrl] += 1
// Vote status
var wg sync.WaitGroup
for _, address := range collector.accAddresses {
wg.Add(1)
go func(address string) {
defer wg.Done()
vote, err := govClient.Vote(
context.Background(),
&govtypes.QueryVoteRequest{
ProposalId: proposal.ProposalId,
Voter: address,
},
)
vote.GetVote()

// When the voter_address hasn't voted, the query returns "not found for proposal" error
if err != nil {
VotedActiveProposalGauge.WithLabelValues(collector.chainID, address, strconv.FormatUint(proposal.ProposalId, 10)).Set(float64(0))
return
}

VotedActiveProposalGauge.WithLabelValues(collector.chainID, address, strconv.FormatUint(proposal.ProposalId, 10)).Set(float64(1))
}(address)
}
wg.Wait()
}

for key, total := range countProposalType {
ActiveProposalGauge.WithLabelValues(collector.chainID, key).Set(float64(total))
}

// Voted active proposal
var wg sync.WaitGroup

for _, address := range collector.accAddresses {
wg.Add(1)
go func(address string) {
defer wg.Done()
votedGovRes, err := govClient.Proposals(
context.Background(),
&govtypes.QueryProposalsRequest{
ProposalStatus: govtypes.StatusVotingPeriod,
Voter: address,
},
)

if err != nil {
ErrorGauge.WithLabelValues("tendermint_active_proposals_total").Inc()
log.Print(err)
return
}

VotedActiveProposalGauge.WithLabelValues(collector.chainID, address).Set(float64(float64(len(votedGovRes.Proposals))))
}(address)
}
wg.Wait()
}
6 changes: 3 additions & 3 deletions collector/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ var (

VotedActiveProposalGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "tendermint_voted_active_proposals_total",
Help: "Total active proposals on chain that voter_address voted",
Name: "tendermint_active_proposals_vote_status",
Help: "Voter_address's vote status, return 1 if voted, return 0 if not voted",
},
[]string{"chain_id", "voter_address"},
[]string{"chain_id", "voter_address", "proposal_id"},
)

AvailableBalanceGauge = prometheus.NewGaugeVec(
Expand Down

0 comments on commit fdc1e37

Please sign in to comment.