Skip to content

Commit

Permalink
feat: configure timezone per reporter (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno authored Jan 6, 2025
1 parent 34e76e4 commit e4134c4
Show file tree
Hide file tree
Showing 14 changed files with 69 additions and 55 deletions.
6 changes: 3 additions & 3 deletions config.example.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# Path to where aliases in .yml will be stored.
# If omitted, no aliases setting/displaying would work.
aliases: cosmos-transactions-bot-aliases.yml
# Timezone in which time (like undelegation finish time) will be displayed.
# Defaults to "Etc/GMT", so UTC+0
timezone: Europe/Moscow
# Prometheus metrics configuration.
metrics:
# Whether to enable Prometheus metrics. Defaults to true.
Expand All @@ -28,6 +25,9 @@ reporters:
- name: telegram-1
# Reporter type. Currently, the only supported type is "telegram", which is the default.
type: telegram
# Timezone in which time (like undelegation finish time) will be displayed for this reporter.
# Defaults to "Etc/GMT", so UTC+0
timezone: Europe/Moscow
# Telegram config configuration. Required if the type is "telegram".
# See README.md for more details.
# Has 3 params:
Expand Down
6 changes: 0 additions & 6 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package config

import (
"main/pkg/fs"
"time"

"gopkg.in/guregu/null.v4"

Expand All @@ -21,7 +20,6 @@ type AppConfig struct {
Subscriptions types.Subscriptions
Reporters types.Reporters
Metrics MetricsConfig
Timezone *time.Location
}

type LogConfig struct {
Expand Down Expand Up @@ -59,8 +57,6 @@ func GetConfig(path string, filesystem fs.FS) (*AppConfig, error) {
}

func FromYamlConfig(c *yamlConfig.YamlConfig) *AppConfig {
timezone, _ := time.LoadLocation(c.Timezone)

return &AppConfig{
AliasesPath: c.AliasesPath,
LogConfig: LogConfig{
Expand All @@ -80,7 +76,6 @@ func FromYamlConfig(c *yamlConfig.YamlConfig) *AppConfig {
Subscriptions: utils.Map(c.Subscriptions, func(s *yamlConfig.Subscription) *types.Subscription {
return s.ToAppConfigSubscription()
}),
Timezone: timezone,
}
}

Expand All @@ -98,7 +93,6 @@ func (c *AppConfig) ToYamlConfig() *yamlConfig.YamlConfig {
Chains: utils.Map(c.Chains, yamlConfig.FromAppConfigChain),
Reporters: utils.Map(c.Reporters, yamlConfig.FromAppConfigReporter),
Subscriptions: utils.Map(c.Subscriptions, yamlConfig.FromAppConfigSubscription),
Timezone: c.Timezone.String(),
}
}

Expand Down
1 change: 0 additions & 1 deletion pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ func TestConfigDisplayAsYaml(t *testing.T) {
require.EqualValues(t, config.LogConfig, configAgain.LogConfig)
require.EqualValues(t, config.AliasesPath, configAgain.AliasesPath)
require.EqualValues(t, config.Metrics, configAgain.Metrics)
require.EqualValues(t, config.Timezone, configAgain.Timezone)

require.Equal(t, len(config.Chains), len(configAgain.Chains))
for index := range config.Chains {
Expand Down
3 changes: 3 additions & 0 deletions pkg/config/types/reporter.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package types

import "time"

type Reporters []*Reporter

type TelegramConfig struct {
Expand All @@ -12,5 +14,6 @@ type Reporter struct {
Name string
Type string

Timezone *time.Location
TelegramConfig *TelegramConfig
}
14 changes: 12 additions & 2 deletions pkg/config/yaml_config/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"main/pkg/constants"
"main/pkg/utils"
"strings"
"time"
)

type TelegramConfig struct {
Expand All @@ -16,8 +17,9 @@ type TelegramConfig struct {
}

type Reporter struct {
Name string `yaml:"name"`
Type string `default:"telegram" yaml:"type"`
Name string `yaml:"name"`
Type string `default:"telegram" yaml:"type"`
Timezone string `default:"Etc/GMT" yaml:"timezone"`

TelegramConfig *TelegramConfig `yaml:"telegram-config"`
}
Expand All @@ -27,6 +29,10 @@ func (reporter *Reporter) Validate() error {
return errors.New("reporter name not provided")
}

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

reporterTypes := constants.GetReporterTypes()
if !utils.Contains(reporterTypes, reporter.Type) {
return fmt.Errorf(
Expand Down Expand Up @@ -90,6 +96,7 @@ func FromAppConfigReporter(reporter *types.Reporter) *Reporter {
return &Reporter{
Name: reporter.Name,
Type: reporter.Type,
Timezone: reporter.Timezone.String(),
TelegramConfig: telegramConfig,
}
}
Expand All @@ -105,9 +112,12 @@ func (reporter *Reporter) ToAppConfigReporter() *types.Reporter {
}
}

timezone, _ := time.LoadLocation(reporter.Timezone)

return &types.Reporter{
Name: reporter.Name,
Type: reporter.Type,
Timezone: timezone,
TelegramConfig: telegramConfig,
}
}
26 changes: 22 additions & 4 deletions pkg/config/yaml_config/reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"main/pkg/config/types"
yamlConfig "main/pkg/config/yaml_config"
"testing"
"time"

"github.com/stretchr/testify/require"
)
Expand All @@ -15,12 +16,20 @@ func TestReporterNoName(t *testing.T) {
require.Error(t, reporter.Validate())
}

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

reporter := yamlConfig.Reporter{Name: "test", Timezone: "invalid"}
require.Error(t, reporter.Validate())
}

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

reporter := yamlConfig.Reporter{
Name: "test",
Type: "unsupported",
Name: "test",
Type: "unsupported",
Timezone: "Etc/GMT",
}
require.Error(t, reporter.Validate())
}
Expand All @@ -29,8 +38,9 @@ func TestReporterNoTelegramConfig(t *testing.T) {
t.Parallel()

reporter := yamlConfig.Reporter{
Name: "test",
Type: "telegram",
Name: "test",
Type: "telegram",
Timezone: "Etc/GMT",
}
require.Error(t, reporter.Validate())
}
Expand All @@ -46,6 +56,7 @@ func TestReporterValidTelegram(t *testing.T) {
Token: "xxx:yyy",
Admins: []int64{123},
},
Timezone: "Etc/GMT",
}
require.NoError(t, reporter.Validate())
}
Expand Down Expand Up @@ -127,6 +138,7 @@ func TestReporterToAppConfigReporter(t *testing.T) {
Token: "xxx:yyy",
Admins: []int64{123},
},
Timezone: "Etc/GMT",
}
appConfigReporter := reporter.ToAppConfigReporter()

Expand All @@ -135,11 +147,15 @@ func TestReporterToAppConfigReporter(t *testing.T) {
require.Equal(t, int64(1), appConfigReporter.TelegramConfig.Chat)
require.Equal(t, "xxx:yyy", appConfigReporter.TelegramConfig.Token)
require.Equal(t, []int64{123}, appConfigReporter.TelegramConfig.Admins)
require.Equal(t, "Etc/GMT", appConfigReporter.Timezone.String())
}

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

timezone, err := time.LoadLocation("Etc/GMT")
require.NoError(t, err)

reporter := &types.Reporter{
Name: "test",
Type: "telegram",
Expand All @@ -148,6 +164,7 @@ func TestReporterToYamlConfigReporter(t *testing.T) {
Token: "xxx:yyy",
Admins: []int64{123},
},
Timezone: timezone,
}
yamlConfigReporter := yamlConfig.FromAppConfigReporter(reporter)

Expand All @@ -156,4 +173,5 @@ func TestReporterToYamlConfigReporter(t *testing.T) {
require.Equal(t, int64(1), yamlConfigReporter.TelegramConfig.Chat)
require.Equal(t, "xxx:yyy", yamlConfigReporter.TelegramConfig.Token)
require.Equal(t, []int64{123}, yamlConfigReporter.TelegramConfig.Admins)
require.Equal(t, "Etc/GMT", yamlConfigReporter.Timezone)
}
6 changes: 0 additions & 6 deletions pkg/config/yaml_config/yaml_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package yaml_config

import (
"fmt"
"time"

"gopkg.in/guregu/null.v4"
)
Expand All @@ -13,7 +12,6 @@ type YamlConfig struct {
MetricsConfig MetricsConfig `yaml:"metrics"`
Chains Chains `yaml:"chains"`
Subscriptions Subscriptions `yaml:"subscriptions"`
Timezone string `default:"Etc/GMT" yaml:"timezone"`

Reporters Reporters `yaml:"reporters"`
}
Expand All @@ -28,10 +26,6 @@ func (c *YamlConfig) Validate() error {
return fmt.Errorf("no chains provided")
}

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

if err := c.Chains.Validate(); err != nil {
return fmt.Errorf("error in chains: %s", err)
}
Expand Down
18 changes: 0 additions & 18 deletions pkg/config/yaml_config/yaml_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,13 @@ func TestYamlConfigNoChains(t *testing.T) {
require.Error(t, config.Validate())
}

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

config := yamlConfig.YamlConfig{
Chains: yamlConfig.Chains{
{},
},
Timezone: "invalid",
}
require.Error(t, config.Validate())
}

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

