From 9ef76ddb845622a0f1af0b2c620bb8300fcceba9 Mon Sep 17 00:00:00 2001 From: Sergey <83376337+freak12techno@users.noreply.github.com> Date: Fri, 15 Dec 2023 00:41:13 +0300 Subject: [PATCH] feat: allow specifying timezone in config (#30) --- config.example.yml | 3 +++ pkg/app/app.go | 4 +++- pkg/config/config.go | 21 +++++++++++++++++---- pkg/load_config.go | 4 ++++ pkg/templates/templates.go | 7 +++++-- pkg/utils/utils.go | 6 ++++-- 6 files changed, 36 insertions(+), 9 deletions(-) diff --git a/config.example.yml b/config.example.yml index fe4d232..2e5b71f 100644 --- a/config.example.yml +++ b/config.example.yml @@ -1,3 +1,6 @@ +# Timezone. Used when displaying dates (like in silences and alerts). +# Defaults to "Etc/GMT" (so GMT+0) +timezone: "Europe/Moscow" # Logging configuration. log: # Log level. Defaults to "info" diff --git a/pkg/app/app.go b/pkg/app/app.go index 850efb8..3692016 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -27,10 +27,12 @@ type App struct { } func NewApp(config *configPkg.Config, version string) *App { + timezone, _ := time.LoadLocation(config.Timezone) + logger := loggerPkg.GetLogger(config.Log) grafana := grafanaPkg.InitGrafana(config.Grafana, logger) alertmanager := alertmanagerPkg.InitAlertmanager(config.Alertmanager, logger) - templateManager := templates.NewTemplateManager() + templateManager := templates.NewTemplateManager(timezone) bot, err := tele.NewBot(tele.Settings{ Token: config.Telegram.Token, diff --git a/pkg/config/config.go b/pkg/config/config.go index 3b8a74c..5d6e054 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -1,8 +1,14 @@ package config +import ( + "fmt" + "time" +) + type Config struct { + Timezone string `default:"Etc/GMT" yaml:"timezone"` Log LogConfig `yaml:"log"` - Telegram TelegramConfig `default:"" yaml:"telegram"` + Telegram TelegramConfig `yaml:"telegram"` Grafana GrafanaConfig `yaml:"grafana"` Alertmanager AlertmanagerConfig `yaml:"alertmanager"` } @@ -19,8 +25,8 @@ type TelegramConfig struct { type GrafanaConfig struct { URL string `default:"http://localhost:3000" yaml:"url"` - User string `yaml:"user"` - Password string `yaml:"password"` + User string `default:"admin" yaml:"user"` + Password string `default:"admin" yaml:"password"` RenderOptions map[string]string `default:"{\"orgId\":\"1\",\"from\":\"now\",\"to\":\"now-30m\"}" yaml:"render_options"` } @@ -28,5 +34,12 @@ type AlertmanagerConfig struct { URL string `default:"http://localhost:9093" yaml:"url"` User string `yaml:"user"` Password string `yaml:"password"` - Timezone string `default:"Europe/Moscow" yaml:"timezone"` +} + +func (c *Config) Validate() error { + if _, err := time.LoadLocation(c.Timezone); err != nil { + return fmt.Errorf("error parsing timezone: %s", err) + } + + return nil } diff --git a/pkg/load_config.go b/pkg/load_config.go index e52f975..65278d0 100644 --- a/pkg/load_config.go +++ b/pkg/load_config.go @@ -26,5 +26,9 @@ func LoadConfig(path string) *configPkg.Config { logger.GetDefaultLogger().Fatal().Err(err).Msg("Could not set default settings") } + if err := config.Validate(); err != nil { + logger.GetDefaultLogger().Fatal().Err(err).Msg("Error validating config") + } + return config } diff --git a/pkg/templates/templates.go b/pkg/templates/templates.go index 9125bc7..3fccb0b 100644 --- a/pkg/templates/templates.go +++ b/pkg/templates/templates.go @@ -6,17 +6,20 @@ import ( "html/template" "main/pkg/types/render" "main/pkg/utils" + "time" templatesList "main/templates" ) type TemplateManager struct { + Timezone *time.Location Templates map[string]*template.Template } -func NewTemplateManager() *TemplateManager { +func NewTemplateManager(timezone *time.Location) *TemplateManager { return &TemplateManager{ Templates: make(map[string]*template.Template, 0), + Timezone: timezone, } } @@ -32,7 +35,7 @@ func (manager *TemplateManager) GetTemplate(name string) (*template.Template, er "GetEmojiBySilenceStatus": utils.GetEmojiBySilenceStatus, "StrToFloat64": utils.StrToFloat64, "FormatDuration": utils.FormatDuration, - "FormatDate": utils.FormatDate, + "FormatDate": utils.FormatDate(manager.Timezone), }).ParseFS(templatesList.Templates, filename) if err != nil { return nil, err diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 1c363c2..7e2e694 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -226,6 +226,8 @@ func FormatDuration(duration time.Duration) string { return strings.Join(parts, " ") } -func FormatDate(date time.Time) string { - return date.Format(time.RFC1123) +func FormatDate(timezone *time.Location) func(date time.Time) string { + return func(date time.Time) string { + return date.In(timezone).Format(time.RFC1123) + } }