From 2b12e7fee1beac368079287c4c6b0271cd37ae9e Mon Sep 17 00:00:00 2001 From: John Colvin Date: Tue, 27 Aug 2019 11:17:28 -0400 Subject: [PATCH] INotificationService.link_to_trades method/impl --- .../Notifications/INotificationService.cs | 65 ++++++++++++++++++- 1 file changed, 62 insertions(+), 3 deletions(-) diff --git a/src/client/spark/Notifications/INotificationService.cs b/src/client/spark/Notifications/INotificationService.cs index 002ead80c..47e61a63a 100644 --- a/src/client/spark/Notifications/INotificationService.cs +++ b/src/client/spark/Notifications/INotificationService.cs @@ -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 toast_notification_predicate); IDisposable link_to_order(IPredicate predicate, SoundNotification sound); IDisposable link_to_order(INotificationPredicate 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 predicate, SoundNotification sound); } [Export(typeof(INotificationService))] partial class NotificationService : INotificationService, IDisposable { + // activity notifications readonly NotificationProcessor _activity_notifications; + // order notifications readonly NotificationProcessor _order_notifications; + // trade notifications + readonly NotificationProcessor _trade_notifications; + readonly MDConsumer _consumer; + readonly Subject> _trade_criteria_subject = new Subject>(); + readonly RefCountMap _trade_criteria_refs = new RefCountMap(); + 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(notifier); _order_notifications = new NotificationProcessor(notifier); + _trade_notifications = new NotificationProcessor(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 toast_notification_predicate) => _activity_notifications.link(toast_notification_predicate); public IDisposable link_to_order(IPredicate predicate, SoundNotification sound) => _order_notifications.link(predicate, sound); public IDisposable link_to_order(INotificationPredicate toast_notification_predicate) => _order_notifications.link(toast_notification_predicate); + + public IDisposable link_to_trades(SecurityCriteria security_criteria, IPredicate 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(PredicateBuilder.create(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 () + void _reset_trade_readers() => + _trade_criteria_subject.OnNext(_trade_criteria_refs.Keys); + void _on_activity(Activity activity) => _activity_notifications.process_item(activity);