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 1fbcea3 commit 92958c9
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 9 deletions.
3 changes: 3 additions & 0 deletions config.example.yml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
4 changes: 3 additions & 1 deletion pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
21 changes: 17 additions & 4 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -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"`
}
Expand All @@ -19,14 +25,21 @@ 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"`
}

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
}
4 changes: 4 additions & 0 deletions pkg/load_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
7 changes: 5 additions & 2 deletions pkg/templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand All @@ -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
Expand Down
6 changes: 4 additions & 2 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 92958c9

Please sign in to comment.