config := yamlConfig.YamlConfig{
Chains: yamlConfig.Chains{
{},
},
Timezone: "Etc/UTC",
}
require.Error(t, config.Validate())
}
Expand All @@ -54,7 +41,6 @@ func TestYamlConfigInvalidReporter(t *testing.T) {
Reporters: yamlConfig.Reporters{
{},
},
Timezone: "Etc/UTC",
}
require.Error(t, config.Validate())
}
Expand Down Expand Up @@ -86,7 +72,6 @@ func TestYamlConfigInvalidSubscription(t *testing.T) {
Subscriptions: yamlConfig.Subscriptions{
{},
},
Timezone: "Etc/UTC",
}
require.Error(t, config.Validate())
}
Expand Down Expand Up @@ -124,7 +109,6 @@ func TestYamlConfigChainSubscriptionChainNotFound(t *testing.T) {
},
},
},
Timezone: "Etc/UTC",
}
require.Error(t, config.Validate())
}
Expand Down Expand Up @@ -162,7 +146,6 @@ func TestYamlConfigSubscriptionReporterNotFound(t *testing.T) {
},
},
},
Timezone: "Etc/UTC",
}
require.Error(t, config.Validate())
}
Expand Down Expand Up @@ -200,7 +183,6 @@ func TestYamlConfigValid(t *testing.T) {
},
},
},
Timezone: "Etc/UTC",
}
require.NoError(t, config.Validate())
}
9 changes: 6 additions & 3 deletions pkg/reporters/telegram/get_aliases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ func TestGetAliasesDisabled(t *testing.T) {
Name: "reporter",
Type: "telegram",
TelegramConfig: &configTypes.TelegramConfig{Token: "xxx:yyy", Chat: 123, Admins: []int64{1}},
Timezone: timezone,
},
&configPkg.AppConfig{Timezone: timezone},
&configPkg.AppConfig{},
logger,
nil,
alias_manager.NewAliasManager(logger, &configPkg.AppConfig{}, &fs.MockFs{}),
Expand Down Expand Up @@ -101,8 +102,9 @@ func TestGetAliasesCouldNotFindSubscription(t *testing.T) {
Name: "reporter",
Type: "telegram",
TelegramConfig: &configTypes.TelegramConfig{Token: "xxx:yyy", Chat: 123, Admins: []int64{1}},
Timezone: timezone,
},
&configPkg.AppConfig{Timezone: timezone},
&configPkg.AppConfig{},
logger,
nil,
aliasManager,
Expand Down Expand Up @@ -169,8 +171,9 @@ func TestGetAliasesOk(t *testing.T) {
Name: "reporter",
Type: "telegram",
TelegramConfig: &configTypes.TelegramConfig{Token: "xxx:yyy", Chat: 123, Admins: []int64{1}},
Timezone: timezone,
},
&configPkg.AppConfig{Timezone: timezone},
&configPkg.AppConfig{},
logger,
nil,
aliasManager,
Expand Down
3 changes: 2 additions & 1 deletion pkg/reporters/telegram/help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ func TestAppHelpOk(t *testing.T) {
Name: "reporter",
Type: "telegram",
TelegramConfig: &configTypes.TelegramConfig{Token: "xxx:yyy", Chat: 123, Admins: []int64{1}},
Timezone: timezone,
},
&configPkg.AppConfig{Timezone: timezone},
&configPkg.AppConfig{},
logger,
nil,
nil,
Expand Down
6 changes: 4 additions & 2 deletions pkg/reporters/telegram/list_nodes_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ func TestListNodesCouldNotFindSubscription(t *testing.T) {
Name: "reporter",
Type: "telegram",
TelegramConfig: &configTypes.TelegramConfig{Token: "xxx:yyy", Chat: 123, Admins: []int64{1}},
Timezone: timezone,
},
&configPkg.AppConfig{Timezone: timezone},
&configPkg.AppConfig{},
logger,
nil,
aliasManager,
Expand Down Expand Up @@ -121,8 +122,9 @@ func TestListNodesOk(t *testing.T) {
Name: "reporter",
Type: "telegram",
TelegramConfig: &configTypes.TelegramConfig{Token: "xxx:yyy", Chat: 123, Admins: []int64{1}},
Timezone: timezone,
},
&configPkg.AppConfig{Timezone: timezone},
&configPkg.AppConfig{},
logger,
nodeManager,
aliasManager,
Expand Down
Loading

0 comments on commit e4134c4

Please sign in to comment.