Skip to content

Commit

Permalink
feat: allow specifying timezone in config
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno committed Dec 14, 2023
1 parent 471f7fa commit 80ad232
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 16 deletions.
2 changes: 2 additions & 0 deletions config.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ mutes-path = "cosmos-proposals-checker-mutes.json"
# See https://pkg.go.dev/github.com/robfig/cron?utm_source=godoc#hdr-CRON_Expression_Format for examples.
# Defaults to "@hourly"
interval = "@hourly"
# Timezone in which time (like undelegation finish time) will be displayed. Defaults to "Etc/GMT", so UTC+0
timezone = "Europe/Moscow"

# Logging configuration
[log]
Expand Down
5 changes: 4 additions & 1 deletion pkg/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"main/pkg/reporters/telegram"
"main/pkg/state"
"main/pkg/types"
"time"

"github.com/robfig/cron/v3"
"github.com/rs/zerolog"
Expand Down Expand Up @@ -43,6 +44,8 @@ func NewApp(configPath string, version string) *App {
stateGenerator := state.NewStateGenerator(log, config.Chains)
dataManager := data.NewManager(log, config.Chains)

timeZone, _ := time.LoadLocation(config.Timezone)

return &App{
Logger: log,
Config: config,
Expand All @@ -52,7 +55,7 @@ func NewApp(configPath string, version string) *App {
StateGenerator: stateGenerator,
Reporters: []reportersPkg.Reporter{
pagerduty.NewPagerDutyReporter(config.PagerDutyConfig, log),
telegram.NewTelegramReporter(config.TelegramConfig, mutesManager, stateGenerator, dataManager, log, version),
telegram.NewTelegramReporter(config.TelegramConfig, mutesManager, stateGenerator, dataManager, log, version, timeZone),
},
}
}
Expand Down
4 changes: 0 additions & 4 deletions pkg/mutes/mutes.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,6 @@ func (m *Mutes) IsMuted(chain string, proposalID string) bool {
return false
}

func (m *Mute) GetExpirationTime() string {
return m.Expires.Format(time.RFC822)
}

func (m *Mutes) AddMute(mute *Mute) {
m.Mutes = append(m.Mutes, mute)
m.Mutes = utils.Filter(m.Mutes, func(m *Mute) bool {
Expand Down
10 changes: 9 additions & 1 deletion pkg/reporters/telegram/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ type Reporter struct {
Logger zerolog.Logger
Templates map[string]*template.Template

Version string
Version string
Timezone *time.Location
}

const (
Expand All @@ -45,6 +46,7 @@ func NewTelegramReporter(
dataManager *data.Manager,
logger *zerolog.Logger,
version string,
timezone *time.Location,
) *Reporter {
return &Reporter{
TelegramToken: config.TelegramToken,
Expand All @@ -55,6 +57,7 @@ func NewTelegramReporter(
Logger: logger.With().Str("component", "telegram_reporter").Logger(),
Templates: make(map[string]*template.Template, 0),
Version: version,
Timezone: timezone,
}
}

Expand Down Expand Up @@ -104,6 +107,7 @@ func (reporter *Reporter) GetTemplate(tmlpType string) (*template.Template, erro

t, err := template.New(filename).Funcs(template.FuncMap{
"SerializeLink": reporter.SerializeLink,
"SerializeDate": reporter.SerializeDate,
"FormatDuration": utils.FormatDuration,
}).ParseFS(templates.TemplatesFs, "telegram/"+filename)
if err != nil {
Expand Down Expand Up @@ -251,3 +255,7 @@ func (reporter *Reporter) SerializeLink(link types.Link) template.HTML {

return template.HTML(link.Name)
}

func (reporter *Reporter) SerializeDate(date time.Time) string {
return date.In(reporter.Timezone).Format(time.RFC1123)
}
10 changes: 9 additions & 1 deletion pkg/types/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package types

import "fmt"
import (
"fmt"
"time"
)

type Config struct {
PagerDutyConfig PagerDutyConfig `toml:"pagerduty"`
Expand All @@ -9,6 +12,7 @@ type Config struct {
StatePath string `toml:"state-path"`
MutesPath string `toml:"mutes-path"`
Chains Chains `toml:"chains"`
Timezone string `toml:"timezone"`
Interval string `default:"* * * * *" toml:"interval"`
}

Expand All @@ -33,5 +37,9 @@ func (c *Config) Validate() error {
}
}

if _, err := time.LoadLocation(c.Timezone); err != nil {
return fmt.Errorf("error parsing timezone: %s", err)
}

return nil
}
19 changes: 19 additions & 0 deletions pkg/types/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,29 @@ func TestValidateConfigWrongProposalType(t *testing.T) {
require.Error(t, err, nil, "Error should be presented!")
}

func TestValidateConfigInvalidTimezone(t *testing.T) {
t.Parallel()

config := Config{
Timezone: "test",
Chains: []*Chain{
{
Name: "chain",
LCDEndpoints: []string{"endpoint"},
Wallets: []*Wallet{{Address: "wallet"}},
ProposalsType: "v1",
},
},
}
err := config.Validate()
require.Error(t, err, nil, "Error should be presented!")
}

func TestValidateConfigValidChain(t *testing.T) {
t.Parallel()

config := Config{
Timezone: "Europe/Moscow",
Chains: []*Chain{
{
Name: "chain",
Expand Down
4 changes: 0 additions & 4 deletions pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ func (p Proposal) GetTimeLeft() string {
return utils.FormatDuration(time.Until(p.EndTime).Round(time.Second))
}

func (p Proposal) GetProposalTime() string {
return p.EndTime.Format(time.RFC1123)
}

type TallyInfo struct {
Proposal Proposal
Tally Tally
Expand Down
2 changes: 1 addition & 1 deletion templates/telegram/mute_added.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
{{- else }}
<strong>Proposal ID:</strong> all proposals
{{- end }}
<strong>Expires: </strong>{{ .GetExpirationTime }}
<strong>Expires: </strong>{{ SerializeDate .Expires }}
2 changes: 1 addition & 1 deletion templates/telegram/mutes.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
{{- else }}
<strong>Proposal ID:</strong> all proposals
{{- end }}
<strong>Expires: </strong>{{ .GetExpirationTime }}
<strong>Expires: </strong>{{ SerializeDate .Expires }}
{{ end }}
2 changes: 1 addition & 1 deletion templates/telegram/not_voted.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
🔴 <strong> Wallet {{ SerializeLink $walletLink }} hasn't voted on proposal {{ .Proposal.ID }} on {{ .Chain.GetName }} </strong>
{{ .Proposal.Title }}

Voting ends at: {{ .Proposal.GetProposalTime }} (in {{ .Proposal.GetTimeLeft }})
Voting ends at: {{ SerializeDate .Proposal.EndTime }} (in {{ .Proposal.GetTimeLeft }})

{{ range .Chain.GetExplorerProposalsLinks .Proposal.ID }}{{ SerializeLink .}}
{{ end }}
Expand Down
2 changes: 1 addition & 1 deletion templates/telegram/revoted.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

Vote: {{ .Vote.ResolveVote }}
Old vote: {{ .OldVote.ResolveVote }}
Voting ends at: {{ .Proposal.GetProposalTime }} (in {{ .Proposal.GetTimeLeft }})
Voting ends at: {{ SerializeDate .Proposal.EndTime }} (in {{ .Proposal.GetTimeLeft }})

{{ range .Chain.GetExplorerProposalsLinks .Proposal.ID }}{{ SerializeLink .}}
{{ end }}
Expand Down
2 changes: 1 addition & 1 deletion templates/telegram/voted.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{{ .Proposal.Title }}

Vote: {{ .Vote.ResolveVote }}
Voting ends at: {{ .Proposal.GetProposalTime }} (in {{ .Proposal.GetTimeLeft }})
Voting ends at: {{ SerializeDate .Proposal.EndTime }} (in {{ .Proposal.GetTimeLeft }})

{{ range .Chain.GetExplorerProposalsLinks .Proposal.ID }}{{ SerializeLink .}}
{{ end }}
Expand Down

0 comments on commit 80ad232

Please sign in to comment.