INotificationService.link_to_trades method/impl
This commit is contained in:
1 parent
0fb69c8beb
commit
2b12e7fee1
1 file changed
+62
-3
@@ -4,6 +4,7 @@ using bts.api.orderman;
|
||||
using bts.collections;
|
||||
using bts.spark;
|
||||
using mbts;
|
||||
using mbts.co2;
|
||||
using mbts.collections;
|
||||
using mbts.spark;
|
||||
using mbts.ui;
|
||||
@@ -13,6 +14,7 @@ using System.Collections.Generic;
|
||||
using System.ComponentModel.Composition;
|
||||
using System.Linq;
|
||||
using System.Reactive.Linq;
|
||||
using System.Reactive.Subjects;
|
||||
|
||||
namespace bts.shell {
|
||||
interface INotificationService {
|
||||
@@ -22,25 +24,48 @@ namespace bts.shell {
|
||||
IDisposable link_to_activity(INotificationPredicate<Activity> toast_notification_predicate);
|
||||
IDisposable link_to_order(IPredicate<OrderSecurity> predicate, SoundNotification sound);
|
||||
IDisposable link_to_order(INotificationPredicate<OrderSecurity> toast_notification_predicate);
|
||||
// Note: security criteria should not be incorporated in the predicate's 'trigger' impl.
|
||||
// It is easier/cleaner if the INotificationService incorporates the criteria internally.
|
||||
IDisposable link_to_trades(SecurityCriteria security_criteria, IPredicate<TradeInfo> predicate, SoundNotification sound);
|
||||
}
|
||||
|
||||
[Export(typeof(INotificationService))]
|
||||
partial class NotificationService : INotificationService, IDisposable {
|
||||
// activity notifications
|
||||
readonly NotificationProcessor<Activity> _activity_notifications;
|
||||
// order notifications
|
||||
readonly NotificationProcessor<OrderSecurity> _order_notifications;
|
||||
// trade notifications
|
||||
readonly NotificationProcessor<TradeInfo> _trade_notifications;
|
||||
readonly MDConsumer _consumer;
|
||||
readonly Subject<IEnumerable<SecurityCriteria>> _trade_criteria_subject = new Subject<IEnumerable<SecurityCriteria>>();
|
||||
readonly RefCountMap<SecurityCriteria> _trade_criteria_refs = new RefCountMap<SecurityCriteria>();
|
||||
readonly object _trade_lock = new object();
|
||||
//
|
||||
readonly DataCache _data_cache = new DataCache();
|
||||
readonly LoggerContext _log = new LoggerContext(Log.Ui, "Notification Service ", null);
|
||||
|
||||
IDisposable _dispose;
|
||||
|
||||
[ImportingConstructor]
|
||||
public NotificationService(IBus bus, INotifier notifier, OrdersModel orders_model) {
|
||||
public NotificationService(IBus bus, INotifier notifier, OrdersModel orders_model, MDConsumer consumer) {
|
||||
this.notifier = notifier;
|
||||
_activity_notifications = new NotificationProcessor<Activity>(notifier);
|
||||
_order_notifications = new NotificationProcessor<OrderSecurity>(notifier);
|
||||
_trade_notifications = new NotificationProcessor<TradeInfo>(notifier);
|
||||
_consumer = consumer;
|
||||
_dispose = Disposable.Create(
|
||||
bus.subscribe(SparkTags.LogActivity).Subscribe(_on_activity),
|
||||
orders_model.on_orders.Subscribe(_on_orders)
|
||||
);
|
||||
orders_model.on_orders.Subscribe(_on_orders),
|
||||
TradesSource.create(
|
||||
consumer,
|
||||
bus.subscribe(SparkTags.SecuritiesTag).BufferLatest(TimeSpan.FromSeconds(2))
|
||||
.Table(s => s.security.Id)
|
||||
.Where(t => t.has_changes())
|
||||
.Select(m => MappedDictionary.create(m.state, smd => smd.security)),
|
||||
_trade_criteria_subject,
|
||||
TimeSpan.FromSeconds(0.2))
|
||||
.Subscribe(_trade_notifications.process_items));
|
||||
}
|
||||
|
||||
[Export(typeof(INotificationServiceDataCache))]
|
||||
@@ -51,8 +76,42 @@ namespace bts.shell {
|
||||
public IDisposable link_to_activity(INotificationPredicate<Activity> toast_notification_predicate) => _activity_notifications.link(toast_notification_predicate);
|
||||
public IDisposable link_to_order(IPredicate<OrderSecurity> predicate, SoundNotification sound) => _order_notifications.link(predicate, sound);
|
||||
public IDisposable link_to_order(INotificationPredicate<OrderSecurity> toast_notification_predicate) => _order_notifications.link(toast_notification_predicate);
|
||||
|
||||
public IDisposable link_to_trades(SecurityCriteria security_criteria, IPredicate<TradeInfo> predicate, SoundNotification sound) {
|
||||
if (predicate == null) {
|
||||
_log.warn("cannot create trade alert sound because the specified predicate is null.");
|
||||
return Disposable.empty;
|
||||
}
|
||||
if (!security_criteria.is_defined()) {
|
||||
// there are simply too many trades to support universal-product predicates, therefor a product filter must be specified (aka SecrityCriteria).
|
||||
_log.warn("cannot create trade alert sound because the specified security criteria is not defined.");
|
||||
return Disposable.empty;
|
||||
}
|
||||
// This criteria predicate may be redundant, but it safe guards against a case
|
||||
// where the criteria has not been incorporated into the predicate,
|
||||
// as there doesn't seem to be a great way to require this in the interface.
|
||||
// Note: verifying the criteria twice (instead of once) should not hurt on perf at all.
|
||||
predicate = predicate.and<TradeInfo>(PredicateBuilder.create<TradeInfo>(t => ContractPickerModel.security_in_criteria(security_criteria, t.security)));
|
||||
var link = _trade_notifications.link(predicate, sound);
|
||||
lock (_trade_lock) {
|
||||
if (_trade_criteria_refs.increment_adds_key(security_criteria))
|
||||
_reset_trade_readers();
|
||||
}
|
||||
return Disposable.Create(() => {
|
||||
Disposable.dispose(ref link);
|
||||
lock (_trade_lock) {
|
||||
if (_trade_criteria_refs.decrement_removes_key(security_criteria))
|
||||
_reset_trade_readers();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void Dispose() => Disposable.dispose(ref _dispose);
|
||||
|
||||
/// should only call this within a locked scope (<see cref="_trade_lock"/>)
|
||||
void _reset_trade_readers() =>
|
||||
_trade_criteria_subject.OnNext(_trade_criteria_refs.Keys);
|
||||
|
||||
void _on_activity(Activity activity) =>
|
||||
_activity_notifications.process_item(activity);
|
||||
|
||||
|
||||
Reference in new issue
Block a user