Skip to content

Commit

Permalink
refactor(pkg/notifier) - rewrite logic for notify; add template var;
Browse files Browse the repository at this point in the history
  • Loading branch information
PxyUp committed Feb 7, 2024
1 parent 93e8d39 commit 55795b2
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 26 deletions.
4 changes: 4 additions & 0 deletions pkg/builder/universal.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,7 @@ func toJson(result gjson.Result) Interfacable {
func ToJsonable(raw json.RawMessage) Interfacable {
return toJson(gjson.ParseBytes(raw))
}

func ToJsonableFromString(str string) Interfacable {
return ToJsonable([]byte(str))
}
2 changes: 2 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ type NotifierConfig struct {
Expression string `yaml:"expression" json:"expression"`
Force bool `json:"force" yaml:"force"`
SendArrayByItem bool `yaml:"send_array_by_item" json:"send_array_by_item"`
Template string `yaml:"template" json:"template"`

Console *ConsoleConfig `yaml:"console" json:"console"`
TelegramBot *TelegramBotConfig `yaml:"telegram_bot" json:"telegram_bot"`
Expand Down Expand Up @@ -230,6 +231,7 @@ type TelegramBotConfig struct {
Token string `json:"token" yaml:"token"`
UsersId []int64 `json:"users_id" yaml:"users_id"`
Pretty bool `json:"pretty" yaml:"pretty"`
OnlyMsg bool `json:"only_msg" yaml:"only_msg"`
}

type Item struct {
Expand Down
9 changes: 4 additions & 5 deletions pkg/notifier/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"github.com/PxyUp/fitter/pkg/config"
"github.com/PxyUp/fitter/pkg/logger"
"github.com/PxyUp/fitter/pkg/parser"
"os"
)

Expand All @@ -15,6 +14,10 @@ type console struct {
cfg *config.ConsoleConfig
}

func (o *console) GetLogger() logger.Logger {
return o.logger
}

func (o *console) notify(record *singleRecord) error {
if o.cfg.OnlyResult {
_, errOut := fmt.Fprintln(os.Stdout, string(record.Body))
Expand Down Expand Up @@ -56,7 +59,3 @@ func (o *console) WithLogger(logger logger.Logger) *console {
o.logger = logger
return o
}

func (o *console) Inform(result *parser.ParseResult, errResult error, asArray bool) error {
return inform(o, o.name, result, errResult, asArray, o.logger)
}
5 changes: 2 additions & 3 deletions pkg/notifier/file_notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"github.com/PxyUp/fitter/pkg/builder"
"github.com/PxyUp/fitter/pkg/config"
"github.com/PxyUp/fitter/pkg/logger"
"github.com/PxyUp/fitter/pkg/parser"
"github.com/PxyUp/fitter/pkg/utils"
"os"
)
Expand Down Expand Up @@ -42,8 +41,8 @@ func (f *fileNotifier) notify(record *singleRecord) error {
return nil
}

func (f *fileNotifier) Inform(result *parser.ParseResult, err error, asArray bool) error {
return inform(f, f.name, result, err, asArray, f.logger)
func (o *fileNotifier) GetLogger() logger.Logger {
return o.logger
}

func NewFile(name string, cfg *config.FileStorageField) *fileNotifier {
Expand Down
5 changes: 2 additions & 3 deletions pkg/notifier/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/PxyUp/fitter/pkg/config"
"github.com/PxyUp/fitter/pkg/http_client"
"github.com/PxyUp/fitter/pkg/logger"
"github.com/PxyUp/fitter/pkg/parser"
"github.com/PxyUp/fitter/pkg/utils"
"net/http"
"time"
Expand Down Expand Up @@ -54,8 +53,8 @@ func (h *httpNotifier) notify(record *singleRecord) error {
return nil
}

func (h *httpNotifier) Inform(result *parser.ParseResult, err error, asArray bool) error {
return inform(h, h.name, result, err, asArray, h.logger)
func (o *httpNotifier) GetLogger() logger.Logger {
return o.logger
}

func (h *httpNotifier) WithLogger(logger logger.Logger) *httpNotifier {
Expand Down
5 changes: 2 additions & 3 deletions pkg/notifier/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ type singleRecord struct {

type Notifier interface {
notify(*singleRecord) error

Inform(result *parser.ParseResult, err error, asArray bool) error
GetLogger() logger.Logger
}

func resultToSingleRecord(name string, result *parser.ParseResult, errResult error, logger logger.Logger) *singleRecord {
Expand Down Expand Up @@ -91,7 +90,7 @@ func ShouldInform(cfg *config.NotifierConfig, result builder.Interfacable) (bool
return value, nil
}

func inform(notifier Notifier, name string, result *parser.ParseResult, errResult error, asArray bool, logger logger.Logger) error {
func Inform(notifier Notifier, name string, result *parser.ParseResult, errResult error, asArray bool, logger logger.Logger) error {
if errResult != nil {
return notifier.notify(resultToSingleRecord(name, nil, errResult, logger))
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/notifier/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"github.com/PxyUp/fitter/pkg/config"
"github.com/PxyUp/fitter/pkg/logger"
"github.com/PxyUp/fitter/pkg/parser"
"github.com/PxyUp/fitter/pkg/utils"
"github.com/redis/go-redis/v9"
"time"
Expand Down Expand Up @@ -39,8 +38,8 @@ func (r *redisNotifier) notify(record *singleRecord) error {
return nil
}

func (r *redisNotifier) Inform(result *parser.ParseResult, err error, asArray bool) error {
return inform(r, r.name, result, err, asArray, r.logger)
func (o *redisNotifier) GetLogger() logger.Logger {
return o.logger
}

func (r *redisNotifier) WithLogger(logger logger.Logger) *redisNotifier {
Expand Down
20 changes: 14 additions & 6 deletions pkg/notifier/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"github.com/PxyUp/fitter/pkg/config"
"github.com/PxyUp/fitter/pkg/logger"
"github.com/PxyUp/fitter/pkg/parser"
"github.com/PxyUp/fitter/pkg/utils"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
Expand All @@ -19,7 +18,16 @@ type telegramBot struct {
}

func (t *telegramBot) notify(record *singleRecord) error {
msg, err := json.Marshal(record)
var forUnmarshal any
forUnmarshal = record
if t.cfg.OnlyMsg {
if record.Error != nil {
forUnmarshal = (*record.Error).Error()
} else {
forUnmarshal = record.Body
}
}
msg, err := json.Marshal(forUnmarshal)
if err != nil {
return err
}
Expand Down Expand Up @@ -65,6 +73,10 @@ func (t *telegramBot) sendMessage(msg string) error {
return nil
}

func (o *telegramBot) GetLogger() logger.Logger {
return o.logger
}

func (t *telegramBot) prettyMsg(msg string) string {
if !t.cfg.Pretty {
return msg
Expand All @@ -77,7 +89,3 @@ func (t *telegramBot) prettyMsg(msg string) string {
}
return prettyJSON.String()
}

func (t *telegramBot) Inform(result *parser.ParseResult, errResult error, asArray bool) error {
return inform(t, t.name, result, errResult, asArray, t.logger)
}
17 changes: 14 additions & 3 deletions pkg/processor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/PxyUp/fitter/pkg/notifier"
"github.com/PxyUp/fitter/pkg/parser"
"github.com/PxyUp/fitter/pkg/references"
"github.com/PxyUp/fitter/pkg/utils"
)

var (
Expand All @@ -25,6 +26,7 @@ type processor struct {
notifier notifier.Notifier
notifierCfg *config.NotifierConfig
engine parser.Engine
name string
}

type nullProcessor struct {
Expand All @@ -45,8 +47,9 @@ func (n *nullProcessor) Process(input builder.Interfacable) (*parser.ParseResult
return nil, n.err
}

func New(engine parser.Engine, model *config.Model, notifier notifier.Notifier, notifierCfg *config.NotifierConfig) *processor {
func New(name string, engine parser.Engine, model *config.Model, notifier notifier.Notifier, notifierCfg *config.NotifierConfig) *processor {
return &processor{
name: name,
engine: engine,
logger: logger.Null,
notifierCfg: notifierCfg,
Expand All @@ -67,7 +70,15 @@ func (p *processor) Process(input builder.Interfacable) (*parser.ParseResult, er
if p.model.ArrayConfig != nil || p.model.IsArray {
isArray = true
}

if p.notifierCfg != nil {
if p.notifierCfg.Template != "" {
strValue := builder.ToJsonableFromString(utils.Format(p.notifierCfg.Template, result, nil, nil))
result = &parser.ParseResult{
RawResult: strValue.Raw(),
Json: strValue.ToJson(),
}
}
need, errShInform := notifier.ShouldInform(p.notifierCfg, result)
if errShInform != nil {
p.logger.Errorw("cannot calculate notification setting", "error", errShInform.Error())
Expand All @@ -77,7 +88,7 @@ func (p *processor) Process(input builder.Interfacable) (*parser.ParseResult, er
return result, nil
}
}
errNot := p.notifier.Inform(result, err, isArray && p.notifierCfg.SendArrayByItem)
errNot := notifier.Inform(p.notifier, p.name, result, err, isArray && p.notifierCfg.SendArrayByItem, p.notifier.GetLogger())
if errNot != nil {
p.logger.Errorw("cannot notify about result", "error", errNot.Error())
}
Expand Down Expand Up @@ -130,5 +141,5 @@ func CreateProcessor(item *config.Item, refMap config.RefMap, logger logger.Logg

logger = logger.With("name", item.Name)

return New(parser.NewEngine(item.ConnectorConfig, logger.With("component", "processor_engine")), item.Model, notifierInstance, item.NotifierConfig).WithLogger(logger)
return New(item.Name, parser.NewEngine(item.ConnectorConfig, logger.With("component", "processor_engine")), item.Model, notifierInstance, item.NotifierConfig).WithLogger(logger)
}

0 comments on commit 55795b2

Please sign in to comment.