rename SlackReporter to SlackNotifier and simplify infof

This commit is contained in:
c9s 2020-07-12 22:57:51 +08:00
parent f6a1c20cfe
commit 6660b75dcb

View File

@ -18,7 +18,7 @@ import (
var USD = accounting.Accounting{Symbol: "$ ", Precision: 2} var USD = accounting.Accounting{Symbol: "$ ", Precision: 2}
var BTC = accounting.Accounting{Symbol: "BTC ", Precision: 8} var BTC = accounting.Accounting{Symbol: "BTC ", Precision: 8}
type SlackReporter struct { type SlackNotifier struct {
Slack *slack.Client Slack *slack.Client
TradingChannel string TradingChannel string
@ -26,7 +26,7 @@ type SlackReporter struct {
InfoChannel string InfoChannel string
} }
func (t *SlackReporter) Infof(format string, args ...interface{}) { func (t *SlackNotifier) Infof(format string, args ...interface{}) {
var slackAttachments []slack.Attachment = nil var slackAttachments []slack.Attachment = nil
var slackArgsStartIdx = -1 var slackArgsStartIdx = -1
for idx, arg := range args { 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...) log.WithError(err).Errorf(format, args...)
_, _, err2 := t.Slack.PostMessageContext(context.Background(), t.ErrorChannel, _, _, err2 := t.Slack.PostMessageContext(context.Background(), t.ErrorChannel,
slack.MsgOptionText("ERROR: "+err.Error()+" "+fmt.Sprintf(format, args...), true)) 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, _, _, err := t.Slack.PostMessageContext(context.Background(), t.TradingChannel,
slack.MsgOptionText(util.Render(`:handshake: trade execution @ {{ .Price }}`, trade), true), slack.MsgOptionText(util.Render(`:handshake: trade execution @ {{ .Price }}`, trade), true),
slack.MsgOptionAttachments(trade.SlackAttachment())) 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() attachment := report.SlackAttachment()
_, _, err := t.Slack.PostMessageContext(context.Background(), t.TradingChannel, _, _, err := t.Slack.PostMessageContext(context.Background(), t.TradingChannel,
@ -101,6 +101,8 @@ func (t *SlackReporter) ReportPnL(report *ProfitAndLossReport) {
type Trader struct { type Trader struct {
notifier *SlackNotifier
// Context is trading Context // Context is trading Context
Context *TradingContext Context *TradingContext
@ -114,40 +116,7 @@ type Trader struct {
} }
func (t *Trader) Infof(format string, args ...interface{}) { func (t *Trader) Infof(format string, args ...interface{}) {
var slackAttachments []slack.Attachment = nil t.notifier.Infof(format, args...)
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())
}
} }
func (t *Trader) Errorf(err error, format string, args ...interface{}) { func (t *Trader) Errorf(err error, format string, args ...interface{}) {