Skip to content

Commit

Permalink
feat: refactor events (#45)
Browse files Browse the repository at this point in the history
* feat: refactor events

* chore: fix linting

* chore: fix tests

* chore: serialize errors to json correctly

* chore: fix tests
  • Loading branch information
freak12techno authored Apr 24, 2023
1 parent ba25bcd commit 6c7c987
Show file tree
Hide file tree
Showing 26 changed files with 368 additions and 210 deletions.
32 changes: 32 additions & 0 deletions pkg/events/not_voted.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package events

import (
configTypes "main/pkg/config/types"
"main/pkg/types"
)

type NotVotedEvent struct {
Chain *configTypes.Chain
Wallet *configTypes.Wallet
Proposal types.Proposal
}

func (e NotVotedEvent) Name() string {
return "not_voted"
}

func (e NotVotedEvent) IsAlert() bool {
return true
}

func (e NotVotedEvent) GetChain() *configTypes.Chain {
return e.Chain
}

func (e NotVotedEvent) GetProposal() types.Proposal {
return e.Proposal
}

func (e NotVotedEvent) GetWallet() *configTypes.Wallet {
return e.Wallet
}
18 changes: 18 additions & 0 deletions pkg/events/proposals_query_error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package events

import (
configTypes "main/pkg/config/types"
)

type ProposalsQueryErrorEvent struct {
Chain *configTypes.Chain
Error error
}

func (e ProposalsQueryErrorEvent) Name() string {
return "proposals_query_error"
}

func (e ProposalsQueryErrorEvent) IsAlert() bool {
return false
}
34 changes: 34 additions & 0 deletions pkg/events/revoted.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package events

import (
configTypes "main/pkg/config/types"
"main/pkg/types"
)

type RevotedEvent struct {
Chain *configTypes.Chain
Wallet *configTypes.Wallet
Proposal types.Proposal
Vote *types.Vote
OldVote *types.Vote
}

func (e RevotedEvent) Name() string {
return "revoted"
}

func (e RevotedEvent) IsAlert() bool {
return true
}

func (e RevotedEvent) GetChain() *configTypes.Chain {
return e.Chain
}

func (e RevotedEvent) GetProposal() types.Proposal {
return e.Proposal
}

func (e RevotedEvent) GetWallet() *configTypes.Wallet {
return e.Wallet
}
20 changes: 20 additions & 0 deletions pkg/events/vote_query_error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package events

import (
configTypes "main/pkg/config/types"
"main/pkg/types"
)

type VoteQueryError struct {
Chain *configTypes.Chain
Proposal types.Proposal
Error error
}

func (e VoteQueryError) Name() string {
return "vote_query_error"
}

func (e VoteQueryError) IsAlert() bool {
return true
}
33 changes: 33 additions & 0 deletions pkg/events/voted.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package events

import (
configTypes "main/pkg/config/types"
"main/pkg/types"
)

type VotedEvent struct {
Chain *configTypes.Chain
Wallet *configTypes.Wallet
Proposal types.Proposal
Vote *types.Vote
}

func (e VotedEvent) Name() string {
return "voted"
}

func (e VotedEvent) IsAlert() bool {
return true
}

func (e VotedEvent) GetChain() *configTypes.Chain {
return e.Chain
}

func (e VotedEvent) GetProposal() types.Proposal {
return e.Proposal
}

func (e VotedEvent) GetWallet() *configTypes.Wallet {
return e.Wallet
}
18 changes: 18 additions & 0 deletions pkg/report/entry/entry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package entry

import (
configTypes "main/pkg/config/types"
"main/pkg/types"
)

type ReportEntry interface {
Name() string
IsAlert() bool // only voted/not_voted are alerts, required for PagerDuty
}

type ReportEntryNotError interface {
ReportEntry
GetChain() *configTypes.Chain
GetWallet() *configTypes.Wallet
GetProposal() types.Proposal
}
65 changes: 30 additions & 35 deletions pkg/report/generator.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package report

import (
"time"

configTypes "main/pkg/config/types"
"main/pkg/events"
"main/pkg/report/entry"
"main/pkg/reporters"
"main/pkg/state"
"main/pkg/tendermint"
"main/pkg/types"

"github.com/rs/zerolog"
)
Expand All @@ -32,20 +31,17 @@ func NewReportGenerator(
}

func (g *Generator) GenerateReport(oldState, newState state.State) reporters.Report {
entries := []reporters.ReportEntry{}
entries := []entry.ReportEntry{}

for chainName, chainInfo := range newState.ChainInfos {
if chainInfo.HasProposalsError() {
g.Logger.Debug().
Str("chain", chainName).
Msg("Error querying for proposals - sending an alert")
entry := reporters.ReportEntry{
Chain: g.Chains.FindByName(chainName),
ProposalVoteEndingTime: time.Now(),
Type: types.ProposalQueryError,
Value: chainInfo.ProposalsError,
}
entries = append(entries, entry)
entries = append(entries, events.ProposalsQueryErrorEvent{
Chain: g.Chains.FindByName(chainName),
Error: chainInfo.ProposalsError,
})
continue
}

Expand All @@ -60,26 +56,19 @@ func (g *Generator) GenerateReport(oldState, newState state.State) reporters.Rep
oldVote, _, _ := oldState.GetVoteAndProposal(chainName, proposalID, wallet)
newVote, proposal, _ := newState.GetVoteAndProposal(chainName, proposalID, wallet)

entry := reporters.ReportEntry{
Chain: g.Chains.FindByName(chainName),
Wallet: newVote.Wallet,
ProposalID: proposalID,
ProposalTitle: proposal.Content.Title,
ProposalDescription: proposal.Content.Description,
ProposalVoteEndingTime: proposal.VotingEndTime,
}

// Error querying for vote - need to notify via Telegram.
if newVote.IsError() {
g.Logger.Debug().
Str("chain", chainName).
Str("proposal", proposalID).
Str("wallet", wallet).
Msg("Error querying for vote - sending an alert")
entry.Type = types.VoteQueryError
entry.Value = newVote.Error
entries = append(entries, events.VoteQueryError{
Chain: g.Chains.FindByName(chainName),
Proposal: proposal,
Error: newVote.Error,
})

entries = append(entries, entry)
continue
}

Expand All @@ -90,25 +79,28 @@ func (g *Generator) GenerateReport(oldState, newState state.State) reporters.Rep
Str("proposal", proposalID).
Str("wallet", wallet).
Msg("Wallet hasn't voted now - sending an alert")
entry.Type = types.NotVoted
entries = append(entries, entry)
entries = append(entries, events.NotVotedEvent{
Chain: g.Chains.FindByName(chainName),
Wallet: newVote.Wallet,
Proposal: proposal,
})
continue
}

// Hasn't voted before but voted now - need to close alert/notify about new vote.
if newVote.HasVoted() && !oldVote.HasVoted() {
vote := *newVote.Vote

g.Logger.Debug().
Str("chain", chainName).
Str("proposal", proposal.ProposalID).
Str("wallet", wallet).
Msg("Wallet hasn't voted before but voted now - closing an alert")

entry.Type = types.Voted
entry.Value = vote.Option

entries = append(entries, entry)
entries = append(entries, events.VotedEvent{
Chain: g.Chains.FindByName(chainName),
Wallet: newVote.Wallet,
Proposal: proposal,
Vote: newVote.Vote,
})
continue
}

Expand All @@ -119,11 +111,14 @@ func (g *Generator) GenerateReport(oldState, newState state.State) reporters.Rep
Str("proposal", proposal.ProposalID).
Str("wallet", wallet).
Msg("Wallet changed its vote - sending an alert")
entry.Type = types.Revoted
entry.Value = newVote.Vote.Option
entry.OldValue = oldVote.Vote.Option

entries = append(entries, entry)
entries = append(entries, events.RevotedEvent{
Chain: g.Chains.FindByName(chainName),
Wallet: newVote.Wallet,
Proposal: proposal,
Vote: newVote.Vote,
OldVote: oldVote.Vote,
})
}
}
}
Expand Down
Loading

0 comments on commit 6c7c987

Please sign in to comment.