From 6660b75dcb71b501599145eea8d4c86c26bbf703 Mon Sep 17 00:00:00 2001 From: c9s Date: Sun, 12 Jul 2020 22:57:51 +0800 Subject: [PATCH] rename SlackReporter to SlackNotifier and simplify infof --- bbgo/trader.go | 47 ++++++++--------------------------------------- 1 file changed, 8 insertions(+), 39 deletions(-) diff --git a/bbgo/trader.go b/bbgo/trader.go index 480b5268a..f5ba5e786 100644 --- a/bbgo/trader.go +++ b/bbgo/trader.go @@ -18,7 +18,7 @@ import ( var USD = accounting.Accounting{Symbol: "$ ", Precision: 2} var BTC = accounting.Accounting{Symbol: "BTC ", Precision: 8} -type SlackReporter struct { +type SlackNotifier struct { Slack *slack.Client TradingChannel string @@ -26,7 +26,7 @@ type SlackReporter struct { InfoChannel string } -func (t *SlackReporter) Infof(format string, args ...interface{}) { +func (t *SlackNotifier) Infof(format string, args ...interface{}) { var slackAttachments []slack.Attachment = nil var slackArgsStartIdx = -1 for idx, arg := range args { @@ -63,7 +63,7 @@ func (t *SlackReporter) Infof(format string, args ...interface{}) { } } -func (t *SlackReporter) Errorf(err error, format string, args ...interface{}) { +func (t *SlackNotifier) Errorf(err error, format string, args ...interface{}) { log.WithError(err).Errorf(format, args...) _, _, err2 := t.Slack.PostMessageContext(context.Background(), t.ErrorChannel, slack.MsgOptionText("ERROR: "+err.Error()+" "+fmt.Sprintf(format, args...), true)) @@ -72,7 +72,7 @@ func (t *SlackReporter) Errorf(err error, format string, args ...interface{}) { } } -func (t *SlackReporter) ReportTrade(trade *types.Trade) { +func (t *SlackNotifier) ReportTrade(trade *types.Trade) { _, _, err := t.Slack.PostMessageContext(context.Background(), t.TradingChannel, slack.MsgOptionText(util.Render(`:handshake: trade execution @ {{ .Price }}`, trade), true), slack.MsgOptionAttachments(trade.SlackAttachment())) @@ -82,7 +82,7 @@ func (t *SlackReporter) ReportTrade(trade *types.Trade) { } } -func (t *SlackReporter) ReportPnL(report *ProfitAndLossReport) { +func (t *SlackNotifier) ReportPnL(report *ProfitAndLossReport) { attachment := report.SlackAttachment() _, _, err := t.Slack.PostMessageContext(context.Background(), t.TradingChannel, @@ -101,6 +101,8 @@ func (t *SlackReporter) ReportPnL(report *ProfitAndLossReport) { type Trader struct { + notifier *SlackNotifier + // Context is trading Context Context *TradingContext @@ -114,40 +116,7 @@ type Trader struct { } func (t *Trader) Infof(format string, args ...interface{}) { - var slackAttachments []slack.Attachment = nil - var slackArgsStartIdx = -1 - for idx, arg := range args { - switch a := arg.(type) { - - // concrete type assert first - case slack.Attachment: - if slackArgsStartIdx == -1 { - slackArgsStartIdx = idx - } - slackAttachments = append(slackAttachments, a) - - case slackstyle.SlackAttachmentCreator: - if slackArgsStartIdx == -1 { - slackArgsStartIdx = idx - } - slackAttachments = append(slackAttachments, a.SlackAttachment()) - - } - } - - var nonSlackArgs = []interface{}{} - if slackArgsStartIdx > 0 { - nonSlackArgs = args[:slackArgsStartIdx] - } - - log.Infof(format, nonSlackArgs...) - - _, _, err := t.Slack.PostMessageContext(context.Background(), t.InfoChannel, - slack.MsgOptionText(fmt.Sprintf(format, nonSlackArgs...), true), - slack.MsgOptionAttachments(slackAttachments...)) - if err != nil { - log.WithError(err).Errorf("slack error: %s", err.Error()) - } + t.notifier.Infof(format, args...) } func (t *Trader) Errorf(err error, format string, args ...interface{}) {