refactor notifiers and add liquidity field to the trade

This commit is contained in:
c9s 2021-05-12 12:37:48 +08:00
parent 85e1b6b1c7
commit 807c049d63
6 changed files with 95 additions and 45 deletions

View File

@ -1,15 +1,15 @@
package bbgo
type Notifier interface {
NotifyTo(channel, format string, args ...interface{})
Notify(format string, args ...interface{})
NotifyTo(channel string, obj interface{}, args ...interface{})
Notify(obj interface{}, args ...interface{})
}
type NullNotifier struct{}
func (n *NullNotifier) NotifyTo(channel, format string, args ...interface{}) {}
func (n *NullNotifier) NotifyTo(channel string, obj interface{}, args ...interface{}) {}
func (n *NullNotifier) Notify(format string, args ...interface{}) {}
func (n *NullNotifier) Notify(obj interface{}, args ...interface{}) {}
type Notifiability struct {
notifiers []Notifier
@ -18,7 +18,7 @@ type Notifiability struct {
ObjectChannelRouter *ObjectChannelRouter `json:"-"`
}
// RouteSession routes symbol name to channel
// RouteSymbol routes symbol name to channel
func (m *Notifiability) RouteSymbol(symbol string) (channel string, ok bool) {
if m.SymbolChannelRouter != nil {
return m.SymbolChannelRouter.Route(symbol)
@ -47,14 +47,14 @@ func (m *Notifiability) AddNotifier(notifier Notifier) {
m.notifiers = append(m.notifiers, notifier)
}
func (m *Notifiability) Notify(format string, args ...interface{}) {
func (m *Notifiability) Notify(obj interface{}, args ...interface{}) {
for _, n := range m.notifiers {
n.Notify(format, args...)
n.Notify(obj, args...)
}
}
func (m *Notifiability) NotifyTo(channel, format string, args ...interface{}) {
func (m *Notifiability) NotifyTo(channel string, obj interface{}, args ...interface{}) {
for _, n := range m.notifiers {
n.NotifyTo(channel, format, args...)
n.NotifyTo(channel, obj, args...)
}
}

View File

@ -35,32 +35,26 @@ func New(token, channel string, options ...NotifyOption) *Notifier {
return notifier
}
func (n *Notifier) Notify(format string, args ...interface{}) {
n.NotifyTo(n.channel, format, args...)
func (n *Notifier) Notify(obj interface{}, args ...interface{}) {
n.NotifyTo(n.channel, obj, args...)
}
func (n *Notifier) NotifyTo(channel, format string, args ...interface{}) {
if len(channel) == 0 {
channel = n.channel
}
var slackAttachments []slack.Attachment
var slackArgsOffset = -1
func filterSlackAttachments(args []interface{}) (slackAttachments []slack.Attachment, pureArgs []interface{}) {
var firstAttachmentOffset = -1
for idx, arg := range args {
switch a := arg.(type) {
// concrete type assert first
case slack.Attachment:
if slackArgsOffset == -1 {
slackArgsOffset = idx
if firstAttachmentOffset == -1 {
firstAttachmentOffset = idx
}
slackAttachments = append(slackAttachments, a)
case SlackAttachmentCreator:
if slackArgsOffset == -1 {
slackArgsOffset = idx
if firstAttachmentOffset == -1 {
firstAttachmentOffset = idx
}
slackAttachments = append(slackAttachments, a.SlackAttachment())
@ -68,19 +62,45 @@ func (n *Notifier) NotifyTo(channel, format string, args ...interface{}) {
}
}
var nonSlackArgs = args
if slackArgsOffset > -1 {
nonSlackArgs = args[:slackArgsOffset]
pureArgs = args
if firstAttachmentOffset > -1 {
pureArgs = args[:firstAttachmentOffset]
}
return
}
func (n *Notifier) NotifyTo(channel string, obj interface{}, args ...interface{}) {
if len(channel) == 0 {
channel = n.channel
}
slackAttachments, pureArgs := filterSlackAttachments(args)
var opts []slack.MsgOption
switch a := obj.(type) {
case string:
opts = append(opts, slack.MsgOptionText(fmt.Sprintf(a, pureArgs...), true),
slack.MsgOptionAttachments(slackAttachments...))
case slack.Attachment:
opts = append(opts, slack.MsgOptionAttachments(append([]slack.Attachment{a}, slackAttachments...)...))
case SlackAttachmentCreator:
// convert object to slack attachment (if supported)
opts = append(opts, slack.MsgOptionAttachments(append([]slack.Attachment{a.SlackAttachment()}, slackAttachments...)...))
default:
log.Errorf("slack notifier error, unsupported object: %T %+v", a, a)
}
go func() {
_, _, err := n.client.PostMessageContext(context.Background(), channel,
slack.MsgOptionText(fmt.Sprintf(format, nonSlackArgs...), true),
slack.MsgOptionAttachments(slackAttachments...))
_, _, err := n.client.PostMessageContext(context.Background(), channel, opts...)
if err != nil {
log.WithError(err).
WithField("channel", channel).
Errorf("slack error: %s", err.Error())
Errorf("slack api error: %s", err.Error())
}
}()

View File

@ -12,7 +12,9 @@ type Notifier struct {
type NotifyOption func(notifier *Notifier)
// start bot daemon
// New
// TODO: register interaction with channel, so that we can route message to the specific telegram bot
func New(interaction *Interaction, options ...NotifyOption) *Notifier {
notifier := &Notifier{
interaction: interaction,
@ -25,14 +27,12 @@ func New(interaction *Interaction, options ...NotifyOption) *Notifier {
return notifier
}
func (n *Notifier) Notify(format string, args ...interface{}) {
n.NotifyTo("", format, args...)
func (n *Notifier) Notify(obj interface{}, args ...interface{}) {
n.NotifyTo("", obj, args...)
}
func (n *Notifier) NotifyTo(_, format string, args ...interface{}) {
func filterPlaintextMessages(args []interface{}) (texts []string, pureArgs []interface{}) {
var textArgsOffset = -1
var texts []string
for idx, arg := range args {
switch a := arg.(type) {
@ -40,21 +40,44 @@ func (n *Notifier) NotifyTo(_, format string, args ...interface{}) {
texts = append(texts, a.PlainText())
textArgsOffset = idx
case types.Stringer:
texts = append(texts, a.String())
textArgsOffset = idx
}
}
var simpleArgs = args
pureArgs = args
if textArgsOffset > -1 {
simpleArgs = args[:textArgsOffset]
pureArgs = args[:textArgsOffset]
}
log.Infof(format, simpleArgs...)
return texts, pureArgs
}
func (n *Notifier) NotifyTo(channel string, obj interface{}, args ...interface{}) {
var texts, pureArgs = filterPlaintextMessages(args)
var message string
switch a := obj.(type) {
case string:
log.Infof(a, pureArgs...)
message = fmt.Sprintf(a, pureArgs...)
case types.Stringer:
message = a.String()
case types.PlainText:
message = a.PlainText()
default:
log.Errorf("unsupported notification format: %T %+v", a, a)
}
message := fmt.Sprintf(format, simpleArgs...)
n.interaction.SendToOwner(message)
for _, text := range texts {
n.interaction.SendToOwner(text)
}
}

View File

@ -110,7 +110,7 @@ func (s *TradeService) Sync(ctx context.Context, exchange types.Exchange, symbol
trade.Side,
trade.Price,
trade.Quantity,
trade.MakerOrTakerLabel(),
trade.Liquidity(),
trade.Time.String())
if err := s.Insert(trade); err != nil {

View File

@ -3,3 +3,7 @@ package types
type PlainText interface {
PlainText() string
}
type Stringer interface {
String() string
}

View File

@ -88,6 +88,8 @@ func (trade Trade) SlackAttachment() slack.Attachment {
color = "#228B22"
}
liquidity := trade.Liquidity()
return slack.Attachment{
Text: fmt.Sprintf("*%s* Trade %s", trade.Symbol, trade.Side),
Color: color,
@ -96,17 +98,18 @@ func (trade Trade) SlackAttachment() slack.Attachment {
Fields: []slack.AttachmentField{
{Title: "Exchange", Value: trade.Exchange, Short: true},
{Title: "Price", Value: util.FormatFloat(trade.Price, 2), Short: true},
{Title: "Quote", Value: util.FormatFloat(trade.Quantity, 4), Short: true},
{Title: "Quantity", Value: util.FormatFloat(trade.Quantity, 4), Short: true},
{Title: "QuoteQuantity", Value: util.FormatFloat(trade.QuoteQuantity, 2)},
{Title: "Fee", Value: util.FormatFloat(trade.Fee, 4), Short: true},
{Title: "FeeCurrency", Value: trade.FeeCurrency, Short: true},
{Title: "Liquidity", Value: liquidity, Short: true},
},
// Footer: tradingCtx.TradeStartTime.Format(time.RFC822),
// FooterIcon: "",
}
}
func (trade Trade) MakerOrTakerLabel() (o string) {
func (trade Trade) Liquidity() (o string) {
if trade.IsMaker {
o += "MAKER"
} else {