Skip to content

Commit

Permalink
feat: refactor templates render (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno authored Sep 6, 2023
1 parent b8325a8 commit 0b86e23
Show file tree
Hide file tree
Showing 21 changed files with 121 additions and 143 deletions.
6 changes: 2 additions & 4 deletions pkg/app_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"main/pkg/reporters/telegram"
snapshotPkg "main/pkg/snapshot"
statePkg "main/pkg/state"
templatesPkg "main/pkg/templates"
"main/pkg/tendermint"
"main/pkg/types"
"main/pkg/utils"
Expand Down Expand Up @@ -54,10 +53,9 @@ func NewAppManager(
stateManager := statePkg.NewManager(managerLogger, config, metricsManager, snapshotManager, database)
websocketManager := tendermint.NewWebsocketManager(managerLogger, config, metricsManager)

templatesManager := templatesPkg.NewManager(logger)
reporters := []reportersPkg.Reporter{
telegram.NewReporter(config, managerLogger, stateManager, metricsManager, templatesManager),
discord.NewReporter(config, managerLogger, stateManager, metricsManager, templatesManager),
telegram.NewReporter(config, managerLogger, stateManager, metricsManager),
discord.NewReporter(config, managerLogger, stateManager, metricsManager),
}

return &AppManager{
Expand Down
34 changes: 10 additions & 24 deletions pkg/reporters/discord/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
reportPkg "main/pkg/report"
statePkg "main/pkg/state"
templatesPkg "main/pkg/templates"
"main/pkg/types"
"main/pkg/utils"
"strings"
"sync"
Expand All @@ -29,7 +28,7 @@ type Reporter struct {
Config *config.ChainConfig
Manager *statePkg.Manager
MetricsManager *metrics.Manager
TemplatesManager *templatesPkg.Manager
TemplatesManager templatesPkg.Manager
Commands map[string]*Command
}

Expand All @@ -38,7 +37,6 @@ func NewReporter(
logger zerolog.Logger,
manager *statePkg.Manager,
metricsManager *metrics.Manager,
templatesManager *templatesPkg.Manager,
) *Reporter {
return &Reporter{
Token: chainConfig.DiscordConfig.Token,
Expand All @@ -48,7 +46,7 @@ func NewReporter(
Logger: logger.With().Str("component", "discord_reporter").Logger(),
Manager: manager,
MetricsManager: metricsManager,
TemplatesManager: templatesManager,
TemplatesManager: templatesPkg.NewManager(logger, constants.DiscordReporterName),
Commands: make(map[string]*Command, 0),
}
}
Expand Down Expand Up @@ -179,7 +177,7 @@ func (reporter *Reporter) Send(report *reportPkg.Report) error {
func (reporter *Reporter) SerializeEntry(rawEntry reportPkg.Entry) string {
validator := rawEntry.GetValidator()
notifiers := reporter.Manager.GetNotifiersForReporter(validator.OperatorAddress, reporter.Name())
notifiersSerialized := " " + reporter.SerializeNotifiers(notifiers)
notifiersSerialized := " " + reporter.TemplatesManager.SerializeNotifiers(notifiers)

switch entry := rawEntry.(type) {
case events.ValidatorGroupChanged:
Expand All @@ -194,45 +192,45 @@ func (reporter *Reporter) SerializeEntry(rawEntry reportPkg.Entry) string {
// a string like "🟡 <validator> is skipping blocks (> 1.0%) (XXX till jail) <notifier> <notifier2>"
"**%s %s %s**%s%s",
entry.GetEmoji(),
reporter.SerializeLink(reporter.Config.ExplorerConfig.GetValidatorLink(entry.Validator)),
reporter.TemplatesManager.SerializeLink(reporter.Config.ExplorerConfig.GetValidatorLink(entry.Validator)),
entry.GetDescription(),
timeToJailStr,
notifiersSerialized,
)
case events.ValidatorJailed:
return fmt.Sprintf(
"**❌ %s was jailed**%s",
reporter.SerializeLink(reporter.Config.ExplorerConfig.GetValidatorLink(entry.Validator)),
reporter.TemplatesManager.SerializeLink(reporter.Config.ExplorerConfig.GetValidatorLink(entry.Validator)),
notifiersSerialized,
)
case events.ValidatorUnjailed:
return fmt.Sprintf(
"**👌 %s was unjailed**%s",
reporter.SerializeLink(reporter.Config.ExplorerConfig.GetValidatorLink(entry.Validator)),
reporter.TemplatesManager.SerializeLink(reporter.Config.ExplorerConfig.GetValidatorLink(entry.Validator)),
notifiersSerialized,
)
case events.ValidatorInactive:
return fmt.Sprintf(
"😔 **%s is now not in the active set**%s",
reporter.SerializeLink(reporter.Config.ExplorerConfig.GetValidatorLink(entry.Validator)),
reporter.TemplatesManager.SerializeLink(reporter.Config.ExplorerConfig.GetValidatorLink(entry.Validator)),
notifiersSerialized,
)
case events.ValidatorActive:
return fmt.Sprintf(
"✅ **%s is now in the active set**%s",
reporter.SerializeLink(reporter.Config.ExplorerConfig.GetValidatorLink(entry.Validator)),
reporter.TemplatesManager.SerializeLink(reporter.Config.ExplorerConfig.GetValidatorLink(entry.Validator)),
notifiersSerialized,
)
case events.ValidatorTombstoned:
return fmt.Sprintf(
"**💀 %s was tombstoned**%s",
reporter.SerializeLink(reporter.Config.ExplorerConfig.GetValidatorLink(entry.Validator)),
reporter.TemplatesManager.SerializeLink(reporter.Config.ExplorerConfig.GetValidatorLink(entry.Validator)),
notifiersSerialized,
)
case events.ValidatorCreated:
return fmt.Sprintf(
"**💡New validator created: %s**",
reporter.SerializeLink(reporter.Config.ExplorerConfig.GetValidatorLink(entry.Validator)),
reporter.TemplatesManager.SerializeLink(reporter.Config.ExplorerConfig.GetValidatorLink(entry.Validator)),
)
default:
return fmt.Sprintf("Unsupported event %+v\n", entry)
Expand All @@ -255,15 +253,3 @@ func (reporter *Reporter) BotRespond(s *discordgo.Session, i *discordgo.Interact
func (reporter *Reporter) SerializeDate(date time.Time) string {
return date.Format(time.RFC822)
}

func (reporter *Reporter) SerializeLink(link types.Link) string {
return reporter.TemplatesManager.SerializeMarkdownLink(link)
}

func (reporter *Reporter) SerializeNotifiers(notifiers []*types.Notifier) string {
return reporter.TemplatesManager.SerializeMarkdownNotifiers(notifiers)
}

func (reporter *Reporter) SerializeNotifier(notifier *types.Notifier) string {
return reporter.TemplatesManager.SerializeMarkdownNotifier(notifier)
}
2 changes: 1 addition & 1 deletion pkg/reporters/discord/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func (reporter *Reporter) GetHelpCommand() *Command {
Handler: func(s *discordgo.Session, i *discordgo.InteractionCreate) {
reporter.MetricsManager.LogReporterQuery(reporter.Config.Name, constants.DiscordReporterName, "help")

template, err := reporter.TemplatesManager.Render("Help", reporter.Commands, constants.FormatTypeMarkdown)
template, err := reporter.TemplatesManager.Render("Help", reporter.Commands)
if err != nil {
reporter.Logger.Error().Err(err).Str("template", "help").Msg("Error rendering template")
return
Expand Down
2 changes: 1 addition & 1 deletion pkg/reporters/discord/missing.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (reporter *Reporter) GetMissingCommand() *Command {
}),
}

template, err := reporter.TemplatesManager.Render("Missing", render, constants.FormatTypeMarkdown)
template, err := reporter.TemplatesManager.Render("Missing", render)
if err != nil {
reporter.Logger.Error().Err(err).Msg("Error rendering missing")
return
Expand Down
2 changes: 1 addition & 1 deletion pkg/reporters/discord/notifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (reporter *Reporter) GetNotifiersCommand() *Command {
template, err := reporter.TemplatesManager.Render("Notifiers", notifierRender{
Entries: entries,
Config: reporter.Config,
}, constants.FormatTypeMarkdown)
})
if err != nil {
reporter.BotRespond(s, i, "Error rendering notifiers template")
return
Expand Down
2 changes: 1 addition & 1 deletion pkg/reporters/discord/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (reporter *Reporter) GetParamsCommand() *Command {
Config: reporter.Config,
BlockTime: blockTime,
MaxTimeToJail: maxTimeToJail,
}, constants.FormatTypeMarkdown)
})
if err != nil {
reporter.Logger.Error().Err(err).Msg("Error rendering params template")
return
Expand Down
8 changes: 4 additions & 4 deletions pkg/reporters/discord/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,24 @@ func (reporter *Reporter) GetStatusCommand() *Command {
if validator.Jailed {
sb.WriteString(fmt.Sprintf(
"**%s:** jailed\n",
reporter.SerializeLink(link),
reporter.TemplatesManager.SerializeLink(link),
))
} else if !validator.Active() {
sb.WriteString(fmt.Sprintf(
"**%s:** not in the active set\n",
reporter.SerializeLink(link),
reporter.TemplatesManager.SerializeLink(link),
))
} else {
if signatureInfo, err := reporter.Manager.GetValidatorMissedBlocks(validator); err != nil {
sb.WriteString(fmt.Sprintf(
"**%s:**: error getting validators missed blocks: %s",
reporter.SerializeLink(link),
reporter.TemplatesManager.SerializeLink(link),
err,
))
} else {
sb.WriteString(fmt.Sprintf(
"**%s:** %d missed blocks (%.2f%%)\n",
reporter.SerializeLink(link),
reporter.TemplatesManager.SerializeLink(link),
signatureInfo.GetNotSigned(),
float64(signatureInfo.GetNotSigned())/float64(reporter.Config.BlocksWindow)*100,
))
Expand Down
2 changes: 1 addition & 1 deletion pkg/reporters/discord/subscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (reporter *Reporter) GetSubscribeCommand() *Command {
}

validatorLink := reporter.Config.ExplorerConfig.GetValidatorLink(validator)
validatorLinkSerialized := reporter.SerializeLink(validatorLink)
validatorLinkSerialized := reporter.TemplatesManager.SerializeLink(validatorLink)

reporter.BotRespond(s, i, fmt.Sprintf(
"Subscribed to validator's notifications on %s: %s",
Expand Down
2 changes: 1 addition & 1 deletion pkg/reporters/discord/unsubscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (reporter *Reporter) GetUnsubscribeCommand() *Command {
}

validatorLink := reporter.Config.ExplorerConfig.GetValidatorLink(validator)
validatorLinkSerialized := reporter.SerializeLink(validatorLink)
validatorLinkSerialized := reporter.TemplatesManager.SerializeLink(validatorLink)

reporter.BotRespond(s, i, fmt.Sprintf(
"Unsubscribed from validator's notifications on %s: %s",
Expand Down
2 changes: 1 addition & 1 deletion pkg/reporters/telegram/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func (reporter *Reporter) HandleHelp(c tele.Context) error {

reporter.MetricsManager.LogReporterQuery(reporter.Config.Name, constants.TelegramReporterName, "help")

template, err := reporter.TemplatesManager.Render("Help", nil, constants.FormatTypeHTML)
template, err := reporter.TemplatesManager.Render("Help", nil)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/reporters/telegram/missing.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (reporter *Reporter) HandleMissingValidators(c tele.Context) error {
}),
}

template, err := reporter.TemplatesManager.Render("Missing", render, constants.FormatTypeHTML)
template, err := reporter.TemplatesManager.Render("Missing", render)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/reporters/telegram/notifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (reporter *Reporter) HandleNotifiers(c tele.Context) error {
template, err := reporter.TemplatesManager.Render("Notifiers", notifierRender{
Entries: entries,
Config: reporter.Config,
}, constants.FormatTypeHTML)
})
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/reporters/telegram/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (reporter *Reporter) HandleParams(c tele.Context) error {
Config: reporter.Config,
BlockTime: blockTime,
MaxTimeToJail: maxTimeToJail,
}, constants.FormatTypeHTML)
})
if err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/reporters/telegram/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,24 @@ func (reporter *Reporter) HandleStatus(c tele.Context) error {
if validator.Jailed {
sb.WriteString(fmt.Sprintf(
"<strong>%s:</strong> jailed\n",
reporter.SerializeLink(link),
reporter.TemplatesManager.SerializeLink(link),
))
} else if !validator.Active() {
sb.WriteString(fmt.Sprintf(
"<strong>%s:</strong> not in the active set\n",
reporter.SerializeLink(link),
reporter.TemplatesManager.SerializeLink(link),
))
} else {
if signatureInfo, err := reporter.Manager.GetValidatorMissedBlocks(validator); err != nil {
sb.WriteString(fmt.Sprintf(
"<strong>%s:</strong>: error getting validators missed blocks: %s",
reporter.SerializeLink(link),
reporter.TemplatesManager.SerializeLink(link),
err,
))
} else {
sb.WriteString(fmt.Sprintf(
"<strong>%s:</strong> %d missed blocks (%.2f%%)\n",
reporter.SerializeLink(link),
reporter.TemplatesManager.SerializeLink(link),
signatureInfo.GetNotSigned(),
float64(signatureInfo.GetNotSigned())/float64(reporter.Config.BlocksWindow)*100,
))
Expand Down
2 changes: 1 addition & 1 deletion pkg/reporters/telegram/subscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (reporter *Reporter) HandleSubscribe(c tele.Context) error {
}

validatorLink := reporter.Config.ExplorerConfig.GetValidatorLink(validator)
validatorLinkSerialized := reporter.SerializeLink(validatorLink)
validatorLinkSerialized := reporter.TemplatesManager.SerializeLink(validatorLink)

return reporter.BotReply(c, fmt.Sprintf(
"Subscribed to validator's notifications on %s: %s",
Expand Down
Loading

0 comments on commit 0b86e23

Please sign in to comment.