mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 01:01:56 +00:00
bbgo: drop legacy notification routing
This commit is contained in:
parent
1c58a44e44
commit
b067d67eab
|
@ -78,13 +78,7 @@ type TelegramNotification struct {
|
|||
|
||||
type NotificationConfig struct {
|
||||
Slack *SlackNotification `json:"slack,omitempty" yaml:"slack,omitempty"`
|
||||
|
||||
Telegram *TelegramNotification `json:"telegram,omitempty" yaml:"telegram,omitempty"`
|
||||
|
||||
SymbolChannels map[string]string `json:"symbolChannels,omitempty" yaml:"symbolChannels,omitempty"`
|
||||
SessionChannels map[string]string `json:"sessionChannels,omitempty" yaml:"sessionChannels,omitempty"`
|
||||
|
||||
Routing *SlackNotificationRouting `json:"routing,omitempty" yaml:"routing,omitempty"`
|
||||
}
|
||||
|
||||
type Session struct {
|
||||
|
|
|
@ -48,13 +48,6 @@ func TestLoadConfig(t *testing.T) {
|
|||
wantErr: false,
|
||||
f: func(t *testing.T, config *Config) {
|
||||
assert.NotNil(t, config.Notifications)
|
||||
assert.NotNil(t, config.Notifications.SessionChannels)
|
||||
assert.NotNil(t, config.Notifications.SymbolChannels)
|
||||
assert.Equal(t, map[string]string{
|
||||
"^BTC": "#btc",
|
||||
"^ETH": "#eth",
|
||||
}, config.Notifications.SymbolChannels)
|
||||
assert.NotNil(t, config.Notifications.Routing)
|
||||
assert.Equal(t, "#dev-bbgo", config.Notifications.Slack.DefaultChannel)
|
||||
assert.Equal(t, "#error", config.Notifications.Slack.ErrorChannel)
|
||||
},
|
||||
|
|
|
@ -30,7 +30,6 @@ import (
|
|||
"github.com/c9s/bbgo/pkg/slack/slacklog"
|
||||
"github.com/c9s/bbgo/pkg/types"
|
||||
"github.com/c9s/bbgo/pkg/util"
|
||||
"github.com/c9s/bbgo/pkg/util/templateutil"
|
||||
)
|
||||
|
||||
func init() {
|
||||
|
@ -306,146 +305,6 @@ func (environ *Environment) ConfigurePersistence(conf *PersistenceConfig) error
|
|||
// for symbol-based routes, we should register the same symbol rules for each session.
|
||||
// for session-based routes, we should set the fixed callbacks for each session
|
||||
func (environ *Environment) ConfigureNotificationRouting(conf *NotificationConfig) error {
|
||||
// configure routing here
|
||||
if conf.SymbolChannels != nil {
|
||||
Notification.SymbolChannelRouter.AddRoute(conf.SymbolChannels)
|
||||
}
|
||||
if conf.SessionChannels != nil {
|
||||
Notification.SessionChannelRouter.AddRoute(conf.SessionChannels)
|
||||
}
|
||||
if conf.Routing != nil {
|
||||
// configure passive object notification routing
|
||||
switch conf.Routing.Trade {
|
||||
case "$silent": // silent, do not setup notification
|
||||
|
||||
case "$session":
|
||||
defaultTradeUpdateHandler := func(trade types.Trade) {
|
||||
Notify(&trade)
|
||||
}
|
||||
for name := range environ.sessions {
|
||||
session := environ.sessions[name]
|
||||
|
||||
// if we can route session name to channel successfully...
|
||||
channel, ok := Notification.SessionChannelRouter.Route(name)
|
||||
if ok {
|
||||
session.UserDataStream.OnTradeUpdate(func(trade types.Trade) {
|
||||
Notification.NotifyTo(channel, &trade)
|
||||
})
|
||||
} else {
|
||||
session.UserDataStream.OnTradeUpdate(defaultTradeUpdateHandler)
|
||||
}
|
||||
}
|
||||
|
||||
case "$symbol":
|
||||
// configure object routes for Trade
|
||||
Notification.ObjectChannelRouter.Route(func(obj interface{}) (channel string, ok bool) {
|
||||
trade, matched := obj.(*types.Trade)
|
||||
if !matched {
|
||||
return
|
||||
}
|
||||
channel, ok = Notification.SymbolChannelRouter.Route(trade.Symbol)
|
||||
return
|
||||
})
|
||||
|
||||
// use same handler for each session
|
||||
handler := func(trade types.Trade) {
|
||||
channel, ok := Notification.RouteObject(&trade)
|
||||
if ok {
|
||||
NotifyTo(channel, &trade)
|
||||
} else {
|
||||
Notify(&trade)
|
||||
}
|
||||
}
|
||||
for _, session := range environ.sessions {
|
||||
session.UserDataStream.OnTradeUpdate(handler)
|
||||
}
|
||||
}
|
||||
|
||||
switch conf.Routing.Order {
|
||||
|
||||
case "$silent": // silent, do not setup notification
|
||||
|
||||
case "$session":
|
||||
defaultOrderUpdateHandler := func(order types.Order) {
|
||||
text := templateutil.Render(TemplateOrderReport, order)
|
||||
Notify(text, &order)
|
||||
}
|
||||
for name := range environ.sessions {
|
||||
session := environ.sessions[name]
|
||||
|
||||
// if we can route session name to channel successfully...
|
||||
channel, ok := Notification.SessionChannelRouter.Route(name)
|
||||
if ok {
|
||||
session.UserDataStream.OnOrderUpdate(func(order types.Order) {
|
||||
text := templateutil.Render(TemplateOrderReport, order)
|
||||
NotifyTo(channel, text, &order)
|
||||
})
|
||||
} else {
|
||||
session.UserDataStream.OnOrderUpdate(defaultOrderUpdateHandler)
|
||||
}
|
||||
}
|
||||
|
||||
case "$symbol":
|
||||
// add object route
|
||||
Notification.ObjectChannelRouter.Route(func(obj interface{}) (channel string, ok bool) {
|
||||
order, matched := obj.(*types.Order)
|
||||
if !matched {
|
||||
return
|
||||
}
|
||||
channel, ok = Notification.SymbolChannelRouter.Route(order.Symbol)
|
||||
return
|
||||
})
|
||||
|
||||
// use same handler for each session
|
||||
handler := func(order types.Order) {
|
||||
text := templateutil.Render(TemplateOrderReport, order)
|
||||
channel, ok := Notification.RouteObject(&order)
|
||||
if ok {
|
||||
NotifyTo(channel, text, &order)
|
||||
} else {
|
||||
Notify(text, &order)
|
||||
}
|
||||
}
|
||||
for _, session := range environ.sessions {
|
||||
session.UserDataStream.OnOrderUpdate(handler)
|
||||
}
|
||||
}
|
||||
|
||||
switch conf.Routing.SubmitOrder {
|
||||
|
||||
case "$silent": // silent, do not setup notification
|
||||
|
||||
case "$symbol":
|
||||
// add object route
|
||||
Notification.ObjectChannelRouter.Route(func(obj interface{}) (channel string, ok bool) {
|
||||
order, matched := obj.(*types.SubmitOrder)
|
||||
if !matched {
|
||||
return
|
||||
}
|
||||
|
||||
channel, ok = Notification.SymbolChannelRouter.Route(order.Symbol)
|
||||
return
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
// currently, not used
|
||||
// FIXME: this is causing cyclic import
|
||||
/*
|
||||
switch conf.Routing.PnL {
|
||||
case "$symbol":
|
||||
environ.ObjectChannelRouter.Route(func(obj interface{}) (channel string, ok bool) {
|
||||
report, matched := obj.(*pnl.AverageCostPnlReport)
|
||||
if !matched {
|
||||
return
|
||||
}
|
||||
channel, ok = environ.SymbolChannelRouter.Route(report.Symbol)
|
||||
return
|
||||
})
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -773,16 +632,9 @@ func (environ *Environment) syncSession(ctx context.Context, session *ExchangeSe
|
|||
}
|
||||
|
||||
func (environ *Environment) ConfigureNotificationSystem(userConfig *Config) error {
|
||||
|
||||
// setup default notification config
|
||||
if userConfig.Notifications == nil {
|
||||
userConfig.Notifications = &NotificationConfig{
|
||||
Routing: &SlackNotificationRouting{
|
||||
Trade: "$session",
|
||||
Order: "$silent",
|
||||
SubmitOrder: "$silent",
|
||||
},
|
||||
}
|
||||
userConfig.Notifications = &NotificationConfig{}
|
||||
}
|
||||
|
||||
var persistence = PersistenceServiceFacade.Get()
|
||||
|
|
Loading…
Reference in New Issue
Block a user