mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-22 14:55:16 +00:00
rename SlackReporter to SlackNotifier and simplify infof
This commit is contained in:
parent
f6a1c20cfe
commit
6660b75dcb
|
@ -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{}) {
|
||||
|
|
Loading…
Reference in New Issue
Block a user