diff --git a/src/client/spark/Notifications/INotificationServiceDataCache.cs b/src/client/spark/Notifications/INotificationServiceDataCache.cs index 6cff8c154..6467ccb79 100644 --- a/src/client/spark/Notifications/INotificationServiceDataCache.cs +++ b/src/client/spark/Notifications/INotificationServiceDataCache.cs @@ -1,89 +1,86 @@ -using bts.api; -using bts.api.orderman; - -using mbts.spark; -using mbts.utils; - -using System; -using System.Collections.Generic; - -namespace bts.shell { - // Any predicate-sound/notifications that need to reference previous data (i.e. notifications based on change/time, or state changes) should use the INotificationServiceDataCache. - // The INotificationService updates these caches in its IObservable.OnNext callbacks, just before calling all of the predicate-trigger methods. - // Therfore, data referenced through this interface is guarenteed to be up-to-date, and thread safe. It also reduces performance overhead by avoiding redundant bookeeping. - // Note: last_order_reset_time can be used by sound/toast-notification sources to avoid sending false-positive notifications for old orders (i.e. all the orders that arrive in the first update on startup/server restart) - // -------------------------------------------------------------------------------------------------------------------------------------------- - // IMPOARTANT!!! The current implementation of this interface is NOT INTENDED FOR GENERAL USE - order_history is NOT THREAD-SAFE - interface INotificationServiceDataCache { - DateTime last_order_reset_time { get; } - bool is_first_order_update { get; } - IReadOnlyDictionary>> order_history { get; } - } - - static class NotificationServiceDataCacheExtensions { - public static OrderSecurity? get_previous_order_state(this INotificationServiceDataCache notification_service_data_cache, uint key) { - if (notification_service_data_cache.order_history.TryGetValue(key, out List> l)) { - if (l.Count > 1) { - return l[l.Count - 2].data; - } - } - return null; - } - } - - struct TimeStampedData { - public readonly T data; - public readonly DateTime time_stamp; - - public TimeStampedData(T data, DateTime time_stamp) { - this.data = data; - this.time_stamp = time_stamp; - } - } - - partial class NotificationService : INotificationService, IDisposable { - class DataCache : INotificationServiceDataCache { - const int _buffer_size = 10; - readonly Dictionary _order_cache = new Dictionary(); - readonly Dictionary>> _order_history = new Dictionary>>(); - - public IReadOnlyDictionary>> order_history => _order_history; - - public DateTime last_order_reset_time { get; private set; } - public bool is_first_order_update { get; private set; } - - internal void update(IIncrementalUpdate<(SecurityImmutable? sec, OrderRecordImmutable order), uint> data) { - DateTime now = DateTime.Now; - if (data.removed.Count == _order_cache.Count) { - last_order_reset_time = now; - _order_cache.Clear(); - } - is_first_order_update = _order_cache.Count == 0 && data.added.Count > 0; - foreach (var kv in data.removed) { - if (data.added.ContainsKey(kv.Key)) { - _order_cache[kv.Key] = kv.Value; - _order_history[kv.Key].Add(new TimeStampedData(kv.Value, now)); - } else { - _order_cache.Remove(kv.Key); - // TODO: maybe these shouldn't be removed? - // if we add delta/time predicates that need to refer to order history, we might need to keep them around - // although, if the server is telling us to remove them, perhaps we shouldn't consider such data. - _order_history.Remove(kv.Key); - } - } - foreach (var kv in data.added) { - if (data.removed.ContainsKey(kv.Key)) - continue; - _order_cache.Add(kv.Key, kv.Value); - _order_history.AddOrUpdate(kv.Key, - k => new List> { new TimeStampedData(kv.Value, now) }, - (k, l) => { - l.Add(new TimeStampedData(kv.Value, now)); - if (l.Count >= 10) - l.RemoveAt(0); - }); - } - } - } - } -} +using bts.api; +using bts.api.orderman; + +using mbts.spark; +using mbts.utils; + +using System; +using System.Collections.Generic; + +namespace bts.shell { + // Any predicate-sound/notifications that need to reference previous data (i.e. notifications based on change/time, or state changes) should use the INotificationServiceDataCache. + // The INotificationService updates these caches in its IObservable.OnNext callbacks, just before calling all of the predicate-trigger methods. + // Therfore, data referenced through this interface is guarenteed to be up-to-date, and thread safe. It also reduces performance overhead by avoiding redundant bookeeping. + // Note: last_order_reset_time can be used by sound/toast-notification sources to avoid sending false-positive notifications for old orders (i.e. all the orders that arrive in the first update on startup/server restart) + // -------------------------------------------------------------------------------------------------------------------------------------------- + // IMPOARTANT!!! The current implementation of this interface is NOT INTENDED FOR GENERAL USE - order_history is NOT THREAD-SAFE + interface INotificationServiceDataCache { + bool is_first_order_update { get; } + IReadOnlyDictionary>> order_history { get; } + } + + static class NotificationServiceDataCacheExtensions { + public static OrderSecurity? get_previous_order_state(this INotificationServiceDataCache notification_service_data_cache, uint key) { + if (notification_service_data_cache.order_history.TryGetValue(key, out List> l)) { + if (l.Count > 1) { + return l[l.Count - 2].data; + } + } + return null; + } + } + + struct TimeStampedData { + public readonly T data; + public readonly DateTime time_stamp; + + public TimeStampedData(T data, DateTime time_stamp) { + this.data = data; + this.time_stamp = time_stamp; + } + } + + partial class NotificationService : INotificationService, IDisposable { + class DataCache : INotificationServiceDataCache { + const int _buffer_size = 10; + readonly Dictionary _order_cache = new Dictionary(); + readonly Dictionary>> _order_history = new Dictionary>>(); + + public IReadOnlyDictionary>> order_history => _order_history; + + public bool is_first_order_update { get; private set; } + + internal void update(IIncrementalUpdate<(SecurityImmutable? sec, OrderRecordImmutable order), uint> data) { + DateTime now = DateTime.Now; + if (data.removed.Count == _order_cache.Count) { + _order_cache.Clear(); + } + is_first_order_update = _order_cache.Count == 0 && data.added.Count > 0; + foreach (var kv in data.removed) { + if (data.added.ContainsKey(kv.Key)) { + _order_cache[kv.Key] = kv.Value; + _order_history[kv.Key].Add(new TimeStampedData(kv.Value, now)); + } else { + _order_cache.Remove(kv.Key); + // TODO: maybe these shouldn't be removed? + // if we add delta/time predicates that need to refer to order history, we might need to keep them around + // although, if the server is telling us to remove them, perhaps we shouldn't consider such data. + _order_history.Remove(kv.Key); + } + } + foreach (var kv in data.added) { + if (data.removed.ContainsKey(kv.Key)) + continue; + _order_cache.Add(kv.Key, kv.Value); + _order_history.AddOrUpdate(kv.Key, + k => new List> { new TimeStampedData(kv.Value, now) }, + (k, l) => { + l.Add(new TimeStampedData(kv.Value, now)); + if (l.Count >= 10) + l.RemoveAt(0); + }); + } + } + } + } +}