From 4e85bf45200ee6023a7b5ade0b1c94a5403de82a Mon Sep 17 00:00:00 2001 From: John Colvin Date: Thu, 14 Mar 2019 19:52:44 -0500 Subject: [PATCH] OrderBook - refactor 'property-update-throttle' code to use CommonValueMonitor --- .../Modules/Trading/OrderBook/OrderBookVM.cs | 222 +++++++++--------- .../spark/core/Utility/CommonValueMonitor.cs | 162 +++++++++++++ 2 files changed, 271 insertions(+), 113 deletions(-) create mode 100644 src/client/spark/core/Utility/CommonValueMonitor.cs diff --git a/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs b/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs index 5beb44abf..32a1212b2 100644 --- a/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs +++ b/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs @@ -83,21 +83,18 @@ namespace mbts.spark { toggle_fills_slave_grid_is_visible_cmd = new DelegateCommand(() => fills_slave_grid_is_visible = !fills_slave_grid_is_visible); grid_settings_manager = _create_grid_settings_manager(owner_title, bus, notification_service, model, rows, core_columns); IDisposable attach_vm_hooks = null; - _selected_algo_mon.common_algo_changed += on_common_selected_algo_changed; - _selected_algo_mon.common_sec_md_changed += on_common_selected_sec_md_changed; - _selected_algo_mon.common_price_changed += on_common_price_changed; - _selected_algo_mon.common_size_changed += on_common_size_changed; - _selected_algo_mon.all_active_changed += on_all_selected_orders_active_changed; begin_attaching += on_begin_attaching; end_attaching += on_end_attaching; concat_disposable( + _selected_algo_mon.subscribe_common_algo().Subscribe(on_common_selected_algo_changed), + _selected_algo_mon.subscribe_common_sec_md().Subscribe(on_common_selected_sec_md_changed), + _selected_algo_mon.subscribe_price().Subscribe(on_common_price_changed), + _selected_algo_mon.subscribe_size().Subscribe(on_common_size_changed), + _selected_algo_mon.subscribe_all_active().Subscribe(on_all_selected_orders_active_changed), model.on_order_tuples.OnDispatcher().Subscribe(_row_collection.update), grid_settings_manager, order_mod_vm.algo_vm.connect_data(Observable.Return(model)), - order_mod_vm.algo_vm.connect_data(Observable.FromEvent, SecurityImmutable?>( - cb => (oldval, newval) => cb(newval), - cb => _selected_algo_mon.common_spreader_sec_changed += cb, - cb => _selected_algo_mon.common_spreader_sec_changed -= cb)), + order_mod_vm.algo_vm.connect_data(_selected_algo_mon.subscribe_common_spreader_sec()), order_mod_vm.algo_vm, bus.subscribe(SparkTags.SecuritiesTag).OnDispatcher().Subscribe(_selected_algo_mon.on_security_md_map), attach_vm, @@ -111,11 +108,6 @@ namespace mbts.spark { Disposable.Create(() => { _columns.filter_changed -= on_filter_changed; _row_collection.CollectionChanged -= _row_collection_CollectionChanged; - _selected_algo_mon.common_algo_changed -= on_common_selected_algo_changed; - _selected_algo_mon.common_sec_md_changed -= on_common_selected_sec_md_changed; - _selected_algo_mon.common_price_changed += on_common_price_changed; - _selected_algo_mon.common_size_changed += on_common_size_changed; - _selected_algo_mon.all_active_changed -= on_all_selected_orders_active_changed; begin_attaching -= on_begin_attaching; end_attaching -= on_end_attaching; Disposable.dispose(ref attach_vm_hooks); @@ -134,7 +126,7 @@ namespace mbts.spark { return ft => keys.Contains(ft.fill.OrderKey); } void on_filter_changed() => _row_collection.filter = _columns.get_filter(); - void on_common_selected_algo_changed(CustomOrderDefinitionImmutable? oldval, CustomOrderDefinitionImmutable? newval) { + void on_common_selected_algo_changed(CustomOrderDefinitionImmutable? newval) { if (newval.HasValue) { order_mod_vm.algo_vm.load_def(newval.Value); } else { @@ -142,11 +134,11 @@ namespace mbts.spark { } } - void on_common_selected_sec_md_changed(SecurityMD? oldval, SecurityMD? newval) => order_mod_vm.security_md = newval; - void on_common_price_changed(double oldval, double newval) => order_mod_vm.price = newval; - void on_common_size_changed(uint? oldval, uint? newval) => order_mod_vm.size_calculator.value = newval; + void on_common_selected_sec_md_changed(SecurityMD? newval) => order_mod_vm.security_md = newval; + void on_common_price_changed(double newval) => order_mod_vm.price = newval; + void on_common_size_changed(uint? newval) => order_mod_vm.size_calculator.value = newval; - void on_all_selected_orders_active_changed(bool oldval, bool newval) { + void on_all_selected_orders_active_changed(bool newval) { order_mod_vm.algo_vm.editing_permissions_mask = newval ? AlgoEditingPermissions.AllowEditValues : AlgoEditingPermissions.None; modify_selected_algos_async_cmd.RaiseCanExecuteChanged(); send_attached_orders_async_cmd.RaiseCanExecuteChanged(); @@ -161,17 +153,8 @@ namespace mbts.spark { await Task.WhenAll(order_tups.Select(t => _model.send_order(t.order.Def.with(attach_target_order_key: t.order.Key, custom_def: algo), price, size))); } - void on_begin_attaching() { - Disposable.dispose(ref attach_vm_hooks); - attach_vm.security_md = _selected_algo_mon.common_sec_md; - _selected_algo_mon.common_sec_md_changed += on_security_md_changed; - attach_vm_hooks = Disposable.Create(() => _selected_algo_mon.common_sec_md_changed -= on_security_md_changed); - void on_security_md_changed(SecurityMD? oldval, SecurityMD? newval) => - attach_vm.security_md = newval; - } - - void on_end_attaching() => - Disposable.dispose(ref attach_vm_hooks); + void on_begin_attaching() => Disposable.replace(ref attach_vm_hooks, _selected_algo_mon.subscribe_common_sec_md().Subscribe(smd => attach_vm.security_md = smd)); + void on_end_attaching() => Disposable.dispose(ref attach_vm_hooks); } private void _row_collection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { @@ -254,22 +237,20 @@ namespace mbts.spark { public void row_selection_changed(SelectionChangedEventArgs e) { try { - using (_selected_algo_mon.update()) { - if (e.RemovedItems != null) { - foreach (var row_vm in e.RemovedItems.Cast()) { - row_vm.is_selected = false; - _selected_algo_mon.on_row_unselected(row_vm); - } + if (e.RemovedItems != null) { + foreach (var row_vm in e.RemovedItems.Cast()) { + row_vm.is_selected = false; + _selected_algo_mon.on_row_unselected(row_vm); } - if (e.AddedItems != null) { - foreach (var row_vm in e.AddedItems.Cast()) { - row_vm.is_selected = true; - _selected_algo_mon.on_row_selected(row_vm); - } - } - RowSelectionHelper.apply_changes(selected_rows, e); - selected_order_changed?.Invoke(current_order); } + if (e.AddedItems != null) { + foreach (var row_vm in e.AddedItems.Cast()) { + row_vm.is_selected = true; + _selected_algo_mon.on_row_selected(row_vm); + } + } + RowSelectionHelper.apply_changes(selected_rows, e); + selected_order_changed?.Invoke(current_order); } catch(Exception ex) { // [John]I observed a strange null-reference exception in this callback when the launcher was shutting down (see https://bluetradesys.atlassian.net/browse/SPARK-2089) Log.Ui.error($"[{nameof(OrderBookVM)}] Error handling selection change: {ex}"); @@ -551,104 +532,119 @@ namespace mbts.spark { } /// - /// Monitors selected rows, and determines whether or not they are all active-orders, with a common algo structure. - /// When all selected rows satisfy this condition, will be non-null, and its value may be used to populate algo-editor panel on the right side of the window. - /// In terms of user-interaction, this means the user may edit the algo params for multiple orders at once if they are all active and share a common algo structure. + /// Monitors selected rows, and determines whether or not they are all active-orders, with common parameters. /// class SelectedAlgosMonitor { static readonly TimeSpan _throttle = TimeSpan.FromSeconds(0.1); - readonly PropertyUpdateThrottle _common_algo = new PropertyUpdateThrottle(_throttle); - readonly PropertyUpdateThrottle _common_spreader_sec = new PropertyUpdateThrottle(_throttle); - readonly PropertyUpdateThrottle _common_sec = new PropertyUpdateThrottle(_throttle); - readonly PropertyUpdateThrottle _common_sec_md = new PropertyUpdateThrottle(_throttle); - readonly PropertyUpdateThrottle _common_price = new PropertyUpdateThrottle(_throttle); - readonly PropertyUpdateThrottle _common_size = new PropertyUpdateThrottle(_throttle); - readonly PropertyUpdateThrottle _all_active = new PropertyUpdateThrottle(_throttle); - readonly SetMap<(AlgoStructureKey structure, SecurityImmutable? spreader_sec), OrderRecordImmutable> _algo_structure_index = new SetMap<(AlgoStructureKey structure, SecurityImmutable? spreader_sec), OrderRecordImmutable>(); - readonly SetMap _price_order_map = new SetMap(); - readonly SetMap _size_order_map = new SetMap(); - readonly SetMap _sec_order_map = new SetMap(); + readonly CommonValueMonitor _algo_mon = new CommonValueMonitor(); + readonly CommonValueMonitor _spreader_sec_mon = new CommonValueMonitor(); + readonly CommonValueMonitor _sec_mon = new CommonValueMonitor(); + readonly CommonValueMonitor _price_mon = new CommonValueMonitor(double.NaN); + readonly CommonValueMonitor _size_mon = new CommonValueMonitor(); + readonly CommonValueMonitor _account_mon = new CommonValueMonitor(); + readonly CommonValueMonitor _active_mon = new CommonValueMonitor(); + readonly CommonValueMonitorMultiplexor _group_mon = new CommonValueMonitorMultiplexor(); readonly DynamicOrderHelper _dyn_helper; IReadOnlyDictionary _security_md_map = DictionaryHelper.empty(); - int _inactive_order_count; - public SelectedAlgosMonitor(DynamicOrderHelper dyn_helper) => + public SelectedAlgosMonitor(DynamicOrderHelper dyn_helper) { _dyn_helper = dyn_helper; + _group_mon.add_member_mon(_algo_mon, _algo_key); + _group_mon.add_member_mon(_spreader_sec_mon, _get_spreader_sec, s => s.HasValue); + _group_mon.add_member_mon(_sec_mon, osam => osam.security, s => s.HasValue); + _group_mon.add_member_mon(_price_mon, osam => osam.order.price()); + _group_mon.add_member_mon(_size_mon, osam => osam.order.quantity()); + _group_mon.add_member_mon(_account_mon, osam => new AccountTuple(osam.order.account_id(), osam.order.account())); + _group_mon.add_member_mon(_active_mon, osam => osam.order.is_active()); + } - public event PropertyChange common_algo_changed { add => _common_algo.changed += value; remove => _common_algo.changed -= value; } - public event PropertyChange common_sec_changed { add => _common_sec.changed += value; remove => _common_sec.changed -= value; } - public event PropertyChange common_sec_md_changed { add => _common_sec_md.changed += value; remove => _common_sec_md.changed -= value; } - public event PropertyChange common_spreader_sec_changed { add => _common_spreader_sec.changed += value; remove => _common_spreader_sec.changed -= value; } - public event PropertyChange common_price_changed { add => _common_price.changed += value; remove => _common_price.changed -= value; } - public event PropertyChange common_size_changed { add => _common_size.changed += value; remove => _common_size.changed -= value; } - public event PropertyChange all_active_changed { add => _all_active.changed += value; remove => _all_active.changed -= value; } + event Action _security_md_map_changed; - public CustomOrderDefinitionImmutable? common_algo => _algo_structure_index.Count == 1 ? _algo_structure_index.First().First().Def.CustomDef : (CustomOrderDefinitionImmutable?)null; - public SecurityImmutable? common_sec => _sec_order_map.TryGetSingle(out IGroupingSet gs) ? gs.Key : null; + public IObservable subscribe_common_algo() => _algo_mon.SubscribeThrottledNullable(_throttle).Select(ask => ask.HasValue ? ask.Value.algo: (CustomOrderDefinitionImmutable?)null); + public IObservable subscribe_common_spreader_sec() => _spreader_sec_mon.SubscribeThrottled(_throttle); + public IObservable subscribe_common_sec() => _sec_mon.SubscribeThrottled(_throttle); + public IObservable subscribe_common_sec_md() => + Observable.Create(o => { + SecurityMD? last_value = null; + _sec_mon.common_value_changed += on_common_sec_changed; + _security_md_map_changed += refresh; + return Disposable.Create(() => { + _sec_mon.common_value_changed -= on_common_sec_changed; + _security_md_map_changed -= refresh; + }); + void on_common_sec_changed(SecurityImmutable? _) => refresh(); + void refresh() { + var curr = common_sec_md; + if (curr != last_value) + o.OnNext(last_value = curr); + } + }); + public IObservable subscribe_price() => _price_mon.SubscribeThrottled(_throttle); + public IObservable subscribe_size() => _size_mon.SubscribeThrottledNullable(_throttle); + public IObservable subscribe_account() => _account_mon.SubscribeThrottledNullable(_throttle); + public IObservable subscribe_all_active() => _active_mon.SubscribeThrottled(_throttle); + + public CustomOrderDefinitionImmutable? common_algo => _algo_mon.common_value_nullable()?.algo; + public SecurityImmutable? common_sec => _sec_mon.common_value; public SecurityMD? common_sec_md => common_sec is SecurityImmutable sec && _security_md_map.TryGetValue(sec.Id, out SecurityMD smd) ? smd : (SecurityMD?)null; - public SecurityImmutable? common_spreader_sec => _algo_structure_index.Count == 1 ? _algo_structure_index.First().Key.spreader_sec : null; - public double common_price => _price_order_map.TryGetSingle(out IGroupingSet gs) ? gs.Key : double.NaN; - public uint? common_size => _size_order_map.TryGetSingle(out IGroupingSet gs) ? gs.Key : (uint?)null; - public bool all_active => _inactive_order_count == 0 && _algo_structure_index.Count > 0; + public SecurityImmutable? common_spreader_sec => _spreader_sec_mon.common_value; + public double common_price => _price_mon.try_get_common_value(out double p) ? p : double.NaN; + public uint? common_size => _size_mon.common_value_nullable(); + public AccountTuple? common_account => _account_mon.common_value_nullable(); + public bool all_active => _active_mon.common_value; public void on_row_selected(OrderBookRowVM row) { - _select_algo(row.order_tuple); + _group_mon.add(row.order_tuple); row.updated += _on_row_updated; } public void on_row_unselected(OrderBookRowVM row) { - _unselect_algo(row.order_tuple); + _group_mon.remove(row.order_tuple); row.updated -= _on_row_updated; } public void on_security_md_map(IReadOnlyDictionary security_md_map) { - using (update()) - _security_md_map = security_md_map; - } - - public IDisposable update() { - var old_algo = common_algo; - bool old_all_active = all_active; - return Disposable.Create(() => { - _common_algo.value = common_algo; - _common_sec.value = common_sec; - _common_sec_md.value = common_sec_md; - _common_spreader_sec.value = common_spreader_sec; - _common_price.value = common_price; - _common_size.value = common_size; - _all_active.value = all_active; - }); + _security_md_map = security_md_map; + _security_md_map_changed?.Invoke(); } void _on_row_updated((OrderSecurityAlgoMeta oldval, OrderSecurityAlgoMeta newval) ch) { - using (update()) { - _unselect_algo(ch.oldval); - _select_algo(ch.newval); - } - } - - void _select_algo(OrderSecurityAlgoMeta osam) { - _algo_structure_index.add((AlgoStructureSerializer.structure_key(osam.order.Def.CustomDef, _dyn_helper.dynamic_type_index), _get_spreader_sec(osam)), osam.order); - _sec_order_map.add(osam.security, osam.order.Key); - _price_order_map.add(osam.order.price(), osam.order.Key); - _size_order_map.add(osam.order.quantity(), osam.order.Key); - if (!osam.order.is_active()) - _inactive_order_count++; - } - - void _unselect_algo(OrderSecurityAlgoMeta osam) { - _algo_structure_index.remove((AlgoStructureSerializer.structure_key(osam.order.Def.CustomDef, _dyn_helper.dynamic_type_index), _get_spreader_sec(osam)), osam.order); - _sec_order_map.remove(osam.security, osam.order.Key); - _price_order_map.remove(osam.order.price(), osam.order.Key); - _size_order_map.remove(osam.order.quantity(), osam.order.Key); - if (!osam.order.is_active()) - _inactive_order_count--; + _group_mon.remove(ch.oldval); + _group_mon.add(ch.newval); } static SecurityImmutable? _get_spreader_sec(OrderSecurityAlgoMeta osam) => OrderDataHelper.enumerate_algo_hierarchy(osam.order.Def.CustomDef.CloneToMutable()) .Any(algo => algo.CustomCase == CustomOrderDefinition.CustomOneofCase.Spreader) ? osam.security : null; + + AlgoSecurityKey _algo_key(OrderSecurityAlgoMeta osam) => + new AlgoSecurityKey(osam.order.Def.CustomDef, _get_spreader_sec(osam), AlgoStructureSerializer.structure_key(osam.order.Def.CustomDef, _dyn_helper.dynamic_type_index)); + + SecurityMD? _get_sec_md(OrderSecurityAlgoMeta osam) => + osam.security.HasValue && _security_md_map.TryGetValue(osam.security.Value.Id, out SecurityMD smd) ? smd : (SecurityMD?)null; + + struct AlgoSecurityKey { + public readonly CustomOrderDefinitionImmutable algo; + public readonly SecurityImmutable? security; + public readonly AlgoStructureKey key; + + public AlgoSecurityKey(CustomOrderDefinitionImmutable algo, SecurityImmutable? security, AlgoStructureKey key) { + this.algo = algo; + this.security = security; + this.key = key; + } + + public bool Equals(AlgoSecurityKey other) => + security == other.security && + key == other.key; + + public override bool Equals(object obj) => obj is AlgoSecurityKey other && Equals(other); + public override int GetHashCode() => key.GetHashCode(); + + public static bool operator ==(AlgoSecurityKey a, AlgoSecurityKey b) => a.Equals(b); + public static bool operator !=(AlgoSecurityKey a, AlgoSecurityKey b) => !a.Equals(b); + } } } } \ No newline at end of file diff --git a/src/client/spark/core/Utility/CommonValueMonitor.cs b/src/client/spark/core/Utility/CommonValueMonitor.cs new file mode 100644 index 000000000..0747df2f4 --- /dev/null +++ b/src/client/spark/core/Utility/CommonValueMonitor.cs @@ -0,0 +1,162 @@ +using mbts.collections; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reactive.Disposables; +using System.Reactive.Linq; +using System.Threading.Tasks; + +namespace bts.utils { + class CommonValueMonitor { + readonly RefCountMap _values; + T _default_value; + + public CommonValueMonitor() + : this(default, EqualityComparer.Default) { } + + public CommonValueMonitor(T default_value) + : this(default_value, EqualityComparer.Default) { } + + public CommonValueMonitor(T default_value, IEqualityComparer comparer) { + _default_value = default_value; + _values = new RefCountMap(comparer); + } + + public event Action common_value_changed; + + public ICollection distinct_values => _values.keys; + public T common_value => _values.keys.SafeSingleOrDefault(); + + public void add(T value) { + if (_values.increment_adds_key(value)) { + switch (_values.Count) { + case 1: common_value_changed?.Invoke(value); break; + case 2: common_value_changed?.Invoke(_default_value); break; + } + } + } + + public void remove(T value) { + if (_values.decrement_removes_key(value)) { + switch (_values.Count) { + case 0: common_value_changed?.Invoke(_default_value); break; + case 1: common_value_changed?.Invoke(_values.keys.First()); break; + } + } + } + } + + class CommonValueMonitorMultiplexor { + int _member_id_counter; + readonly Dictionary add, Action remove)> _members = new Dictionary add, Action remove)>(); + + public void add(TSource value) { + foreach ((var add, var remove) in _members.Values) + add(value); + } + + public void remove(TSource value) { + foreach ((var add, var remove) in _members.Values) + remove(value); + } + + public IDisposable add_member_mon(CommonValueMonitor member_mon, Func get_value) { + int id = _member_id_counter++; + _members.Add(id, ( + v => member_mon.add(get_value(v)), + v => member_mon.remove(get_value(v)))); + return Disposable.Create(() => _members.Remove(id)); + } + + public IDisposable add_member_mon(CommonValueMonitor member_mon, Func get_value, Func where) { + int id = _member_id_counter++; + _members.Add(id, ( + v => { + var res = get_value(v); + if (where(res)) + member_mon.add(res); + }, + v => { + var res = get_value(v); + if (where(res)) + member_mon.remove(res); + })); + return Disposable.Create(() => _members.Remove(id)); + } + } + + static class CommonValueMonitorExtensions { + public static bool try_get_common_value(this CommonValueMonitor monitor, out T result) => + monitor.distinct_values.TryGetSingle(out result); + + public static T? common_value_nullable(this CommonValueMonitor monitor) where T : struct => + monitor.try_get_common_value(out T result) ? result : (T?)null; + + public static IObservable SubscribeThrottled(this CommonValueMonitor monitor, TimeSpan throttle) => + Observable.Create(o => { + bool busy = false, dirty = false; + o.OnNext(monitor.common_value); + monitor.common_value_changed += on_changed; + return Disposable.Create(() => monitor.common_value_changed -= on_changed); + async void on_changed(T _) { + if (busy) { + dirty = true; + return; + } + dirty = false; + do { + o.OnNext(monitor.common_value); + await Task.Delay(throttle); + } while (dirty); + busy = false; + } + }); + + public static IObservable SubscribeThrottled(this CommonValueMonitor monitor, TimeSpan throttle, T default_value) => + Observable.Create(o => { + bool busy = false, dirty = false; + notify(); + monitor.common_value_changed += on_changed; + return Disposable.Create(() => monitor.common_value_changed -= on_changed); + async void on_changed(T _) { + if (busy) { + dirty = true; + return; + } + dirty = false; + do { + notify(); + await Task.Delay(throttle); + } while (dirty); + busy = false; + } + void notify() => + o.OnNext(monitor.try_get_common_value(out T result) ? result : default_value); + }); + + public static IObservable SubscribeThrottled(this CommonValueMonitor monitor, TimeSpan throttle, Func selector, TResult default_value) => + Observable.Create(o => { + bool busy = false, dirty = false; + notify(); + monitor.common_value_changed += on_changed; + return Disposable.Create(() => monitor.common_value_changed -= on_changed); + async void on_changed(T _) { + if (busy) { + dirty = true; + return; + } + dirty = false; + do { + notify(); + await Task.Delay(throttle); + } while (dirty); + busy = false; + } + void notify() => + o.OnNext(monitor.try_get_common_value(out T result) ? selector(result) : default_value); + }); + + public static IObservable SubscribeThrottledNullable(this CommonValueMonitor monitor, TimeSpan throttle) where T : struct => + monitor.SubscribeThrottled(throttle, val => val, null); + } +} \ No newline at end of file