diff --git a/src/client/spark/Modules/Market/TimeAndSales/TimeAndSalesModel.cs b/src/client/spark/Modules/Market/TimeAndSales/TimeAndSalesModel.cs index 1805c995a..a3c54339a 100644 --- a/src/client/spark/Modules/Market/TimeAndSales/TimeAndSalesModel.cs +++ b/src/client/spark/Modules/Market/TimeAndSales/TimeAndSalesModel.cs @@ -1,46 +1,53 @@ using bts.api; using bts.co2; using mbts.co2; +using mbts.collections.extensions; using System; -using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Reactive.Linq; -namespace mbts.spark -{ +namespace mbts.spark { struct TradeInfo { - public SecurityImmutable security; - public TradeMessageImmutable trade; + public readonly SecurityImmutable security; + public readonly TradeMessageImmutable trade; + + public TradeInfo(SecurityImmutable security, TradeMessageImmutable trade) { + this.security = security; + this.trade = trade; + } } - class TimeAndSalesModel - { - public TimeAndSalesModel(IBus bus, MDConsumer consumer, SecurityCriteria security_criteria) { - var user_syms = UserSymbol.get_all(); - var cache = new ConcurrentDictionary(); - trades = security_criteria - .subscribe_md_map(bus) - .Select(md_map => { - var channels = md_map.Values.Select(smd => cache.GetOrAdd(smd.security.Id, _ => consumer.get_channel(smd.security.Symbol, smd.security.security_type(), smd.security.Exchange))); - return Observable.Interval(SparkUtils.DefaultPollInterval) - .Select(_ => { - var info = new List(); - foreach (var channel in channels) { - foreach (var trade in channel.read_trades(ref _cursor)) { - if (md_map.TryGetValue(trade.Key, out SecurityMD smd)) { - info.Add(new TradeInfo() { - trade = trade, - security = smd.security - }); - } - } - } - return info; - }); - }).SelectMany(vals => vals); - } + + class TimeAndSalesModel { + static readonly TimeSpan _poll_interval = TimeSpan.FromSeconds(0.4); + + public TimeAndSalesModel(IBus bus, MDConsumer consumer, SecurityCriteria security_criteria) => + trades = security_criteria.subscribe_md_map(bus) + .Scan(new Dictionary(), + (cache, md_map) => { + var copy = new Dictionary(cache); + copy.sync_with(md_map, (id, smd) => new SecurityTradeConsumer(smd.security, consumer.get_channel(smd.security.Symbol, smd.security.security_type(), smd.security.Exchange))); + return copy; + } + ).Select(cache => + Observable.Interval(_poll_interval) + .Select(_ => new List(cache.Values.SelectMany(c => c.read_trades()))) + ).SelectMany(_ => _); - Int64 _cursor = 0; public IObservable> trades { get; } + + private class SecurityTradeConsumer { + readonly SecurityImmutable _security; + readonly MDConsumerChannel _channel; + long _cursor = 0; + + public SecurityTradeConsumer(SecurityImmutable security, MDConsumerChannel channel) { + _security = security; + _channel = channel; + } + + public IEnumerable read_trades() => + _channel.read_trades(ref _cursor).Select(tm => new TradeInfo(_security, tm)); + } } } \ No newline at end of file diff --git a/src/client/spark/core/GlobalExtensions/GenericCollectionExtensions.cs b/src/client/spark/core/GlobalExtensions/GenericCollectionExtensions.cs index 9c45ed72e..54f319466 100644 --- a/src/client/spark/core/GlobalExtensions/GenericCollectionExtensions.cs +++ b/src/client/spark/core/GlobalExtensions/GenericCollectionExtensions.cs @@ -588,5 +588,33 @@ namespace mbts.collections.extensions if (dictionary.TryGetValue(key, out TValue curr)) update_existing(curr); else dictionary.Add(key, create_new()); } + + // this overload prevents 'ambiguous calls' compilation errors due to Dictionary implementing IDictionary, and IReadOnlyDictionary. + public static void sync_with(this IDictionary dictionary, Dictionary source, Func selector) => + _sync_with(dictionary, source, selector); + + public static void sync_with(this IDictionary dictionary, IDictionary source, Func selector) => + _sync_with(dictionary, source, selector); + + public static void sync_with(this IDictionary dictionary, IReadOnlyDictionary source, Func selector) => + _sync_with_ro(dictionary, source, selector); + + static void _sync_with(this IDictionary dictionary, IDictionary source, Func selector) { + var removes = dictionary.Keys.Except(source.Keys).ToList(); + foreach (var k in removes) + dictionary.Remove(k); + var adds = source.Except(dictionary.Keys, kv => kv.Key).ToList(); + foreach (var kv in adds) + dictionary.Add(kv.Key, selector(kv.Key, kv.Value)); + } + + static void _sync_with_ro(this IDictionary dictionary, IReadOnlyDictionary source, Func selector) { + var removes = dictionary.Keys.Except(source.Keys).ToList(); + foreach (var k in removes) + dictionary.Remove(k); + var adds = source.Except(dictionary.Keys, kv => kv.Key).ToList(); + foreach (var kv in adds) + dictionary.Add(kv.Key, selector(kv.Key, kv.Value)); + } } } \ No newline at end of file