From 04cc18553b4a65ad20fe6d8a64f863f467354df8 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Sun, 21 Oct 2018 16:52:02 -0500 Subject: [PATCH 01/46] DIYGrid.IRowVM - remove 'row_animations' and 'cell_stores' members --- .../Market/TimeAndSales/TimeAndSalesVM.cs | 3 --- .../spark/Modules/Trading/Fills/FillsVM.cs | 3 --- .../Trading/OrderBook/OrderBookRowVM.cs | 6 +----- .../view/Controls/DIYGrid/Row/RowBase.cs | 8 ++++---- .../Settings/Animation/PredicateAnimator.cs | 2 +- .../DIYGrid/Settings/GridSettingsManager.cs | 19 +------------------ 6 files changed, 7 insertions(+), 34 deletions(-) diff --git a/src/client/spark/Modules/Market/TimeAndSales/TimeAndSalesVM.cs b/src/client/spark/Modules/Market/TimeAndSales/TimeAndSalesVM.cs index 7720d7541..7ab8fc44e 100644 --- a/src/client/spark/Modules/Market/TimeAndSales/TimeAndSalesVM.cs +++ b/src/client/spark/Modules/Market/TimeAndSales/TimeAndSalesVM.cs @@ -144,9 +144,6 @@ namespace mbts.spark { public AggressorSide aggressor_side { get; } public ICommand show_ladder { get; } - ObservableCollection DIYGrid.IRowVM.row_animations { get; } = new ObservableCollection(); - IReadOnlyList DIYGrid.IRowVM.cell_stores => new DIYGrid.IGridCellStore[0]; - public string this[TimeAndSalesView.Columns col] { get { switch (col) { diff --git a/src/client/spark/Modules/Trading/Fills/FillsVM.cs b/src/client/spark/Modules/Trading/Fills/FillsVM.cs index f4de61a8c..47a925245 100644 --- a/src/client/spark/Modules/Trading/Fills/FillsVM.cs +++ b/src/client/spark/Modules/Trading/Fills/FillsVM.cs @@ -170,9 +170,6 @@ namespace mbts.spark { public string fill_key => _fill.FillKey; public string order_key => _order.Key.ToString(); - ObservableCollection IRowVM.row_animations { get; } = new ObservableCollection(); - IReadOnlyList IRowVM.cell_stores => new IGridCellStore[0]; - public bool Equals(Row other) => other != null && other._fill.Equals(_fill) && other._order.Equals(_order); public override bool Equals(object obj) => Equals(obj as Row); public override int GetHashCode() => _fill.FillKey.GetHashCode() ^ _order.Key.GetHashCode(); diff --git a/src/client/spark/Modules/Trading/OrderBook/OrderBookRowVM.cs b/src/client/spark/Modules/Trading/OrderBook/OrderBookRowVM.cs index 1e53687e8..4bb8c0322 100644 --- a/src/client/spark/Modules/Trading/OrderBook/OrderBookRowVM.cs +++ b/src/client/spark/Modules/Trading/OrderBook/OrderBookRowVM.cs @@ -8,15 +8,12 @@ using mbts.ui.shell; using System; using System.Collections.Generic; using System.Collections.ObjectModel; -using System.Reactive.Linq; using System.Windows.Input; using static mbts.spark.OrderBookView; using OrderStatus = bts.api.orderman.OrderStatus; namespace mbts.spark { - partial class OrderBookRowVM : DisposableViewModelBase, IEquatable, DIYGrid.IRowVM, DIYGrid.IActionableRowVM { - //public readonly PredicateAnimator animator; - + partial class OrderBookRowVM : DisposableViewModelBase, IEquatable, DIYGrid.IRowVM, DIYGrid.IActionableRowVM { const string _unknown_security_price_format_string = "0.#########"; const int _unknown_security_price_num_fractional_digits = 9; readonly OrderBookVM _book_vm; @@ -97,7 +94,6 @@ namespace mbts.spark { _order = record; _security_md = s; _symbol = _security_md.HasValue ? _security_md.Value.security.Symbol : null; - //animator = new PredicateAnimator(this); _visible = backing(nameof(is_visible), true); _expanded = backing(nameof(is_expanded), false); _depth_level = backing(nameof(depth_level), () => { diff --git a/src/client/spark/view/Controls/DIYGrid/Row/RowBase.cs b/src/client/spark/view/Controls/DIYGrid/Row/RowBase.cs index a48ecc63c..bb5c72810 100644 --- a/src/client/spark/view/Controls/DIYGrid/Row/RowBase.cs +++ b/src/client/spark/view/Controls/DIYGrid/Row/RowBase.cs @@ -18,13 +18,13 @@ namespace mbts.ui.controls { public interface IRowVM { event Action invalidated; void raise_invalidated(); - ObservableCollection row_animations { get; } - IReadOnlyList cell_stores { get; } } - public interface IRowVM : IRowVM { - event Action<(TRowData old_val, TRowData new_val)> updated; + public interface IAnimatableRowVM : IRowVM { + ObservableCollection row_animations { get; } + IReadOnlyList cell_stores { get; } TRowData current_data { get; } + event Action<(TRowData old_val, TRowData new_val)> updated; } public interface IActionableRowVM { diff --git a/src/client/spark/view/Controls/DIYGrid/Settings/Animation/PredicateAnimator.cs b/src/client/spark/view/Controls/DIYGrid/Settings/Animation/PredicateAnimator.cs index f4e4f9603..0f62c5918 100644 --- a/src/client/spark/view/Controls/DIYGrid/Settings/Animation/PredicateAnimator.cs +++ b/src/client/spark/view/Controls/DIYGrid/Settings/Animation/PredicateAnimator.cs @@ -5,7 +5,7 @@ using System.Collections.Generic; using System.Threading.Tasks; namespace mbts.ui { - class PredicateAnimator where TRow : DIYGrid.IRowVM { + class PredicateAnimator where TRow : DIYGrid.IAnimatableRowVM { readonly TRow _row; SetMap, AnimationDefinitionWrapper> _predicate_animation_map = new SetMap, AnimationDefinitionWrapper>(); HashSet> _active_predicates = new HashSet>(); diff --git a/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsManager.cs b/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsManager.cs index 22e31b721..004e61dd5 100644 --- a/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsManager.cs +++ b/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsManager.cs @@ -19,7 +19,7 @@ using static mbts.ui.controls.DIYGrid; namespace mbts.ui { class GridSettingsManager : ViewModelBase, IValueFactory<(GridSettings grid, IEnumerable> predicate_animations_data)>, IDisposable - where TRow : IRowVM + where TRow : IRowVM where TPredicateBuilder : IPredicateBuilder, new() { // public readonly ObservableCollection live_sorting_props; @@ -31,7 +31,6 @@ namespace mbts.ui { readonly ObservableCollection _rows; readonly ICollectionView _rows_view; readonly ObservableCollection _columns; - //readonly ObservableAutoWrapper, TRow> _animators; readonly Property _grid_settings; readonly DelegateProperty _font_family; readonly DelegateProperty _font_weight; @@ -59,18 +58,11 @@ namespace mbts.ui { _font_size = backing(nameof(font_size), () => grid_settings.font_size, nameof(font_size)); _font_color = backing(nameof(font_color), () => grid_settings.font_color.rgb_solid_brush().frozen(), nameof(grid_settings)); _selection_highlight = backing(nameof(selection_highlight), () => grid_settings.selection_highlight.to_brush().frozen(), nameof(grid_settings)); - //_animators = new ObservableAutoWrapper, TRow>(_rows = rows, create_animator); _resources = new List { ObservableAutoWrapper.connect( rows_view.SortDescriptions, live_sorting_props = ((ICollectionViewLiveShaping)rows_view).LiveSortingProperties, sd => sd.PropertyName), (launch_settings_editor = new DialogCommand(create_settings_view)) }; - // - //PredicateAnimator create_animator(TRow r) { - // var pa = new PredicateAnimator(r); - // pa.set_predicate_animations(_animation_defs); - // return pa; - //} Window create_settings_view() { var vm = new GridSettingsVM(owner_title, _bus, _orders_model, create_predicate_vm, create_column_predicate_vm); // need to refresh before launching the settings-view bc users can chanve some grid-column-settings properties directly from the grid (e.g. width, is_collapsed, etc.) @@ -130,13 +122,7 @@ namespace mbts.ui { if (pa.predicate_builder is INotificationPredicateBuilder tpb && tpb.is_toast_enabled) { _notification_subs.Add(_notification_service.link(tpb.create_notification_predicate(_arg))); } - if (pa.row_animations.Count > 0 || pa.cell_animations.Count > 0) { - _animation_defs.add(pa.predicate_builder.create_predicate(_arg), PredicateAnimations.flat_definitions(pa.row_animations, pa.cell_animations, lookup_column)); - } } - //foreach (var a in _animators) { - // a.set_predicate_animations(_animation_defs); - //} // ** in the unfortunate event that we botch backwards compatability, and the columns don't bear a 1:1 relationship with the column-settings // don't add insult to injury by crashing ** if (grid_settings.columns != null && _columns != null) { @@ -155,9 +141,6 @@ namespace mbts.ui { // string id_key(string id) => id.ToLower(); } - // - IGridColumn lookup_column(string col_id) => - _columns.OfType>().FirstOrDefault(c => string.Equals(col_id, c.id, StringComparison.CurrentCultureIgnoreCase)); } } } \ No newline at end of file From 8b816f149146b401efb75608d24e98f205be6120 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Sun, 21 Oct 2018 17:29:17 -0500 Subject: [PATCH 02/46] SPARK-1801 OrderRecordTupleSource - introduce updates w/ 'change' member, broadcast order_securities --- .../spark/Orders/OrderRecordTupleSource.cs | 98 +++++++++++-------- src/client/spark/Orders/OrdersModel.cs | 8 +- src/client/spark/core/Collections/SetMap.cs | 1 + .../core/Utility/TableUpdateTransform.cs | 60 +++++++++++- 4 files changed, 125 insertions(+), 42 deletions(-) diff --git a/src/client/spark/Orders/OrderRecordTupleSource.cs b/src/client/spark/Orders/OrderRecordTupleSource.cs index 468063bb2..2bb3ca29a 100644 --- a/src/client/spark/Orders/OrderRecordTupleSource.cs +++ b/src/client/spark/Orders/OrderRecordTupleSource.cs @@ -7,80 +7,94 @@ using System.Collections.Generic; using System.Linq; using System.Reactive.Linq; using System.Reactive.Subjects; -using System.Threading; namespace mbts.spark { - class OrderRecordTupleSource : IObservable>, IDisposable { - // returns a ref-count Observable.Create-wrapper that initializes/destroys the [hot-observable] OrderRecordTupleSource. - // (initializes when the first subscription is made, and destroys when the last subscription is disposed). - public static IObservable> create_observable( - IObservable> securities, - IObservable> orders) { - OrderRecordTupleSource source = null; - int ref_count = 0; - return Observable.Create>(observer => { - if (Interlocked.Increment(ref ref_count) == 1) { - SparkUtils.lazy_assert(source == null, () => fmt_assert_msg("source should be null, there must be a leak.")); - source = new OrderRecordTupleSource(securities, orders); - } - IDisposable sub = source.Subscribe(observer); - return Disposable.Create(() => { - Disposable.dispose_once(ref sub); - if (Interlocked.Decrement(ref ref_count) == 0) - Disposable.dispose_once(ref source); - }); - }); - string fmt_assert_msg(string msg) => $"{nameof(OrderRecordTupleSource)}.{nameof(create_observable)}: {msg}"; - } - + class OrderRecordTupleSource : IDisposable { + readonly object _lock = new object(); + // subjects readonly Subject> _subject = new Subject>(); + readonly Subject> _table_subject = new Subject>(); + readonly Subject> _order_securities_subject = new Subject>(); + // data cache readonly Dictionary _state = new Dictionary(); readonly SetMap _security_orders = new SetMap(); IReadOnlyDictionary _orders_cache = new Dictionary(0); IReadOnlyDictionary _security_md_cache = new Dictionary(0); IDisposable _dispose; - object _lock = new object(); - OrderRecordTupleSource(IObservable> securities, IObservable> orders) => + public OrderRecordTupleSource(IObservable> securities, IObservable> orders) => _dispose = Disposable.Create( securities.Subscribe(_update_securities), - orders.AddRemoveOnly().Subscribe(_update_orders)); + orders.Subscribe(_update_orders)); - public IDisposable Subscribe(IObserver> observer) { - lock (_lock) { - observer.OnNext(new IncrementalUpdate(new Dictionary(_state), new Dictionary(0))); - return _subject.Subscribe(observer); - } - } + public IObservable> subscribe_incremental_update() => + Observable.Create>(observer => { + lock (_lock) { + observer.OnNext(new IncrementalUpdate(new Dictionary(_state), new Dictionary(0))); + return _subject.Subscribe(observer); + } + }); + + public IObservable> subscribe_incremental_table_update() => + Observable.Create>(observer => { + lock (_lock) { + observer.OnNext(IncrementalTableUpdateTranform.init(_state.Values)); + return _table_subject.Subscribe(observer); + } + }); + + public IObservable> subscribe_order_securities() => + Observable.Create>(observer => { + lock (_lock) { + var init = new Dictionary(); + foreach (uint sec_key in _security_orders.Keys) { + if (_security_md_cache.TryGetValue(sec_key, out SecurityMD smd)) + init.Add(sec_key, smd); + } + observer.OnNext(new IncrementalUpdate(init, new Dictionary(0))); + return _order_securities_subject.Subscribe(observer); + } + }); public void Dispose() => Disposable.dispose(ref _dispose); - void _update_orders(LegacyTableUpdate orders) { + void _update_orders(TableUpdate orders) { lock (_lock) { Dictionary removed = new Dictionary(), added = new Dictionary(); + Dictionary added_securities = new Dictionary(), removed_securities = new Dictionary(); foreach (OrderRecordImmutable order in orders.removed) { uint sec_key = order.security_key(); OrderRecordTuple old_ft = new OrderRecordTuple(order, _get_security_md(sec_key)); removed.Add(old_ft.order.Key, old_ft); - _security_orders.remove(sec_key, order.Key); + if (_security_orders.removes_key(sec_key, order.Key) && old_ft.security_md.HasValue) + removed_securities.Add(sec_key, old_ft.security_md.Value); + } + foreach (Change ch in orders.changes) { + // orders cannot change securities; ch.New can use the same security_md as ch.Old + var sec_md = _get_security_md(ch.Old.security_key()); + removed.Add(ch.Old.Key, new OrderRecordTuple(ch.Old, sec_md)); + added.Add(ch.New.Key, new OrderRecordTuple(ch.New, sec_md)); } foreach (OrderRecordImmutable order in orders.added) { uint sec_key = order.security_key(); OrderRecordTuple new_ort = new OrderRecordTuple(order, _get_security_md(sec_key)); added.Add(new_ort.order.Key, new_ort); - _security_orders.add(sec_key, order.Key); + if (_security_orders.adds_key(sec_key, order.Key) && new_ort.security_md.HasValue) + added_securities.Add(sec_key, new_ort.security_md.Value); } _orders_cache = orders.state; - _apply_changes(added, removed); + _apply_changes(added, removed, added_securities, removed_securities); } } void _update_securities(IReadOnlyDictionary securities) { lock (_lock) { Dictionary added = new Dictionary(), removed = new Dictionary(); + Dictionary added_securities = new Dictionary(), removed_securities = new Dictionary(); _security_md_cache.Keys.difference(securities.Keys, out IEnumerable added_keys, out IEnumerable removed_keys); foreach (var k in removed_keys) { if (_security_orders.try_lookup(k, out ISet order_keys)) { + removed_securities.Add(k, _security_md_cache[k]); IEnumerable orders = order_keys.Select(fk => _orders_cache[fk]); SecurityMD old_sec_md = _security_md_cache[k]; foreach (OrderRecordImmutable order in orders) { @@ -93,6 +107,7 @@ namespace mbts.spark { } foreach (var k in added_keys) { if (_security_orders.try_lookup(k, out ISet order_keys)) { + added_securities.Add(k, securities[k]); IEnumerable orders = order_keys.Select(fk => _orders_cache[fk]); SecurityMD new_sec_md = securities[k]; foreach (OrderRecordImmutable order in orders) { @@ -104,7 +119,7 @@ namespace mbts.spark { } } _security_md_cache = securities; - _apply_changes(added, removed); + _apply_changes(added, removed, added_securities, removed_securities); } } @@ -114,12 +129,15 @@ namespace mbts.spark { OrderRecordImmutable? _get_order(uint key) => _orders_cache.TryGetValue(key, out OrderRecordImmutable o) ? o : new OrderRecordImmutable?(); - void _apply_changes(Dictionary added, Dictionary removed) { + void _apply_changes(Dictionary added, Dictionary removed, Dictionary added_securities, Dictionary removed_securities) { foreach (var kv in removed) _state.Remove(kv.Key); foreach (var kv in added) _state.Add(kv.Key, kv.Value); - _subject.OnNext(new IncrementalUpdate(added, removed)); + var update = new IncrementalUpdate(added, removed); + _subject.OnNext(update); + _table_subject.OnNext(update.as_table()); + _order_securities_subject.OnNext(new IncrementalUpdate(added_securities, removed_securities)); } } } \ No newline at end of file diff --git a/src/client/spark/Orders/OrdersModel.cs b/src/client/spark/Orders/OrdersModel.cs index 0b5c3ba1e..74768d2bc 100644 --- a/src/client/spark/Orders/OrdersModel.cs +++ b/src/client/spark/Orders/OrdersModel.cs @@ -48,7 +48,10 @@ namespace mbts.spark { ImmutableDictionary.Empty, (workers, table) => _update_working_orders(workers, table)) .Replay(1).RefCount(); - on_order_tuples = OrderRecordTupleSource.create_observable(bus.subscribe(SparkTags.SecuritiesTag), on_orders); + var order_record_tuple_source = new OrderRecordTupleSource(bus.subscribe(SparkTags.SecuritiesTag), on_orders); + on_order_tuples = order_record_tuple_source.subscribe_incremental_update(); + on_order_tuples_table = order_record_tuple_source.subscribe_incremental_table_update(); + on_order_securities = order_record_tuple_source.subscribe_order_securities(); on_fills = _service.subscribei_fills().Naggle(interval, scheduler).Table().Cache(); _activity = new OrderActivitySummarizer(bus, on_orders); on_fill_tuples = FillTupleSource.create_observable(bus.subscribe(SparkTags.SecuritiesTag), on_orders, on_fills); @@ -57,6 +60,7 @@ namespace mbts.spark { _dispose = Disposable.Create( on_orders.AddRemoveOnly().Subscribe(_security_orders_demuxer), on_positions.AddRemoveOnly().Subscribe(_security_positions_demuxer), + order_record_tuple_source, _bus.subscribe(SparkTags.CreateOrder).Subscribe(a => send_order(a.definition, a.price, a.size).ignore()), _bus.subscribe(SparkTags.RecreateOrder).Subscribe(a => a.ForEach(x => send_order(x.definition, x.price, x.size).ignore())), _bus.subscribe(SparkTags.CancelOrders).Subscribe(x => cancel_orders(x).ignore()), @@ -70,6 +74,8 @@ namespace mbts.spark { public SpreaderModel spreader => _spreader; public IObservable> on_orders { get; } public IObservable> on_order_tuples { get; } + public IObservable> on_order_tuples_table { get; } + public IObservable> on_order_securities { get; } public IObservable> on_fills { get; } public IObservable> on_fill_tuples { get; } public IObservable> working_orders { get; } diff --git a/src/client/spark/core/Collections/SetMap.cs b/src/client/spark/core/Collections/SetMap.cs index 95aa9505f..751b56e86 100644 --- a/src/client/spark/core/Collections/SetMap.cs +++ b/src/client/spark/core/Collections/SetMap.cs @@ -29,6 +29,7 @@ namespace mbts.collections { public int Count => _dictionary.Count; public object SyncRoot => null; public bool IsSynchronized => false; + public IEnumerable Keys => _dictionary.Keys; public ISet this[TKey key] => _dictionary[key]; public void CopyTo(Array array, int index) => diff --git a/src/client/spark/core/Utility/TableUpdateTransform.cs b/src/client/spark/core/Utility/TableUpdateTransform.cs index a694fafe3..6dee3f511 100644 --- a/src/client/spark/core/Utility/TableUpdateTransform.cs +++ b/src/client/spark/core/Utility/TableUpdateTransform.cs @@ -18,6 +18,13 @@ namespace mbts.utils { IEnumerable removed { get; } } + interface IIncrementalTableUpdateTransform where TKey : IEquatable { + IEnumerable added { get; } + IEnumerable removed { get; } + IEnumerable> changes { get; } + IEnumerable changed { get; } + } + class IncrementalUpdate : IIncrementalUpdate where TKey : IEquatable { public IncrementalUpdate() : this(new Dictionary(0), new Dictionary(0)) { } @@ -76,6 +83,51 @@ namespace mbts.utils { new TableUpdateTransform(_state, _state.Values.ToList(), new T[0]); } + class IncrementalTableUpdateTranform : IIncrementalTableUpdateTransform where TKey : IEquatable { + public static IncrementalTableUpdateTranform init(IEnumerable initial_values) => + new IncrementalTableUpdateTranform(initial_values, Enumerable.Empty(), Enumerable.Empty>()); + + public static IncrementalTableUpdateTranform from_incremental_update(IIncrementalUpdate source) { + // copy the smaller dictionary (removed vs added) + if (source.removed.Count < source.added.Count) { + var changes_list = new List>(); + var removed_copy = source.removed.ToDictionary(); + var added_list = new List(); + foreach (var kv in source.added) { + if (removed_copy.TryPopValue(kv.Key, out T oldval)) { + changes_list.Add(new Change(oldval, kv.Value)); + } else { + added_list.Add(kv.Value); + } + } + return new IncrementalTableUpdateTranform(added_list, removed_copy.Values, changes_list); + } else { + var changes_list = new List>(); + var added_copy = source.added.ToDictionary(); + var removed_list = new List(); + foreach (var kv in source.removed) { + if (added_copy.TryPopValue(kv.Key, out T newval)) { + changes_list.Add(new Change(kv.Value, newval)); + } else { + removed_list.Add(kv.Value); + } + } + return new IncrementalTableUpdateTranform(added_copy.Values, removed_list, changes_list); + } + } + + IncrementalTableUpdateTranform(IEnumerable added, IEnumerable removed, IEnumerable> changes) { + this.added = added; + this.removed = removed; + this.changes = changes; + } + + public IEnumerable added { get; } + public IEnumerable removed { get; } + public IEnumerable> changes { get; } + public IEnumerable changed => changes.Select(ch => ch.New); + } + [Browsable(false)] [EditorBrowsable(EditorBrowsableState.Never)] static class TableUpdateTransformExtensions { @@ -135,6 +187,12 @@ namespace mbts.utils { .Select(u => new IncrementalUpdate(u.added.Where(kv => predicate(kv.Value)).ToDictionary(), u.removed.Where(kv => predicate(kv.Value)).ToDictionary())) .Where(u => u.added.Count > 0 || u.removed.Count > 0); + public static IIncrementalTableUpdateTransform as_table(this IIncrementalUpdate update) where TKey : IEquatable => + IncrementalTableUpdateTranform.from_incremental_update(update); + + public static IEnumerable changed(this IIncrementalTableUpdateTransform update) where TKey : IEquatable => + update.changes.Select(ch => ch.New); + class FilteredTableUpdateTransform : ITableUpdateTransform where TKey : IEquatable { internal FilteredTableUpdateTransform(ITableUpdateTransform source, Func predicate) { state = source.state; @@ -247,6 +305,6 @@ namespace mbts.utils { state_clone.Add(kv.Key, kv.Value); return new PivotTableUpdateTransform(state_clone, added.Values.ToList(), removed.Values.ToList(), _key_map, _selector, _result_key, _key); } - } + } } } \ No newline at end of file From ba9bd53c1e63cf41b21c783af399abf783e00067 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Sun, 21 Oct 2018 17:53:03 -0500 Subject: [PATCH 03/46] DIYGrid - attach/detach Sorting event if DataContext implements IHandleGridSorting --- .../spark/view/Controls/DIYGrid/DIYGrid.cs | 49 ++++++++++++++----- .../view/Extensions/DisposeExtensions.cs | 11 +++++ .../spark_core/utils/AnonymousDisposable.cs | 7 +++ 3 files changed, 54 insertions(+), 13 deletions(-) diff --git a/src/client/spark/view/Controls/DIYGrid/DIYGrid.cs b/src/client/spark/view/Controls/DIYGrid/DIYGrid.cs index ceeb28e42..0ef6739de 100644 --- a/src/client/spark/view/Controls/DIYGrid/DIYGrid.cs +++ b/src/client/spark/view/Controls/DIYGrid/DIYGrid.cs @@ -20,6 +20,10 @@ using System.Windows.Input; using System.Windows.Media; namespace mbts.ui.controls { + interface IHandleGridSorting { + void on_sorting(object sender, DIYGridSortingEventArgs e); + } + class DIYGridColumnCollection : ObservableCollection { } class DIYGridSortingEventArgs : EventArgs { public DIYGridSortingEventArgs(DIYGrid.ColumnBase column) => Column = column; @@ -159,7 +163,7 @@ namespace mbts.ui.controls { public static readonly SolidColorBrush DefaultSelectionHighlight = new SolidColorBrush(Colors.MediumSlateBlue) { Opacity = 0.4 }; public static readonly Pen DefaultSelectionStroke = new Pen(Brushes.MediumSlateBlue, 1); readonly NotifyCollectionChangedEventHandler _sort_descriptions_collection_changed_cb; - IDisposable _selection_handler; + DataContextHooks _data_context_hooks; public static readonly DependencyProperty SelectionHighlightProperty = WPFHelper.create_dp( @@ -168,7 +172,7 @@ namespace mbts.ui.controls { o.Resources[ActiveSelectionHighlightKey] = v; o.Resources[InactiveSelectionHighlightKey] = new SolidColorBrush(v.Color) { Opacity = 0.2 }.frozen(); o.Resources[SelectionStrokeKey] = new Pen(o.SelectionHighlight.Color.to_brush().frozen(), 1).frozen(); - foreach (var col in o.Columns.empty_if_null().OfType()) + foreach (var col in o.Columns.OfType()) col.on_selection_fill(v); }, (o, v) => v as SolidColorBrush ?? DefaultSelectionHighlight, DefaultSelectionHighlight); @@ -241,7 +245,8 @@ namespace mbts.ui.controls { })); Resources[InactiveSelectionHighlightKey] = new SolidColorBrush(SelectionHighlight.Color) { Opacity = 0.2 }; Resources[ActiveSelectionHighlightKey] = SelectionHighlight; - DataContextChanged += (s, e) => _try_connect_selection_handler(); + _data_context_hooks = new DataContextHooks(this, DataContext); + DataContextChanged += OnDataContextChanged; } public event DIYGridSortingEventHandler Sorting; @@ -306,15 +311,8 @@ namespace mbts.ui.controls { // Private Methods #region Private Methods - void _try_connect_selection_handler() { - Disposable.dispose(ref _selection_handler); - if (_row_presenter != null && DataContext is IHandleRowSelection hrs) { - _row_presenter.SelectionChanged += on_selection_changed; - _selection_handler = Disposable.Create(() => _row_presenter.SelectionChanged -= on_selection_changed); - void on_selection_changed(object sender, SelectionChangedEventArgs e) => - hrs.row_selection_changed(e); - } - } + void OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e) => + Disposable.replace(ref _data_context_hooks, new DataContextHooks(this, e.NewValue)); #region Column Handling private void invalidate_column_layout(object sender = null, EventArgs args = null) { @@ -789,7 +787,7 @@ namespace mbts.ui.controls { _scroll_horizontal.ValueChanged += on_horizontal_scroll; _superheader_container = GetTemplateChild("_super_headers") as Panel; - _try_connect_selection_handler(); + Disposable.replace(ref _data_context_hooks, new DataContextHooks(this, DataContext)); update_superheaders(); // TODO: All edit controls go in a canvas - edit api is called with an edit type. @@ -908,6 +906,31 @@ namespace mbts.ui.controls { } #endregion + + class DataContextHooks : IDisposable { + List _hooks = new List(3); + + // this constructor provides a way to avoid repetative dp lookups ala owner.DataContext + public DataContextHooks(DIYGrid owner, object data_context) + : this(owner, data_context as IHandleRowSelection, data_context as IHandleGridSorting, data_context as IWillLetYouKnowWhenIDispose) { } + + public DataContextHooks(DIYGrid owner, IHandleRowSelection selection_handler, IHandleGridSorting sorting_handler, IWillLetYouKnowWhenIDispose dispose_broadcaster) { + if (selection_handler != null && owner._row_presenter is ListBox rp) { + rp.SelectionChanged += on_selection_changed; + _hooks.Add(Disposable.Create(() => rp.SelectionChanged -= on_selection_changed)); + void on_selection_changed(object sender, SelectionChangedEventArgs e) => selection_handler.row_selection_changed(e); + } + if (sorting_handler != null) { + owner.Sorting += sorting_handler.on_sorting; + _hooks.Add(Disposable.Create(() => owner.Sorting -= sorting_handler.on_sorting)); + } + if (dispose_broadcaster != null) + _hooks.Add(dispose_broadcaster.SubscribeDisposed(this)); + } + + public void Dispose() => + Disposable.dispose(_hooks); + } } static class DIYGridExtensions { diff --git a/src/client/spark/view/Extensions/DisposeExtensions.cs b/src/client/spark/view/Extensions/DisposeExtensions.cs index 105cfffee..a76572b31 100644 --- a/src/client/spark/view/Extensions/DisposeExtensions.cs +++ b/src/client/spark/view/Extensions/DisposeExtensions.cs @@ -63,5 +63,16 @@ namespace mbts.ui }); } } + + public static IDisposable SubscribeDisposed(this IWillLetYouKnowWhenIDispose dispose_broadcaster, Action callback) => + dispose_broadcaster.SubscribeDisposed(_ => { callback?.Invoke(); callback = null; }); + + public static IDisposable SubscribeDisposed(this IWillLetYouKnowWhenIDispose dispose_broadcaster, IDisposable dispose) => + dispose_broadcaster.SubscribeDisposed(_ => Disposable.dispose(ref dispose)); + + public static IDisposable SubscribeDisposed(this IWillLetYouKnowWhenIDispose dispose_broadcaster, Action callback) { + dispose_broadcaster.OnDisposed += callback; + return Disposable.Create(() => dispose_broadcaster.OnDisposed -= callback); + } } } diff --git a/src/client/spark_core/utils/AnonymousDisposable.cs b/src/client/spark_core/utils/AnonymousDisposable.cs index 5948d6484..a4750ea9d 100644 --- a/src/client/spark_core/utils/AnonymousDisposable.cs +++ b/src/client/spark_core/utils/AnonymousDisposable.cs @@ -92,6 +92,13 @@ namespace mbts item = null; } } + + public static void replace(ref T current, T newval) where T : IDisposable { + if (current != null) { + current.Dispose(); + } + current = newval; + } } // inspired by: https://blogs.msdn.microsoft.com/blambert/2009/07/24/a-simple-and-totally-thread-safe-implementation-of-idisposable/ From 5218b748e893b3418811b3cc0eeddc82b6bfeedb Mon Sep 17 00:00:00 2001 From: John Colvin Date: Sun, 21 Oct 2018 18:07:07 -0500 Subject: [PATCH 04/46] SPARK-1791 / SPARK-1792 Improve OB performance --- .../Trading/OrderBook/OrderBookCell.cs | 288 +++++----- .../Trading/OrderBook/OrderBookColumns.cs | 477 +++++++++-------- .../OrderBook/OrderBookRowCollection.cs | 141 +++++ .../Trading/OrderBook/OrderBookRowVM.cs | 497 ++++-------------- .../Modules/Trading/OrderBook/OrderBookVM.cs | 355 +++---------- .../Trading/OrderBook/OrderBookView.xaml | 4 +- src/client/spark/core/Utility/CompareUtils.cs | 6 + .../spark/core/Utility/ComparerBuilder.cs | 44 ++ .../DIYGrid/Settings/GridSettingsManager.cs | 18 +- .../spark/view/Spark/Core/SparkUtils.cs | 6 + .../spark_core/Collections/SortedList.cs | 12 + .../spark_core/Collections/SortedTree.cs | 63 ++- 12 files changed, 878 insertions(+), 1033 deletions(-) create mode 100644 src/client/spark/Modules/Trading/OrderBook/OrderBookRowCollection.cs create mode 100644 src/client/spark/core/Utility/ComparerBuilder.cs diff --git a/src/client/spark/Modules/Trading/OrderBook/OrderBookCell.cs b/src/client/spark/Modules/Trading/OrderBook/OrderBookCell.cs index 22d8b2808..2e4d28806 100644 --- a/src/client/spark/Modules/Trading/OrderBook/OrderBookCell.cs +++ b/src/client/spark/Modules/Trading/OrderBook/OrderBookCell.cs @@ -1,168 +1,168 @@ -using bts.utils; -using mbts.ui; -using mbts.ui.controls; -using mbts.utils; -using System; -using System.Collections.Generic; -using System.Collections.ObjectModel; -using System.ComponentModel; +//using bts.utils; +//using mbts.ui; +//using mbts.ui.controls; +//using mbts.utils; +//using System; +//using System.Collections.Generic; +//using System.Collections.ObjectModel; +//using System.ComponentModel; -namespace mbts.spark { - partial class OrderBookRowVM { - public interface IOrderBookCell : DIYGrid.IGridCell, IComparable { } +//namespace mbts.spark { +// partial class OrderBookRowVM { +// public interface IOrderBookCell : DIYGrid.IGridCell, IComparable { } - public interface IOrderBookCell : IOrderBookCell { - T value { get; } - IOrderBookCellStore store { get; } - } +// public interface IOrderBookCell : IOrderBookCell { +// T value { get; } +// IOrderBookCellStore store { get; } +// } - public interface IOrderBookCellStore : DIYGrid.IGridCellStore { - OrderBookRowVM row { get; } - new ICVOrderBookColumn column { get; } - string prop_name { get; } - new IOrderBookCell current { get; } - // - IOrderBookCellStore select_other(OrderBookRowVM row); - } +// public interface IOrderBookCellStore : DIYGrid.IGridCellStore { +// OrderBookRowVM row { get; } +// new ICVOrderBookColumn column { get; } +// string prop_name { get; } +// new IOrderBookCell current { get; } +// // +// IOrderBookCellStore select_other(OrderBookRowVM row); +// } - public interface IOrderBookCellStore : IOrderBookCellStore, IComparer, IEqualityComparer { - new IOrderBookCell current { get; } - new IOrderBookCellStore select_other(OrderBookRowVM row); - // - T get_value(OrderBookRowVM row); - string format(T value); - } +// public interface IOrderBookCellStore : IOrderBookCellStore, IComparer, IEqualityComparer { +// new IOrderBookCell current { get; } +// new IOrderBookCellStore select_other(OrderBookRowVM row); +// // +// T get_value(OrderBookRowVM row); +// string format(T value); +// } - public static class CellFactory { - public static IOrderBookCellStore create(OrderBookRowVM row, ICVOrderBookColumn column, string prop_name, Func get_value, Func> select_other, Func format = null) => - new OrderBookCellStore(row, column, prop_name, get_value, select_other, format, new Comparison(Comparer.Default.Compare), EqualityComparer.Default); +// public static class CellFactory { +// public static IOrderBookCellStore create(OrderBookRowVM row, ICVOrderBookColumn column, string prop_name, Func get_value, Func> select_other, Func format = null) => +// new OrderBookCellStore(row, column, prop_name, get_value, select_other, format, new Comparison(Comparer.Default.Compare), EqualityComparer.Default); - public static IOrderBookCellStore create(OrderBookRowVM row, ICVOrderBookColumn column, string prop_name, Func get_value, Func> select_other, Comparison comparison, Func format = null) => - new OrderBookCellStore(row, column, prop_name, get_value, select_other, format, comparison, EqualityComparer.Default); +// public static IOrderBookCellStore create(OrderBookRowVM row, ICVOrderBookColumn column, string prop_name, Func get_value, Func> select_other, Comparison comparison, Func format = null) => +// new OrderBookCellStore(row, column, prop_name, get_value, select_other, format, comparison, EqualityComparer.Default); - public static IOrderBookCellStore create_nullable(OrderBookRowVM row, ICVOrderBookColumn column, string prop_name, Func get_value, Func> select_other, Func format = null) - where T : struct => new OrderBookCellStore(row, column, prop_name, get_value, select_other, format, CompareUtils.nullable_comparison(Comparer.Default.Compare), EqualityComparer.CreateNullable(EqualityComparer.Default.Equals)); +// public static IOrderBookCellStore create_nullable(OrderBookRowVM row, ICVOrderBookColumn column, string prop_name, Func get_value, Func> select_other, Func format = null) +// where T : struct => new OrderBookCellStore(row, column, prop_name, get_value, select_other, format, CompareUtils.nullable_comparison(Comparer.Default.Compare), EqualityComparer.CreateNullable(EqualityComparer.Default.Equals)); - public static IOrderBookCellStore create_nullable(OrderBookRowVM row, ICVOrderBookColumn column, string prop_name, Func get_value, Func> select_other, Func compare, Func format = null) - where T : struct => - new OrderBookCellStore(row, column, prop_name, get_value, select_other, format, CompareUtils.nullable_comparison(compare), EqualityComparer.CreateNullable(EqualityComparer.Default.Equals)); +// public static IOrderBookCellStore create_nullable(OrderBookRowVM row, ICVOrderBookColumn column, string prop_name, Func get_value, Func> select_other, Func compare, Func format = null) +// where T : struct => +// new OrderBookCellStore(row, column, prop_name, get_value, select_other, format, CompareUtils.nullable_comparison(compare), EqualityComparer.CreateNullable(EqualityComparer.Default.Equals)); - class OrderBookCellStore : IOrderBookCellStore, INotifyPropertyChanged { - readonly OrderBookRowVM _row; - readonly ICVOrderBookColumn _column; - readonly string _prop_name; - readonly Func _get_value; - readonly Func> _select_other; - readonly Func _format; - readonly Comparison _comparison; - readonly IEqualityComparer _eq_comparer; - OrderBookCell _current; +// class OrderBookCellStore : IOrderBookCellStore, INotifyPropertyChanged { +// readonly OrderBookRowVM _row; +// readonly ICVOrderBookColumn _column; +// readonly string _prop_name; +// readonly Func _get_value; +// readonly Func> _select_other; +// readonly Func _format; +// readonly Comparison _comparison; +// readonly IEqualityComparer _eq_comparer; +// OrderBookCell _current; - // Property changed handler to avoid WPF binding leak - // https://blog.jetbrains.com/dotnet/2014/09/04/fighting-common-wpf-memory-leaks-with-dotmemory/ - public event PropertyChangedEventHandler PropertyChanged = (_,__) => { }; +// // Property changed handler to avoid WPF binding leak +// // https://blog.jetbrains.com/dotnet/2014/09/04/fighting-common-wpf-memory-leaks-with-dotmemory/ +// public event PropertyChangedEventHandler PropertyChanged = (_,__) => { }; - public OrderBookCellStore( - OrderBookRowVM row, - ICVOrderBookColumn column, - string prop_name, - Func get_value, - Func> select_other, - Func format, - Comparison comparison, - IEqualityComparer eq_comparer) { - // - _row = row; - _column = column; - _prop_name = prop_name; - _get_value = get_value; - _select_other = select_other; - _format = format ?? new Func(v => v?.ToString() ?? string.Empty); - _comparison = comparison; - _eq_comparer = eq_comparer; - _current = new OrderBookCell(this, _get_value(_row)); - } +// public OrderBookCellStore( +// OrderBookRowVM row, +// ICVOrderBookColumn column, +// string prop_name, +// Func get_value, +// Func> select_other, +// Func format, +// Comparison comparison, +// IEqualityComparer eq_comparer) { +// // +// _row = row; +// _column = column; +// _prop_name = prop_name; +// _get_value = get_value; +// _select_other = select_other; +// _format = format ?? new Func(v => v?.ToString() ?? string.Empty); +// _comparison = comparison; +// _eq_comparer = eq_comparer; +// _current = new OrderBookCell(this, _get_value(_row)); +// } - public OrderBookRowVM row => _row; - public ICVOrderBookColumn column => _column; - public string prop_name => _prop_name; - public IOrderBookCell current => _current; +// public OrderBookRowVM row => _row; +// public ICVOrderBookColumn column => _column; +// public string prop_name => _prop_name; +// public IOrderBookCell current => _current; - IOrderBookCell IOrderBookCellStore.current => _current; - DIYGrid.IGridColumn DIYGrid.IGridCellStore.column => _column; - DIYGrid.IGridCell DIYGrid.IGridCellStore.current => _current; +// IOrderBookCell IOrderBookCellStore.current => _current; +// DIYGrid.IGridColumn DIYGrid.IGridCellStore.column => _column; +// DIYGrid.IGridCell DIYGrid.IGridCellStore.current => _current; - public ObservableCollection animations { get; } = new ObservableCollection(); +// public ObservableCollection animations { get; } = new ObservableCollection(); - public int Compare(T x, T y) => _comparison(x, y); - public bool Equals(T x, T y) => _eq_comparer.Equals(x, y); - public string format(T value) => _format(value); - public int GetHashCode(T obj) => _eq_comparer.GetHashCode(obj); - public T get_value(OrderBookRowVM row) => _get_value(row); - public IOrderBookCellStore select_other(OrderBookRowVM row) => _select_other(row); - IOrderBookCellStore IOrderBookCellStore.select_other(OrderBookRowVM row) => select_other(row); +// public int Compare(T x, T y) => _comparison(x, y); +// public bool Equals(T x, T y) => _eq_comparer.Equals(x, y); +// public string format(T value) => _format(value); +// public int GetHashCode(T obj) => _eq_comparer.GetHashCode(obj); +// public T get_value(OrderBookRowVM row) => _get_value(row); +// public IOrderBookCellStore select_other(OrderBookRowVM row) => _select_other(row); +// IOrderBookCellStore IOrderBookCellStore.select_other(OrderBookRowVM row) => select_other(row); - public bool refresh() { - T v = _get_value(_row); - if (!_eq_comparer.Equals(v, _current.value)) { - _current = new OrderBookCell(this, v); - _row.RaisePropertyChanged(_prop_name); - return true; - } else return false; - } +// public bool refresh() { +// T v = _get_value(_row); +// if (!_eq_comparer.Equals(v, _current.value)) { +// _current = new OrderBookCell(this, v); +// _row.RaisePropertyChanged(_prop_name); +// return true; +// } else return false; +// } - public void regenerate_cell() { - _current = new OrderBookCell(this, _current.value); - _row.RaisePropertyChanged(_prop_name); - } +// public void regenerate_cell() { +// _current = new OrderBookCell(this, _current.value); +// _row.RaisePropertyChanged(_prop_name); +// } - public void invalidate_text() => regenerate_cell(); - } +// public void invalidate_text() => regenerate_cell(); +// } - class OrderBookCell : IOrderBookCell { - string _text; +// class OrderBookCell : IOrderBookCell { +// string _text; - public OrderBookCell(OrderBookCellStore store, T value) { - this.store = store; - this.value = value; - } +// public OrderBookCell(OrderBookCellStore store, T value) { +// this.store = store; +// this.value = value; +// } - public string text => _text ?? (_text = store.format(value)); - public T value { get; } - public IOrderBookCellStore store { get; } +// public string text => _text ?? (_text = store.format(value)); +// public T value { get; } +// public IOrderBookCellStore store { get; } - public int CompareTo(OrderBookCell other) { - if (other == null) - return 1; - var lin = lineage(store.row); - var olin = lineage(other.store.row); - int c = 0; - using (var en = lin.GetEnumerator()) - using (var oen = olin.GetEnumerator()) { - while (en.MoveNext() && oen.MoveNext()) { - if (en.Current.order_key.value != oen.Current.order_key.value) { - c = store.Compare( - store.select_other(en.Current).current.value, - store.select_other(oen.Current).current.value); - if (c != 0) - return c; - else { - c = en.Current.order_key.value.CompareTo( - oen.Current.order_key.value); - return store.column.SortDirection.HasValue && store.column.SortDirection.Value == ListSortDirection.Ascending ? c : -c; - } - } - } - } - return store.column.SortDirection.HasValue && store.column.SortDirection.Value == ListSortDirection.Ascending ? - lin.Count.CompareTo(olin.Count) : - olin.Count.CompareTo(lin.Count); - } +// public int CompareTo(OrderBookCell other) { +// if (other == null) +// return 1; +// var lin = lineage(store.row); +// var olin = lineage(other.store.row); +// int c = 0; +// using (var en = lin.GetEnumerator()) +// using (var oen = olin.GetEnumerator()) { +// while (en.MoveNext() && oen.MoveNext()) { +// if (en.Current.order_key.value != oen.Current.order_key.value) { +// c = store.Compare( +// store.select_other(en.Current).current.value, +// store.select_other(oen.Current).current.value); +// if (c != 0) +// return c; +// else { +// c = en.Current.order_key.value.CompareTo( +// oen.Current.order_key.value); +// return store.column.SortDirection.HasValue && store.column.SortDirection.Value == ListSortDirection.Ascending ? c : -c; +// } +// } +// } +// } +// return store.column.SortDirection.HasValue && store.column.SortDirection.Value == ListSortDirection.Ascending ? +// lin.Count.CompareTo(olin.Count) : +// olin.Count.CompareTo(lin.Count); +// } - public int CompareTo(object obj) => CompareTo(obj as OrderBookCell); +// public int CompareTo(object obj) => CompareTo(obj as OrderBookCell); - public override string ToString() => text; - } - } - } -} \ No newline at end of file +// public override string ToString() => text; +// } +// } +// } +//} \ No newline at end of file diff --git a/src/client/spark/Modules/Trading/OrderBook/OrderBookColumns.cs b/src/client/spark/Modules/Trading/OrderBook/OrderBookColumns.cs index fa1695f6b..0a1448d66 100644 --- a/src/client/spark/Modules/Trading/OrderBook/OrderBookColumns.cs +++ b/src/client/spark/Modules/Trading/OrderBook/OrderBookColumns.cs @@ -1,4 +1,5 @@ -using bts.api; +using bts; +using bts.api; using bts.api.orderman; using bts.utils; using mbts.spark.filters; @@ -6,6 +7,7 @@ using mbts.ui; using mbts.ui.converters; using mbts.ui.utility; using mbts.ui.utility.text; +using mbts.utils; using System; using System.Collections.Generic; using System.Collections.ObjectModel; @@ -18,7 +20,6 @@ using System.Windows.Controls.Primitives; using System.Windows.Data; using System.Windows.Media; using static bts.shell.OrderBookWindowFactory; -using static mbts.spark.OrderBookRowVM; using static mbts.spark.OrderBookView; using static mbts.ui.controls.DIYGrid; using OrderType = bts.api.orderman.CustomOrderDefinition.CustomOneofCase; @@ -35,20 +36,23 @@ namespace mbts.spark { // TODO: Maybe binding to the SortDirection would be less smelly? // - initially, I tried to bind to the SortDirection of each column, unfortunately, the binding target was not updated before the ICollectionView sorting took place. // I decided it was not worth changing the DIYGrid.Sorting function to force the SortDirection binding targets to update immediately, before the ICollectionView sorting. - interface ICVOrderBookColumn : IKeyedColumn, IGridColumn { + interface ICVOrderBookColumn : IKeyedColumn, IGridColumn { string sort_member_path { get; } - ListSortDirection? SortDirection { get; } - IDataGridColumnFilter filter { get; } + ListSortDirection? SortDirection { get; set; } + IDataGridColumnFilter filter { get; } + IComparer comparer { get; } } partial class OrderBookVM { public class OrderBookColumnCollection : ObservableCollection, IValueFactory, IDisposable { + readonly List _active_comparers = new List(); + public OrderBookColumnCollection(IBus bus, OrdersModel model, IObservable<(IEnumerable added, IEnumerable removed)> order_securities) { // security Add(security_column = new SecurityColumn( - DiscreteValuesFilterVM.Create( + DiscreteValuesFilterVM.Create( order_securities, - r => r.security.value.GetValueOrDefault(SecurityDataHelper.Defaults.SecurityImmutable), + ort => ort.security_md.HasValue ? ort.security_md.Value.security : SecurityDataHelper.Defaults.SecurityImmutable, SecurityCriteria.from_securities, SecurityCriteria.get_to_securities(bus), nameof(OrderBookRowVM.security), @@ -60,172 +64,222 @@ namespace mbts.spark { Columns.Symbol, DefaultColumnHeaders.Symbol, nameof(OrderBookRowVM.symbol), - r => r.symbol.store, + r => r.symbol, DefaultColumnWidths.Symbol, - DiscreteValuesFilterVM.Create( + DiscreteValuesFilterVM.Create( mbts.co2.UserSymbol.get_all().Select(u => u.symbol).Distinct().ToList(), - r => r.symbol.value, - value_path(nameof(OrderBookRowVM.symbol)) + ort => ort.security_md.HasValue ? ort.security_md.Value.security.Symbol : string.Empty, + nameof(OrderBookRowVM.symbol) ), - DataGridFilterKnownTemplates.DiscreteValues)); + DataGridFilterKnownTemplates.DiscreteValues, + _wrap_comparer(r => r.symbol))); // time_created - Add(time_created_column = new TimeCreatedColumn( - new TimeFilterVM(r => r.time_created.value, value_path(nameof(OrderBookRowVM.time_created))))); + Add(time_created_column = new SimpleTextColumn( + Columns.Time, + "Time", + nameof(OrderBookRowVM.time_created), + r => r.time_created_text, + DefaultColumnWidths.Time, + new TimeFilterVM( + ort => ort.order.created(), + nameof(OrderBookRowVM.time_created)), + DataGridFilterKnownTemplates.TimeFilter, + _wrap_comparer(r => r.time_created))); // last_change - Add(last_change_column = new LastChangeTimeColumn( - new TimeFilterVM(r => r.last_change.value, value_path(nameof(OrderBookRowVM.last_change))))); + Add(last_change_column = new SimpleTextColumn( + Columns.LastChange, + "Last Change", + nameof(OrderBookRowVM.last_change), + r => r.last_change_text, + DefaultColumnWidths.Time, + new TimeFilterVM( + ort => ort.order.last_change(), + nameof(OrderBookRowVM.last_change)), + DataGridFilterKnownTemplates.TimeFilter, + _wrap_comparer(r => r.last_change))); // user Add(user_column = new SimpleTextColumn( Columns.User, DefaultColumnHeaders.User, nameof(OrderBookRowVM.user), - r => r.user.store, + r => r.user, DefaultColumnWidths.User, - DiscreteValuesFilterVM.Create( + DiscreteValuesFilterVM.Create( model.factory.subscribe_users().Select(d => d.Values), - r => r.user.value, - value_path(nameof(OrderBookRowVM.user))), - DataGridFilterKnownTemplates.DiscreteValues)); + ort => ort.order.user(), + nameof(OrderBookRowVM.user)), + DataGridFilterKnownTemplates.DiscreteValues, + _wrap_comparer(r => r.user))); // account Add(account_column = new SimpleTextColumn( Columns.Account, DefaultColumnHeaders.Account, nameof(OrderBookRowVM.account), - r => r.account.store, + r => r.account, DefaultColumnWidths.Account, - DiscreteValuesFilterVM.Create( + DiscreteValuesFilterVM.Create( model.factory.subscribe_accounts().Select(d => d.Values), - r => r.account.value, + ort => ort.order.account(), vals => vals.ToArray(), vals => Observable.Return(vals), - value_path(nameof(OrderBookRowVM.account))), - DataGridFilterKnownTemplates.DiscreteValues)); + nameof(OrderBookRowVM.account)), + DataGridFilterKnownTemplates.DiscreteValues, + _wrap_comparer(r => r.account))); // exchange Add(exchange_column = new SimpleTextColumn( Columns.Exchange, DefaultColumnHeaders.Exchange, nameof(OrderBookRowVM.exchange), - r => r.exchange.store, + r => r.exchange.ToString(), DefaultColumnWidths.Exchange, - DiscreteValuesFilterVM.Create( + DiscreteValuesFilterVM.Create( EnumHelper.get_values_except(Venue.Unset).ToList(), - r => r.exchange.value, - value_path(nameof(OrderBookRowVM.exchange))), - DataGridFilterKnownTemplates.EnumValues)); + ort => ort.security_md.HasValue ? ort.security_md.Value.security.Exchange : Venue.Unset, + nameof(OrderBookRowVM.exchange)), + DataGridFilterKnownTemplates.EnumValues, + _wrap_comparer(r => r.exchange))); // time in force Add(time_in_force_column = new SimpleTextColumn( Columns.TimeInForce, DefaultColumnHeaders.TimeInForce, nameof(OrderBookRowVM.time_in_force), - r => r.time_in_force.store, + r => r.time_in_force.ToString(), DefaultColumnWidths.TimeInForce, - DiscreteValuesFilterVM.Create( + DiscreteValuesFilterVM.Create( EnumHelper.get_values().ToList(), - r => r.time_in_force.value, - value_path(nameof(OrderBookRowVM.time_in_force))), - DataGridFilterKnownTemplates.EnumValues)); + ort => ort.order.time_in_force(), + nameof(OrderBookRowVM.time_in_force)), + DataGridFilterKnownTemplates.EnumValues, + _wrap_comparer(r => r.time_in_force))); // order type Add(order_type_column = new OrderBookOrderTypeColumn( - DiscreteValuesFilterVM.Create( + DiscreteValuesFilterVM.Create( utilities.OrderFactory.all_order_types, - r => r.order_type.value, - value_path(nameof(OrderBookRowVM.order_type))))); + ort => ort.order.order_type(), + nameof(OrderBookRowVM.order_type)))); // order sub type Add(sub_type_column = new SimpleTextColumn( Columns.SubType, "Sub Type", nameof(OrderBookRowVM.order_sub_type), - r => r.order_sub_type.store, + r => r.order_sub_type_text, 80, - DiscreteValuesFilterVM.Create( + DiscreteValuesFilterVM.Create( EnumHelper.get_values().ToList(), - r => r.order_sub_type.value, - value_path(nameof(OrderBookRowVM.order_sub_type))), - SubTypeFilterTemplateKey)); + ort => ort.order.sub_type(), + nameof(OrderBookRowVM.order_sub_type)), + SubTypeFilterTemplateKey, + _wrap_comparer(r => r.order_sub_type))); // side Add(side_column = new SideColumn( - DiscreteValuesFilterVM.Create( + DiscreteValuesFilterVM.Create( new[] { Side.Buy, Side.Sell }, - r => r.side.value, - value_path(nameof(OrderBookRowVM.side))))); + ort=> ort.order.side(), + nameof(OrderBookRowVM.side)))); // quantity Add(qty_column = new SizeColumn( - new UIntMinMax(r => r.quantity.value, value_path(nameof(OrderBookRowVM.quantity))))); - // price - Add(price_column = new PriceColumn( - new DoubleMinMax(r => r.price.value, value_path(nameof(OrderBookRowVM.price))))); + new UIntMinMax(ort => ort.order.quantity(), nameof(OrderBookRowVM.quantity)))); // filled quantity Add(filled_qty_column = new FilledColumn( - new UIntMinMax(r => r.filled_quantity.value, value_path(nameof(OrderBookRowVM.filled_quantity))))); + new UIntMinMax(ort => ort.order.filled(), nameof(OrderBookRowVM.filled_quantity)))); + // price + Add(price_column = new SimpleTextColumn( + Columns.Price, + "Price", + nameof(OrderBookRowVM.price), + r => r.price_text, + DefaultColumnWidths.Price, + new DoubleMinMax( + ort => ort.order.price(), + nameof(OrderBookRowVM.price)), + DataGridFilterKnownTemplates.DoubleMinMax, + _wrap_comparer(r => r.price))); // avg filled price - Add(avg_price_column = new AvgFilledColumn( - new DoubleMinMax(r => r.avg_price.value, value_path(nameof(OrderBookRowVM.avg_price))))); + Add(avg_price_column = new SimpleTextColumn( + Columns.AvgFilled, + "Avg Price", + nameof(OrderBookRowVM.avg_price), + r => r.avg_price_text, + DefaultColumnWidths.Price, + new DoubleMinMax( + ort => ort.order.avg_price(), + nameof(OrderBookRowVM.avg_price)), + DataGridFilterKnownTemplates.DoubleMinMax, + _wrap_comparer(r => r.avg_price))); // last filled price - Add(last_price_column = new LastFilledColumn( - new DoubleMinMax(r => r.last_price.value, value_path(nameof(OrderBookRowVM.last_price))))); + Add(last_price_column = new SimpleTextColumn( + Columns.LastPrice, + "Last Filled", + nameof(OrderBookRowVM.last_price), + r => r.last_price_text, + DefaultColumnWidths.Price, + new DoubleMinMax( + ort => ort.order.last_price(), + nameof(OrderBookRowVM.last_price)), + DataGridFilterKnownTemplates.DoubleMinMax, + _wrap_comparer(r => r.last_price))); // status Add(status_column = new SimpleTextColumn( Columns.Status, "Status", nameof(OrderBookRowVM.status), - r => r.status.store, + r => r.status.ToString(), 80, - DiscreteValuesFilterVM.Create( + DiscreteValuesFilterVM.Create( EnumHelper.get_values().ToList(), - r => r.status.value, - value_path(nameof(OrderBookRowVM.status))), - DataGridFilterKnownTemplates.EnumValues)); + ort => ort.order.status(), + nameof(OrderBookRowVM.status)), + DataGridFilterKnownTemplates.EnumValues, + _wrap_comparer(r => r.status))); // last message Add(last_message_column = new OrderBookLastMessageColumn( - new RegexFilterVM( - r => r.last_msg.value, - value_path(nameof(OrderBookRowVM.last_msg))))); + new RegexFilterVM( + ort => ort.order.last_msg(), + nameof(OrderBookRowVM.last_msg)))); // security key Add(security_key_column = new SimpleTextColumn( Columns.SecurityKey, DefaultColumnHeaders.SecurityKey, nameof(OrderBookRowVM.security_key), - r => r.security_key.store, + r => r.security_key.ToString(), DefaultColumnWidths.SecurityKey, - new RegexFilterVM( - r => r.security_key.text, - value_path(nameof(OrderBookRowVM.security_key))), - DataGridFilterKnownTemplates.Regex)); + new RegexFilterVM( + ort => ort.order.security_key().ToString(), + nameof(OrderBookRowVM.security_key)), + DataGridFilterKnownTemplates.Regex, + _wrap_comparer(r => r.security_key))); // order key Add(order_key_column = new SimpleTextColumn( Columns.OrderKey, DefaultColumnHeaders.OrderKey, nameof(OrderBookRowVM.order_key), - r => r.order_key.store, + r => r.order_key.ToString(), DefaultColumnWidths.OrderKey, - new RegexFilterVM( - r => r.order_key.text, - value_path(nameof(OrderBookRowVM.order_key))), - DataGridFilterKnownTemplates.Regex)); + new RegexFilterVM( + ort => ort.order.Key.ToString(), + nameof(OrderBookRowVM.order_key)), + DataGridFilterKnownTemplates.Regex, + _wrap_comparer(r => r.order_key))); // parent key (this is for diagnostic purposes, remove this column once OrderBook seems stable again) Add(parent_key_column = new SimpleTextColumn( Columns.ParentKey, "Parent Key", nameof(OrderBookRowVM.parent_order_key), - r => r.parent_order_key.store, + r => r.parent_order_key.ToString(), 80, - new RegexFilterVM( - r => r.parent_order_key.text, - value_path(nameof(OrderBookRowVM.parent_order_key))), - DataGridFilterKnownTemplates.Regex)); - - string value_path(string cell_name) => - string.Join(".", cell_name, nameof(OrderBookRowVM.IOrderBookCell.value)); //uint is an arbitrary 'T', just want to get compile check on 'value' path. + new RegexFilterVM( + ort => ort.order.ParentKey.ToString(), + nameof(OrderBookRowVM.parent_order_key)), + DataGridFilterKnownTemplates.Regex, + _wrap_comparer(r => r.parent_order_key))); } - List> _active_filters = new List>(0); - public event Action filter_changed; - + public SecurityColumn security_column { get; } public SimpleTextColumn symbol_column { get; } - public TimeCreatedColumn time_created_column { get; } - public LastChangeTimeColumn last_change_column { get; } + public SimpleTextColumn time_created_column { get; } + public SimpleTextColumn last_change_column { get; } public SimpleTextColumn user_column { get; } public SimpleTextColumn account_column { get; } public SimpleTextColumn exchange_column { get; } @@ -234,18 +288,16 @@ namespace mbts.spark { public SimpleTextColumn sub_type_column { get; } public SideColumn side_column { get; } public SizeColumn qty_column { get; } - public PriceColumn price_column { get; } + public SimpleTextColumn price_column { get; } public FilledColumn filled_qty_column { get; } - public LastFilledColumn last_price_column { get; } - public AvgFilledColumn avg_price_column { get; } + public SimpleTextColumn last_price_column { get; } + public SimpleTextColumn avg_price_column { get; } public SimpleTextColumn status_column { get; } public OrderBookLastMessageColumn last_message_column { get; } public SimpleTextColumn security_key_column { get; } public SimpleTextColumn order_key_column { get; } public SimpleTextColumn parent_key_column { get; } - public bool filter(OrderBookRowVM row) => _active_filters.All(f => f.filter(row)); - public FilterStates create() => new FilterStates { order_key_regex = order_key_column.filter.get_state(), @@ -271,9 +323,6 @@ namespace mbts.spark { public void load_from(FilterStates fs) { if (fs != null) { - foreach (var c in this.Cast()) - c.filter.filter_changed -= _set_active_filters; - // security_column.filter.load_state(fs.security_criteria); symbol_column.filter.load_state(fs.symbols); time_created_column.filter.load_state(fs.time_created); @@ -293,54 +342,121 @@ namespace mbts.spark { status_column.filter.load_state(fs.statuses); last_message_column.filter.load_state(fs.exchange_message_regex); last_change_column.filter.load_state(fs.last_change); - // - _set_active_filters(); - foreach (var c in this.Cast()) - c.filter.filter_changed += _set_active_filters; + } + } + + public Func get_filter() { + var active_filters = this.OfType().Where(c => c.filter.active.GetValueOrDefault()).Select(c => c.filter).ToArray(); + return active_filters.Length == 0 ? null : new Func(r => active_filters.All(f => f.filter(r))); + } + + public (IComparer comp, string desc) get_comparer_data() { + var desc_parts = new List(); + var comp_builder = new ComparerBuilder(); + // this should be unneccesary, safe_active_comparers is just an extra guard against unexpected results from future code changes + // (get_comparer_data() should NEVER return a null/non-deterministic IComparer, fallback to sorting by Time Created in descending order if necessary) + var safe_active_comparers = _active_comparers.Where(c => c.SortDirection.HasValue).ToList(); + if (safe_active_comparers.Count == 0) { + time_created_column.SortDirection = ListSortDirection.Descending; + _active_comparers.Clear(); + _active_comparers.Add(time_created_column); + safe_active_comparers = _active_comparers; + } + foreach (var col in safe_active_comparers) { + desc_parts.Add($"{col.id}:{col.SortDirection.Value}"); + comp_builder.add(col.SortDirection.Value == ListSortDirection.Ascending ? col.comparer : col.comparer.reverse()); + } + return (comp_builder.to_comparer(), string.Join(", ", desc_parts)); + } + + public void on_sorting(ICVOrderBookColumn col, bool clear_current_sort) { + col.SortDirection = col.SortDirection.HasValue && col.SortDirection.Value == ListSortDirection.Ascending ? ListSortDirection.Descending : ListSortDirection.Ascending; + if (_active_comparers.Contains(col, EqualityComparer.Create(r => r.key))) { + if (clear_current_sort) _reset_active_comparer(col); + } else { + if (clear_current_sort) _reset_active_comparer(col); + else _active_comparers.Add(col); } } public void Dispose() { - foreach (var c in this.Cast()) { - c.filter.filter_changed -= _set_active_filters; - (c.filter as IDisposable)?.Dispose(); + foreach (var col in this.OfType()) + (col.filter as IDisposable)?.Dispose(); + Clear(); + } + + protected override void InsertItem(int index, ColumnBase item) { + if (item is ICVOrderBookColumn col) { + try { + _hook(col); + base.InsertItem(index, item); + } catch { + _unhook(col); + throw; + } + } else { + Log.Ui.warn($"{nameof(OrderBookColumnCollection)} added a column that is not of type {nameof(ICVOrderBookColumn)}"); + base.InsertItem(index, item); } } - void _set_active_filters() { - _active_filters = this - .Cast() - .Select(c => c.filter) - .Where(f => f.active.GetValueOrDefault()) - .ToList(); - filter_changed?.Invoke(); + protected override void RemoveItem(int index) { + if (Count > index && this[index] is ICVOrderBookColumn col) + _unhook(col); + base.RemoveItem(index); + } + protected override void ClearItems() { + var ob_columns = this.OfType().ToList(); + Log.Ui.info($"{nameof(OrderBookColumnCollection)} is unhooking all columns of type {nameof(ICVOrderBookColumn)} ({ob_columns.Count}/{Count})."); + foreach (var col in ob_columns) + _unhook(col); + base.ClearItems(); + } + + void _hook(ICVOrderBookColumn new_item) => new_item.filter.filter_changed += _on_column_filter_changed; + void _unhook(ICVOrderBookColumn old_item) => old_item.filter.filter_changed -= _on_column_filter_changed; + void _on_column_filter_changed() => filter_changed?.Invoke(); + + void _reset_active_comparer(ICVOrderBookColumn column) { + foreach (var col in _active_comparers) { + if (!col.Equals(column)) col.SortDirection = null; + } + _active_comparers.Clear(); + _active_comparers.Add(column); } } + static IComparer _wrap_comparer(Func get) => + Comparer.Default.wrap(get); + + static IComparer _wrap_comparer(Func get, IComparer comparer) => + comparer.wrap(get); + public abstract class OrderbookColumnBase : ColumnBase, IKeyedColumn, IFilterableColumn, ICVOrderBookColumn { ListSortDirection? _cached_sort_direction; readonly string _filter_template; - protected readonly Func _get_cell_store; + protected readonly Func _get_text; - protected OrderbookColumnBase(Columns col_key, string header, Func get_cell_store, string sort_prop, double width, IDataGridColumnPersistentFilter filter, string filter_template) : base(col_key) { + protected OrderbookColumnBase(Columns col_key, string header, Func get_text, string sort_prop, double width, + IDataGridColumnPersistentFilter filter, string filter_template, IComparer comparer) : base(col_key) { id = key.ToString(); Width = width; Header = header; - _get_cell_store = get_cell_store; + _get_text = get_text; sort_member_path = sort_prop; this.filter = filter; _filter_template = filter_template; _cached_sort_direction = SortDirection; + this.comparer = comparer; } - public IDataGridColumnPersistentFilter filter { get; } + public IDataGridColumnPersistentFilter filter { get; } + public IComparer comparer { get; } - IDataGridColumnFilter ICVOrderBookColumn.filter => filter; + IDataGridColumnFilter ICVOrderBookColumn.filter => filter; IDataGridColumnFilter IFilterableColumn.filter => filter; string IFilterableColumn.filter_template => _filter_template; - ListSortDirection? ICVOrderBookColumn.SortDirection => _cached_sort_direction; - - public IGridCellStore get_cell_store(OrderBookRowVM row) => _get_cell_store(row); + ListSortDirection? ICVOrderBookColumn.SortDirection { get => _cached_sort_direction; set => SortDirection = value; } protected override void OnSortDirectionChanged(ListSortDirection? new_value) { _cached_sort_direction = new_value; @@ -349,99 +465,51 @@ namespace mbts.spark { } public class SimpleTextColumn : OrderbookColumnBase, IRenderColumn { - public SimpleTextColumn(Columns key, string header, string sort_prop, Func get_cell_store, double width, IDataGridColumnPersistentFilter filter, string filter_template) - : base(key, header, get_cell_store, sort_prop, width, filter, filter_template) { } + public SimpleTextColumn(Columns key, string header, string sort_prop, Func get_text, double width, + IDataGridColumnPersistentFilter filter, string filter_template, IComparer comparer) + : base(key, header, get_text, sort_prop, width, filter, filter_template, comparer) { } - public void draw(OrderBookRowVM row, DrawingContext dc, ColumnGeometry position, double row_height) { - var cell = _get_cell_store(row); - if (!string.IsNullOrEmpty(cell.current.text)) - CellRender.draw_text(dc, RenderUtils.get_glyph_context(row), font_size, cell.current.text, position, RowHeight, - font_brush, null, horizontal_alignment, VerticalAlignment.Center, DefaultCellPadding, cell.animations); - } + public void draw(OrderBookRowVM row, DrawingContext dc, ColumnGeometry position, double row_height) => + CellRender.draw_text(dc, RenderUtils.get_glyph_context(row), font_size, _get_text(row), position, RowHeight, + font_brush, null, horizontal_alignment, VerticalAlignment.Center, DefaultCellPadding); } #region Explicit Column Defs public class SideColumn : OrderbookColumnBase, IRenderColumn { - public SideColumn(IDataGridColumnPersistentFilter filter) - : base(Columns.Side, "B/S", r => r.side.store, nameof(OrderBookRowVM.side), DefaultColumnWidths.Side, filter, DataGridFilterKnownTemplates.EnumValues) { } + public SideColumn(IDataGridColumnPersistentFilter filter) + : base(Columns.Side, "B/S", r => r.side.ToString(), nameof(OrderBookRowVM.side), DefaultColumnWidths.Side, filter, DataGridFilterKnownTemplates.EnumValues, _wrap_comparer(r => r.side)) { } public void draw(OrderBookRowVM row, DrawingContext dc, ColumnGeometry position, double row_height) => - CellRender.draw_text(dc, RenderUtils.get_glyph_context(row), font_size, row.side.text, position, RowHeight, - RenderUtils.get_side_brush(row), null, horizontal_alignment, VerticalAlignment.Center, DefaultCellPadding, row.side.store.animations); + CellRender.draw_text(dc, RenderUtils.get_glyph_context(row), font_size, row.side.ToString(), position, RowHeight, + RenderUtils.get_side_brush(row), null, horizontal_alignment, VerticalAlignment.Center, DefaultCellPadding); } public class SizeColumn : OrderbookColumnBase, IRenderColumn { - public SizeColumn(IDataGridColumnPersistentFilter filter) - : base(Columns.Size, "Qty", r => r.quantity.store, nameof(OrderBookRowVM.quantity), DefaultColumnWidths.Size, filter, DataGridFilterKnownTemplates.UIntMinMax) { + public SizeColumn(IDataGridColumnPersistentFilter filter) + : base(Columns.Size, "Qty", r => r.quantity.ToString(), nameof(OrderBookRowVM.quantity), DefaultColumnWidths.Size, filter, DataGridFilterKnownTemplates.UIntMinMax, _wrap_comparer(r => r.quantity)) { sort_member_path = nameof(OrderBookRowVM.quantity); } public void draw(OrderBookRowVM row, DrawingContext dc, ColumnGeometry position, double row_height) => - CellRender.draw_text(dc, RenderUtils.get_glyph_context(row), font_size, row.quantity.text, position, RowHeight, - RenderUtils.get_side_brush(row), null, horizontal_alignment, VerticalAlignment.Center, DefaultCellPadding, row.quantity.store.animations); + CellRender.draw_text(dc, RenderUtils.get_glyph_context(row), font_size, row.quantity.ToString(), position, RowHeight, + RenderUtils.get_side_brush(row), null, horizontal_alignment, VerticalAlignment.Center, DefaultCellPadding); } public class FilledColumn : OrderbookColumnBase, IRenderColumn { - public FilledColumn(IDataGridColumnPersistentFilter filter) - : base(Columns.Filled, "Filled Qty", r => r.filled_quantity.store, nameof(OrderBookRowVM.filled_quantity), DefaultColumnWidths.Size, filter, DataGridFilterKnownTemplates.UIntMinMax) { + public FilledColumn(IDataGridColumnPersistentFilter filter) + : base(Columns.Filled, "Filled Qty", r => r.filled_quantity.ToString(), nameof(OrderBookRowVM.filled_quantity), DefaultColumnWidths.Size, filter, DataGridFilterKnownTemplates.UIntMinMax, _wrap_comparer(r => r.filled_quantity)) { sort_member_path = nameof(OrderBookRowVM.filled_quantity); } public void draw(OrderBookRowVM row, DrawingContext dc, ColumnGeometry position, double row_height) => - CellRender.draw_text(dc, RenderUtils.get_glyph_context(row), font_size, row.filled_quantity.text, position, RowHeight, - RenderUtils.get_side_brush(row), null, horizontal_alignment, VerticalAlignment.Center, DefaultCellPadding, row.filled_quantity.store.animations); - } - - public class PriceColumn : OrderbookColumnBase, IRenderColumn { - public PriceColumn(IDataGridColumnPersistentFilter filter) - : base(Columns.Price, "Price", r => r.price.store, nameof(OrderBookRowVM.price), DefaultColumnWidths.Price, filter, DataGridFilterKnownTemplates.DoubleMinMax) { } - - public void draw(OrderBookRowVM row, DrawingContext dc, ColumnGeometry position, double row_height) => - CellRender.draw_text(dc, RenderUtils.get_glyph_context(row), font_size, row.price.text, - position, RowHeight, font_brush, null, horizontal_alignment, VerticalAlignment.Center, DefaultCellPadding, row.price.store.animations); - } - - public class AvgFilledColumn : OrderbookColumnBase, IRenderColumn { - public AvgFilledColumn(IDataGridColumnPersistentFilter filter) - : base(Columns.AvgFilled, "Avg Filled", r => r.avg_price.store, nameof(OrderBookRowVM.avg_price), DefaultColumnWidths.Price, filter, DataGridFilterKnownTemplates.DoubleMinMax) { } - - public void draw(OrderBookRowVM row, DrawingContext dc, ColumnGeometry position, double row_height) { - CellRender.draw_text(dc, RenderUtils.get_glyph_context(row), font_size, row.avg_price.text, - position, RowHeight, font_brush, null, horizontal_alignment, VerticalAlignment.Center, DefaultCellPadding, row.avg_price.store.animations); - } - } - - public class LastFilledColumn : OrderbookColumnBase, IRenderColumn { - public LastFilledColumn(IDataGridColumnPersistentFilter filter) - : base(Columns.LastPrice, "Last Filled", r => r.last_price.store, nameof(OrderBookRowVM.last_price), DefaultColumnWidths.Price, filter, DataGridFilterKnownTemplates.DoubleMinMax) { } - - public void draw(OrderBookRowVM row, DrawingContext dc, ColumnGeometry position, double row_height) => - CellRender.draw_text(dc, RenderUtils.get_glyph_context(row), font_size, row.last_price.text, - position, RowHeight, font_brush, null, horizontal_alignment, VerticalAlignment.Center, DefaultCellPadding, row.last_price.store.animations); - } - - public class TimeCreatedColumn : OrderbookColumnBase, IRenderColumn { - public TimeCreatedColumn(IDataGridColumnPersistentFilter filter) - : base(Columns.Time, "Time", r => r.time_created.store, nameof(OrderBookRowVM.time_created), DefaultColumnWidths.Time, filter, DataGridFilterKnownTemplates.TimeFilter) { } - - public void draw(OrderBookRowVM row, DrawingContext dc, ColumnGeometry position, double row_height) => - CellRender.draw_text(dc, RenderUtils.get_glyph_context(row), font_size, row.time_created.text, - position, RowHeight, font_brush, null, horizontal_alignment, VerticalAlignment.Center, DefaultCellPadding, row.time_created.store.animations); - } - - public class LastChangeTimeColumn : OrderbookColumnBase, IRenderColumn { - public LastChangeTimeColumn(IDataGridColumnPersistentFilter filter) - : base(Columns.LastChange, "Last Change", r => r.last_change.store, nameof(OrderBookRowVM.last_change), DefaultColumnWidths.Time, filter, DataGridFilterKnownTemplates.TimeFilter) { } - - public void draw(OrderBookRowVM row, DrawingContext dc, ColumnGeometry position, double row_height) => - CellRender.draw_text(dc, RenderUtils.get_glyph_context(row), font_size, row.last_change.text, - position, RowHeight, font_brush, null, horizontal_alignment, VerticalAlignment.Center, DefaultCellPadding, row.last_change.store.animations); + CellRender.draw_text(dc, RenderUtils.get_glyph_context(row), font_size, row.filled_quantity.ToString(), position, RowHeight, + RenderUtils.get_side_brush(row), null, horizontal_alignment, VerticalAlignment.Center, DefaultCellPadding); } public class OrderBookOrderTypeColumn : OrderTypeColumn, ICVOrderBookColumn { ListSortDirection? _cached_sort_direction; - public OrderBookOrderTypeColumn(IDataGridColumnPersistentFilter filter) - : base(Columns.OrderType, new Binding($"{nameof(OrderBookRowVM.order_type)}.{nameof(IOrderBookCell.value)}"), new Binding(nameof(OrderBookRowVM.order_metadata)), filter) { + public OrderBookOrderTypeColumn(IDataGridColumnPersistentFilter filter) + : base(Columns.OrderType, new Binding(nameof(OrderBookRowVM.order_type)), new Binding(nameof(OrderBookRowVM.order_metadata)), filter) { id = key.ToString(); this.filter = filter; sort_member_path = nameof(OrderBookRowVM.order_type); @@ -449,12 +517,11 @@ namespace mbts.spark { _cached_sort_direction = SortDirection; } - public new IDataGridColumnPersistentFilter filter { get; } + public new IDataGridColumnPersistentFilter filter { get; } + public IComparer comparer => _wrap_comparer(r => r.order_type); - IDataGridColumnFilter ICVOrderBookColumn.filter => filter; - ListSortDirection? ICVOrderBookColumn.SortDirection => _cached_sort_direction; - - public IGridCellStore get_cell_store(OrderBookRowVM row) => row.order_type.store; + IDataGridColumnFilter ICVOrderBookColumn.filter => filter; + ListSortDirection? ICVOrderBookColumn.SortDirection { get => _cached_sort_direction; set => SortDirection = value; } protected override void OnSortDirectionChanged(ListSortDirection? new_value) { _cached_sort_direction = new_value; @@ -465,8 +532,8 @@ namespace mbts.spark { public class OrderBookLastMessageColumn : TextColumn, ICVOrderBookColumn { ListSortDirection? _cached_sort_direction; - public OrderBookLastMessageColumn(IDataGridColumnPersistentFilter filter) - : base(Columns.LastMessage, new Binding($"{nameof(OrderBookRowVM.last_msg)}.{nameof(OrderBookRowVM.IOrderBookCell.value)}"), filter, DataGridFilterKnownTemplates.Regex) { + public OrderBookLastMessageColumn(IDataGridColumnPersistentFilter filter) + : base(Columns.LastMessage, new Binding(nameof(OrderBookRowVM.last_msg)), filter, DataGridFilterKnownTemplates.Regex) { id = key.ToString(); this.filter = filter; Header = "Last Message"; @@ -475,12 +542,11 @@ namespace mbts.spark { tooltip_binding = new Binding(nameof(OrderBookRowVM.last_msg)); } - public new IDataGridColumnPersistentFilter filter { get; } + public new IDataGridColumnPersistentFilter filter { get; } + public IComparer comparer => _wrap_comparer(r => r.last_msg); - IDataGridColumnFilter ICVOrderBookColumn.filter => filter; - ListSortDirection? ICVOrderBookColumn.SortDirection => _cached_sort_direction; - - public IGridCellStore get_cell_store(OrderBookRowVM row) => row.last_msg.store; + IDataGridColumnFilter ICVOrderBookColumn.filter => filter; + ListSortDirection? ICVOrderBookColumn.SortDirection { get => _cached_sort_direction; set => SortDirection = value; } protected override void OnSortDirectionChanged(ListSortDirection? new_value) { _cached_sort_direction = new_value; @@ -495,7 +561,7 @@ namespace mbts.spark { ListSortDirection? _cached_sort_direction; - public SecurityColumn(IDataGridColumnPersistentFilter filter) : base(Columns.Security) { + public SecurityColumn(IDataGridColumnPersistentFilter filter) : base(Columns.Security) { id = key.ToString(); this.filter = filter; Header = DefaultColumnHeaders.Security; @@ -504,14 +570,13 @@ namespace mbts.spark { _cached_sort_direction = SortDirection; } - public IDataGridColumnPersistentFilter filter { get; } + public IDataGridColumnPersistentFilter filter { get; } + public IComparer comparer => _wrap_comparer(r => r.security, SecurityHelper.Comparer.InstanceImmutable.to_nullable()); - IDataGridColumnFilter ICVOrderBookColumn.filter => filter; + IDataGridColumnFilter ICVOrderBookColumn.filter => filter; IDataGridColumnFilter IFilterableColumn.filter => filter; string IFilterableColumn.filter_template => DataGridFilterKnownTemplates.DiscreteValues; - ListSortDirection? ICVOrderBookColumn.SortDirection => _cached_sort_direction; - - public IGridCellStore get_cell_store(OrderBookRowVM row) => row.security.store; + ListSortDirection? ICVOrderBookColumn.SortDirection { get => _cached_sort_direction; set => SortDirection = value; } public override FrameworkElement view_factory() { // expander @@ -522,7 +587,7 @@ namespace mbts.spark { toggle_button.SetResourceReference(ToggleButton.StyleProperty, ExpandCollapseToggleStyleKey); // var security_view = new TextBlock { FontFamily = font_family, FontSize = font_size, Foreground = font_brush }; - security_view.SetBinding(TextBlock.TextProperty, $"{nameof(OrderBookRowVM.security)}.{nameof(IGridCell.text)}"); + security_view.SetBinding(TextBlock.TextProperty, nameof(OrderBookRowVM.security_text)); security_view.SetBinding(TextBlock.HorizontalAlignmentProperty, new Binding(nameof(horizontal_alignment)) { Source = this }); security_view.SetBinding(TextBlock.ForegroundProperty, new Binding(nameof(font_brush)) { Source = this }); security_view.SetBinding(TextBlock.FontFamilyProperty, new Binding(nameof(font_family)) { Source = this }); @@ -536,7 +601,7 @@ namespace mbts.spark { grid.Children.Add(security_view); // var border = new Border { Child = grid }; - CellRender.set_background_animation_bindings(border, $"{nameof(OrderBookRowVM.security)}.{nameof(OrderBookRowVM.security.store)}"); + CellRender.set_background_animation_bindings(border, nameof(OrderBookRowVM.security)); return border; } @@ -568,7 +633,7 @@ namespace mbts.spark { } public static GlyphContext get_glyph_context(OrderBookRowVM r) => r.is_child ? _child_glyph_context : TextUtils.StandardGlyphContext; - public static Brush get_side_brush(OrderBookRowVM r) => r.side.value == Side.Buy ? BTSColors.MarketData.bid : BTSColors.MarketData.ask; + public static Brush get_side_brush(OrderBookRowVM r) => r.side == Side.Buy ? BTSColors.MarketData.bid : BTSColors.MarketData.ask; public static BindingBase get_font_weight_binding() => new Binding(nameof(OrderBookRowVM.is_child)) { Converter = _font_weight_converter }; } } diff --git a/src/client/spark/Modules/Trading/OrderBook/OrderBookRowCollection.cs b/src/client/spark/Modules/Trading/OrderBook/OrderBookRowCollection.cs new file mode 100644 index 000000000..ffd8d1006 --- /dev/null +++ b/src/client/spark/Modules/Trading/OrderBook/OrderBookRowCollection.cs @@ -0,0 +1,141 @@ +using bts.api.core; +using bts.collections; +using bts.utils; +using mbts.utils; +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Linq; +using System.Reactive.Linq; + +namespace mbts.spark { + class OrderBookRowCollection : ObservableCollection { + static readonly IEqualityComparer> _node_equality_comparer = EqualityComparer.Create, uint>(n => n.value.order_key); + readonly Func _row_factory; + SortedTree _tree; + IComparer _comparer; + Func _filter; + bool _is_refresh_pending; + int _last_row_count = 0, _deferment_count = 0; + + public OrderBookRowCollection(Func row_factory, IComparer comparer) { + _row_factory = row_factory; + _tree = new SortedTree(_comparer = comparer, r => r.order_key, r => r.parent_order_key); + } + + public Func filter { get => _filter; set { _filter = value; refresh(); } } + public IComparer comparer { get => _comparer; set { _tree = _tree.Resort(_comparer = value); refresh(); } } + public IEnumerable all_rows => _tree; + + public IEnumerable _visible_rows => + filter == null ? + _tree.Where(r => r.is_visible) : + _tree.Where(r => r.is_visible && filter(r.order_tuple)); + + bool _is_refresh_deferred => _deferment_count > 0; + + public IEnumerable> get_inner_nodes(OrderBookRowVM row) => + _tree.EnumerateInnerNodes(row); + + public IEnumerable get_descendent_rows(OrderBookRowVM row) => + _tree.EnumerateInnerValues(row); + + public void refresh() { + if (_is_refresh_deferred) { + _is_refresh_pending = true; + } else { + _is_refresh_pending = false; + this.sync_with(_visible_rows); + } + } + + public IDisposable defer_refresh(bool force_refresh_after = false) { + // use a counter instead of a simple bool flag to gracefully handle recursive calls to defer_refresh(), + // by only calling refresh() after all IDisposables returned by defer_refresh() have been disposed. + _deferment_count++; + _is_refresh_pending |= force_refresh_after; + return Disposable.Create(() => { + _deferment_count--; + if (_is_refresh_pending) + refresh(); + }); + } + + public void update(IIncrementalTableUpdateTransform table) { + IEnumerable> impacted_branch_roots; + if (table.removed.Count() == _last_row_count) { + // full refresh (most likely this is the first update or a server restart) + _tree.Clear(); + _tree.AddAll(table.added.Select(_row_factory)); + impacted_branch_roots = table.added.Select(ort => _tree.GetRootNode(ort.order.Key)).Distinct(_node_equality_comparer); + } else { + /// The update proceedure, broken down into steps, will be executed in the following order: + /// 1) Removes - independent of filter, as is idempotent and cheap + /// 2) Changes + /// 3) Adds + + // 1) Removes + foreach (OrderRecordTuple ort in table.removed) + _tree.Remove(ort.order.Key); + if (filter == null) { + // 2) Changes (unfiltered) + foreach (Change change in table.changes) + change_row(change.New); + // 3) Adds (unfiltered) + foreach (OrderRecordTuple ort in table.added) { + _tree.Insert(_row_factory(ort)); + } + } else { + // 2) Changes (filtered) + foreach (Change change in table.changes) { + if (filter(change.Old)) { + if (filter(change.New)) { + change_row(change.New); + } else { + _tree.Remove(change.Old.order.Key); + } + } else if (filter(change.New)) { + _tree.Insert(_row_factory(change.New)); + } + } + // 3 Adds (filtered) + foreach (OrderRecordTuple item in table.added.Where(filter)) + _tree.Insert(_row_factory(item)); + } + impacted_branch_roots = + table.added.Concat(table.changed).Select(ort => ort.order.Key) + .Concat( + table.removed + .Where(ort => ort.order.has_parent()) + .Select(ort => ort.order.ParentKey) + .Distinct() // since these are ParentKeys, there could be duplicates. + ).Select(_tree.GetRootNode) + .Distinct(_node_equality_comparer); + } + _last_row_count += table.added.Count() - table.removed.Count(); + foreach (var n in impacted_branch_roots) { + _refresh_tree_properties(0, true, n); + } + refresh(); + // local methods + void change_row(OrderRecordTuple newval) => + _tree.Replace(newval.order.Key, r => { + r.update(newval); + return r; + }); + } + + void _refresh_tree_properties(int depth_level, bool is_visible, INode node) { + node.value.depth_level = depth_level; + node.value.is_visible = is_visible; + using (var en = node.inner.GetEnumerator()) { + if (en.MoveNext()) { + node.value.is_parent = true; + depth_level++; + do _refresh_tree_properties(depth_level, node.value.is_visible && node.value.is_expanded, en.Current); + while (en.MoveNext()); + } + } + } + } +} \ No newline at end of file diff --git a/src/client/spark/Modules/Trading/OrderBook/OrderBookRowVM.cs b/src/client/spark/Modules/Trading/OrderBook/OrderBookRowVM.cs index 4bb8c0322..30c4559cf 100644 --- a/src/client/spark/Modules/Trading/OrderBook/OrderBookRowVM.cs +++ b/src/client/spark/Modules/Trading/OrderBook/OrderBookRowVM.cs @@ -6,403 +6,130 @@ using mbts.ui; using mbts.ui.controls; using mbts.ui.shell; using System; -using System.Collections.Generic; -using System.Collections.ObjectModel; using System.Windows.Input; -using static mbts.spark.OrderBookView; -using OrderStatus = bts.api.orderman.OrderStatus; namespace mbts.spark { partial class OrderBookRowVM : DisposableViewModelBase, IEquatable, DIYGrid.IRowVM, DIYGrid.IActionableRowVM { const string _unknown_security_price_format_string = "0.#########"; const int _unknown_security_price_num_fractional_digits = 9; - readonly OrderBookVM _book_vm; - readonly OrdersModel _model; - readonly Property _visible, _expanded, _passes_filter; - readonly DelegateProperty _depth_level; - readonly HashSet _child_rows = new HashSet(); - readonly ObservantCommand _show_order_modify_cmd; - OrderRecordImmutable _order; - SecurityMD? _security_md; - string _symbol; - OrderBookRowVM _parent; - OrderBookVM.BranchFilterSource _branch_filter_source; - // Core - IOrderBookCellStore _order_key_store_backing; - IOrderBookCellStore _parent_order_key_store_backing; - IOrderBookCellStore _security_key_store_backing; - IOrderBookCellStore _security_store_backing; - IOrderBookCellStore _exchange_store_backing; - IOrderBookCellStore _symbol_store_backing; - // Def - IOrderBookCellStore _order_type_store_backing; - IOrderBookCellStore _side_store_backing; - IOrderBookCellStore _user_store_backing; - IOrderBookCellStore _account_store_backing; - IOrderBookCellStore _time_in_force_store_backing; - IOrderBookCellStore _time_created_store_backing; - // State - IOrderBookCellStore _last_change_store_backing; - IOrderBookCellStore _status_store_backing; - IOrderBookCellStore _price_store_backing; - IOrderBookCellStore _quantity_store_backing; - IOrderBookCellStore _filled_quantity_store_backing; - IOrderBookCellStore _last_price_store_backing; - IOrderBookCellStore _avg_price_store_backing; - IOrderBookCellStore _last_msg_store_backing; - IOrderBookCellStore _order_sub_type_store_backing; - // Core - IOrderBookCellStore _order_key_store => _order_key_store_backing ?? (_order_key_store_backing = CellFactory.create(this, _book_vm.columns.order_key_column, nameof(order_key), r => r._order.Key, r => r._order_key_store)); - IOrderBookCellStore _parent_order_key_store => _parent_order_key_store_backing ?? (_parent_order_key_store_backing = CellFactory.create(this, _book_vm.columns.parent_key_column, nameof(parent_order_key), r => r._order.ParentKey, r => r._parent_order_key_store)); - IOrderBookCellStore _security_key_store => _security_key_store_backing ?? (_security_key_store_backing = CellFactory.create(this, _book_vm.columns.security_key_column, nameof(security_key), r => r._order.security_key(), r => r._security_key_store)); - IOrderBookCellStore _security_store => _security_store_backing ?? (_security_store_backing = CellFactory.create_nullable(this, _book_vm.columns.security_column, nameof(security), r => r._security, r => r._security_store, v => v.display_name())); - IOrderBookCellStore _exchange_store => _exchange_store_backing ?? (_exchange_store_backing = CellFactory.create(this, _book_vm.columns.exchange_column, nameof(exchange), r => r._security.HasValue ? _security.Value.Exchange : Venue.Unset, r => r._exchange_store)); - IOrderBookCellStore _symbol_store => _symbol_store_backing ?? (_symbol_store_backing = CellFactory.create(this, _book_vm.columns.symbol_column, nameof(symbol), r => r._symbol, r => r._symbol_store, v => !String.IsNullOrEmpty(v) ? v : "unknown symbol")); - // Def - IOrderBookCellStore _order_type_store => _order_type_store_backing ?? (_order_type_store_backing = CellFactory.create(this, _book_vm.columns.order_type_column, nameof(order_type), r => r._order.order_type(), r => r._order_type_store)); - IOrderBookCellStore _side_store => _side_store_backing ?? (_side_store_backing = CellFactory.create(this, _book_vm.columns.side_column, nameof(side), r => r._order.side(), r => r._side_store)); - IOrderBookCellStore _user_store => _user_store_backing ?? (_user_store_backing = CellFactory.create(this, _book_vm.columns.user_column, nameof(user), r => _order.user(), r => r._user_store)); - IOrderBookCellStore _account_store => _account_store_backing ?? (_account_store_backing = CellFactory.create(this, _book_vm.columns.account_column, nameof(account), r => r._order.account(), r => r._account_store)); - IOrderBookCellStore _time_in_force_store => _time_in_force_store_backing ?? (_time_in_force_store_backing = CellFactory.create(this, _book_vm.columns.time_in_force_column, nameof(time_in_force), r => r._order.time_in_force(), r => r._time_in_force_store)); - IOrderBookCellStore _time_created_store => _time_created_store_backing ?? (_time_created_store_backing = CellFactory.create(this, _book_vm.columns.time_created_column, nameof(time_created), r => r._order.created(), r => r._time_created_store, format_date)); - // State - IOrderBookCellStore _last_change_store => _last_change_store_backing ?? (_last_change_store_backing = CellFactory.create(this, _book_vm.columns.last_change_column, nameof(last_change), r => _order.last_change(), r => r._last_change_store, format_date)); - IOrderBookCellStore _status_store => _status_store_backing ?? (_status_store_backing = CellFactory.create(this, _book_vm.columns.status_column, nameof(status), r => _order.status(), r => r._status_store)); - IOrderBookCellStore _price_store => _price_store_backing ?? (_price_store_backing = CellFactory.create(this, _book_vm.columns.price_column, nameof(price), r => _order.price(), r => r._price_store, format_price)); - IOrderBookCellStore _quantity_store => _quantity_store_backing ?? (_quantity_store_backing = CellFactory.create(this, _book_vm.columns.qty_column, nameof(quantity), r => _order.quantity(), r => r._quantity_store)); - IOrderBookCellStore _filled_quantity_store => _filled_quantity_store_backing ?? (_filled_quantity_store_backing = CellFactory.create(this, _book_vm.columns.filled_qty_column, nameof(filled_quantity), r => _order.filled(), r => r._filled_quantity_store)); - IOrderBookCellStore _last_price_store => _last_price_store_backing ?? (_last_price_store_backing = CellFactory.create(this, _book_vm.columns.last_price_column, nameof(last_price), r => _order.last_price(), r => r._last_price_store, format_price)); - IOrderBookCellStore _avg_price_store => _avg_price_store_backing ?? (_avg_price_store_backing = CellFactory.create(this, _book_vm.columns.avg_price_column, nameof(avg_price), r => _order.avg_price(), r => r._avg_price_store, format_price)); - IOrderBookCellStore _last_msg_store => _last_msg_store_backing ?? (_last_msg_store_backing = CellFactory.create(this, _book_vm.columns.last_message_column, nameof(last_msg), r => _order.last_msg(), r => r._last_msg_store)); - IOrderBookCellStore _order_sub_type_store => _order_sub_type_store_backing ?? (_order_sub_type_store_backing = CellFactory.create(this, _book_vm.columns.sub_type_column, nameof(order_sub_type), r => r._order.sub_type(), r => r._order_sub_type_store, format_sub_type)); + readonly OrderBookVM _owner; + IRaiseCanExecuteChangedCommand _show_order_modify_cmd, _toggle_expanded_cmd; + bool _is_parent, _is_expanded; + int _depth_level; + SecurityMD? _security_md => order_tuple.security_md; + + public OrderBookRowVM(OrderBookVM owner, OrderRecordTuple order_tuple) { + _owner = owner; + this.order_tuple = order_tuple; + is_visible = !is_child; + } - static string format_date(DateTime v) => $"{v:M/d} {v:h:mm:ss.fff tt}"; - string format_price(double v) => _security.HasValue ? - _security.Value.ContractSpec.format(v) : - Math.Round(v, _unknown_security_price_num_fractional_digits, MidpointRounding.AwayFromZero).ToString(_unknown_security_price_format_string); - static string format_sub_type(OrderSubType v) { + public event Action invalidated; + + // Core + public OrderRecordTuple order_tuple { get; private set; } + public OrderRecordImmutable order => order_tuple.order; + public OrderMetadata order_metadata => _owner.get_metadata(this); + public uint order_key => order.Key; + public uint parent_order_key => order.ParentKey; + public uint security_key => order.security_key(); + public SecurityImmutable? security => _security_md.security(); + public string security_text => security.display_name(); + public Venue exchange => _security_md.exchange(); + public string symbol => _security_md.symbol(); + // Def + public CustomOrderDefinition.CustomOneofCase order_type => order.order_type(); + public OrderSubType order_sub_type => order.sub_type(); + public string order_sub_type_text => _fmt_order_sub_type(order_sub_type); + public Side side => order.side(); + public string user => order.user(); + public string account => order.account(); + public TimeInForce time_in_force => order.time_in_force(); + public DateTime time_created => order.created(); + public string time_created_text => _fmt_date(time_created); + // State + public DateTime last_change => order.last_change(); + public string last_change_text => _fmt_date(last_change); + public OrderStatus status => order.status(); + public double price => order.price(); + public double last_price => order.last_price(); + public double avg_price => order.avg_price(); + public string price_text => _fmt_price(price); + public string last_price_text => _fmt_price(last_price); + public string avg_price_text => _fmt_price(avg_price); + public uint quantity => order.quantity(); + public uint filled_quantity => order.filled(); + public string last_msg => order.last_msg(); + // Tree + public bool is_parent { get => _is_parent; set => Set(ref _is_parent, value); } + public bool is_child => order.has_parent(); + public bool is_visible { get; set; } + public bool is_expanded { + get => _is_expanded; + set { + if (Set(ref _is_expanded, value)) + _owner.on_expanded_changed(this); + } + } + public int depth_level { get => _depth_level; set => Set(ref _depth_level, value); } + // Commands + public ICommand toggle_command => _toggle_expanded_cmd ?? ( + _toggle_expanded_cmd = new DelegateCommand(() => is_expanded = !is_expanded)); + + ICommand DIYGrid.IActionableRowVM.default_command => _show_order_modify_cmd ?? + (_show_order_modify_cmd = new DelegateCommand(() => _owner.show_order_modify(this), () => status == OrderStatus.Working)); + + public void update(OrderRecordTuple order_tuple) { + Log.assert(order_tuple.order.Key == this.order.Key, "OrderBookRowVM: update_order was called with an order with the wrong key."); + bool had_security = _security_md.HasValue; + // _show_order_modify_cmd is lazy (instantiated in the default_command getter); it might be null, don't not force instantiation + bool? could_modify_execute = _show_order_modify_cmd?.CanExecute(null); + OrderRecordImmutable old_order = order; + this.order_tuple = order_tuple; + // most cells are "render-cells," which don't use databindings - the only way to update such cells is to raise the invalidated event + bool invalidate = !( + old_order.last_change() == order.last_change() && + old_order.filled() == order.filled() && + old_order.last_price() == order.last_price() && + old_order.avg_price().Equals(order.avg_price()) && + old_order.price().Equals(order.price()) && + old_order.quantity() == order.quantity() && + old_order.last_msg() == order.last_msg()); + // however, the order-type and security cells DO use databindings - if these cells have changed, the PropertyChanged event must be raised regardless of whether or not invalidated is raised + if (old_order.order_type() != order.order_type()) + RaisePropertyChanged(nameof(order_type), nameof(order_metadata)); + if (!had_security && _security_md.HasValue) { + RaisePropertyChanged(nameof(security)); + // need to raise invalidated to refresh the price cells (the security.ContractSpec determines how they are formatted - Ticks!) + invalidate = true; + } + if (old_order.status() != order.status()) { + invalidate = true; + RaisePropertyChanged(nameof(status)); + } + if (could_modify_execute.HasValue && could_modify_execute.Value != _show_order_modify_cmd.CanExecute(null)) + _show_order_modify_cmd.RaiseCanExecuteChanged(); + if (invalidate) + raise_invalidated(); + } + + public void raise_invalidated() => invalidated?.Invoke(); + + public bool Equals(OrderBookRowVM other) => other != null && other.order.Key == order.Key; + public override bool Equals(object obj) => Equals(obj as OrderBookRowVM); + public override int GetHashCode() => (int)order.Key; + public override string ToString() => $"{order_key} {last_change} {parent_order_key}"; + + static string _fmt_date(DateTime v) => $"{v:M/d} {v:h:mm:ss.fff tt}"; + static string _fmt_order_sub_type(OrderSubType v) { switch (v) { case OrderSubType.HedgingLeg: return "H"; case OrderSubType.QuotingLeg: return "Q"; default: return string.Empty; } } - - public OrderBookRowVM(IBus bus, OrderBookVM source_vm, OrdersModel model, OrderRecordImmutable record, SecurityMD? s) { - _book_vm = source_vm; - _model = model; - _order = record; - _security_md = s; - _symbol = _security_md.HasValue ? _security_md.Value.security.Symbol : null; - _visible = backing(nameof(is_visible), true); - _expanded = backing(nameof(is_expanded), false); - _depth_level = backing(nameof(depth_level), () => { - int i = 0; - var curr = this; - while (curr._parent != null) { - i++; - curr = curr._parent; - } - return i; - }); - toggle_command = new DelegateCommand(() => is_expanded = !is_expanded); - _show_order_modify_cmd = new ObservantCommand(() => bus.post(SparkTags.ShowModifyOrderTag, _order.Key)); - _show_order_modify_cmd.OnNext(status.value == OrderStatus.Working); - row_passes_filter = _book_vm.columns.filter(this); - _passes_filter = backing(nameof(passes_filter), row_passes_filter); - } - - public event Action invalidated; - public event Action<(OrderRecordTuple old_val, OrderRecordTuple new_val)> updated; - - public OrderRecordImmutable order => _order; - public OrderMetadata order_metadata => _model.factory.get_metadata(order_type.value); - public OrderBookRowVM parent => _parent; - public OrderRecordTuple current_data => new OrderRecordTuple(_order, _security_md); - // Core - public IOrderBookCell order_key => _order_key_store.current; - public IOrderBookCell parent_order_key => _parent_order_key_store.current; - public IOrderBookCell security_key => _security_key_store.current; - public IOrderBookCell security => _security_store.current; - public IOrderBookCell exchange => _exchange_store.current; - public IOrderBookCell symbol => _symbol_store.current; - // Def - public IOrderBookCell order_type => _order_type_store.current; - public IOrderBookCell side => _side_store.current; - public IOrderBookCell user => _user_store.current; - public IOrderBookCell account => _account_store.current; - public IOrderBookCell time_in_force => _time_in_force_store.current; - public IOrderBookCell time_created => _time_created_store.current; - // State - public IOrderBookCell last_change => _last_change_store.current; - public IOrderBookCell status => _status_store.current; - public IOrderBookCell price => _price_store.current; - public IOrderBookCell quantity => _quantity_store.current; - public IOrderBookCell filled_quantity => _filled_quantity_store.current; - public IOrderBookCell last_price => _last_price_store.current; - public IOrderBookCell avg_price => _avg_price_store.current; - public IOrderBookCell last_msg => _last_msg_store.current; - public IOrderBookCell order_sub_type => _order_sub_type_store.current; - // Commands - public ICommand toggle_command { get; } - ICommand ui.controls.DIYGrid.IActionableRowVM.default_command => _show_order_modify_cmd; - // Accessories - public bool is_parent => _child_rows.Count > 0; - public bool is_child => _order.has_parent(); - public IReadOnlyCollection child_orders => _child_rows; - public OrderBookVM.BranchFilterSource branch_filter_source => _branch_filter_source; - public bool passes_filter => _passes_filter.Value; - public bool row_passes_filter { get; private set; } - public bool is_visible { - get => _visible.Value; - set { - _visible.set(value); - if (is_expanded) { - foreach (var child_row in _child_rows) - child_row.is_visible = value; - } - } - } - public bool is_expanded { - get => _expanded.Value; - set { - _expanded.set(value); - foreach (var child_row in child_orders) - child_row.is_visible = value; - } - } - public int depth_level => _depth_level.Value; - - SecurityImmutable? _security => _security_md?.security; - // '_dynamic_render_cells' are presented by 'RenderColumns,' and may change value during the lifetime of this row - // Note: by instantiating all these cells inside a CellPropertyGroup, we can update all of them with a single call. - // this reduces the risk of accidentally failing to update one of the cells, esp if we make changes to them down the road. - IEnumerable _dynamic_render_cells { - get { - yield return _last_change_store; - yield return _price_store; - yield return _quantity_store; - yield return _filled_quantity_store; - yield return _avg_price_store; - yield return _last_price_store; - yield return _status_store; - yield return _last_msg_store; - } - } - - IEnumerable _security_dependent_cells { - get { - yield return _security_store; - yield return _exchange_store; - yield return _symbol_store; - } - } - - IEnumerable _price_cells { - get { - yield return _price_store; - yield return _avg_price_store; - yield return _last_price_store; - yield return _symbol_store; - } - } - - public ObservableCollection row_animations { get; } = new ObservableCollection(); - - DIYGrid.IGridCellStore[] _cell_stores_backing; - public IReadOnlyList cell_stores => _cell_stores_backing ?? (_cell_stores_backing = new DIYGrid.IGridCellStore[] { - _security_store, - _security_key_store, - _parent_order_key_store, - _order_key_store, - _symbol_store, - _time_created_store, - _user_store, - _account_store, - _exchange_store, - _time_in_force_store, - _order_type_store, - _order_sub_type_store, - _side_store, - _last_change_store, - _price_store, - _quantity_store, - _filled_quantity_store, - _avg_price_store, - _last_price_store, - _status_store, - _last_msg_store - }); - - public static Stack lineage(OrderBookRowVM node) { - var rv = new Stack(); - rv.Push(node); - while (node._parent != null) - rv.Push((node = node._parent)); - return rv; - } - - public void join_branch_filter_source(OrderBookVM.BranchFilterSource new_branch_filter_source) { - if (_branch_filter_source != null) { - _branch_filter_source.passes_filter_changed -= _passes_filter.set; - } - _branch_filter_source = new_branch_filter_source; - _branch_filter_source.set_row_passes_filter(_order.Key, row_passes_filter); - foreach (var child_row in _child_rows) - child_row.join_branch_filter_source(_branch_filter_source); - _passes_filter.set(_branch_filter_source.passes_filter); - _branch_filter_source.passes_filter_changed += _passes_filter.set; - } - - /// - /// Attaches the child-row to this row's branch-filter-source, and recursively refreshes sort/filter properties for the child and it's descendents. - /// - public void add_child_order(OrderBookRowVM child_row) { - if (_child_rows.Add(child_row) && _child_rows.Count == 1) - RaisePropertyChanged(nameof(is_parent)); - child_row._parent = this; - // child orders that arrived in an earlier update use a temporary branch_filter_source - // need to tell _book_vm to remove the now-stale branch_filter_source. - if (child_row._branch_filter_source != null) - _book_vm.remove_branch_filter_root(child_row._order.Key); - // recursively updates 'live_sorting_props,' and 'is_visible' for child_row and descendents - child_row._on_lineage_changed(); - } - - public void remove_child_order(OrderBookRowVM child_row) { - if (_child_rows.Remove(child_row) && _child_rows.Count == 0) - RaisePropertyChanged(nameof(is_parent)); - } - - public void remove_parent(OrderBookVM.BranchFilterSource new_branch_filter_source) { - _parent = null; - join_branch_filter_source(new_branch_filter_source); - refresh_row_passes_filter(); - foreach (var p in _book_vm.grid_settings_manager.live_sorting_props) - RaisePropertyChanged(p); - _depth_level.refresh(); - foreach (OrderBookRowVM child_row in _child_rows) - child_row._on_lineage_changed(); - } - - public void update_order(OrderRecordImmutable order) { - Log.assert(order.ParentKey == _order.ParentKey, "Parent Keys can change?"); - Log.assert(order.Key == _order.Key, "Keys can change?"); - var old_data = current_data; - _order = order; - if (_update_partial(_dynamic_render_cells)) - raise_invalidated(); - _show_order_modify_cmd.OnNext(status.value == OrderStatus.Working); - refresh_row_passes_filter(); - updated?.Invoke((old_data, current_data)); - } - - public void update_security(SecurityMD security_md) { - var old_data = current_data; - var old_spec = _security.spec_or_default(); - _security_md = security_md; - _symbol = _security_md.HasValue ? _security_md.Value.security.Symbol : null; - if (!Equals(old_spec, _security.spec_or_default())) { - _update_partial(_security_dependent_cells); - foreach (var cell_store in _price_cells) - cell_store.regenerate_cell(); - raise_invalidated(); - } else if (_update_partial(_security_dependent_cells)) { - raise_invalidated(); - } - refresh_row_passes_filter(); - updated?.Invoke((old_data, current_data)); - } - - public void refresh_row_passes_filter() { - row_passes_filter = _book_vm.columns.filter(this); - if (_branch_filter_source != null) { - _branch_filter_source.set_row_passes_filter(_order.Key, row_passes_filter); - } else { - _passes_filter.set(row_passes_filter); - } - } - - public void raise_invalidated() => invalidated?.Invoke(); - - public bool Equals(OrderBookRowVM other) => other != null && other._order.Key == _order.Key; - public override bool Equals(object obj) => Equals(obj as OrderBookRowVM); - public override int GetHashCode() => (int)_order.Key; - public override string ToString() => $"{order_key} {last_change} {parent_order_key}"; - - protected override void Dispose(bool disposing) { - // idempotent - if (_branch_filter_source != null) { - _branch_filter_source.passes_filter_changed -= _passes_filter.set; - _branch_filter_source = null; - } - } - - static IEnumerable _descendents(OrderBookRowVM root) { - foreach (var child in root.child_orders) { - yield return child; - foreach (var grand_child in _descendents(child)) - yield return grand_child; - } - } - - static IOrderBookCellStore _cell_store_from_column(OrderBookRowVM row, Columns column) { - switch (column) { - case Columns.OrderKey: return row._order_key_store; - case Columns.ParentKey: return row._parent_order_key_store; - case Columns.SecurityKey: return row._security_key_store; - case Columns.Security: return row._security_store; - case Columns.Symbol: return row._symbol_store; - case Columns.Exchange: return row._exchange_store; - case Columns.OrderType: return row._order_type_store; - case Columns.Side: return row._side_store; - case Columns.User: return row._user_store; - case Columns.Account: return row._account_store; - case Columns.TimeInForce: return row._time_in_force_store; - case Columns.Time: return row._time_created_store; - case Columns.LastChange: return row._last_change_store; - case Columns.Status: return row._status_store; - case Columns.Price: return row._price_store; - case Columns.Size: return row._quantity_store; - case Columns.Filled: return row._filled_quantity_store; - case Columns.LastPrice: return row._last_price_store; - case Columns.AvgFilled: return row._avg_price_store; - case Columns.LastMessage: return row._last_msg_store; - case Columns.SubType: return row._order_sub_type_store; - default: throw new ArgumentException(nameof(column)); - } - } - - // Note: this method assumes that lineage exists (i.e. _parent is not null). - void _on_lineage_changed() { - _depth_level.refresh(); - // parent 'branch_filter_source' can be null: - // if the parent is itself an orphan - meaning this is the middle of an update - - // the final branch-filter-root will be determined after all new orders have been added - if (_parent._branch_filter_source != null) - join_branch_filter_source(_parent._branch_filter_source); - _visible.set(_parent.is_visible && _parent.is_expanded); - // refresh the sort by calling regenerate_cell() for each live-sorting-columns corresponding cell-store - foreach (Columns column in _book_vm.live_sorting_columns) - _cell_store_from_column(this, column).regenerate_cell(); - // finally, repeat for each child-row - foreach (var child_row in _child_rows) - child_row._on_lineage_changed(); - } - - bool _update_partial(IEnumerable target_cell_stores) { - bool updated = false; - foreach (var cell_store in target_cell_stores) { - if (cell_store.refresh()) { - updated = true; - if (_book_vm.live_sorting_columns.Contains(cell_store.column.key)) { - // refresh the sort by calling regenerate_cell() for each descendent-row - foreach (OrderBookRowVM dr in _descendents(this)) - cell_store.select_other(dr).regenerate_cell(); - } - } - } - return updated; - } + string _fmt_price(double v) => security.HasValue ? + security.Value.ContractSpec.format(v) : + Math.Round(v, _unknown_security_price_num_fractional_digits, MidpointRounding.AwayFromZero).ToString(_unknown_security_price_format_string); } } \ No newline at end of file diff --git a/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs b/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs index 04bfd3d7e..528e8d7e5 100644 --- a/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs +++ b/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs @@ -1,220 +1,74 @@ using bts; using bts.api; -using bts.api.core; using bts.api.orderman; using bts.shell; -using mbts.collections; +using bts.spark; using mbts.ui; using mbts.ui.controls; using mbts.ui.settings; using mbts.ui.shell; using mbts.ui.utility; using System; -using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Collections.Specialized; -using System.ComponentModel; using System.Linq; using System.Reactive.Linq; -using System.Reactive.Subjects; using System.Windows.Controls; -using System.Windows.Data; using System.Windows.Input; using static bts.shell.OrderBookWindowFactory; using static mbts.spark.OrderBookView; -namespace mbts.spark { - partial class OrderBookVM : DisposableViewModelBase, IHandleRowSelection { - readonly LiveSortingColumKeys _live_sorting_columns; +namespace mbts.spark { + partial class OrderBookVM : ViewModelBase, IDisposable, IHandleRowSelection, IHandleGridSorting, IWillLetYouKnowWhenIDispose { + readonly IBus _bus; + readonly OrdersModel _model; readonly Property _current_row; + readonly OrderBookRowCollection _row_collection; + readonly ObservableCollection _selected_rows = new ObservableCollection(); + readonly OrderBookColumnCollection _columns; + readonly List _resources; OrderBookStartupData _state; - Dictionary _order_rows_cache = new Dictionary(); - Dictionary _branch_filter_roots = new Dictionary(); - ObservableCollection _order_rows = new ObservableCollection(); - ObservableCollection _selected_rows = new ObservableCollection(); - SetMap _parent_to_children = new SetMap(); - SetMap _security_orders = new SetMap(); public OrderBookVM(IObservable owner_title, IBus bus, INotificationService notification_service, OrdersModel model) { - var order_securities_subject = new Subject<(IEnumerable added, IEnumerable removed)>(); - columns = new OrderBookColumnCollection(bus, model, order_securities_subject); - rows_view = CollectionViewSource.GetDefaultView(_order_rows); - rows_view.Filter = obj => obj is OrderBookRowVM vm && vm.is_visible && vm.passes_filter; - columns.filter_changed += () => { - foreach (var vm in _order_rows) - vm.refresh_row_passes_filter(); - }; - var cvls = (ICollectionViewLiveShaping)rows_view; - cvls.IsLiveFiltering = true; - cvls.IsLiveSorting = true; - cvls.LiveFilteringProperties.AddRange(nameof(OrderBookRowVM.is_visible), nameof(OrderBookRowVM.passes_filter)); + _bus = bus; + _model = model; + _columns = new OrderBookColumnCollection(bus, model, model.on_order_securities.Select(u => (u.added.Values.Select(smd => smd.security), u.removed.Values.Select(smd => smd.security)))); + _row_collection = new OrderBookRowCollection(ort => new OrderBookRowVM(this, ort), _columns.get_comparer_data().comp) { filter = _columns.get_filter() }; _current_row = backing(nameof(current_row)); - - collapse_all_rows_command = new DelegateCommand( - () => { - foreach (var r in _order_rows.Where(r => r.is_visible && r.passes_filter)) - r.is_expanded = false; - }); - expand_all_rows_command = new DelegateCommand( - () => { - foreach (var r in _order_rows.Where(r => r.is_visible && r.passes_filter)) - r.is_expanded = true; - }); - - grid_settings_manager = _create_grid_settings_manager(owner_title, bus, notification_service, model, _order_rows, rows_view, columns); - _live_sorting_columns = new LiveSortingColumKeys(grid_settings_manager.live_sorting_props); - - SetMap missing_security_orders = new SetMap(); - IReadOnlyDictionary security_dict = new Dictionary(); - - _dispose = Disposable.Create( - bus.subscribe(SparkTags.SecuritiesTag).OnDispatcher().Subscribe(on_securities), - model.on_orders.OnDispatcher().Subscribe(on_orders), - grid_settings_manager, - columns, - order_securities_subject); - - void on_orders(TableUpdate table) { - // clear everything (most likely the server restarted) - if (table.removed.Count() == _order_rows.Count && !table.added.Any()) { - _selected_rows.Clear(); - _order_rows.DisposeAll(); - _order_rows.Clear(); - _order_rows_cache.Clear(); - _branch_filter_roots.Clear(); - _parent_to_children.clear(); - _security_orders.clear(); - missing_security_orders.clear(); - Log.Ui.warn("OrderBook received reset message."); - return; - } - var added_securities = new List(1); - var removed_securities = new List(1); - - foreach (var o in table.removed) { - if (_order_rows_cache.TryPopValue(o.Key, out OrderBookRowVM row)) { - row.Dispose(); - _order_rows.Remove(row); - if (_security_orders.removes_key(o.Def.SecurityKey, o.Key)) { - if (row.security.value.HasValue) - removed_securities.Add(row.security.value.Value); - } - if (o.has_parent()) { - _parent_to_children.remove(o.ParentKey, o.Key); - if (row.parent != null) { - row.parent.remove_child_order(row); - } else { - _branch_filter_roots.Remove(o.Key); - } - } else { - _branch_filter_roots.Remove(o.Key); - } - // the child_rows will most-likely be removed in this update, but we can't be sure, - // until that happens they must serve as tempororay branch-filter-roots - foreach (var child_row in row.child_orders) { - var bfs = new BranchFilterSource(); - child_row.remove_parent(bfs); - _branch_filter_roots.Add(child_row.order.Key, bfs); - } - missing_security_orders.remove(o.Key); + _columns.filter_changed += on_filter_changed; + collapse_all_rows_command = new DelegateCommand(() => set_all_is_expanded(false)); + expand_all_rows_command = new DelegateCommand(() => set_all_is_expanded(true)); + _resources = new List { + model.on_order_tuples_table.OnDispatcher().Subscribe(_row_collection.update), + (grid_settings_manager = _create_grid_settings_manager(owner_title, bus, notification_service, model, columns)), + Disposable.Create(() => { + _columns.filter_changed -= on_filter_changed; + Disposable.dispose(_row_collection); + }), + _columns + }; + // local methods + void set_all_is_expanded(bool is_expanded) { + using (_row_collection.defer_refresh()) { + foreach (var r in _row_collection.all_rows) { + r.is_expanded = is_expanded; + r.is_visible = is_expanded || !r.is_child; } } - foreach (var ch in table.changes) - _order_rows_cache[ch.New.Key].update_order(ch.New); - var orphans = new Dictionary(); - foreach (var o in table.added) { - SecurityMD? security_md; - if (security_dict.TryGetValue(o.Def.SecurityKey, out SecurityMD smd)) { - security_md = smd; - if (_security_orders.adds_key(o.Def.SecurityKey, o.Key)) - added_securities.Add(security_md.Value.security); - } else { - missing_security_orders.add(o.Def.SecurityKey, o.Key); - security_md = null; - _security_orders.add(o.Def.SecurityKey, o.Key); - } - OrderBookRowVM row = new OrderBookRowVM(bus, this, model, o, security_md); - if (!o.has_parent()) { - // if it doesn't have a parent, it must be a root; create a new BranchFilterSource - var bfs = new BranchFilterSource(); - _branch_filter_roots.Add(o.Key, bfs); - row.join_branch_filter_source(bfs); - } else { - _parent_to_children.add(o.ParentKey, o.Key); - if (_order_rows_cache.TryGetValue(o.ParentKey, out OrderBookRowVM pvm)) { - pvm.add_child_order(row); - } else { - // (**) look for orphans' parents again after all new orders have been added to _order_vm_cache - // don't create a temporary BranchFilterSource root just yet. (**) - orphans.Add(o.Key, row); - _order_rows_cache.Add(o.Key, row); - try_add_children(row); - continue; - } - } - _order_rows_cache.Add(o.Key, row); - _order_rows.Add(row); - try_add_children(row); - } - // (**) since all new rows have been added by now, any remaining orphans will be assigned their own branch-filter-roots - // (usually, rows are tempororily added to orphans bc their parent is just behind them in the list of added orders) (**) - foreach (OrderBookRowVM row in orphans.Values) { - var bfs = new BranchFilterSource(); - row.join_branch_filter_source(bfs); - _branch_filter_roots.Add(row.order.Key, bfs); - _order_rows.Add(row); - } - if (added_securities.Count > 0 || removed_securities.Count > 0) - order_securities_subject.OnNext((added_securities, removed_securities)); - - void try_add_children(OrderBookRowVM row) { - if (_parent_to_children.try_lookup(row.order.Key, out ISet children)) - foreach (uint child_key in children) { - OrderBookRowVM child_vm = _order_rows_cache[child_key]; - // (**) if children arrived in an earlier update, this will notify them that their parent has arrived (**) - // - their tempororary branch-filter-roots will be removed, and they will join that of their parent's - // - for the live-filter-properties, their corresponding cells will be regenerated and they will be re-inserted beneath their new parent. - row.add_child_order(child_vm); - if (orphans.Remove(child_key)) - _order_rows.Add(child_vm); - else { - _order_rows.Remove(child_vm); - _order_rows.Add(child_vm); - } - } - } - } - - // I assume we don't need to worry about orders "losing" or "changing" their security - // once the order vm discovers its security (by id), it will never change. - void on_securities(IReadOnlyDictionary securities) { - security_dict = securities; - var added_securities = new List(); // assume securities won't be removed - foreach (var missing_sec_orders in missing_security_orders.ToList()) { - if (securities.TryGetValue(missing_sec_orders.Key, out SecurityMD smd)) { - missing_security_orders.remove(missing_sec_orders.Key); - added_securities.Add(smd.security); - foreach (var order_key in missing_sec_orders) { - var vm = _order_rows_cache[order_key]; - vm.update_security(smd); - } - } - } - if (added_securities.Count > 0) - order_securities_subject.OnNext((added_securities, new List(0))); } + void on_filter_changed() => + _row_collection.filter = _columns.get_filter(); } public event Action selected_order_changed; + public event Action OnDisposed; - public OrderBookColumnCollection columns { get; } - public ISet live_sorting_columns => _live_sorting_columns; - public ICollectionView rows_view { get; } + public ObservableCollection columns => _columns; + public ObservableCollection rows => _row_collection; public OrderBookRowVM current_row { get => _current_row.Value; set => _current_row.Value = value ?? _selected_rows.FirstOrDefault(); } public OrderRecordImmutable? current_order => current_row?.order; public IEnumerable selected_orders => _selected_rows.Select(r => r.order); - public GridSettingsManager grid_settings_manager { get; } public ICommand collapse_all_rows_command { get; } public ICommand expand_all_rows_command { get; } @@ -226,25 +80,63 @@ namespace mbts.spark { public OrderBookStartupData create() { var (grid, animations) = grid_settings_manager.create(); - return new OrderBookStartupData(columns.create(), grid, animations.Select(a => new OrderRecordTuplePredicateAnimations(a.predicate_builder, a.cell_animations, a.row_animations, a.sound_notifications))); + return new OrderBookStartupData(_columns.create(), grid, animations.Select(a => new OrderRecordTuplePredicateAnimations(a.predicate_builder, a.cell_animations, a.row_animations, a.sound_notifications))); } public void load_from(OrderBookStartupData state) { if (state == null) return; _state = state; grid_settings_manager.load_from((state.grid, state.predicate_animations)); - columns.load_from(state.filters); + _columns.load_from(state.filters); } - - internal void remove_branch_filter_root(uint key) => - _branch_filter_roots.Remove(key); - protected override void Dispose(bool disposing) => - Disposable.dispose(_order_rows); + public void on_sorting(object sender, DIYGridSortingEventArgs e) { + if (e.Column is ICVOrderBookColumn col) { + e.Handled = true; + _columns.on_sorting(col, !KeyboardExtensions.IsShiftDown()); + (var comp, var desc) = _columns.get_comparer_data(); + Log.Ui.info($"{nameof(OrderBookRowVM)} begin resort: {desc}"); + _row_collection.comparer = comp; + } else { + Log.Ui.warn($"{nameof(OrderBookVM)} failed to process sorting event: Column was not of type {nameof(ICVOrderBookColumn)}"); + } + } + + internal void show_order_modify(OrderBookRowVM row) => + _bus.post(SparkTags.ShowModifyOrderTag, row.order_key); + + internal void on_expanded_changed(OrderBookRowVM row) { + using (_row_collection.defer_refresh(true)) { + if (row.is_expanded) { + _on_expanded(row); + } else { + _on_collapsed(row); + } + } + } + + void _on_expanded(OrderBookRowVM row) { + foreach (var node in _row_collection.get_inner_nodes(row)) { + node.value.is_visible = true; + if (node.value.is_expanded) + _on_expanded(node.value); + } + } + + void _on_collapsed(OrderBookRowVM row) { + foreach (var dr in _row_collection.get_descendent_rows(row)) + dr.is_visible = false; + } + + public OrderMetadata get_metadata(OrderBookRowVM row) => _model.factory.get_metadata(row.order_type); + + public void Dispose() { + Disposable.dispose(_resources); + OnDisposed?.Invoke(true); + } static GridSettingsManager _create_grid_settings_manager( - IObservable owner_title, IBus bus, INotificationService notification_service, OrdersModel model, - ObservableCollection rows, ICollectionView rows_view, ObservableCollection columns) { + IObservable owner_title, IBus bus, INotificationService notification_service, OrdersModel model, ObservableCollection columns) { // make font-weight immutable for all columns (the font-weight rule is hard-coded: Regular for root-orders, Light for all others) // TODO: Remove the immutable font-weight flags when cell animation is ready var col_settings_flags = EnumHelper.get_values().ToDictionary( @@ -255,7 +147,7 @@ namespace mbts.spark { col_settings_flags[c.ToString()] = GridColumnSettingsFeatureFlags.ImmutableFontColor | GridColumnSettingsFeatureFlags.ImmutableFontWeight; } return new GridSettingsManager( - owner_title, bus, notification_service, model, rows, rows_view, columns, + owner_title, bus, notification_service, model, columns, () => { var pvm = PredicateBuilderVMWrapper.create( OrderRecordTuplePredicateVMFactory.get_oneofs(bus, model), rp => rp.predicate_type_case); @@ -295,84 +187,5 @@ namespace mbts.spark { default: return default; } } - - internal class BranchFilterSource { - readonly HashSet _orders_pass_filter = new HashSet(); - - public event Action passes_filter_changed; - - public bool passes_filter => _orders_pass_filter.Count > 0; - - public void set_row_passes_filter(uint key, bool value) { - if (value) { - if (_orders_pass_filter.Add(key) && _orders_pass_filter.Count == 1) - passes_filter_changed?.Invoke(value); - } else if (_orders_pass_filter.Remove(key) && _orders_pass_filter.Count == 0) { - passes_filter_changed?.Invoke(value); - } - } - } - - class LiveSortingColumKeys : HashSet { - public LiveSortingColumKeys(ObservableCollection live_sorting_props) { - _add_items(live_sorting_props); - live_sorting_props.CollectionChanged += (s, e) => { - switch (e.Action) { - case NotifyCollectionChangedAction.Add: _add_items(e.NewItems); break; - case NotifyCollectionChangedAction.Remove: _remove_items(e.OldItems); break; - case NotifyCollectionChangedAction.Replace: - _remove_items(e.OldItems); - _add_items(e.NewItems); - break; - case NotifyCollectionChangedAction.Reset: - Clear(); - _add_items(live_sorting_props); - break; - } - }; - } - - public static bool try_parse(string prop_name, out Columns result) { - switch (prop_name) { - case nameof(OrderBookRowVM.security): result = Columns.Security; break; - case nameof(OrderBookRowVM.symbol): result = Columns.Symbol; break; - case nameof(OrderBookRowVM.time_created): result = Columns.Time; break; - case nameof(OrderBookRowVM.last_change): result = Columns.LastChange; break; - case nameof(OrderBookRowVM.user): result = Columns.User; break; - case nameof(OrderBookRowVM.account): result = Columns.Account; break; - case nameof(OrderBookRowVM.exchange): result = Columns.Exchange; break; - case nameof(OrderBookRowVM.time_in_force): result = Columns.TimeInForce; break; - case nameof(OrderBookRowVM.order_type): result = Columns.OrderType; break; - case nameof(OrderBookRowVM.order_sub_type): result = Columns.SubType; break; - case nameof(OrderBookRowVM.side): result = Columns.Side; break; - case nameof(OrderBookRowVM.quantity): result = Columns.Size; break; - case nameof(OrderBookRowVM.filled_quantity): result = Columns.Filled; break; - case nameof(OrderBookRowVM.price): result = Columns.Price; break; - case nameof(OrderBookRowVM.avg_price): result = Columns.AvgFilled; break; - case nameof(OrderBookRowVM.last_price): result = Columns.LastPrice; break; - case nameof(OrderBookRowVM.status): result = Columns.Status; break; - case nameof(OrderBookRowVM.last_msg): result = Columns.LastMessage; break; - case nameof(OrderBookRowVM.security_key): result = Columns.SecurityKey; break; - case nameof(OrderBookRowVM.order_key): result = Columns.OrderKey; break; - case nameof(OrderBookRowVM.parent_order_key): result = Columns.ParentKey; break; - default: result = default; return false; - } - return true; - } - - void _add_items(IList prop_names) { - foreach (string pn in prop_names) { - if (try_parse(pn, out Columns c)) - Add(c); - } - } - - void _remove_items(IList prop_names) { - foreach (string pn in prop_names) { - if (try_parse(pn, out Columns c)) - Remove(c); - } - } - } } } \ No newline at end of file diff --git a/src/client/spark/Modules/Trading/OrderBook/OrderBookView.xaml b/src/client/spark/Modules/Trading/OrderBook/OrderBookView.xaml index 4395b146e..8d7004421 100644 --- a/src/client/spark/Modules/Trading/OrderBook/OrderBookView.xaml +++ b/src/client/spark/Modules/Trading/OrderBook/OrderBookView.xaml @@ -48,7 +48,7 @@ - + @@ -67,7 +67,7 @@ Grid.Row="2" Columns="{Binding columns}" CurrentRow="{Binding current_row, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" - Rows="{Binding rows_view}" + Rows="{Binding rows}" RowStyle="{StaticResource {x:Static sp:OrderBookView.RowStyleKey}}" RowTemplate="{StaticResource {x:Static sp:OrderBookView.RowTemplateKey}}" SelectionHighlight="{Binding grid_settings_manager.selection_highlight, Mode=OneWay}" diff --git a/src/client/spark/core/Utility/CompareUtils.cs b/src/client/spark/core/Utility/CompareUtils.cs index 89ebe5f3e..369e19191 100644 --- a/src/client/spark/core/Utility/CompareUtils.cs +++ b/src/client/spark/core/Utility/CompareUtils.cs @@ -29,6 +29,12 @@ namespace mbts.utils { public static IComparer wrap(this IComparer inner_comparer, Func selector) => Comparer.Create((a, b) => inner_comparer.Compare(a == null ? default : selector(a), b == null ? default : selector(b))); + public static IComparer to_nullable(this IComparer comparer) where T : struct => + Comparer.Create(nullable_comparison(comparer.Compare)); + + public static IComparer reverse(this IComparer comparer) => + Comparer.Create((a, b) => comparer.Compare(b, a)); + class NonGenericComparerWrapper : IComparer { readonly IComparer _comparer; diff --git a/src/client/spark/core/Utility/ComparerBuilder.cs b/src/client/spark/core/Utility/ComparerBuilder.cs new file mode 100644 index 000000000..b8aaebc2f --- /dev/null +++ b/src/client/spark/core/Utility/ComparerBuilder.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; + +namespace mbts.utils { + class ComparerBuilder { + public readonly List> _comparers_list; + + public ComparerBuilder() => + _comparers_list = new List>(2); + + public ComparerBuilder(IEnumerable> comparers) => + _comparers_list = new List>(comparers); + + public void add(IComparer comparer) => + _comparers_list.Add(comparer); + + public void add(Func get) => + add(get, Comparer.Default); + + public void add(Func get, IComparer comparer) => + add(CompareUtils.wrap(comparer, get)); + + public void clear() => + _comparers_list.Clear(); + + public IComparer to_comparer() => new _Comparer(_comparers_list.ToArray()); + + class _Comparer : IComparer { + readonly IComparer[] _comparers; + + public _Comparer(IComparer[] comparers) => + _comparers = comparers; + + public int Compare(T x, T y) { + foreach (var comp in _comparers) { + int result = comp.Compare(x, y); + if (result != 0) + return result; + } + return 0; + } + } + } +} \ No newline at end of file diff --git a/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsManager.cs b/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsManager.cs index 004e61dd5..5a8077495 100644 --- a/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsManager.cs +++ b/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsManager.cs @@ -10,7 +10,6 @@ using mbts.utils; using System; using System.Collections.Generic; using System.Collections.ObjectModel; -using System.ComponentModel; using System.Linq; using System.Windows; using System.Windows.Media; @@ -22,14 +21,10 @@ namespace mbts.ui { where TRow : IRowVM where TPredicateBuilder : IPredicateBuilder, new() { // - public readonly ObservableCollection live_sorting_props; - readonly IBus _bus; readonly OrdersModel _orders_model; readonly INotificationService _notification_service; readonly List _resources, _notification_subs = new List(); - readonly ObservableCollection _rows; - readonly ICollectionView _rows_view; readonly ObservableCollection _columns; readonly Property _grid_settings; readonly DelegateProperty _font_family; @@ -41,15 +36,13 @@ namespace mbts.ui { IEnumerable> _predicate_animations_data; public GridSettingsManager( - IObservable owner_title, IBus bus, INotificationService notification_service, OrdersModel orders_model, ObservableCollection rows, - ICollectionView rows_view, ObservableCollection columns, Func> create_predicate_vm, + IObservable owner_title, IBus bus, INotificationService notification_service, OrdersModel orders_model, + ObservableCollection columns, Func> create_predicate_vm, Func> create_column_predicate_vm, TPredicateBuilderArg arg, IReadOnlyDictionary column_flags = null) { // _bus = bus; _notification_service = notification_service; _orders_model = orders_model; - _rows = rows; - _rows_view = rows_view; _columns = columns; _arg = arg; _grid_settings = backing(nameof(grid_settings), GridSettings.Default); @@ -59,8 +52,6 @@ namespace mbts.ui { _font_color = backing(nameof(font_color), () => grid_settings.font_color.rgb_solid_brush().frozen(), nameof(grid_settings)); _selection_highlight = backing(nameof(selection_highlight), () => grid_settings.selection_highlight.to_brush().frozen(), nameof(grid_settings)); _resources = new List { - ObservableAutoWrapper.connect( - rows_view.SortDescriptions, live_sorting_props = ((ICollectionViewLiveShaping)rows_view).LiveSortingProperties, sd => sd.PropertyName), (launch_settings_editor = new DialogCommand(create_settings_view)) }; Window create_settings_view() { @@ -78,7 +69,6 @@ namespace mbts.ui { vm.committed += () => { var (grid, predicate_animations_data) = vm.create(); // IMPORTANT: don't overwrite current sorting - grid = grid.with(sorting_criterion: rows_view.get_sorting_criteria(columns)); load_from((grid, predicate_animations_data)); }; vm.RequestClose += (s, e) => WindowUtilities.force_close(window); @@ -101,7 +91,7 @@ namespace mbts.ui { public (GridSettings grid, IEnumerable> predicate_animations_data) create() { // IMPORTANT: don't overwrite current sorting - _grid_settings.set(grid_settings.with(_columns.Select(c => c.create()), _rows_view.get_sorting_criteria(_columns))); + _grid_settings.set(grid_settings.with(_columns.Select(c => c.create()))); return (grid_settings, _predicate_animations_data); } @@ -111,7 +101,7 @@ namespace mbts.ui { _grid_settings.set(grid); foreach (ColumnBase col in _columns) col.set_fallback_font_params(new FontFamily(grid.font_family), grid.font_size, grid.font_color.rgb_color(), grid.font_weight); - _rows_view.set_sort_descriptions(grid.sorting_criterion, _columns); + //_rows_view.set_sort_descriptions(grid.sorting_criterion, _columns); _predicate_animations_data = value.predicate_animations_data; _animation_defs.clear(); foreach (var pa in _predicate_animations_data) { diff --git a/src/client/spark/view/Spark/Core/SparkUtils.cs b/src/client/spark/view/Spark/Core/SparkUtils.cs index 0aa748cfe..fb620cb3a 100644 --- a/src/client/spark/view/Spark/Core/SparkUtils.cs +++ b/src/client/spark/view/Spark/Core/SparkUtils.cs @@ -59,6 +59,12 @@ namespace mbts.spark { public static SecurityImmutable? security(this SecurityMD? security_md) => security_md.HasValue ? new SecurityImmutable?(security_md.Value.security) : new SecurityImmutable?(); + public static Venue exchange(this SecurityMD? security_md) => + security_md.HasValue ? security_md.Value.security.Exchange : Venue.Unset; + + public static string symbol(this SecurityMD? security_md) => + security_md.HasValue ? security_md.Value.security.Symbol : "unknown"; + public static async Task get_security_async(IBus bus, uint security_key) => await bus.subscribe(SparkTags.SecuritiesTag).Where(map => map.ContainsKey(security_key)).Select(map => map[security_key].security).FirstAsync(); diff --git a/src/client/spark_core/Collections/SortedList.cs b/src/client/spark_core/Collections/SortedList.cs index dd31ef0ff..2c30b41c4 100644 --- a/src/client/spark_core/Collections/SortedList.cs +++ b/src/client/spark_core/Collections/SortedList.cs @@ -24,6 +24,8 @@ namespace bts.collections { } } + public int Count => _size; + bool Contains(T item) { return BinarySearch(item) >= 0; } @@ -57,6 +59,16 @@ namespace bts.collections { return false; } + public void Clear() { + Array.Clear(_values, 0, _values.Length); + _size = 0; + } + + public bool TryGetIndex(T item, out int result) { + result = BinarySearch(item); + return result > 0; + } + public T this [int index] { get { return _values[index]; diff --git a/src/client/spark_core/Collections/SortedTree.cs b/src/client/spark_core/Collections/SortedTree.cs index d6db11772..f2eb4b7da 100644 --- a/src/client/spark_core/Collections/SortedTree.cs +++ b/src/client/spark_core/Collections/SortedTree.cs @@ -2,11 +2,14 @@ using System.Collections; using System.Collections.Generic; using System.Linq; -using System.Text; namespace bts.collections { + interface INode { + T value { get; } + IEnumerable> inner { get; } + } - public class SortedTree : IEnumerable { + class SortedTree : IEnumerable { readonly IComparer _comparer; readonly Func id; readonly Func parent; @@ -57,10 +60,20 @@ namespace bts.collections { } } } - public void Remove(T item) { - var item_id = id(item); + + public void Replace(TKey key, Func replace) { + Node node = _index[key]; + SortedList node_list = _index.TryGetValue(parent(node.value), out Node pnode) ? pnode.children : _tree; + node_list.Remove(node); + node.value = replace(node.value); + node_list.Add(node); + } + + public void Remove(T item) => Remove(id(item)); + + public void Remove(TKey item_id) { if (_index.TryGetValue(item_id, out Node itemnode)) { - if (_index.TryGetValue(parent(item), out Node parentnode)) { + if (_index.TryGetValue(parent(itemnode.value), out Node parentnode)) { parentnode.children.Remove(itemnode); } else { @@ -95,6 +108,19 @@ namespace bts.collections { } } + public void Clear() { + foreach (Node n in _tree) + _clear_node(n); + _tree.Clear(); + _index.Clear(); + + void _clear_node(Node n) { + foreach (Node cn in n.children) + _clear_node(cn); + n.children.Clear(); + } + } + IEnumerable iterate(SortedList nodes) { foreach (var n in nodes) { yield return n.value; @@ -114,14 +140,27 @@ namespace bts.collections { return null; } + public INode GetRootNode(TKey key) => + _index.TryGetValue(key, out Node node) ? GetRootNode(node) : null; + + Node GetRootNode(Node node) => + _index.TryGetValue(parent(node.value), out Node pnode) ? GetRootNode(pnode) : node; public IEnumerable EnumerateGroup(T item) { var pnode = parentmost(item); - if (pnode != null) { - yield return pnode.value; - foreach (var child in iterate(pnode.children)) { - yield return child; - } + return pnode == null ? Enumerable.Empty() : EnumerateNode(pnode); + } + + public IEnumerable EnumerateInnerValues(T item) => + iterate(_index[id(item)].children); + + public IEnumerable> EnumerateInnerNodes(T item) => + _index[id(item)].children; + + IEnumerable EnumerateNode(Node node) { + yield return node.value; + foreach (var child in iterate(node.children)) { + yield return child; } } public IEnumerator GetEnumerator() { @@ -131,10 +170,12 @@ namespace bts.collections { IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } - class Node { + class Node : INode { public Node(T i, IComparer c) { value = i; children = new SortedList(c); } public T value; public SortedList children; + T INode.value => value; + IEnumerable> INode.inner => children; } } } From 862e1994954005d86b1874c31c9ce795d597abc1 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Mon, 22 Oct 2018 12:02:37 -0500 Subject: [PATCH 05/46] make SortedTree/INode public (again) --- src/client/spark_core/Collections/SortedTree.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/client/spark_core/Collections/SortedTree.cs b/src/client/spark_core/Collections/SortedTree.cs index f2eb4b7da..e62bd6031 100644 --- a/src/client/spark_core/Collections/SortedTree.cs +++ b/src/client/spark_core/Collections/SortedTree.cs @@ -4,12 +4,12 @@ using System.Collections.Generic; using System.Linq; namespace bts.collections { - interface INode { + public interface INode { T value { get; } IEnumerable> inner { get; } } - class SortedTree : IEnumerable { + public class SortedTree : IEnumerable { readonly IComparer _comparer; readonly Func id; readonly Func parent; From 6c03a263a1cfabd794166fa2b827c7855ee99a3b Mon Sep 17 00:00:00 2001 From: John Colvin Date: Mon, 22 Oct 2018 12:27:18 -0500 Subject: [PATCH 06/46] OrderBookRowCollection - create new SortedTree on reset, rather than Clearing current --- .../Trading/OrderBook/OrderBookRowCollection.cs | 10 ++++++---- src/client/spark_core/Collections/SortedTree.cs | 2 ++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/client/spark/Modules/Trading/OrderBook/OrderBookRowCollection.cs b/src/client/spark/Modules/Trading/OrderBook/OrderBookRowCollection.cs index ffd8d1006..501ac078d 100644 --- a/src/client/spark/Modules/Trading/OrderBook/OrderBookRowCollection.cs +++ b/src/client/spark/Modules/Trading/OrderBook/OrderBookRowCollection.cs @@ -20,7 +20,7 @@ namespace mbts.spark { public OrderBookRowCollection(Func row_factory, IComparer comparer) { _row_factory = row_factory; - _tree = new SortedTree(_comparer = comparer, r => r.order_key, r => r.parent_order_key); + _tree = _mk_tree(_comparer = comparer); } public Func filter { get => _filter; set { _filter = value; refresh(); } } @@ -65,9 +65,8 @@ namespace mbts.spark { IEnumerable> impacted_branch_roots; if (table.removed.Count() == _last_row_count) { // full refresh (most likely this is the first update or a server restart) - _tree.Clear(); - _tree.AddAll(table.added.Select(_row_factory)); - impacted_branch_roots = table.added.Select(ort => _tree.GetRootNode(ort.order.Key)).Distinct(_node_equality_comparer); + _tree = _mk_tree(_comparer, table.added.Select(_row_factory)); + impacted_branch_roots = _tree.RootNodes; } else { /// The update proceedure, broken down into steps, will be executed in the following order: /// 1) Removes - independent of filter, as is idempotent and cheap @@ -125,6 +124,9 @@ namespace mbts.spark { }); } + static SortedTree _mk_tree(IComparer comp, IEnumerable init = null) => + new SortedTree(comp, r => r.order_key, r => r.parent_order_key, init); + void _refresh_tree_properties(int depth_level, bool is_visible, INode node) { node.value.depth_level = depth_level; node.value.is_visible = is_visible; diff --git a/src/client/spark_core/Collections/SortedTree.cs b/src/client/spark_core/Collections/SortedTree.cs index e62bd6031..21beccb70 100644 --- a/src/client/spark_core/Collections/SortedTree.cs +++ b/src/client/spark_core/Collections/SortedTree.cs @@ -28,6 +28,8 @@ namespace bts.collections { } } + public IEnumerable> RootNodes => _tree; + // Insert (return: true) or overwrite (return false) a new value // NOTE: you should use AddAll if you are adding multiple values public bool Insert(T item) { From f90961ce22667bbb1ae90410dd5640766b246e24 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Tue, 23 Oct 2018 18:19:27 -0500 Subject: [PATCH 07/46] SortedTree - add nursery-dictionary for items without parents --- .../spark_core/Collections/SortedTree.cs | 162 +++++++++++++----- 1 file changed, 115 insertions(+), 47 deletions(-) diff --git a/src/client/spark_core/Collections/SortedTree.cs b/src/client/spark_core/Collections/SortedTree.cs index 21beccb70..1b8f3caf0 100644 --- a/src/client/spark_core/Collections/SortedTree.cs +++ b/src/client/spark_core/Collections/SortedTree.cs @@ -9,20 +9,25 @@ namespace bts.collections { IEnumerable> inner { get; } } - public class SortedTree : IEnumerable { + public class SortedTree : IEnumerable where TKey : IEquatable { readonly IComparer _comparer; readonly Func id; readonly Func parent; + readonly TKey _root_id; Dictionary _index = new Dictionary(); + Dictionary> _nursery = new Dictionary>(); SortedList _tree; + public SortedTree(IComparer comparer, Func fnid, Func fnparent, IEnumerable items = null) + : this(comparer, fnid, fnparent, default, items) { } - public SortedTree(IComparer comparer, Func fnid, Func fnparent, IEnumerable items=null) { + public SortedTree(IComparer comparer, Func fnid, Func fnparent, TKey root_id, IEnumerable items=null) { _comparer = Comparer.Create((l, r) => comparer.Compare(l.value, r.value)); id = fnid; parent = fnparent; _tree = new SortedList(_comparer); + _root_id = root_id; if (items != null) { AddAll(items); } @@ -33,55 +38,47 @@ namespace bts.collections { // Insert (return: true) or overwrite (return false) a new value // NOTE: you should use AddAll if you are adding multiple values public bool Insert(T item) { - var item_id = id(item); - if (!_index.TryGetValue(item_id, out Node itemnode)) { - // Item is new - itemnode = new Node(item, _comparer); - _index[item_id] = itemnode; - if (_index.TryGetValue(parent(item), out Node pnode)) { - pnode.children.Add(itemnode); - } - else { - _tree.Add(itemnode); + var k = id(item); + if (_index.TryGetValue(k, out Node node)) { + _replace(node, item); + return false; + } else { + var pk = parent(item); + if (_nursery.TryGetValue(k, out SortedList nursery_list)) { + _nursery.Remove(k); + _add_node(pk, node = new Node(item, nursery_list)); + } else { + _add_node(pk, node = new Node(item, _comparer)); } + _index.Add(k, node); return true; } - else { - // Item exists - if (!parent(itemnode.value).Equals(parent(item))) { - // Item has a new parent - // Remove it entirely and re-insert at a new tree position - Remove(item); - Insert(item); - return false; - } - else { - // Item has same parent. Just overwrite in place - itemnode.value = item; - return false; - } - } } - public void Replace(TKey key, Func replace) { - Node node = _index[key]; - SortedList node_list = _index.TryGetValue(parent(node.value), out Node pnode) ? pnode.children : _tree; - node_list.Remove(node); - node.value = replace(node.value); - node_list.Add(node); + public void Replace(TKey k, Func replace) { + if (!_index.TryGetValue(k, out Node node)) + throw new KeyNotFoundException($"Replace was called with unknown key ({k})."); + var newval = replace(node.value); + Log.assert(id(newval).Equals(k)); + _replace(node, newval); } + public void Remove(T item) => Remove(id(item)); - public void Remove(TKey item_id) { - if (_index.TryGetValue(item_id, out Node itemnode)) { - if (_index.TryGetValue(parent(itemnode.value), out Node parentnode)) { - parentnode.children.Remove(itemnode); - } - else { - _tree.Remove(itemnode); - } - _index.Remove(item_id); + // this is NOT idempotent, and will throw an exception if the Key is not present. + // (if we decide to change it to be idempotent, we should return a bool to indicate whether or not the remove succeeded) + public void Remove(TKey k) { + // must be in the index, bc this is a remove + var node = _index[k]; + _index.Remove(k); + var pk = parent(node.value); + if (pk.Equals(_root_id)) { + _tree.Remove(node); + } else if (_index.TryGetValue(pk, out Node pnode)) { + pnode.children.Remove(node); + } else { + _remove_from_nursery(pk, node); } } @@ -112,17 +109,76 @@ namespace bts.collections { public void Clear() { foreach (Node n in _tree) - _clear_node(n); + clear_node(n); + foreach (var nl in _nursery.Values) { + foreach (var n in nl) + clear_node(n); + nl.Clear(); + } _tree.Clear(); _index.Clear(); + _nursery.Clear(); - void _clear_node(Node n) { + void clear_node(Node n) { foreach (Node cn in n.children) - _clear_node(cn); + clear_node(cn); n.children.Clear(); } } + void _replace(Node node, T newval) { + TKey pk = parent(newval); + TKey old_pk = parent(node.value); + if (pk.Equals(old_pk)) { + SortedList node_list; + if (pk.Equals(_root_id)) + node_list = _tree; + else if (_index.TryGetValue(pk, out Node pnode)) { + node_list = pnode.children; + } else if (!_nursery.TryGetValue(pk, out node_list)) { + throw new KeyNotFoundException($"Replace was called with a parent-key ({pk}) that was not found in _index or _nursery; item-key was ({id(newval)})."); + } + //var node_list = pk.Equals(_root_id) ? _tree : _index.TryGetValue(pk, out Node pnode) ? pnode.children : _nursery[pk]; + node_list.Remove(node); + node.value = newval; + node_list.Add(node); + } else { + if (old_pk.Equals(_root_id)) { + _tree.Remove(node); + } else { + if (_index.TryGetValue(old_pk, out Node pnode)) + pnode.children.Remove(node); + else { + _remove_from_nursery(old_pk, node); + } + node.value = newval; + _add_node(pk, node); + } + } + } + + void _remove_from_nursery(TKey pk, Node node) { + var nursery_list = _nursery[pk]; + nursery_list.Remove(node); + if (nursery_list.Count == 0) { + // no need to keep the nursey_list around anymore; remove it + _nursery.Remove(pk); + } + } + + void _add_node(TKey pk, Node node) { + if (pk.Equals(_root_id)) { + _tree.Add(node); + } else if (_index.TryGetValue(pk, out Node pnode)) { + pnode.children.Add(node); + } else { + if (!_nursery.TryGetValue(pk, out SortedList nursery_list)) { + _nursery.Add(pk, nursery_list = new SortedList(_comparer)); + } + nursery_list.Add(node); + } + } + IEnumerable iterate(SortedList nodes) { foreach (var n in nodes) { yield return n.value; @@ -166,14 +222,26 @@ namespace bts.collections { } } public IEnumerator GetEnumerator() { - return iterate(_tree).GetEnumerator(); + foreach (T item in iterate(_tree)) + yield return item; + foreach (var kv in _nursery) { + foreach (T item in iterate(kv.Value)) + yield return item; + } } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } class Node : INode { - public Node(T i, IComparer c) { value = i; children = new SortedList(c); } + public Node(T i, IComparer c) + : this(i, new SortedList(c)) { } + + public Node(T i, SortedList children) { + value = i; + this.children = children; + } + public T value; public SortedList children; T INode.value => value; From 95a7f7ccb64c037eecbd63d2823a9ad21efafc5b Mon Sep 17 00:00:00 2001 From: John Colvin Date: Tue, 23 Oct 2018 18:27:29 -0500 Subject: [PATCH 08/46] OrderBookRowCollection - show whole branch if any order passes filter --- .../OrderBook/OrderBookRowCollection.cs | 87 +++++++------------ 1 file changed, 31 insertions(+), 56 deletions(-) diff --git a/src/client/spark/Modules/Trading/OrderBook/OrderBookRowCollection.cs b/src/client/spark/Modules/Trading/OrderBook/OrderBookRowCollection.cs index 501ac078d..d3c326fec 100644 --- a/src/client/spark/Modules/Trading/OrderBook/OrderBookRowCollection.cs +++ b/src/client/spark/Modules/Trading/OrderBook/OrderBookRowCollection.cs @@ -23,15 +23,10 @@ namespace mbts.spark { _tree = _mk_tree(_comparer = comparer); } - public Func filter { get => _filter; set { _filter = value; refresh(); } } + public Func filter { get => _filter; set { _filter = value; _refresh_branches(_tree.RootNodes); refresh(); } } public IComparer comparer { get => _comparer; set { _tree = _tree.Resort(_comparer = value); refresh(); } } public IEnumerable all_rows => _tree; - public IEnumerable _visible_rows => - filter == null ? - _tree.Where(r => r.is_visible) : - _tree.Where(r => r.is_visible && filter(r.order_tuple)); - bool _is_refresh_deferred => _deferment_count > 0; public IEnumerable> get_inner_nodes(OrderBookRowVM row) => @@ -45,7 +40,7 @@ namespace mbts.spark { _is_refresh_pending = true; } else { _is_refresh_pending = false; - this.sync_with(_visible_rows); + this.sync_with(_tree.Where(r => r.is_visible)); } } @@ -68,39 +63,15 @@ namespace mbts.spark { _tree = _mk_tree(_comparer, table.added.Select(_row_factory)); impacted_branch_roots = _tree.RootNodes; } else { - /// The update proceedure, broken down into steps, will be executed in the following order: - /// 1) Removes - independent of filter, as is idempotent and cheap - /// 2) Changes - /// 3) Adds - - // 1) Removes foreach (OrderRecordTuple ort in table.removed) _tree.Remove(ort.order.Key); - if (filter == null) { - // 2) Changes (unfiltered) - foreach (Change change in table.changes) - change_row(change.New); - // 3) Adds (unfiltered) - foreach (OrderRecordTuple ort in table.added) { - _tree.Insert(_row_factory(ort)); - } - } else { - // 2) Changes (filtered) - foreach (Change change in table.changes) { - if (filter(change.Old)) { - if (filter(change.New)) { - change_row(change.New); - } else { - _tree.Remove(change.Old.order.Key); - } - } else if (filter(change.New)) { - _tree.Insert(_row_factory(change.New)); - } - } - // 3 Adds (filtered) - foreach (OrderRecordTuple item in table.added.Where(filter)) - _tree.Insert(_row_factory(item)); - } + foreach (Change change in table.changes) + _tree.Replace(change.New.order.Key, r => { + r.update(change.New); + return r; + }); + foreach (OrderRecordTuple ort in table.added) + _tree.Insert(_row_factory(ort)); impacted_branch_roots = table.added.Concat(table.changed).Select(ort => ort.order.Key) .Concat( @@ -112,32 +83,36 @@ namespace mbts.spark { .Distinct(_node_equality_comparer); } _last_row_count += table.added.Count() - table.removed.Count(); - foreach (var n in impacted_branch_roots) { - _refresh_tree_properties(0, true, n); - } + _refresh_branches(impacted_branch_roots); refresh(); - // local methods - void change_row(OrderRecordTuple newval) => - _tree.Replace(newval.order.Key, r => { - r.update(newval); - return r; - }); } static SortedTree _mk_tree(IComparer comp, IEnumerable init = null) => new SortedTree(comp, r => r.order_key, r => r.parent_order_key, init); - void _refresh_tree_properties(int depth_level, bool is_visible, INode node) { - node.value.depth_level = depth_level; - node.value.is_visible = is_visible; - using (var en = node.inner.GetEnumerator()) { - if (en.MoveNext()) { - node.value.is_parent = true; - depth_level++; - do _refresh_tree_properties(depth_level, node.value.is_visible && node.value.is_expanded, en.Current); - while (en.MoveNext()); + void _refresh_branches(IEnumerable> roots) { + if (filter == null) { + foreach (var n in roots) + refresh_tree_properties(0, true, n); + } else { + foreach (var n in roots) + refresh_tree_properties(0, get_branch_passes_filter(n), n); + } + // local methods + void refresh_tree_properties(int depth_level, bool is_visible, INode node) { + node.value.depth_level = depth_level; + node.value.is_visible = is_visible; + using (var en = node.inner.GetEnumerator()) { + if (en.MoveNext()) { + node.value.is_parent = true; + depth_level++; + do refresh_tree_properties(depth_level, node.value.is_visible && node.value.is_expanded, en.Current); + while (en.MoveNext()); + } } } + bool get_branch_passes_filter(INode n) => + filter(n.value.order_tuple) || n.inner.Any(get_branch_passes_filter); } } } \ No newline at end of file From ba7a28aa8aaa1a7656560560fceb030a10b75815 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Tue, 23 Oct 2018 18:32:07 -0500 Subject: [PATCH 09/46] SPARK-1806 orderbook-performance crash --- .../Trading/OrderBook/OrderBookRowCollection.cs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/client/spark/Modules/Trading/OrderBook/OrderBookRowCollection.cs b/src/client/spark/Modules/Trading/OrderBook/OrderBookRowCollection.cs index d3c326fec..22584fc54 100644 --- a/src/client/spark/Modules/Trading/OrderBook/OrderBookRowCollection.cs +++ b/src/client/spark/Modules/Trading/OrderBook/OrderBookRowCollection.cs @@ -73,14 +73,15 @@ namespace mbts.spark { foreach (OrderRecordTuple ort in table.added) _tree.Insert(_row_factory(ort)); impacted_branch_roots = - table.added.Concat(table.changed).Select(ort => ort.order.Key) - .Concat( - table.removed - .Where(ort => ort.order.has_parent()) - .Select(ort => ort.order.ParentKey) - .Distinct() // since these are ParentKeys, there could be duplicates. - ).Select(_tree.GetRootNode) - .Distinct(_node_equality_comparer); + table.added.Concat(table.changed).Select(ort => ort.order.Key).Select(_tree.GetRootNode) + .Concat( + table.removed + .Where(ort => ort.order.has_parent()) + .Select(ort => ort.order.ParentKey) + .Distinct() // since these are ParentKeys, there could be duplicates. + .Select(_tree.GetRootNode) + .Where(n => n != null) + ).Distinct(_node_equality_comparer); } _last_row_count += table.added.Count() - table.removed.Count(); _refresh_branches(impacted_branch_roots); From c127f488ff7b461d48f9da7b55b92bd5a820cb4d Mon Sep 17 00:00:00 2001 From: John Colvin Date: Tue, 23 Oct 2018 20:37:04 -0500 Subject: [PATCH 10/46] OrderBookRowCollection - when sorting changes, call clear/addall instead of sync_with --- .../OrderBook/OrderBookRowCollection.cs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/client/spark/Modules/Trading/OrderBook/OrderBookRowCollection.cs b/src/client/spark/Modules/Trading/OrderBook/OrderBookRowCollection.cs index 22584fc54..406fcc403 100644 --- a/src/client/spark/Modules/Trading/OrderBook/OrderBookRowCollection.cs +++ b/src/client/spark/Modules/Trading/OrderBook/OrderBookRowCollection.cs @@ -23,8 +23,22 @@ namespace mbts.spark { _tree = _mk_tree(_comparer = comparer); } - public Func filter { get => _filter; set { _filter = value; _refresh_branches(_tree.RootNodes); refresh(); } } - public IComparer comparer { get => _comparer; set { _tree = _tree.Resort(_comparer = value); refresh(); } } + public Func filter { + get => _filter; + set { + _filter = value; + _refresh_branches(_tree.RootNodes); + refresh(); + } + } + public IComparer comparer { + get => _comparer; + set { + _tree = _tree.Resort(_comparer = value); + Clear(); + this.AddRange(_tree.Where(r => r.is_visible)); + } + } public IEnumerable all_rows => _tree; bool _is_refresh_deferred => _deferment_count > 0; From 94f3c5a6c45e6c87cd8728525de9fdf45c989b73 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Wed, 24 Oct 2018 11:30:38 -0500 Subject: [PATCH 11/46] OrderBookRowCollection - when comparer changes, don't reset if refresh is deferred --- .../Trading/OrderBook/OrderBookRowCollection.cs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/client/spark/Modules/Trading/OrderBook/OrderBookRowCollection.cs b/src/client/spark/Modules/Trading/OrderBook/OrderBookRowCollection.cs index 406fcc403..3d1cc4e87 100644 --- a/src/client/spark/Modules/Trading/OrderBook/OrderBookRowCollection.cs +++ b/src/client/spark/Modules/Trading/OrderBook/OrderBookRowCollection.cs @@ -35,11 +35,16 @@ namespace mbts.spark { get => _comparer; set { _tree = _tree.Resort(_comparer = value); - Clear(); - this.AddRange(_tree.Where(r => r.is_visible)); + if (_is_refresh_deferred) { + _is_refresh_pending = true; + } else { + Clear(); + this.AddRange(_visible_rows); + } } } public IEnumerable all_rows => _tree; + IEnumerable _visible_rows => _tree.Where(r => r.is_visible); bool _is_refresh_deferred => _deferment_count > 0; @@ -54,7 +59,7 @@ namespace mbts.spark { _is_refresh_pending = true; } else { _is_refresh_pending = false; - this.sync_with(_tree.Where(r => r.is_visible)); + this.sync_with(_visible_rows); } } From ae100f66c7baf26b3eadf9ec5956c205ad629c7b Mon Sep 17 00:00:00 2001 From: John Colvin Date: Wed, 24 Oct 2018 19:04:30 -0500 Subject: [PATCH 12/46] OrderBookRowCollection - optimize expand/collapse all (avoid noisy intermittent collection changes) --- .../OrderBook/OrderBookRowCollection.cs | 49 +++++++++++++++++++ .../Trading/OrderBook/OrderBookRowVM.cs | 5 ++ .../Modules/Trading/OrderBook/OrderBookVM.cs | 13 +---- .../spark_core/Collections/SortedTree.cs | 10 ++++ 4 files changed, 66 insertions(+), 11 deletions(-) diff --git a/src/client/spark/Modules/Trading/OrderBook/OrderBookRowCollection.cs b/src/client/spark/Modules/Trading/OrderBook/OrderBookRowCollection.cs index 3d1cc4e87..d60525235 100644 --- a/src/client/spark/Modules/Trading/OrderBook/OrderBookRowCollection.cs +++ b/src/client/spark/Modules/Trading/OrderBook/OrderBookRowCollection.cs @@ -54,6 +54,55 @@ namespace mbts.spark { public IEnumerable get_descendent_rows(OrderBookRowVM row) => _tree.EnumerateInnerValues(row); + public void collapse_all() { + foreach (var n in _tree.RootNodes) { + collapse_node(n); + } + if (_is_refresh_deferred) { + _is_refresh_pending = true; + } else { + Clear(); + foreach (var n in _tree.RootNodes) { + if (n.value.is_visible) + Add(n.value); + } + } + // local methods + void collapse_node(INode node) { + node.value.collapse_internal(); + foreach (var cn in node.inner) { + cn.value.is_visible = false; + collapse_node(cn); + } + } + } + + public void expand_all() { + foreach (var n in _tree.RootNodes) { + expand_node(n); + } + if (_is_refresh_deferred) { + _is_refresh_pending = true; + } else { + Clear(); + foreach (var n in _tree.RootNodes) { + if (n.value.is_visible) { + Add(n.value); + foreach (var r in n.inner_values()) + Add(r); + } + } + } + // local methods + void expand_node(INode node) { + node.value.expand_internal(); + foreach (var cn in node.inner) { + cn.value.is_visible = node.value.is_visible; + expand_node(cn); + } + } + } + public void refresh() { if (_is_refresh_deferred) { _is_refresh_pending = true; diff --git a/src/client/spark/Modules/Trading/OrderBook/OrderBookRowVM.cs b/src/client/spark/Modules/Trading/OrderBook/OrderBookRowVM.cs index 30c4559cf..dbcd9cdc7 100644 --- a/src/client/spark/Modules/Trading/OrderBook/OrderBookRowVM.cs +++ b/src/client/spark/Modules/Trading/OrderBook/OrderBookRowVM.cs @@ -79,6 +79,11 @@ namespace mbts.spark { ICommand DIYGrid.IActionableRowVM.default_command => _show_order_modify_cmd ?? (_show_order_modify_cmd = new DelegateCommand(() => _owner.show_order_modify(this), () => status == OrderStatus.Working)); + // These fns present a way for the orderbook to avoid noisy, intermittent collection changes during "Expand/Collapse All" ops, + // when incremental changes are ignored in favor of a full reset, after 'is_expanded' is updated for all rows. + internal void expand_internal() => Set(ref _is_expanded, true); + internal void collapse_internal() => Set(ref _is_expanded, false); + public void update(OrderRecordTuple order_tuple) { Log.assert(order_tuple.order.Key == this.order.Key, "OrderBookRowVM: update_order was called with an order with the wrong key."); bool had_security = _security_md.HasValue; diff --git a/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs b/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs index 528e8d7e5..1eae3cc13 100644 --- a/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs +++ b/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs @@ -37,8 +37,8 @@ namespace mbts.spark { _row_collection = new OrderBookRowCollection(ort => new OrderBookRowVM(this, ort), _columns.get_comparer_data().comp) { filter = _columns.get_filter() }; _current_row = backing(nameof(current_row)); _columns.filter_changed += on_filter_changed; - collapse_all_rows_command = new DelegateCommand(() => set_all_is_expanded(false)); - expand_all_rows_command = new DelegateCommand(() => set_all_is_expanded(true)); + collapse_all_rows_command = new DelegateCommand(_row_collection.collapse_all); + expand_all_rows_command = new DelegateCommand(_row_collection.expand_all); _resources = new List { model.on_order_tuples_table.OnDispatcher().Subscribe(_row_collection.update), (grid_settings_manager = _create_grid_settings_manager(owner_title, bus, notification_service, model, columns)), @@ -48,15 +48,6 @@ namespace mbts.spark { }), _columns }; - // local methods - void set_all_is_expanded(bool is_expanded) { - using (_row_collection.defer_refresh()) { - foreach (var r in _row_collection.all_rows) { - r.is_expanded = is_expanded; - r.is_visible = is_expanded || !r.is_child; - } - } - } void on_filter_changed() => _row_collection.filter = _columns.get_filter(); } diff --git a/src/client/spark_core/Collections/SortedTree.cs b/src/client/spark_core/Collections/SortedTree.cs index 1b8f3caf0..d5b0686b2 100644 --- a/src/client/spark_core/Collections/SortedTree.cs +++ b/src/client/spark_core/Collections/SortedTree.cs @@ -9,6 +9,16 @@ namespace bts.collections { IEnumerable> inner { get; } } + public static class NodeExtensions { + public static IEnumerable inner_values(this INode node) { + foreach (var n in node.inner) { + yield return n.value; + foreach (var v in n.inner_values()) + yield return v; + } + } + } + public class SortedTree : IEnumerable where TKey : IEquatable { readonly IComparer _comparer; readonly Func id; From 079a7ad439216141a35b69715c07e6f404b05dfd Mon Sep 17 00:00:00 2001 From: John Colvin Date: Wed, 24 Oct 2018 22:33:43 -0500 Subject: [PATCH 13/46] OrderBookRowCollection - when filter changes, call clear/addall instead of sync_with --- .../OrderBook/OrderBookRowCollection.cs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/client/spark/Modules/Trading/OrderBook/OrderBookRowCollection.cs b/src/client/spark/Modules/Trading/OrderBook/OrderBookRowCollection.cs index d60525235..33a90e7fd 100644 --- a/src/client/spark/Modules/Trading/OrderBook/OrderBookRowCollection.cs +++ b/src/client/spark/Modules/Trading/OrderBook/OrderBookRowCollection.cs @@ -28,19 +28,14 @@ namespace mbts.spark { set { _filter = value; _refresh_branches(_tree.RootNodes); - refresh(); + _reset(); } } public IComparer comparer { get => _comparer; set { _tree = _tree.Resort(_comparer = value); - if (_is_refresh_deferred) { - _is_refresh_pending = true; - } else { - Clear(); - this.AddRange(_visible_rows); - } + _reset(); } } public IEnumerable all_rows => _tree; @@ -159,6 +154,16 @@ namespace mbts.spark { static SortedTree _mk_tree(IComparer comp, IEnumerable init = null) => new SortedTree(comp, r => r.order_key, r => r.parent_order_key, init); + void _reset() { + if (_is_refresh_deferred) { + _is_refresh_pending = true; + } else { + Clear(); + this.AddRange(_visible_rows); + _is_refresh_pending = false; + } + } + void _refresh_branches(IEnumerable> roots) { if (filter == null) { foreach (var n in roots) From 7620b7878e7e621739cc3bd1f19fb6cf263f90c6 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Thu, 25 Oct 2018 13:27:37 -0500 Subject: [PATCH 14/46] UInt/Double min-max filters implement IEquatable --- .../Filters/VM/DoubleMinMaxFilterVM.cs | 24 ++++++++++++++++--- .../Filters/VM/UIntMinMaxFilterVM.cs | 24 ++++++++++++++++--- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/src/client/spark/view/Controls/DataGridEx/Filters/VM/DoubleMinMaxFilterVM.cs b/src/client/spark/view/Controls/DataGridEx/Filters/VM/DoubleMinMaxFilterVM.cs index 493acf1dc..52d5da971 100644 --- a/src/client/spark/view/Controls/DataGridEx/Filters/VM/DoubleMinMaxFilterVM.cs +++ b/src/client/spark/view/Controls/DataGridEx/Filters/VM/DoubleMinMaxFilterVM.cs @@ -1,4 +1,5 @@ -using ProtoBuf; +using bts.utils; +using ProtoBuf; using System; namespace mbts.spark.filters { @@ -8,7 +9,7 @@ namespace mbts.spark.filters { } [ProtoContract] - class DoubleStatus : INullableStructProto { + class DoubleStatus : INullableStructProto, IEquatable { [ProtoMember(1)] bool _active; [ProtoMember(2)] @@ -19,10 +20,19 @@ namespace mbts.spark.filters { get => _active ? new double?(_value) : new double?(); set { if (_active = value.HasValue && !double.IsNaN(value.Value)) _value = value.Value; } } + + public bool Equals(DoubleStatus other) => + other != null && + _active == other._active && + _value == other._value; + + public override bool Equals(object obj) => Equals(obj as DoubleStatus); + public override int GetHashCode() => EqualityComparer.aggregate_hash(_active.GetHashCode(), _value.GetHashCode()); + public override string ToString() => _active ? _value.ToString() : $"({_value})"; } [ProtoContract] - class DoubleMinMaxState : IMinMaxState { + class DoubleMinMaxState : IMinMaxState, IEquatable { [ProtoMember(1)] readonly DoubleStatus _minimum; [ProtoMember(2)] @@ -35,5 +45,13 @@ namespace mbts.spark.filters { public INullableStructProto minimum => _minimum; public INullableStructProto maximum => _maximum; + + public bool Equals(DoubleMinMaxState other) => + other != null && + _minimum.safe_equals(other._minimum) && + _maximum.safe_equals(other._maximum); + public override bool Equals(object obj) => Equals(obj as DoubleMinMaxState); + public override int GetHashCode() => EqualityComparer.aggregate_hash(_minimum?.GetHashCode() ?? 0, _maximum?.GetHashCode() ?? 0); + public override string ToString() => $"Min:{_minimum}, Max:{_maximum}"; } } \ No newline at end of file diff --git a/src/client/spark/view/Controls/DataGridEx/Filters/VM/UIntMinMaxFilterVM.cs b/src/client/spark/view/Controls/DataGridEx/Filters/VM/UIntMinMaxFilterVM.cs index 18aa2433e..ae614a2f5 100644 --- a/src/client/spark/view/Controls/DataGridEx/Filters/VM/UIntMinMaxFilterVM.cs +++ b/src/client/spark/view/Controls/DataGridEx/Filters/VM/UIntMinMaxFilterVM.cs @@ -1,4 +1,5 @@ -using ProtoBuf; +using bts.utils; +using ProtoBuf; using System; namespace mbts.spark.filters { @@ -8,7 +9,7 @@ namespace mbts.spark.filters { } [ProtoContract] - class UIntStatus : INullableStructProto { + class UIntStatus : INullableStructProto, IEquatable { [ProtoMember(1)] bool _active; [ProtoMember(2)] @@ -19,10 +20,19 @@ namespace mbts.spark.filters { get => _active ? new uint?(_value) : new uint?(); set { if (_active = value.HasValue) _value = value.Value; } } + + public bool Equals(UIntStatus other) => + other != null && + _active == other._active && + _value == other._value; + + public override bool Equals(object obj) => Equals(obj as UIntStatus); + public override int GetHashCode() => EqualityComparer.aggregate_hash(_active.GetHashCode(), _value.GetHashCode()); + public override string ToString() => _active ? _value.ToString() : $"({_value})"; } [ProtoContract] - class UIntMinMaxState : IMinMaxState { + class UIntMinMaxState : IMinMaxState, IEquatable { [ProtoMember(1)] readonly UIntStatus _minimum; [ProtoMember(2)] @@ -35,5 +45,13 @@ namespace mbts.spark.filters { public INullableStructProto minimum => _minimum; public INullableStructProto maximum => _maximum; + + public bool Equals(UIntMinMaxState other) => + other != null && + _minimum.safe_equals(other._minimum) && + _maximum.safe_equals(other._maximum); + public override bool Equals(object obj) => Equals(obj as UIntMinMaxState); + public override int GetHashCode() => EqualityComparer.aggregate_hash(_minimum?.GetHashCode() ?? 0, _maximum?.GetHashCode() ?? 0); + public override string ToString() => $"Min:{_minimum}, Max:{_maximum}"; } } \ No newline at end of file From dfe480d1f7b2b275fe064faf42e4712dc36a0850 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Thu, 25 Oct 2018 13:28:40 -0500 Subject: [PATCH 15/46] OrderBookStartupData.FilterStates implement IEquatable --- .../OrderBook/OrderBookWindowFactory.cs | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/client/spark/Modules/Trading/OrderBook/OrderBookWindowFactory.cs b/src/client/spark/Modules/Trading/OrderBook/OrderBookWindowFactory.cs index c3037472b..5ba1434b3 100644 --- a/src/client/spark/Modules/Trading/OrderBook/OrderBookWindowFactory.cs +++ b/src/client/spark/Modules/Trading/OrderBook/OrderBookWindowFactory.cs @@ -1,6 +1,7 @@ using bts.api; using bts.api.orderman; using bts.spark; +using bts.utils; using Google.Protobuf.Collections; using mbts; using mbts.spark; @@ -129,7 +130,7 @@ namespace bts.shell { } [ProtoContract] - public class FilterStates { + public class FilterStates : IEquatable { [ProtoMember(1)] public SecurityCriteria security_criteria; [ProtoMember(2)] @@ -170,6 +171,28 @@ namespace bts.shell { public api.orderman.OrderSubType[] order_sub_types; [ProtoMember(20)] public string[] symbols; + + public bool Equals(FilterStates other) => + other != null && + security_criteria.safe_equals(other.security_criteria) && + order_key_regex == other.order_key_regex && + time_created.Equals(other.time_created) && + EqualityComparer.safe_sequence_equals(users, other.users) && + EqualityComparer.safe_sequence_equals(accounts, other.accounts) && + EqualityComparer.safe_sequence_equals(exchanges, other.exchanges) && + EqualityComparer.safe_sequence_equals(tifs, other.tifs) && + EqualityComparer.safe_sequence_equals(order_types, other.order_types) && + EqualityComparer.safe_sequence_equals(sides, other.sides) && + qty.safe_equals(other.qty) && + price.safe_equals(other.price) && + qty_filled.safe_equals(other.qty_filled) && + avg_price.safe_equals(other.avg_price) && + last_price.safe_equals(other.last_price) && + EqualityComparer.safe_sequence_equals(statuses, other.statuses) && + exchange_message_regex == other.exchange_message_regex && + last_change.Equals(other.last_change) && + EqualityComparer.safe_sequence_equals(order_sub_types, other.order_sub_types) && + EqualityComparer.safe_sequence_equals(symbols, other.symbols); } } } \ No newline at end of file From c19aad55a7b6dde35c1944e181998e6f2f8c6dec Mon Sep 17 00:00:00 2001 From: John Colvin Date: Thu, 25 Oct 2018 15:57:09 -0500 Subject: [PATCH 16/46] OrderBook - load/save filters and sorting --- .../Trading/OrderBook/OrderBookColumns.cs | 66 +++++++++++++------ .../OrderBook/OrderBookRowCollection.cs | 30 +++++---- .../Modules/Trading/OrderBook/OrderBookVM.cs | 42 ++++++++---- .../spark/view/Utility/SortingCriteria.cs | 2 + .../spark_core/utils/GlobalExtensions.cs | 3 + 5 files changed, 99 insertions(+), 44 deletions(-) diff --git a/src/client/spark/Modules/Trading/OrderBook/OrderBookColumns.cs b/src/client/spark/Modules/Trading/OrderBook/OrderBookColumns.cs index 0a1448d66..81dd3d1cc 100644 --- a/src/client/spark/Modules/Trading/OrderBook/OrderBookColumns.cs +++ b/src/client/spark/Modules/Trading/OrderBook/OrderBookColumns.cs @@ -26,16 +26,6 @@ using OrderType = bts.api.orderman.CustomOrderDefinition.CustomOneofCase; namespace mbts.spark { // This interface is used to hide ColumnBase.SortDirection, and offer a SortDirection member that does NOT require a DependencyProperty lookup. - // In OrderBookRowVM, each "cell(property)" has a corresponding TreeComparable property that the column uses for sorting - // - each columns' sort_member_path points to the TreeComparable. - // The TreeComparable class needs to know which direction it is sorting so it can force child-orders to always be beneath their parents. - // - when comparing with their parent, it must return 1 if Ascending, -1 if Descending. - // Since the TreeComparables in each row must access their respective column SortDirections in their CompareTo methods, those DP lookups could add significant overhead in large collections. - // I considered adding ListSortDirection members directly to OrderBookVM, and letting the TreeComparables refer to that property, - // but decided it would be better to let the Columns own it so we wouldn't have to worry about keeping the 2 props in sync. - // TODO: Maybe binding to the SortDirection would be less smelly? - // - initially, I tried to bind to the SortDirection of each column, unfortunately, the binding target was not updated before the ICollectionView sorting took place. - // I decided it was not worth changing the DIYGrid.Sorting function to force the SortDirection binding targets to update immediately, before the ICollectionView sorting. interface ICVOrderBookColumn : IKeyedColumn, IGridColumn { string sort_member_path { get; } ListSortDirection? SortDirection { get; set; } @@ -43,8 +33,28 @@ namespace mbts.spark { IComparer comparer { get; } } + class OrderBookSortingData : IEquatable { + public readonly IReadOnlyList sorting_criterion; + public readonly IComparer comparer; + public readonly string desc; + + public OrderBookSortingData(IEnumerable column_sorting_hierarchy) { + var comp_builder = new ComparerBuilder(); + foreach (var col in column_sorting_hierarchy) + comp_builder.add(col.SortDirection.Value == ListSortDirection.Ascending ? col.comparer : col.comparer.reverse()); + comparer = comp_builder.to_comparer(); + sorting_criterion = column_sorting_hierarchy.Select(col => new SortingCriteria(col.id, col.SortDirection.GetValueOrDefault() == ListSortDirection.Ascending)).ToArray(); + desc = string.Join(", ", sorting_criterion); + } + + public bool Equals(OrderBookSortingData other) => other != null && EqualityComparer.safe_sequence_equals(sorting_criterion, other.sorting_criterion); + public override bool Equals(object obj) => base.Equals(obj); + public override int GetHashCode() => EqualityComparer.aggregate_hash(sorting_criterion); + public override string ToString() => desc; + } + partial class OrderBookVM { - public class OrderBookColumnCollection : ObservableCollection, IValueFactory, IDisposable { + public class OrderBookColumnCollection : ObservableCollection, IValueFactory<(FilterStates filters, IReadOnlyList sorting)>, IDisposable { readonly List _active_comparers = new List(); public OrderBookColumnCollection(IBus bus, OrdersModel model, IObservable<(IEnumerable added, IEnumerable removed)> order_securities) { @@ -298,7 +308,7 @@ namespace mbts.spark { public SimpleTextColumn order_key_column { get; } public SimpleTextColumn parent_key_column { get; } - public FilterStates create() => + public (FilterStates filters, IReadOnlyList sorting) create() => ( new FilterStates { order_key_regex = order_key_column.filter.get_state(), time_created = time_created_column.filter.get_state(), @@ -319,10 +329,11 @@ namespace mbts.spark { last_change = last_change_column.filter.get_state(), security_criteria = security_column.filter.get_state(), symbols = symbol_column.filter.get_state() - }; + }, + get_sorting_data().sorting_criterion); - public void load_from(FilterStates fs) { - if (fs != null) { + public void load_from((FilterStates filters, IReadOnlyList sorting) state) { + if (state.filters is FilterStates fs) { security_column.filter.load_state(fs.security_criteria); symbol_column.filter.load_state(fs.symbols); time_created_column.filter.load_state(fs.time_created); @@ -343,6 +354,23 @@ namespace mbts.spark { last_message_column.filter.load_state(fs.exchange_message_regex); last_change_column.filter.load_state(fs.last_change); } + _active_comparers.Clear(); + foreach (var col in this) + col.SortDirection = null; + if (state.sorting is IReadOnlyList sorting) { + foreach (var sc in sorting) { + this.OfType().ApplyToFirst(col => col.id.ic_equals(sc.col_name), + col => { + col.SortDirection = sc.is_ascending ? ListSortDirection.Ascending : ListSortDirection.Descending; + _active_comparers.Add(col); + }); + } + } + if (_active_comparers.Count == 0) { + // safeguard against empty sort, fallback to time-created + time_created_column.SortDirection = ListSortDirection.Descending; + _active_comparers.Add(time_created_column); + } } public Func get_filter() { @@ -350,7 +378,7 @@ namespace mbts.spark { return active_filters.Length == 0 ? null : new Func(r => active_filters.All(f => f.filter(r))); } - public (IComparer comp, string desc) get_comparer_data() { + public OrderBookSortingData get_sorting_data() { var desc_parts = new List(); var comp_builder = new ComparerBuilder(); // this should be unneccesary, safe_active_comparers is just an extra guard against unexpected results from future code changes @@ -362,11 +390,7 @@ namespace mbts.spark { _active_comparers.Add(time_created_column); safe_active_comparers = _active_comparers; } - foreach (var col in safe_active_comparers) { - desc_parts.Add($"{col.id}:{col.SortDirection.Value}"); - comp_builder.add(col.SortDirection.Value == ListSortDirection.Ascending ? col.comparer : col.comparer.reverse()); - } - return (comp_builder.to_comparer(), string.Join(", ", desc_parts)); + return new OrderBookSortingData(safe_active_comparers); } public void on_sorting(ICVOrderBookColumn col, bool clear_current_sort) { diff --git a/src/client/spark/Modules/Trading/OrderBook/OrderBookRowCollection.cs b/src/client/spark/Modules/Trading/OrderBook/OrderBookRowCollection.cs index 33a90e7fd..dd462d4f4 100644 --- a/src/client/spark/Modules/Trading/OrderBook/OrderBookRowCollection.cs +++ b/src/client/spark/Modules/Trading/OrderBook/OrderBookRowCollection.cs @@ -28,14 +28,14 @@ namespace mbts.spark { set { _filter = value; _refresh_branches(_tree.RootNodes); - _reset(); + reset(); } } public IComparer comparer { get => _comparer; set { _tree = _tree.Resort(_comparer = value); - _reset(); + reset(); } } public IEnumerable all_rows => _tree; @@ -98,6 +98,9 @@ namespace mbts.spark { } } + /// + /// Refreshes the collection by calling with all visible rows. + /// public void refresh() { if (_is_refresh_deferred) { _is_refresh_pending = true; @@ -107,6 +110,19 @@ namespace mbts.spark { } } + /// + /// Resets the collection by calling , then adding all visible rows. + /// + public void reset(bool force = false) { + if (_is_refresh_deferred && !force) { + _is_refresh_pending = true; + } else { + Clear(); + this.AddRange(_visible_rows); + _is_refresh_pending = false; + } + } + public IDisposable defer_refresh(bool force_refresh_after = false) { // use a counter instead of a simple bool flag to gracefully handle recursive calls to defer_refresh(), // by only calling refresh() after all IDisposables returned by defer_refresh() have been disposed. @@ -154,16 +170,6 @@ namespace mbts.spark { static SortedTree _mk_tree(IComparer comp, IEnumerable init = null) => new SortedTree(comp, r => r.order_key, r => r.parent_order_key, init); - void _reset() { - if (_is_refresh_deferred) { - _is_refresh_pending = true; - } else { - Clear(); - this.AddRange(_visible_rows); - _is_refresh_pending = false; - } - } - void _refresh_branches(IEnumerable> roots) { if (filter == null) { foreach (var n in roots) diff --git a/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs b/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs index 1eae3cc13..59de836a0 100644 --- a/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs +++ b/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs @@ -3,6 +3,7 @@ using bts.api; using bts.api.orderman; using bts.shell; using bts.spark; +using bts.utils; using mbts.ui; using mbts.ui.controls; using mbts.ui.settings; @@ -34,22 +35,20 @@ namespace mbts.spark { _bus = bus; _model = model; _columns = new OrderBookColumnCollection(bus, model, model.on_order_securities.Select(u => (u.added.Values.Select(smd => smd.security), u.removed.Values.Select(smd => smd.security)))); - _row_collection = new OrderBookRowCollection(ort => new OrderBookRowVM(this, ort), _columns.get_comparer_data().comp) { filter = _columns.get_filter() }; + _row_collection = new OrderBookRowCollection(ort => new OrderBookRowVM(this, ort), _columns.get_sorting_data().comparer) { filter = _columns.get_filter() }; _current_row = backing(nameof(current_row)); - _columns.filter_changed += on_filter_changed; + _columns.filter_changed += _on_filter_changed; collapse_all_rows_command = new DelegateCommand(_row_collection.collapse_all); expand_all_rows_command = new DelegateCommand(_row_collection.expand_all); _resources = new List { model.on_order_tuples_table.OnDispatcher().Subscribe(_row_collection.update), (grid_settings_manager = _create_grid_settings_manager(owner_title, bus, notification_service, model, columns)), Disposable.Create(() => { - _columns.filter_changed -= on_filter_changed; + _columns.filter_changed -= _on_filter_changed; Disposable.dispose(_row_collection); }), _columns }; - void on_filter_changed() => - _row_collection.filter = _columns.get_filter(); } public event Action selected_order_changed; @@ -71,23 +70,41 @@ namespace mbts.spark { public OrderBookStartupData create() { var (grid, animations) = grid_settings_manager.create(); - return new OrderBookStartupData(_columns.create(), grid, animations.Select(a => new OrderRecordTuplePredicateAnimations(a.predicate_builder, a.cell_animations, a.row_animations, a.sound_notifications))); + var (filters, sorting) = _columns.create(); + grid = grid.with(sorting_criterion: sorting); + return new OrderBookStartupData(filters, grid, animations.Select(a => new OrderRecordTuplePredicateAnimations(a.predicate_builder, a.cell_animations, a.row_animations, a.sound_notifications))); } public void load_from(OrderBookStartupData state) { if (state == null) return; + (var old_filter, var old_sort) = _columns.create(); _state = state; - grid_settings_manager.load_from((state.grid, state.predicate_animations)); - _columns.load_from(state.filters); + // unhook filter_changed; in the event that both filters AND sorting changes, we'll avoid resetting the rows twice (i.e. once for new filters, and again for new sorting). + _columns.filter_changed -= _on_filter_changed; + using (_row_collection.defer_refresh()) { + grid_settings_manager.load_from((state.grid, state.predicate_animations)); + _columns.load_from((state.filters, state.grid.sorting_criterion)); + (var new_filter, var new_sort) = _columns.create(); + if (!new_filter.Equals(old_filter) || !EqualityComparer.safe_sequence_equals(new_sort, old_sort)) { + // since we are in a 'defer_refresh()' scope, setting these properties will not cause a reset. + _row_collection.filter = _columns.get_filter(); + _row_collection.comparer = _columns.get_sorting_data().comparer; + // call reset() here so _row_collection does not internally call sync_with() when we exit the defer_refresh() scope. + // (in this case, sync_with potentially involves a lot of individual Remove() calls, which can be very expensive as the view responds to each CollectionChanged event) + _row_collection.reset(true); + } + } + // hook filter_changed back up + _columns.filter_changed += _on_filter_changed; } public void on_sorting(object sender, DIYGridSortingEventArgs e) { if (e.Column is ICVOrderBookColumn col) { e.Handled = true; _columns.on_sorting(col, !KeyboardExtensions.IsShiftDown()); - (var comp, var desc) = _columns.get_comparer_data(); - Log.Ui.info($"{nameof(OrderBookRowVM)} begin resort: {desc}"); - _row_collection.comparer = comp; + var sorting_data = _columns.get_sorting_data(); + Log.Ui.info($"{nameof(OrderBookRowVM)} begin resort: {sorting_data}"); + _row_collection.comparer = sorting_data.comparer; } else { Log.Ui.warn($"{nameof(OrderBookVM)} failed to process sorting event: Column was not of type {nameof(ICVOrderBookColumn)}"); } @@ -119,6 +136,9 @@ namespace mbts.spark { dr.is_visible = false; } + void _on_filter_changed() => + _row_collection.filter = _columns.get_filter(); + public OrderMetadata get_metadata(OrderBookRowVM row) => _model.factory.get_metadata(row.order_type); public void Dispose() { diff --git a/src/client/spark/view/Utility/SortingCriteria.cs b/src/client/spark/view/Utility/SortingCriteria.cs index 4b09ac9dc..6845f9470 100644 --- a/src/client/spark/view/Utility/SortingCriteria.cs +++ b/src/client/spark/view/Utility/SortingCriteria.cs @@ -21,5 +21,7 @@ namespace mbts.ui public bool is_ascending; public SortDescription to_sort_description() { return new SortDescription(col_name, is_ascending ? ListSortDirection.Ascending : ListSortDirection.Descending); } + + public override string ToString() => $"{col_name}:{(is_ascending ? "Ascending" : "Descending")}"; } } \ No newline at end of file diff --git a/src/client/spark_core/utils/GlobalExtensions.cs b/src/client/spark_core/utils/GlobalExtensions.cs index 8a094fdb6..ed021abed 100644 --- a/src/client/spark_core/utils/GlobalExtensions.cs +++ b/src/client/spark_core/utils/GlobalExtensions.cs @@ -18,6 +18,9 @@ namespace mbts { } return sb.ToString(); } + public static bool ic_equals(this string str, string comparand) => + string.Equals(str, comparand, StringComparison.CurrentCultureIgnoreCase); + public static T choose(this Random r, IEnumerable range) { var index = r.Next(0, range.Count()); return range.Skip(index).First(); From 81880779c344415b1b3b62d3eeb767bc9a86f28d Mon Sep 17 00:00:00 2001 From: John Colvin Date: Thu, 25 Oct 2018 19:33:18 -0500 Subject: [PATCH 17/46] OrderBook - retain column sorting when syncing with new default grid settings --- .../Modules/Trading/OrderBook/OrderBookVM.cs | 32 ++++--------------- .../Trading/OrderBook/OrderBookWindow.cs | 8 ++--- 2 files changed, 11 insertions(+), 29 deletions(-) diff --git a/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs b/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs index b972c8bb0..a8c6dfce3 100644 --- a/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs +++ b/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs @@ -41,7 +41,7 @@ namespace mbts.spark { _columns = new OrderBookColumnCollection(bus, model, model.on_order_securities.Select(u => (u.added.Values.Select(smd => smd.security), u.removed.Values.Select(smd => smd.security)))); _row_collection = new OrderBookRowCollection(ort => new OrderBookRowVM(this, ort), _columns.get_sorting_data().comparer) { filter = _columns.get_filter() }; _current_row = backing(nameof(current_row)); - _columns.filter_changed += _on_filter_changed; + _columns.filter_changed += on_filter_changed; collapse_all_rows_command = new DelegateCommand(_row_collection.collapse_all); expand_all_rows_command = new DelegateCommand(_row_collection.expand_all); grid_settings_manager = _create_grid_settings_manager(owner_title, bus, notification_service, model, columns); @@ -50,11 +50,12 @@ namespace mbts.spark { settings_manager.subscribe_current().OnDispatcher().Subscribe(grid_settings_manager), grid_settings_manager, Disposable.Create(() => { - _columns.filter_changed -= _on_filter_changed; + _columns.filter_changed -= on_filter_changed; Disposable.dispose(_row_collection); }), _columns }; + void on_filter_changed() => _row_collection.filter = _columns.get_filter(); } public event Action selected_order_changed; @@ -76,36 +77,22 @@ namespace mbts.spark { public OrderBookStartupData create() { var (filters, sorting) = _columns.create(); - if (_settings_manager.instance_settings == null) - return new OrderBookStartupData(filters, use_defaults: true); var gs = grid_settings_manager.create(); - return new OrderBookStartupData(filters, gs.general.with(sorting_criterion: sorting), gs.predicate_animations, use_defaults: false); + return new OrderBookStartupData(filters, gs.general.with(sorting_criterion: sorting), gs.predicate_animations, use_defaults: _settings_manager.instance_settings == null); } public void load_from(OrderBookStartupData state) { if (state == null) return; (var old_filter, var old_sort) = _columns.create(); _state = state; - // unhook filter_changed; in the event that both filters AND sorting changes, we'll avoid resetting the rows twice (i.e. once for new filters, and again for new sorting). - _columns.filter_changed -= _on_filter_changed; using (_row_collection.defer_refresh()) { var gs = state.grid_settings_wrapper(); - // TODO: move the sorting criterion out of GridSettings, and store it as a seperate field in OrderBookStartupData, like FilterStates (unf for now, this method is a little messy). - // the issue is this: - // - grid_settings_manager loads GridSettings - // - _columns loads sorting_criteria - // - but sorting_criteria is in GridSettings! - IReadOnlyList sorting_criterion = null; if (gs != null) { grid_settings_manager.load_from(gs); -#pragma warning disable CS0612 // Type or member is obsolete - sorting_criterion = gs.general.sorting_criterion; -#pragma warning restore CS0612 // Type or member is obsolete - } else { - // if null, fallback to a good default sort. - sorting_criterion = new[] { new SortingCriteria(Columns.Time.ToString(), false) }; } - _columns.load_from((state.filters, sorting_criterion)); +#pragma warning disable CS0612 // Type or member is obsolete + _columns.load_from((state.filters, state.grid.sorting_criterion)); +#pragma warning restore CS0612 // Type or member is obsolete (var new_filter, var new_sort) = _columns.create(); if (!new_filter.Equals(old_filter) || !EqualityComparer.safe_sequence_equals(new_sort, old_sort)) { // since we are in a 'defer_refresh()' scope, setting these properties will not cause a reset. @@ -116,8 +103,6 @@ namespace mbts.spark { _row_collection.reset(true); } } - // hook filter_changed back up - _columns.filter_changed += _on_filter_changed; } public void on_sorting(object sender, DIYGridSortingEventArgs e) { @@ -158,9 +143,6 @@ namespace mbts.spark { dr.is_visible = false; } - void _on_filter_changed() => - _row_collection.filter = _columns.get_filter(); - public OrderMetadata get_metadata(OrderBookRowVM row) => _model.factory.get_metadata(row.order_type); public void Dispose() { diff --git a/src/client/spark/Modules/Trading/OrderBook/OrderBookWindow.cs b/src/client/spark/Modules/Trading/OrderBook/OrderBookWindow.cs index 398d78020..1bbf0173e 100644 --- a/src/client/spark/Modules/Trading/OrderBook/OrderBookWindow.cs +++ b/src/client/spark/Modules/Trading/OrderBook/OrderBookWindow.cs @@ -121,7 +121,7 @@ namespace mbts.spark { _vm.expand_all_rows_command); _show_grid_settings_command = SparkCommands.window_settings_command(new DialogCommand(() => { - var vm = new WindowSettingsDialogVM( + var gs_vm = new WindowSettingsDialogVM( _settings_manager, us => us.order_book_grid, UserSettings.FileExtensions.GRID, @@ -137,11 +137,11 @@ namespace mbts.spark { } ); // TODO: bind to main window's Title - var dialog = new SimpleGridSettingsDialog { DataContext = vm }; - dialog.SetBinding(Window.TitleProperty, new Binding($"{nameof(vm.instance_vm)}.{nameof(OrderBookGridSettingsVM.owner_title)}") { + var dialog = new SimpleGridSettingsDialog { DataContext = gs_vm }; + dialog.SetBinding(Window.TitleProperty, new Binding($"{nameof(gs_vm.instance_vm)}.{nameof(OrderBookGridSettingsVM.owner_title)}") { Converter = ValueConverter.create(str => (string.IsNullOrEmpty(str) ? _default_title : str) + " Settings") }); - vm.RequestClose += (s, e) => WindowUtilities.force_close(dialog); + gs_vm.RequestClose += (s, e) => WindowUtilities.force_close(dialog); return dialog; })); From eff1954260a083b044cdd033c5a5ce0b89f465d0 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Thu, 25 Oct 2018 19:35:06 -0500 Subject: [PATCH 18/46] DIYGrid - accidentally removed an 'empty_if_null()' call --- src/client/spark/view/Controls/DIYGrid/DIYGrid.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client/spark/view/Controls/DIYGrid/DIYGrid.cs b/src/client/spark/view/Controls/DIYGrid/DIYGrid.cs index 0ef6739de..50bd4cb93 100644 --- a/src/client/spark/view/Controls/DIYGrid/DIYGrid.cs +++ b/src/client/spark/view/Controls/DIYGrid/DIYGrid.cs @@ -172,7 +172,7 @@ namespace mbts.ui.controls { o.Resources[ActiveSelectionHighlightKey] = v; o.Resources[InactiveSelectionHighlightKey] = new SolidColorBrush(v.Color) { Opacity = 0.2 }.frozen(); o.Resources[SelectionStrokeKey] = new Pen(o.SelectionHighlight.Color.to_brush().frozen(), 1).frozen(); - foreach (var col in o.Columns.OfType()) + foreach (var col in o.Columns.empty_if_null().OfType()) col.on_selection_fill(v); }, (o, v) => v as SolidColorBrush ?? DefaultSelectionHighlight, DefaultSelectionHighlight); From 407dbc4c0ad4daf26599bf864413f8da02f3fe43 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Thu, 25 Oct 2018 19:55:25 -0500 Subject: [PATCH 19/46] GridSettingsManager - invalidate all rows after settings update to reflect changes --- src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs | 6 +++--- .../view/Controls/DIYGrid/Settings/GridSettingsManager.cs | 7 ++++++- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs b/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs index a8c6dfce3..8b969eb3e 100644 --- a/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs +++ b/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs @@ -44,7 +44,7 @@ namespace mbts.spark { _columns.filter_changed += on_filter_changed; collapse_all_rows_command = new DelegateCommand(_row_collection.collapse_all); expand_all_rows_command = new DelegateCommand(_row_collection.expand_all); - grid_settings_manager = _create_grid_settings_manager(owner_title, bus, notification_service, model, columns); + grid_settings_manager = _create_grid_settings_manager(owner_title, bus, notification_service, model, rows, columns); _resources = new List { model.on_order_tuples_table.OnDispatcher().Subscribe(_row_collection.update), settings_manager.subscribe_current().OnDispatcher().Subscribe(grid_settings_manager), @@ -151,7 +151,7 @@ namespace mbts.spark { } static _GridSettingsManager _create_grid_settings_manager( - IObservable owner_title, IBus bus, INotificationService notification_service, OrdersModel model, ObservableCollection columns) { + IObservable owner_title, IBus bus, INotificationService notification_service, OrdersModel model, ObservableCollection rows, ObservableCollection columns) { // make font-weight immutable for all columns (the font-weight rule is hard-coded: Regular for root-orders, Light for all others) // TODO: Remove the immutable font-weight flags when cell animation is ready var col_settings_flags = EnumHelper.get_values().ToDictionary( @@ -162,7 +162,7 @@ namespace mbts.spark { col_settings_flags[c.ToString()] = GridColumnSettingsFeatureFlags.ImmutableFontColor | GridColumnSettingsFeatureFlags.ImmutableFontWeight; } return new _GridSettingsManager( - owner_title, bus, notification_service, model, columns, + owner_title, bus, notification_service, model, rows, columns, () => { var pvm = PredicateBuilderVMWrapper.create( OrderRecordTuplePredicateVMFactory.get_oneofs(bus, model), rp => rp.predicate_type_case); diff --git a/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsManager.cs b/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsManager.cs index 9eb160784..e4eccfc55 100644 --- a/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsManager.cs +++ b/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsManager.cs @@ -27,6 +27,7 @@ namespace mbts.ui { readonly INotificationService _notification_service; readonly List _resources, _notification_subs = new List(); readonly ObservableCollection _columns; + readonly ObservableCollection _rows; readonly Property _grid_settings; readonly DelegateProperty _font_family; readonly DelegateProperty _font_weight; @@ -42,7 +43,8 @@ namespace mbts.ui { public GridSettingsManager( IObservable owner_title, IBus bus, INotificationService notification_service, OrdersModel orders_model, - ObservableCollection columns, Func> create_predicate_vm, + ObservableCollection rows, ObservableCollection columns, + Func> create_predicate_vm, Func> create_column_predicate_vm, Func>, TSettingsData> create_settings_obj, TPredicateBuilderArg arg, IReadOnlyDictionary column_flags = null) { @@ -50,6 +52,7 @@ namespace mbts.ui { _bus = bus; _notification_service = notification_service; _orders_model = orders_model; + _rows = rows; _columns = columns; _create_predicate_vm = create_predicate_vm; _create_column_predicate_vm = create_column_predicate_vm; @@ -153,6 +156,8 @@ namespace mbts.ui { if (col_settings_id_map.TryGetValue(id_key(col.id), out GridColumnSettings cs)) col.load_from(cs); } + // invalidate all the rows to draw them again to reflect the latest settings + foreach (var r in _rows) r.raise_invalidated(); // string id_key(string id) => id.ToLower(); } From 7149a20865340a3a6150028f8c1adbf72df65884 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Thu, 25 Oct 2018 20:03:38 -0500 Subject: [PATCH 20/46] OrderBookRowCollection - add total_row_count property (for upcoming status bar) --- .../Trading/OrderBook/OrderBookRowCollection.cs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/client/spark/Modules/Trading/OrderBook/OrderBookRowCollection.cs b/src/client/spark/Modules/Trading/OrderBook/OrderBookRowCollection.cs index dd462d4f4..ef1113f5a 100644 --- a/src/client/spark/Modules/Trading/OrderBook/OrderBookRowCollection.cs +++ b/src/client/spark/Modules/Trading/OrderBook/OrderBookRowCollection.cs @@ -5,6 +5,7 @@ using mbts.utils; using System; using System.Collections.Generic; using System.Collections.ObjectModel; +using System.ComponentModel; using System.Linq; using System.Reactive.Linq; @@ -16,7 +17,7 @@ namespace mbts.spark { IComparer _comparer; Func _filter; bool _is_refresh_pending; - int _last_row_count = 0, _deferment_count = 0; + int _deferment_count = 0; public OrderBookRowCollection(Func row_factory, IComparer comparer) { _row_factory = row_factory; @@ -38,7 +39,7 @@ namespace mbts.spark { reset(); } } - public IEnumerable all_rows => _tree; + public int total_row_count { get; private set; } IEnumerable _visible_rows => _tree.Where(r => r.is_visible); bool _is_refresh_deferred => _deferment_count > 0; @@ -137,7 +138,8 @@ namespace mbts.spark { public void update(IIncrementalTableUpdateTransform table) { IEnumerable> impacted_branch_roots; - if (table.removed.Count() == _last_row_count) { + int n_removed = table.removed.Count(); + if (n_removed == total_row_count) { // full refresh (most likely this is the first update or a server restart) _tree = _mk_tree(_comparer, table.added.Select(_row_factory)); impacted_branch_roots = _tree.RootNodes; @@ -162,9 +164,12 @@ namespace mbts.spark { .Where(n => n != null) ).Distinct(_node_equality_comparer); } - _last_row_count += table.added.Count() - table.removed.Count(); _refresh_branches(impacted_branch_roots); refresh(); + int old_row_count = total_row_count; + total_row_count += table.added.Count() - n_removed; + if (old_row_count != total_row_count) + OnPropertyChanged(new PropertyChangedEventArgs(nameof(total_row_count))); } static SortedTree _mk_tree(IComparer comp, IEnumerable init = null) => From 5e2a7c82c0f99dca56454267ef0d62ad01c5c2f9 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Fri, 26 Oct 2018 14:01:05 -0500 Subject: [PATCH 21/46] remove OrderBookCell.cs --- .../Trading/OrderBook/OrderBookCell.cs | 168 ------------------ 1 file changed, 168 deletions(-) delete mode 100644 src/client/spark/Modules/Trading/OrderBook/OrderBookCell.cs diff --git a/src/client/spark/Modules/Trading/OrderBook/OrderBookCell.cs b/src/client/spark/Modules/Trading/OrderBook/OrderBookCell.cs deleted file mode 100644 index 2e4d28806..000000000 --- a/src/client/spark/Modules/Trading/OrderBook/OrderBookCell.cs +++ /dev/null @@ -1,168 +0,0 @@ -//using bts.utils; -//using mbts.ui; -//using mbts.ui.controls; -//using mbts.utils; -//using System; -//using System.Collections.Generic; -//using System.Collections.ObjectModel; -//using System.ComponentModel; - -//namespace mbts.spark { -// partial class OrderBookRowVM { -// public interface IOrderBookCell : DIYGrid.IGridCell, IComparable { } - -// public interface IOrderBookCell : IOrderBookCell { -// T value { get; } -// IOrderBookCellStore store { get; } -// } - -// public interface IOrderBookCellStore : DIYGrid.IGridCellStore { -// OrderBookRowVM row { get; } -// new ICVOrderBookColumn column { get; } -// string prop_name { get; } -// new IOrderBookCell current { get; } -// // -// IOrderBookCellStore select_other(OrderBookRowVM row); -// } - -// public interface IOrderBookCellStore : IOrderBookCellStore, IComparer, IEqualityComparer { -// new IOrderBookCell current { get; } -// new IOrderBookCellStore select_other(OrderBookRowVM row); -// // -// T get_value(OrderBookRowVM row); -// string format(T value); -// } - -// public static class CellFactory { -// public static IOrderBookCellStore create(OrderBookRowVM row, ICVOrderBookColumn column, string prop_name, Func get_value, Func> select_other, Func format = null) => -// new OrderBookCellStore(row, column, prop_name, get_value, select_other, format, new Comparison(Comparer.Default.Compare), EqualityComparer.Default); - -// public static IOrderBookCellStore create(OrderBookRowVM row, ICVOrderBookColumn column, string prop_name, Func get_value, Func> select_other, Comparison comparison, Func format = null) => -// new OrderBookCellStore(row, column, prop_name, get_value, select_other, format, comparison, EqualityComparer.Default); - -// public static IOrderBookCellStore create_nullable(OrderBookRowVM row, ICVOrderBookColumn column, string prop_name, Func get_value, Func> select_other, Func format = null) -// where T : struct => new OrderBookCellStore(row, column, prop_name, get_value, select_other, format, CompareUtils.nullable_comparison(Comparer.Default.Compare), EqualityComparer.CreateNullable(EqualityComparer.Default.Equals)); - -// public static IOrderBookCellStore create_nullable(OrderBookRowVM row, ICVOrderBookColumn column, string prop_name, Func get_value, Func> select_other, Func compare, Func format = null) -// where T : struct => -// new OrderBookCellStore(row, column, prop_name, get_value, select_other, format, CompareUtils.nullable_comparison(compare), EqualityComparer.CreateNullable(EqualityComparer.Default.Equals)); - -// class OrderBookCellStore : IOrderBookCellStore, INotifyPropertyChanged { -// readonly OrderBookRowVM _row; -// readonly ICVOrderBookColumn _column; -// readonly string _prop_name; -// readonly Func _get_value; -// readonly Func> _select_other; -// readonly Func _format; -// readonly Comparison _comparison; -// readonly IEqualityComparer _eq_comparer; -// OrderBookCell _current; - -// // Property changed handler to avoid WPF binding leak -// // https://blog.jetbrains.com/dotnet/2014/09/04/fighting-common-wpf-memory-leaks-with-dotmemory/ -// public event PropertyChangedEventHandler PropertyChanged = (_,__) => { }; - -// public OrderBookCellStore( -// OrderBookRowVM row, -// ICVOrderBookColumn column, -// string prop_name, -// Func get_value, -// Func> select_other, -// Func format, -// Comparison comparison, -// IEqualityComparer eq_comparer) { -// // -// _row = row; -// _column = column; -// _prop_name = prop_name; -// _get_value = get_value; -// _select_other = select_other; -// _format = format ?? new Func(v => v?.ToString() ?? string.Empty); -// _comparison = comparison; -// _eq_comparer = eq_comparer; -// _current = new OrderBookCell(this, _get_value(_row)); -// } - -// public OrderBookRowVM row => _row; -// public ICVOrderBookColumn column => _column; -// public string prop_name => _prop_name; -// public IOrderBookCell current => _current; - -// IOrderBookCell IOrderBookCellStore.current => _current; -// DIYGrid.IGridColumn DIYGrid.IGridCellStore.column => _column; -// DIYGrid.IGridCell DIYGrid.IGridCellStore.current => _current; - -// public ObservableCollection animations { get; } = new ObservableCollection(); - -// public int Compare(T x, T y) => _comparison(x, y); -// public bool Equals(T x, T y) => _eq_comparer.Equals(x, y); -// public string format(T value) => _format(value); -// public int GetHashCode(T obj) => _eq_comparer.GetHashCode(obj); -// public T get_value(OrderBookRowVM row) => _get_value(row); -// public IOrderBookCellStore select_other(OrderBookRowVM row) => _select_other(row); -// IOrderBookCellStore IOrderBookCellStore.select_other(OrderBookRowVM row) => select_other(row); - -// public bool refresh() { -// T v = _get_value(_row); -// if (!_eq_comparer.Equals(v, _current.value)) { -// _current = new OrderBookCell(this, v); -// _row.RaisePropertyChanged(_prop_name); -// return true; -// } else return false; -// } - -// public void regenerate_cell() { -// _current = new OrderBookCell(this, _current.value); -// _row.RaisePropertyChanged(_prop_name); -// } - -// public void invalidate_text() => regenerate_cell(); -// } - -// class OrderBookCell : IOrderBookCell { -// string _text; - -// public OrderBookCell(OrderBookCellStore store, T value) { -// this.store = store; -// this.value = value; -// } - -// public string text => _text ?? (_text = store.format(value)); -// public T value { get; } -// public IOrderBookCellStore store { get; } - -// public int CompareTo(OrderBookCell other) { -// if (other == null) -// return 1; -// var lin = lineage(store.row); -// var olin = lineage(other.store.row); -// int c = 0; -// using (var en = lin.GetEnumerator()) -// using (var oen = olin.GetEnumerator()) { -// while (en.MoveNext() && oen.MoveNext()) { -// if (en.Current.order_key.value != oen.Current.order_key.value) { -// c = store.Compare( -// store.select_other(en.Current).current.value, -// store.select_other(oen.Current).current.value); -// if (c != 0) -// return c; -// else { -// c = en.Current.order_key.value.CompareTo( -// oen.Current.order_key.value); -// return store.column.SortDirection.HasValue && store.column.SortDirection.Value == ListSortDirection.Ascending ? c : -c; -// } -// } -// } -// } -// return store.column.SortDirection.HasValue && store.column.SortDirection.Value == ListSortDirection.Ascending ? -// lin.Count.CompareTo(olin.Count) : -// olin.Count.CompareTo(lin.Count); -// } - -// public int CompareTo(object obj) => CompareTo(obj as OrderBookCell); - -// public override string ToString() => text; -// } -// } -// } -//} \ No newline at end of file From 8bd2e5e5f3eac6b0c3d1abbbb3d0ceb8116aaf9f Mon Sep 17 00:00:00 2001 From: John Colvin Date: Fri, 26 Oct 2018 14:40:39 -0500 Subject: [PATCH 22/46] OrderBook - guard against null GridSettings --- src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs b/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs index 8b969eb3e..090b40019 100644 --- a/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs +++ b/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs @@ -91,7 +91,7 @@ namespace mbts.spark { grid_settings_manager.load_from(gs); } #pragma warning disable CS0612 // Type or member is obsolete - _columns.load_from((state.filters, state.grid.sorting_criterion)); + _columns.load_from((state.filters, (state.grid ?? OrderBookStartupData.Default.grid).sorting_criterion)); #pragma warning restore CS0612 // Type or member is obsolete (var new_filter, var new_sort) = _columns.create(); if (!new_filter.Equals(old_filter) || !EqualityComparer.safe_sequence_equals(new_sort, old_sort)) { From a3de2c198307a4c5cfc98a69b2c0532a5973a95a Mon Sep 17 00:00:00 2001 From: John Colvin Date: Fri, 26 Oct 2018 14:44:58 -0500 Subject: [PATCH 23/46] SortedTree - _replace was not re-adding node if parentkey changed --- src/client/spark_core/Collections/SortedTree.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/client/spark_core/Collections/SortedTree.cs b/src/client/spark_core/Collections/SortedTree.cs index d5b0686b2..4d4b8ee21 100644 --- a/src/client/spark_core/Collections/SortedTree.cs +++ b/src/client/spark_core/Collections/SortedTree.cs @@ -161,9 +161,9 @@ namespace bts.collections { else { _remove_from_nursery(old_pk, node); } - node.value = newval; - _add_node(pk, node); } + node.value = newval; + _add_node(pk, node); } } From eedf19474ee76733ed1bd4b185ebc014847ff2fd Mon Sep 17 00:00:00 2001 From: John Colvin Date: Sun, 28 Oct 2018 15:57:08 -0500 Subject: [PATCH 24/46] SortedTree - when node is removed, any remaining children (orphans) go to the nursery --- src/client/spark_core/Collections/SortedTree.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/client/spark_core/Collections/SortedTree.cs b/src/client/spark_core/Collections/SortedTree.cs index 4d4b8ee21..185a76f03 100644 --- a/src/client/spark_core/Collections/SortedTree.cs +++ b/src/client/spark_core/Collections/SortedTree.cs @@ -90,6 +90,9 @@ namespace bts.collections { } else { _remove_from_nursery(pk, node); } + if (node.children.Count > 0) { + _nursery.Add(k, node.children); + } } public SortedTree Resort(IComparer comparer) { From 3acaf5b2c0f9260f881769b9b852830f5bdba027 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Mon, 29 Oct 2018 11:57:20 -0500 Subject: [PATCH 25/46] OrderBookVM - slight optimization in load_from(startupdata) method --- .../Modules/Trading/OrderBook/OrderBookVM.cs | 38 ++++++++++++------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs b/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs index 090b40019..bfe67778b 100644 --- a/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs +++ b/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs @@ -83,25 +83,35 @@ namespace mbts.spark { public void load_from(OrderBookStartupData state) { if (state == null) return; - (var old_filter, var old_sort) = _columns.create(); _state = state; - using (_row_collection.defer_refresh()) { - var gs = state.grid_settings_wrapper(); - if (gs != null) { - grid_settings_manager.load_from(gs); - } + var gs = state.grid_settings_wrapper(); + if (gs != null) { + grid_settings_manager.load_from(gs); + } + (var old_filter, var old_sort) = _columns.create(); #pragma warning disable CS0612 // Type or member is obsolete - _columns.load_from((state.filters, (state.grid ?? OrderBookStartupData.Default.grid).sorting_criterion)); + _columns.load_from((state.filters, (state.grid ?? OrderBookStartupData.Default.grid).sorting_criterion)); #pragma warning restore CS0612 // Type or member is obsolete - (var new_filter, var new_sort) = _columns.create(); - if (!new_filter.Equals(old_filter) || !EqualityComparer.safe_sequence_equals(new_sort, old_sort)) { - // since we are in a 'defer_refresh()' scope, setting these properties will not cause a reset. + (var new_filter, var new_sort) = _columns.create(); + bool reset_filter = !new_filter.Equals(old_filter); + bool reset_sorting = !new_sort.Equals(old_sort); + if (reset_filter) { + if (reset_sorting) { + // both filter and sort changed; use defer_refresh to avoid refreshing the collection twice + using (_row_collection.defer_refresh()) { + _row_collection.filter = _columns.get_filter(); + _row_collection.comparer = _columns.get_sorting_data().comparer; + // reset(true) is an optimization to avoid potentially Removing a lot of individual rows (the view responds to each CollectionChanged event), it calls Clear() then AddRange(_visible_rows) + // (otherwise, sync_with(_visible_rows) would be called when we exit/dispose the 'defer_refresh' scope) + _row_collection.reset(true); + } + } else { + // only filter changed; row_collection will refresh accordingly _row_collection.filter = _columns.get_filter(); - _row_collection.comparer = _columns.get_sorting_data().comparer; - // call reset() here so _row_collection does not internally call sync_with() when we exit the defer_refresh() scope. - // (in this case, sync_with potentially involves a lot of individual Remove() calls, which can be very expensive as the view responds to each CollectionChanged event) - _row_collection.reset(true); } + } else if (reset_sorting) { + // only sorting changed; row_collection will refresh accordingly + _row_collection.comparer = _columns.get_sorting_data().comparer; } } From ca0c17a41b67a994ee9706a7550aa4b05710ff31 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Mon, 29 Oct 2018 12:55:27 -0500 Subject: [PATCH 26/46] OrderBookVM - change comment in load_from() --- src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs b/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs index bfe67778b..d86ea35bf 100644 --- a/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs +++ b/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs @@ -101,8 +101,9 @@ namespace mbts.spark { using (_row_collection.defer_refresh()) { _row_collection.filter = _columns.get_filter(); _row_collection.comparer = _columns.get_sorting_data().comparer; - // reset(true) is an optimization to avoid potentially Removing a lot of individual rows (the view responds to each CollectionChanged event), it calls Clear() then AddRange(_visible_rows) - // (otherwise, sync_with(_visible_rows) would be called when we exit/dispose the 'defer_refresh' scope) + /// is an optimization; a way to hedge our bets. Interally it calls Clear(), then AddAll(_visible_rows). + /// Otherwise, would be called when we exit/dispose the defer_refresh() scope. + /// The latter method calls sync_with(_visible_rows) internally, which won't perform as well as the former if _visible_rows has changed significantly. _row_collection.reset(true); } } else { From ca5334fd876285f451040cf75cf5d5193b255f9c Mon Sep 17 00:00:00 2001 From: John Colvin Date: Mon, 29 Oct 2018 17:17:20 -0500 Subject: [PATCH 27/46] OrderBook/SortedTree - comparer needs to be 100% deterministic --- .../Modules/Trading/OrderBook/OrderBookColumns.cs | 4 ++++ src/client/spark_core/Collections/SortedList.cs | 8 +++++++- src/client/spark_core/Collections/SortedTree.cs | 9 +++++++++ .../spark_test/SortedTree/SortedTreeTests.cs | 15 ++++++++++----- 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/client/spark/Modules/Trading/OrderBook/OrderBookColumns.cs b/src/client/spark/Modules/Trading/OrderBook/OrderBookColumns.cs index 218f5fef7..d8d1ebfcb 100644 --- a/src/client/spark/Modules/Trading/OrderBook/OrderBookColumns.cs +++ b/src/client/spark/Modules/Trading/OrderBook/OrderBookColumns.cs @@ -43,6 +43,10 @@ namespace mbts.spark { var comp_builder = new ComparerBuilder(); foreach (var col in column_sorting_hierarchy) comp_builder.add(col.SortDirection.Value == ListSortDirection.Ascending ? col.comparer : col.comparer.reverse()); + if (!column_sorting_hierarchy.Any(col => col.key == Columns.OrderKey)) { + // the resultant comparer must to be 100% deterministic, as SortedList (the building block of the SortedTree) uses a BinarySearch to find/remove items and does NOT check for item-equality. + comp_builder.add(Comparer.Create((l, r) => l.order_key.CompareTo(r.order_key))); + } comparer = comp_builder.to_comparer(); sorting_criterion = column_sorting_hierarchy.Select(col => new SortingCriteria(col.id, col.SortDirection.GetValueOrDefault() == ListSortDirection.Ascending)).ToArray(); desc = string.Join(", ", sorting_criterion); diff --git a/src/client/spark_core/Collections/SortedList.cs b/src/client/spark_core/Collections/SortedList.cs index 2c30b41c4..d6ddc3c2a 100644 --- a/src/client/spark_core/Collections/SortedList.cs +++ b/src/client/spark_core/Collections/SortedList.cs @@ -2,7 +2,6 @@ using System.Collections; using System.Collections.Generic; using System.Linq; -using System.Text; namespace bts.collections { public class SortedList : IEnumerable { @@ -10,11 +9,18 @@ namespace bts.collections { int _size = 0; readonly IComparer _comparer; + /// + /// Note: comparer MUST be 100% deterministic for to work properly; item-equality is NOT checked. + /// public SortedList(IComparer comparer) { _comparer = comparer; _values = new T[4]; _size = 0; } + + /// + /// Note: comparer MUST be 100% deterministic for to work properly; item-equality is NOT checked. + /// public SortedList(IEnumerable items, IComparer comparer) { _comparer = comparer; var capacity = items.Count(); diff --git a/src/client/spark_core/Collections/SortedTree.cs b/src/client/spark_core/Collections/SortedTree.cs index 185a76f03..737a8fdc0 100644 --- a/src/client/spark_core/Collections/SortedTree.cs +++ b/src/client/spark_core/Collections/SortedTree.cs @@ -29,9 +29,15 @@ namespace bts.collections { Dictionary> _nursery = new Dictionary>(); SortedList _tree; + /// + /// comparer MUST be 100% deterministic, else Removes won't work right (SortedList uses a simple BinarySearch to find/remove items, and does NOT check item-equality). + /// public SortedTree(IComparer comparer, Func fnid, Func fnparent, IEnumerable items = null) : this(comparer, fnid, fnparent, default, items) { } + /// + /// comparer MUST be 100% deterministic, else Removes won't work right (SortedList uses a simple BinarySearch to find/remove items, and does NOT check item-equality). + /// public SortedTree(IComparer comparer, Func fnid, Func fnparent, TKey root_id, IEnumerable items=null) { _comparer = Comparer.Create((l, r) => comparer.Compare(l.value, r.value)); id = fnid; @@ -95,6 +101,9 @@ namespace bts.collections { } } + /// + /// comparer MUST be 100% deterministic, else Removes won't work right (SortedList uses a simple BinarySearch to find/remove items, and does NOT check item-equality). + /// public SortedTree Resort(IComparer comparer) { return new SortedTree(comparer, id, parent, this); } diff --git a/src/client/spark_test/SortedTree/SortedTreeTests.cs b/src/client/spark_test/SortedTree/SortedTreeTests.cs index 10d389f5b..001008d98 100644 --- a/src/client/spark_test/SortedTree/SortedTreeTests.cs +++ b/src/client/spark_test/SortedTree/SortedTreeTests.cs @@ -10,9 +10,16 @@ using System.Threading.Tasks; namespace bts.tests { [TestGroup] class SortedTreeTests { - class Order { const int no_parent = 0; + public static readonly IComparer name_asc = Comparer.Create((l, r) => { + int val_comp = l.name.CompareTo(r.name); + return val_comp == 0 ? l.id.CompareTo(r.id) : val_comp; + }); + public static readonly IComparer name_desc = Comparer.Create((l, r) => { + int val_comp = r.name.CompareTo(l.name); + return val_comp == 0 ? r.id.CompareTo(l.id) : val_comp; + }); public Order(char name, int id) : this(name, id, no_parent) { } public Order(char name, int id, int parent) { this.name = name; @@ -79,13 +86,11 @@ namespace bts.tests { [Test] public void test_sordid_tree() { - var name_asc = Comparer.Create((l, r) => l.name.CompareTo(r.name)); - var name_desc = Comparer.Create((l, r) => r.name.CompareTo(l.name)); for (int x = 0; x < 10; ++x) { var input = orders.ToList(); input.Shuffle(); - var tree_asc = new SortedTree(name_asc, id, pid, input); - var tree_desc = new SortedTree(name_desc, id, pid, input); + var tree_asc = new SortedTree(Order.name_asc, id, pid, input); + var tree_desc = new SortedTree(Order.name_desc, id, pid, input); Assert.IsTrue(tree_asc.SequenceEqual(orders)); Assert.IsTrue(tree_desc.SequenceEqual(orders_desc)); } From 58b9c27098e15c511ad631a9b4df7c0ce0e34876 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Mon, 29 Oct 2018 17:22:15 -0500 Subject: [PATCH 28/46] SortedTreeTests - new test: 'test_sorted_tree_state_transitions' --- .../spark_test/SortedTree/SortedTreeTests.cs | 158 +++++++++++++++++- 1 file changed, 156 insertions(+), 2 deletions(-) diff --git a/src/client/spark_test/SortedTree/SortedTreeTests.cs b/src/client/spark_test/SortedTree/SortedTreeTests.cs index 001008d98..03cc003a8 100644 --- a/src/client/spark_test/SortedTree/SortedTreeTests.cs +++ b/src/client/spark_test/SortedTree/SortedTreeTests.cs @@ -11,14 +11,14 @@ namespace bts.tests { [TestGroup] class SortedTreeTests { class Order { - const int no_parent = 0; + public const int no_parent = 0; public static readonly IComparer name_asc = Comparer.Create((l, r) => { int val_comp = l.name.CompareTo(r.name); return val_comp == 0 ? l.id.CompareTo(r.id) : val_comp; }); public static readonly IComparer name_desc = Comparer.Create((l, r) => { int val_comp = r.name.CompareTo(l.name); - return val_comp == 0 ? r.id.CompareTo(l.id) : val_comp; + return val_comp == 0 ? l.id.CompareTo(r.id) : val_comp; }); public Order(char name, int id) : this(name, id, no_parent) { } public Order(char name, int id, int parent) { @@ -84,6 +84,9 @@ namespace bts.tests { int id(Order order) => order.id; int pid(Order order) => order.parent_id; + /// + /// This test has been made obsolete by ; leaving it in place until the latter has been code-reviewed. + /// [Test] public void test_sordid_tree() { for (int x = 0; x < 10; ++x) { @@ -121,5 +124,156 @@ namespace bts.tests { Assert.SequenceEqual(children_of_g.Select(o => o.name), new [] { 'G' }); } + [Test] + public void test_sorted_tree_state_transitions() { + for (int x = 0; x < 10; ++x) { + var input = orders.ToList(); + input.Shuffle(); + /// after each step in run_tests, clear items via + run_tests(t => t.Clear()); + /// after each step in run_tests, remove each item via + run_tests(t => { + foreach (var v in input) + t.Remove(v); + }); + /// after each step in run_tests, remove each item via + run_tests(t => { + foreach (var v in input) + t.Remove(id(v)); + }); + // + void run_tests(Action> clear_action) { + /// Test Sequence: + /// - each step is performed twice: once with an ascending tree, and once with a descending tree. + /// - clear_action is executed/verified after each step (for both ascending/descending tree). + /// 1) Constructor initialization - all items are properly added when passed in through the constructor + /// 2) Insert - all items are properly added via + /// 3) AddAll - all items are properly added via + /// 4) Replace - all items are properly updated with a new value and/or parent via + + // 1) Constructor Initialization + var tree_asc = new SortedTree(Order.name_asc, id, pid, input); + var tree_desc = new SortedTree(Order.name_desc, id, pid, input); + assert_populated_then_clear(); + // 2) Insert + foreach (var o in input) { + tree_asc.Insert(o); + tree_desc.Insert(o); + } + assert_populated_then_clear(); + // 3) AddAll + tree_asc.AddAll(input); + tree_desc.AddAll(input); + assert_populated_then_clear(); + // 4) Replace + tree_asc.AddAll(input); + tree_desc.AddAll(input); + var modified_input = _randomize_values(input); + foreach (var v in modified_input) { + tree_asc.Replace(id(v), _ => v); + tree_desc.Replace(id(v), _ => v); + } + // verify asc + var (expected_asc, expected_nursery_asc) = _build_tree(modified_input, Order.name_asc); + assert_sequence_then_clear(tree_asc, expected_asc, expected_nursery_asc); + // verify desc + var (expected_desc, expected_nursery_desc) = _build_tree(modified_input, Order.name_desc); + assert_sequence_then_clear(tree_desc, expected_desc, expected_nursery_desc); + // DONE!! (local methods) + void assert_populated_then_clear() { + Assert.IsTrue(tree_asc.SequenceEqual(orders)); + Assert.IsTrue(tree_desc.SequenceEqual(orders_desc)); + clear_action(tree_asc); + clear_action(tree_desc); + Assert.IsFalse(tree_asc.Any()); + Assert.IsFalse(tree_desc.Any()); + } + + void assert_sequence_then_clear(SortedTree tree, List expected_items, HashSet expected_nursery) { + var tree_list = tree.ToList(); + Assert.AreEqual(tree_list.Count, expected_items.Count + expected_nursery.Count); + Assert.IsTrue(tree_list.Take(expected_items.Count).SequenceEqual(expected_items)); + // nursery order is non-deterministic, just make sure they are equivalent id-sets. + Assert.IsTrue(new HashSet(tree_list.Skip(expected_items.Count).Select(id)).SetEquals(expected_nursery)); + clear_action(tree); + Assert.IsFalse(tree.Any()); + } + } + } + } + + List _randomize_values(List input) { + // shuffle ids + var rand_ids = input.Select(id).ToList(); + rand_ids.Shuffle(); + // shuffle pids + var rand_pids = input.Select(pid).ToList(); + rand_pids.Shuffle(); + // shuffle names + var rand_names = input.Select(o => o.name).ToList(); + rand_names.Shuffle(); + var reshuffled = new List(); + var rand = new Random(); + for (int i = 0; i < input.Count; i++) { + int id = rand_ids[i]; + // keep id, just change value and pid (nee to make sure they aren't the same + int pk = rand_pids[i]; + while (pk == id) { + // id can't be the same as pk, keep trying until rand gives us something different (shouldn't throw a StackOverflow ex unless there is something wrong with the data) + pk = rand_pids[rand.Next(rand_pids.Count)]; + } + reshuffled.Add(new Order(rand_names[i], id, pk)); + } + return reshuffled; + } + + /// + /// Returns a list of Orders, hierarchically sorted, and a HashSet of Order-ids whose parent is not present. Parentless nodes are NOT included in the list of values. + /// + (List sorted_vals, HashSet nursery) _build_tree(IEnumerable items, IComparer comp) { + var roots = new List(); + var nursery_map = new Dictionary>(); + var raw_item_map = items.ToDictionary(id); + var node_map = new Dictionary(); + foreach (var v in items) { + int k = id(v); + int pk = pid(v); + TestNode node; + if (nursery_map.TryGetValue(k, out Dictionary inner)) { + // adopt orphans from nursery + node = new TestNode(v, inner); + nursery_map.Remove(k); + } else { + node = new TestNode(v); + } + node_map.Add(k, node); + if (pk.Equals(Order.no_parent)) { + roots.Add(node); + } else if (node_map.TryGetValue(pk, out TestNode pnode)) { + pnode.inner.Add(k, node); + } else { + if (!nursery_map.TryGetValue(pk, out Dictionary ndict)) { + nursery_map.Add(pk, ndict = new Dictionary()); + } + ndict.Add(k, node); + } + } + return (roots.OrderBy(n => n.value, comp).SelectMany(iterate_inner_vals).ToList(), new HashSet(nursery_map.Values.SelectMany(d => d.Values.SelectMany(iterate_inner_vals)).Select(id))); + // local methods + IEnumerable iterate_inner_vals(TestNode n) => + new[] { n.value }.Concat(n.inner.Values.OrderBy(cn => cn.value, comp).SelectMany(iterate_inner_vals)); + } + + class TestNode { + public TestNode(Order value) + : this(value, new Dictionary()) { } + + public TestNode(Order value, Dictionary inner) { + this.value = value; + this.inner = inner; + } + public Order value { get; set; } + public readonly Dictionary inner; + } } } From 12d637415842faf1eb75dfdbdd23a76c1ce50c60 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Mon, 29 Oct 2018 17:24:24 -0500 Subject: [PATCH 29/46] SortedTreeTests - reverse id-comp for name-descending comparer --- src/client/spark_test/SortedTree/SortedTreeTests.cs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/client/spark_test/SortedTree/SortedTreeTests.cs b/src/client/spark_test/SortedTree/SortedTreeTests.cs index 03cc003a8..ae420b9d1 100644 --- a/src/client/spark_test/SortedTree/SortedTreeTests.cs +++ b/src/client/spark_test/SortedTree/SortedTreeTests.cs @@ -1,11 +1,8 @@ using bts.collections; using bts.test; using System; -using System.Collections; using System.Collections.Generic; using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace bts.tests { [TestGroup] @@ -18,7 +15,7 @@ namespace bts.tests { }); public static readonly IComparer name_desc = Comparer.Create((l, r) => { int val_comp = r.name.CompareTo(l.name); - return val_comp == 0 ? l.id.CompareTo(r.id) : val_comp; + return val_comp == 0 ? r.id.CompareTo(l.id) : val_comp; }); public Order(char name, int id) : this(name, id, no_parent) { } public Order(char name, int id, int parent) { From 2243dc566eeedf285a859bffad259579609d873d Mon Sep 17 00:00:00 2001 From: John Colvin Date: Mon, 29 Oct 2018 21:25:40 -0500 Subject: [PATCH 30/46] move DictionaryExtensions to spark_core --- .../GenericCollectionExtensions.cs | 89 ------------------- .../Extensions/DictionaryExtensions.cs | 81 +++++++++++++++++ 2 files changed, 81 insertions(+), 89 deletions(-) create mode 100644 src/client/spark_core/Collections/Extensions/DictionaryExtensions.cs diff --git a/src/client/spark/core/GlobalExtensions/GenericCollectionExtensions.cs b/src/client/spark/core/GlobalExtensions/GenericCollectionExtensions.cs index 900a84bdb..33111b93e 100644 --- a/src/client/spark/core/GlobalExtensions/GenericCollectionExtensions.cs +++ b/src/client/spark/core/GlobalExtensions/GenericCollectionExtensions.cs @@ -427,95 +427,6 @@ namespace System.Collections.Generic { } } - public static class DictionaryExtensions - { - #region TryGetValue - public static bool TryGetValue(this Dictionary>> target, K k1, VK1 k2, VK2 k3, out V value) { - if (target.TryGetValue(k1, out Dictionary> d1) && d1.TryGetValue(k2, out Dictionary d2)) - return d2.TryGetValue(k3, out value); - value = default; - return false; - } - - public static bool TryGetValue(this IDictionary target, K k1, VK k2, out V value) where VKD : IDictionary { - if (target.TryGetValue(k1, out VKD d)) - return d.TryGetValue(k2, out value); - value = default; - return false; - } - #endregion - - public static void AddOrUpdate(this IDictionary dictionary, TKey key, Func add_value_factory, Action update_value) { - if (!dictionary.TryGetValue(key, out TValue value)) - dictionary.Add(key, add_value_factory(key)); - else update_value(key, value); - } - - public static void AddOrUpdate(this IDictionary dictionary, TKey key, Func add_value_factory, Func update_value) { - if (!dictionary.TryGetValue(key, out TValue value)) - dictionary.Add(key, add_value_factory(key)); - dictionary[key] = update_value(key, value); - } - - public static TValue GetOrAdd(this IDictionary dictionary, TKey key, Func add_value_factory) { - if (!dictionary.TryGetValue(key, out TValue value)) - dictionary.Add(key, value = add_value_factory(key)); - return value; - } - - public static void SetValue(this IDictionary target, K k1, VK k2, V value) where VKD : IDictionary { - if (!target.TryGetValue(k1, out VKD d)) - target.Add(k1, d = Activator.CreateInstance()); - d[k2] = value; - } - - public static IReadOnlyDictionary Join(this IReadOnlyDictionary dictionary, IReadOnlyDictionary merge_target) => - Join(dictionary, merge_target, (v1, v2) => (v1, v2)); - - public static IReadOnlyDictionary Join(this IReadOnlyDictionary dictionary, IReadOnlyDictionary merge_target, Func select) { - var iks = dictionary.Keys.Intersect(merge_target.Keys); - return new ReadOnlyDictionary(iks.ToDictionary(k => k, k => select(dictionary[k], merge_target[k]))); - } - - public static IReadOnlyDictionary LeftJoin(this IReadOnlyDictionary dictionary, IReadOnlyDictionary merge_target, - Func select, Func select_left_only) => - dictionary.ToDictionary(kv => kv.Key, kv => { - if (merge_target.TryGetValue(kv.Key, out TValue2 v2)) - return select(kv.Value, v2); - else - return select_left_only(kv.Value); - }); - - public static IReadOnlyDictionary SuperJoin(this IReadOnlyDictionary dictionary, IReadOnlyDictionary merge_target) { - var eks1 = dictionary.Keys.Except(merge_target.Keys); - var eks2 = merge_target.Keys.Except(dictionary.Keys); - var iks = dictionary.Keys.Intersect(merge_target.Keys); - return new ReadOnlyDictionary(eks1 - .Select(k => (k, dictionary[k], default(TValue2))) - .Concat(eks2.Select(k => (k, default(TValue1), merge_target[k]))) - .Concat(iks.Select(k => (k, dictionary[k], merge_target[k]))) - .ToDictionary(vals => vals.k, vals => (vals.Item2, vals.Item3)) - ); - } - - /// - /// If TryGetValue succeeds, result will be set with the corresponding value, and the key will be removed (returns true). - /// Otherwise, result is set to default, and nothing is removed (returns false). - /// - /// - /// - /// - /// - /// - /// - public static bool TryPopValue(this IDictionary dictionary, TKey key, out TValue result) { - if (dictionary.TryGetValue(key, out result)) { - dictionary.Remove(key); - return true; - } else return false; - } - } - public static class ListExtensions { #region InserSorted diff --git a/src/client/spark_core/Collections/Extensions/DictionaryExtensions.cs b/src/client/spark_core/Collections/Extensions/DictionaryExtensions.cs new file mode 100644 index 000000000..5f7576ad8 --- /dev/null +++ b/src/client/spark_core/Collections/Extensions/DictionaryExtensions.cs @@ -0,0 +1,81 @@ +using System.Collections.ObjectModel; +using System.Linq; + +namespace System.Collections.Generic { + public static class DictionaryExtensions { + public static void AddOrUpdate(this IDictionary dictionary, TKey key, Func add_value_factory, Action update_value) { + if (!dictionary.TryGetValue(key, out TValue value)) + dictionary.Add(key, add_value_factory(key)); + else update_value(key, value); + } + + public static void AddOrUpdate(this IDictionary dictionary, TKey key, Func add_value_factory, Func update_value) { + if (!dictionary.TryGetValue(key, out TValue value)) + dictionary.Add(key, add_value_factory(key)); + dictionary[key] = update_value(key, value); + } + + public static TValue GetOrAdd(this IDictionary dictionary, TKey key, Func add_value_factory) { + if (!dictionary.TryGetValue(key, out TValue value)) + dictionary.Add(key, value = add_value_factory(key)); + return value; + } + + public static IReadOnlyDictionary Join(this IReadOnlyDictionary dictionary, IReadOnlyDictionary merge_target) => + Join(dictionary, merge_target, (v1, v2) => (v1, v2)); + + public static IReadOnlyDictionary Join(this IReadOnlyDictionary dictionary, IReadOnlyDictionary merge_target, Func select) { + var iks = dictionary.Keys.Intersect(merge_target.Keys); + return new ReadOnlyDictionary(iks.ToDictionary(k => k, k => select(dictionary[k], merge_target[k]))); + } + + public static IReadOnlyDictionary LeftJoin(this IReadOnlyDictionary dictionary, IReadOnlyDictionary merge_target, + Func select, Func select_left_only) => + dictionary.ToDictionary(kv => kv.Key, kv => { + if (merge_target.TryGetValue(kv.Key, out TValue2 v2)) + return select(kv.Value, v2); + else + return select_left_only(kv.Value); + }); + + public static IReadOnlyDictionary SuperJoin(this IReadOnlyDictionary dictionary, IReadOnlyDictionary merge_target) { + var eks1 = dictionary.Keys.Except(merge_target.Keys); + var eks2 = merge_target.Keys.Except(dictionary.Keys); + var iks = dictionary.Keys.Intersect(merge_target.Keys); + return new ReadOnlyDictionary(eks1 + .Select(k => (k, dictionary[k], default(TValue2))) + .Concat(eks2.Select(k => (k, default(TValue1), merge_target[k]))) + .Concat(iks.Select(k => (k, dictionary[k], merge_target[k]))) + .ToDictionary(vals => vals.k, vals => (vals.Item2, vals.Item3)) + ); + } + + /// + /// Gets the value associated with the specified key, then removes the key if the lookup was successful. + /// + /// The type of keys in the dictionary. + /// The type of values in the dictionary. + /// A dictionary to try to get/remove a KeyValuePair from. + /// The key of the value to get/remove. + /// When this method returns, contains the value associated with the specified key, if the key is found; otherwise, the default value for the type of the value parameter. This parameter is passed uninitialized. + /// true if the dictionary contains an element with the specified key; otherwise, false. + public static bool TryPopValue(this IDictionary dictionary, TKey key, out TValue result) { + if (dictionary.TryGetValue(key, out result)) { + dictionary.Remove(key); + return true; + } else return false; + } + + /// + /// Gets the value associated with the specified key, then calls with the result if the lookup was successful. If returns true, the key is removed. + /// + /// The type of keys in the dictionary. + /// The type of values in the dictionary. + /// A dictionary to try to get/remove a KeyValuePair from. + /// The key of the value to get/remove. + /// When this method returns, contains the value associated with the specified key, if the key is found; otherwise, the default value for the type of the value parameter. This parameter is passed uninitialized. + /// true if the dictionary contains an element with the specified key, AND returns true; otherwise, false. + public static bool TryPopValueIf(this IDictionary dictionary, TKey key, Func should_pop) => + dictionary.TryGetValue(key, out TValue result) && should_pop(result) && dictionary.Remove(key); + } +} \ No newline at end of file From c8790f766ad5473679096c49bef6dda7a978065b Mon Sep 17 00:00:00 2001 From: John Colvin Date: Mon, 29 Oct 2018 21:38:19 -0500 Subject: [PATCH 31/46] SortedTree - replace AddAll() with Reset(), and support absent parents --- .../spark_core/Collections/SortedTree.cs | 74 +++++++++++-------- .../spark_test/SortedTree/SortedTreeTests.cs | 8 +- 2 files changed, 46 insertions(+), 36 deletions(-) diff --git a/src/client/spark_core/Collections/SortedTree.cs b/src/client/spark_core/Collections/SortedTree.cs index 737a8fdc0..5ee95c5b0 100644 --- a/src/client/spark_core/Collections/SortedTree.cs +++ b/src/client/spark_core/Collections/SortedTree.cs @@ -38,15 +38,13 @@ namespace bts.collections { /// /// comparer MUST be 100% deterministic, else Removes won't work right (SortedList uses a simple BinarySearch to find/remove items, and does NOT check item-equality). /// - public SortedTree(IComparer comparer, Func fnid, Func fnparent, TKey root_id, IEnumerable items=null) { + public SortedTree(IComparer comparer, Func fnid, Func fnparent, TKey root_id, IEnumerable items = null) { _comparer = Comparer.Create((l, r) => comparer.Compare(l.value, r.value)); id = fnid; parent = fnparent; _tree = new SortedList(_comparer); _root_id = root_id; - if (items != null) { - AddAll(items); - } + Reset(items); } public IEnumerable> RootNodes => _tree; @@ -59,13 +57,8 @@ namespace bts.collections { _replace(node, item); return false; } else { - var pk = parent(item); - if (_nursery.TryGetValue(k, out SortedList nursery_list)) { - _nursery.Remove(k); - _add_node(pk, node = new Node(item, nursery_list)); - } else { - _add_node(pk, node = new Node(item, _comparer)); - } + node = _init_node(item); + _insert_node(node); _index.Add(k, node); return true; } @@ -108,23 +101,39 @@ namespace bts.collections { return new SortedTree(comparer, id, parent, this); } - // Note: This method does not handle replacements correctly. - public void AddAll(IEnumerable items) { + public void Reset(IEnumerable items) { + if (items == null) return; + Clear(); // When adding multiple items we build the index first to avoid the children before parents problem foreach (var item in items) { - //_index[id(item)] = new Node(item, _comparer); _index.Add(id(item), new Node(item, _comparer)); } - // Assume all parents are in this collection - // There's not really anything else we can do foreach (var item in items) { // We are sure it is in the index because we just added it - var itemnode = _index[id(item)]; - if (_index.TryGetValue(parent(item), out Node pnode)) { - pnode.children.Add(itemnode); + var node = _index[id(item)]; + if (_nursery.TryPopValue(id(item), out SortedList nl)) { + if (node.children.Count == 0) { + node.children = nl; + } else if (node.children.Count > nl.Count) { + // merge nl into node.children + foreach (var cn in nl) + node.children.Add(cn); + } else { + // merge node.children into nl + foreach (var cn in node.children) + nl.Add(cn); + node.children = nl; + } } - else { - _tree.Add(itemnode); + TKey pk = parent(item); + if (pk.Equals(_root_id)) { + _tree.Add(node); + } else if (_index.TryGetValue(pk, out Node pnode)) { + pnode.children.Add(node); + } else { + if (!_nursery.TryGetValue(pk, out nl)) + _nursery.Add(pk, nl = new SortedList(_comparer)); + nl.Add(node); } } } @@ -175,20 +184,18 @@ namespace bts.collections { } } node.value = newval; - _add_node(pk, node); + _insert_node(node); } } + + Node _init_node(T item) => + _nursery.TryPopValue(id(item), out SortedList nursery_list) ? new Node(item, nursery_list) : new Node(item, _comparer); - void _remove_from_nursery(TKey pk, Node node) { - var nursery_list = _nursery[pk]; - nursery_list.Remove(node); - if (nursery_list.Count == 0) { - // no need to keep the nursey_list around anymore; remove it - _nursery.Remove(pk); - } - } - - void _add_node(TKey pk, Node node) { + /// + /// Inserts the node into the appropriate SortedList (i.e. roots, parent.inner, or nursery), but does NOT add it to , NOR check the nursery for any child-nodes waiting on this item. + /// + void _insert_node(Node node) { + TKey pk = parent(node.value); if (pk.Equals(_root_id)) { _tree.Add(node); } else if (_index.TryGetValue(pk, out Node pnode)) { @@ -201,6 +208,9 @@ namespace bts.collections { } } + void _remove_from_nursery(TKey pk, Node node) => + _nursery.TryPopValueIf(pk, nl => nl.Remove(node) && nl.Count == 0); + IEnumerable iterate(SortedList nodes) { foreach (var n in nodes) { yield return n.value; diff --git a/src/client/spark_test/SortedTree/SortedTreeTests.cs b/src/client/spark_test/SortedTree/SortedTreeTests.cs index ae420b9d1..cfb92d3f0 100644 --- a/src/client/spark_test/SortedTree/SortedTreeTests.cs +++ b/src/client/spark_test/SortedTree/SortedTreeTests.cs @@ -159,12 +159,12 @@ namespace bts.tests { } assert_populated_then_clear(); // 3) AddAll - tree_asc.AddAll(input); - tree_desc.AddAll(input); + tree_asc.Reset(input); + tree_desc.Reset(input); assert_populated_then_clear(); // 4) Replace - tree_asc.AddAll(input); - tree_desc.AddAll(input); + tree_asc.Reset(input); + tree_desc.Reset(input); var modified_input = _randomize_values(input); foreach (var v in modified_input) { tree_asc.Replace(id(v), _ => v); From e4ea5cd769ee5eeddb3b55b725f1dfd1a202e1e2 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Mon, 29 Oct 2018 23:56:08 -0500 Subject: [PATCH 32/46] SortedTreeTests - update comments, break up linq query for readability --- .../spark_test/SortedTree/SortedTreeTests.cs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/client/spark_test/SortedTree/SortedTreeTests.cs b/src/client/spark_test/SortedTree/SortedTreeTests.cs index cfb92d3f0..dbbbb33ed 100644 --- a/src/client/spark_test/SortedTree/SortedTreeTests.cs +++ b/src/client/spark_test/SortedTree/SortedTreeTests.cs @@ -145,7 +145,7 @@ namespace bts.tests { /// - clear_action is executed/verified after each step (for both ascending/descending tree). /// 1) Constructor initialization - all items are properly added when passed in through the constructor /// 2) Insert - all items are properly added via - /// 3) AddAll - all items are properly added via + /// 3) Reset - all items are properly added via /// 4) Replace - all items are properly updated with a new value and/or parent via // 1) Constructor Initialization @@ -158,7 +158,7 @@ namespace bts.tests { tree_desc.Insert(o); } assert_populated_then_clear(); - // 3) AddAll + // 3) Reset tree_asc.Reset(input); tree_desc.Reset(input); assert_populated_then_clear(); @@ -255,10 +255,16 @@ namespace bts.tests { ndict.Add(k, node); } } - return (roots.OrderBy(n => n.value, comp).SelectMany(iterate_inner_vals).ToList(), new HashSet(nursery_map.Values.SelectMany(d => d.Values.SelectMany(iterate_inner_vals)).Select(id))); + return (roots.OrderBy(n => n.value, comp) + .SelectMany(iterate_inner_vals).ToList(), + new HashSet(nursery_map.Values + .SelectMany(d => d.Values.SelectMany(iterate_inner_vals)) + .Select(id))); // local methods IEnumerable iterate_inner_vals(TestNode n) => - new[] { n.value }.Concat(n.inner.Values.OrderBy(cn => cn.value, comp).SelectMany(iterate_inner_vals)); + new[] { n.value }.Concat( + n.inner.Values.OrderBy(cn => cn.value, comp) + .SelectMany(iterate_inner_vals)); } class TestNode { From 64bef0bd061efb2678e2b8d08d68acbacd15785a Mon Sep 17 00:00:00 2001 From: John Colvin Date: Mon, 29 Oct 2018 23:57:07 -0500 Subject: [PATCH 33/46] missed one --- src/client/spark_test/SortedTree/SortedTreeTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client/spark_test/SortedTree/SortedTreeTests.cs b/src/client/spark_test/SortedTree/SortedTreeTests.cs index dbbbb33ed..46ce10e80 100644 --- a/src/client/spark_test/SortedTree/SortedTreeTests.cs +++ b/src/client/spark_test/SortedTree/SortedTreeTests.cs @@ -213,7 +213,7 @@ namespace bts.tests { var rand = new Random(); for (int i = 0; i < input.Count; i++) { int id = rand_ids[i]; - // keep id, just change value and pid (nee to make sure they aren't the same + // keep id, just change value and pid (need to make sure they aren't the same) int pk = rand_pids[i]; while (pk == id) { // id can't be the same as pk, keep trying until rand gives us something different (shouldn't throw a StackOverflow ex unless there is something wrong with the data) From d36a5b30e6b880b3fc6db384c7deab3e2a0e8512 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Tue, 30 Oct 2018 15:47:31 -0500 Subject: [PATCH 34/46] SortedTree - change Replace() to TryReplace() --- .../OrderBook/OrderBookRowCollection.cs | 20 +++++++++++++------ src/client/spark/Orders/OrderRecordTuple.cs | 1 + .../spark_core/Collections/SortedTree.cs | 15 ++++++++------ .../spark_test/SortedTree/SortedTreeTests.cs | 6 +++--- 4 files changed, 27 insertions(+), 15 deletions(-) diff --git a/src/client/spark/Modules/Trading/OrderBook/OrderBookRowCollection.cs b/src/client/spark/Modules/Trading/OrderBook/OrderBookRowCollection.cs index ef1113f5a..a014a05bc 100644 --- a/src/client/spark/Modules/Trading/OrderBook/OrderBookRowCollection.cs +++ b/src/client/spark/Modules/Trading/OrderBook/OrderBookRowCollection.cs @@ -1,4 +1,5 @@ -using bts.api.core; +using bts; +using bts.api.core; using bts.collections; using bts.utils; using mbts.utils; @@ -18,6 +19,11 @@ namespace mbts.spark { Func _filter; bool _is_refresh_pending; int _deferment_count = 0; + /// When processing changed-items, the _tree.Replace() method is used to update the changed row. _tree.Replace() is not expected to fail (return false), + /// but if it does, we don't want to overwhelm the logs with a bunch of useless noise in case it starts happening repeatedly, hence the int fields below. + /// , , . + const int _max_replace_misses_logged = 10; + int _n_replace_misses_logged; public OrderBookRowCollection(Func row_factory, IComparer comparer) { _row_factory = row_factory; @@ -146,11 +152,13 @@ namespace mbts.spark { } else { foreach (OrderRecordTuple ort in table.removed) _tree.Remove(ort.order.Key); - foreach (Change change in table.changes) - _tree.Replace(change.New.order.Key, r => { - r.update(change.New); - return r; - }); + foreach (Change change in table.changes) { + if (!_tree.TryReplace(change.New.order.Key, r => { r.update(change.New); return r; })) { + // uh oh, replace failed! Let's log this, but not a million times (this seems like the kind of error that, if it happens once, could happen a lot). + if (_n_replace_misses_logged++ < _max_replace_misses_logged) + Log.Ui.error($"{nameof(OrderBookRowCollection)} failed to process Changed {nameof(OrderRecordTuple)} in update (old:{change.Old.key}, new:{change.New.key}). This is the '{_n_replace_misses_logged}' time this has happened in this OrderBook."); + } + } foreach (OrderRecordTuple ort in table.added) _tree.Insert(_row_factory(ort)); impacted_branch_roots = diff --git a/src/client/spark/Orders/OrderRecordTuple.cs b/src/client/spark/Orders/OrderRecordTuple.cs index 4e420335b..4e6255364 100644 --- a/src/client/spark/Orders/OrderRecordTuple.cs +++ b/src/client/spark/Orders/OrderRecordTuple.cs @@ -42,5 +42,6 @@ namespace mbts.spark { public override bool Equals(object obj) => Equals(obj as OrderRecordTuple); public override int GetHashCode() => key.GetHashCode(); + public override string ToString() => $"order:{order.Key}, sec:{security_md.display_name()}"; } } \ No newline at end of file diff --git a/src/client/spark_core/Collections/SortedTree.cs b/src/client/spark_core/Collections/SortedTree.cs index 5ee95c5b0..f1da09def 100644 --- a/src/client/spark_core/Collections/SortedTree.cs +++ b/src/client/spark_core/Collections/SortedTree.cs @@ -64,12 +64,15 @@ namespace bts.collections { } } - public void Replace(TKey k, Func replace) { - if (!_index.TryGetValue(k, out Node node)) - throw new KeyNotFoundException($"Replace was called with unknown key ({k})."); - var newval = replace(node.value); - Log.assert(id(newval).Equals(k)); - _replace(node, newval); + public bool TryReplace(TKey k, Func replace) { + if (!_index.TryGetValue(k, out Node node)) { + return false; + } else { + var newval = replace(node.value); + Log.assert(id(newval).Equals(k)); + _replace(node, newval); + return true; + } } diff --git a/src/client/spark_test/SortedTree/SortedTreeTests.cs b/src/client/spark_test/SortedTree/SortedTreeTests.cs index 46ce10e80..8b1f903e1 100644 --- a/src/client/spark_test/SortedTree/SortedTreeTests.cs +++ b/src/client/spark_test/SortedTree/SortedTreeTests.cs @@ -146,7 +146,7 @@ namespace bts.tests { /// 1) Constructor initialization - all items are properly added when passed in through the constructor /// 2) Insert - all items are properly added via /// 3) Reset - all items are properly added via - /// 4) Replace - all items are properly updated with a new value and/or parent via + /// 4) Replace - all items are properly updated with a new value and/or parent via // 1) Constructor Initialization var tree_asc = new SortedTree(Order.name_asc, id, pid, input); @@ -167,8 +167,8 @@ namespace bts.tests { tree_desc.Reset(input); var modified_input = _randomize_values(input); foreach (var v in modified_input) { - tree_asc.Replace(id(v), _ => v); - tree_desc.Replace(id(v), _ => v); + Assert.IsTrue(tree_asc.TryReplace(id(v), _ => v)); + Assert.IsTrue(tree_desc.TryReplace(id(v), _ => v)); } // verify asc var (expected_asc, expected_nursery_asc) = _build_tree(modified_input, Order.name_asc); From dc8b0b83597f704e72e307de683d74e7bc535607 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Tue, 30 Oct 2018 15:50:51 -0500 Subject: [PATCH 35/46] OrderBookRowCollection - reword comment --- .../spark/Modules/Trading/OrderBook/OrderBookRowCollection.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client/spark/Modules/Trading/OrderBook/OrderBookRowCollection.cs b/src/client/spark/Modules/Trading/OrderBook/OrderBookRowCollection.cs index a014a05bc..6866c5874 100644 --- a/src/client/spark/Modules/Trading/OrderBook/OrderBookRowCollection.cs +++ b/src/client/spark/Modules/Trading/OrderBook/OrderBookRowCollection.cs @@ -19,7 +19,7 @@ namespace mbts.spark { Func _filter; bool _is_refresh_pending; int _deferment_count = 0; - /// When processing changed-items, the _tree.Replace() method is used to update the changed row. _tree.Replace() is not expected to fail (return false), + /// When processing changed-items, the method is used to update the changed row. It is not expected to fail (return false), /// but if it does, we don't want to overwhelm the logs with a bunch of useless noise in case it starts happening repeatedly, hence the int fields below. /// , , . const int _max_replace_misses_logged = 10; From 2a295237a64e34db62ddd796d2de60b9d896bf35 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Tue, 30 Oct 2018 15:54:04 -0500 Subject: [PATCH 36/46] refactor string extension ic_equals -> iequals --- src/client/spark/Modules/Trading/OrderBook/OrderBookColumns.cs | 2 +- src/client/spark_core/utils/GlobalExtensions.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/client/spark/Modules/Trading/OrderBook/OrderBookColumns.cs b/src/client/spark/Modules/Trading/OrderBook/OrderBookColumns.cs index d8d1ebfcb..1ab09d927 100644 --- a/src/client/spark/Modules/Trading/OrderBook/OrderBookColumns.cs +++ b/src/client/spark/Modules/Trading/OrderBook/OrderBookColumns.cs @@ -364,7 +364,7 @@ namespace mbts.spark { col.SortDirection = null; if (state.sorting is IReadOnlyList sorting) { foreach (var sc in sorting) { - this.OfType().ApplyToFirst(col => col.id.ic_equals(sc.col_name), + this.OfType().ApplyToFirst(col => col.id.iequals(sc.col_name), col => { col.SortDirection = sc.is_ascending ? ListSortDirection.Ascending : ListSortDirection.Descending; _active_comparers.Add(col); diff --git a/src/client/spark_core/utils/GlobalExtensions.cs b/src/client/spark_core/utils/GlobalExtensions.cs index 77875884c..0f231a633 100644 --- a/src/client/spark_core/utils/GlobalExtensions.cs +++ b/src/client/spark_core/utils/GlobalExtensions.cs @@ -19,7 +19,7 @@ namespace mbts { } return sb.ToString(); } - public static bool ic_equals(this string str, string comparand) => + public static bool iequals(this string str, string comparand) => string.Equals(str, comparand, StringComparison.CurrentCultureIgnoreCase); public static T choose(this Random r, IEnumerable range) { From 37c9707c635afbcce1639cade63240c344e6e8d5 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Tue, 30 Oct 2018 16:55:49 -0500 Subject: [PATCH 37/46] DeleteSyntheticDaemon - show support dialog if delete request fails due to working-orders verification timeout --- src/client/spark/Daemons/DeleteSyntheticDaemon.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/client/spark/Daemons/DeleteSyntheticDaemon.cs b/src/client/spark/Daemons/DeleteSyntheticDaemon.cs index 97c646f82..15bc8d00c 100644 --- a/src/client/spark/Daemons/DeleteSyntheticDaemon.cs +++ b/src/client/spark/Daemons/DeleteSyntheticDaemon.cs @@ -12,11 +12,13 @@ namespace bts { [Export, Export(typeof(IShellDaemon))] class DeleteSyntheticDaemon : IShellDaemon, IDisposable { readonly OrdersModel _orders_model; + readonly ErrorReporter _error_reporter; IDisposable _dispose; [ImportingConstructor] - public DeleteSyntheticDaemon(IBus bus, OrdersModel orders_model) { + public DeleteSyntheticDaemon(IBus bus, OrdersModel orders_model, ErrorReporter error_reporter) { _orders_model = orders_model; + _error_reporter = error_reporter; _dispose = bus.subscribe(SparkTags.SyntheticDeleteTag) .OnDispatcher() .Subscribe(_delete_synthetic); @@ -28,6 +30,7 @@ namespace bts { async void _delete_synthetic(uint id) { var (orders, orders_success) = await _orders_model.on_orders.FirstAsyncTask(SparkUtils.DefaultTimeOut); if (!orders_success) { + _error_reporter.report_error($"Unable to delete synthetic security ({id}): unable to determine whether or not security ({id}) has working orders (deletion is not permitted while there are working orders)."); Log.Ui.warn($"Ignoring request to delete synthetic security ({id}) because the orders lookup timed out (must confirm no working orders)."); } else if (orders.state.Values.Any(o => o.Def.SecurityKey == id && o.is_non_terminal())) { Log.Ui.warn($"Ignoring request to delete synthetic security ({id}) because non-terminal-orders were detected."); From d946cb476576fe7bc11a8910a1e66c29372cd8f8 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Tue, 30 Oct 2018 16:57:15 -0500 Subject: [PATCH 38/46] OrderBookWindowFactory - ensure default UserSettings.order_book_grid != null --- .../spark/Modules/Trading/OrderBook/OrderBookWindowFactory.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/client/spark/Modules/Trading/OrderBook/OrderBookWindowFactory.cs b/src/client/spark/Modules/Trading/OrderBook/OrderBookWindowFactory.cs index dfb363438..77f1a5b14 100644 --- a/src/client/spark/Modules/Trading/OrderBook/OrderBookWindowFactory.cs +++ b/src/client/spark/Modules/Trading/OrderBook/OrderBookWindowFactory.cs @@ -36,7 +36,8 @@ namespace bts.shell { public override Task load(OrderBookStartupData state) { var settings_manager = new WindowSettingsManager(_settings_hub, state.grid_settings_wrapper(), - us => us.order_book_grid, (us, s) => us.with(order_book_grid: s)); + us => us.order_book_grid ?? OrderBookStartupData.DefaultGrid, + (us, s) => us.with(order_book_grid: s)); var w = new OrderBookWindow(_model, this, _bus, _notification_service, state, SparkCommands.ShowOrders.icon, settings_manager.as_non_shared()); return Task.FromResult(w); } From 87437883a63806117aae5ba3b7778a72bd2332f5 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Tue, 30 Oct 2018 17:03:29 -0500 Subject: [PATCH 39/46] SortedTree - in Reset() method, clear existing items if new_items are null --- src/client/spark_core/Collections/SortedTree.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client/spark_core/Collections/SortedTree.cs b/src/client/spark_core/Collections/SortedTree.cs index f1da09def..3a4db65ea 100644 --- a/src/client/spark_core/Collections/SortedTree.cs +++ b/src/client/spark_core/Collections/SortedTree.cs @@ -105,8 +105,8 @@ namespace bts.collections { } public void Reset(IEnumerable items) { - if (items == null) return; Clear(); + if (items == null) return; // When adding multiple items we build the index first to avoid the children before parents problem foreach (var item in items) { _index.Add(id(item), new Node(item, _comparer)); From 74aed2144ded665cd13eb9b4193c89bc6e928cb9 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Tue, 30 Oct 2018 17:16:18 -0500 Subject: [PATCH 40/46] OrderBookColumnCollection - add comment explaining the preference for columns that implement ICVOrderBookColumn --- src/client/spark/Modules/Trading/OrderBook/OrderBookColumns.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/client/spark/Modules/Trading/OrderBook/OrderBookColumns.cs b/src/client/spark/Modules/Trading/OrderBook/OrderBookColumns.cs index 1ab09d927..acb550f67 100644 --- a/src/client/spark/Modules/Trading/OrderBook/OrderBookColumns.cs +++ b/src/client/spark/Modules/Trading/OrderBook/OrderBookColumns.cs @@ -414,6 +414,8 @@ namespace mbts.spark { Clear(); } + /// ONLY supports sorting/filtering behavior for columns that implement . + /// This is because OrderBook, unlike the other windows (as of 10/30/2018), does not use as the sorting/filtering model. protected override void InsertItem(int index, ColumnBase item) { if (item is ICVOrderBookColumn col) { try { From 4ddb8fe9c5e100ef2fa55b8ddf3408e8d7aea519 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Tue, 30 Oct 2018 17:17:32 -0500 Subject: [PATCH 41/46] OrderBookColumnCollection - add 'finally' block to try/catch in InsertItem() --- src/client/spark/Modules/Trading/OrderBook/OrderBookColumns.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/client/spark/Modules/Trading/OrderBook/OrderBookColumns.cs b/src/client/spark/Modules/Trading/OrderBook/OrderBookColumns.cs index acb550f67..d4f26aff7 100644 --- a/src/client/spark/Modules/Trading/OrderBook/OrderBookColumns.cs +++ b/src/client/spark/Modules/Trading/OrderBook/OrderBookColumns.cs @@ -423,8 +423,7 @@ namespace mbts.spark { base.InsertItem(index, item); } catch { _unhook(col); - throw; - } + } finally { } } else { Log.Ui.warn($"{nameof(OrderBookColumnCollection)} added a column that is not of type {nameof(ICVOrderBookColumn)}"); base.InsertItem(index, item); From 4d8a2f28d3203e3f07210235a8352ab88d07d7e2 Mon Sep 17 00:00:00 2001 From: Jake Norbut Date: Wed, 31 Oct 2018 08:44:20 -0500 Subject: [PATCH 42/46] SPARK-1736 - client-based ICMP ping --- .../spark/Daemons/ShellLatencyDaemon.cs | 64 ++++++++++++++++++- src/client/spark/view/Spark/SparkWindow.cs | 3 +- src/lib/config/protos/UI.proto | 10 +++ 3 files changed, 74 insertions(+), 3 deletions(-) diff --git a/src/client/spark/Daemons/ShellLatencyDaemon.cs b/src/client/spark/Daemons/ShellLatencyDaemon.cs index 827e80d49..0f98fb35a 100644 --- a/src/client/spark/Daemons/ShellLatencyDaemon.cs +++ b/src/client/spark/Daemons/ShellLatencyDaemon.cs @@ -1,11 +1,13 @@ using bts.api.core; using bts.metrics; +using mbts; using mbts.config; using mbts.ui; using System; using System.Collections.Generic; using System.ComponentModel.Composition; using System.Linq; +using System.Net.NetworkInformation; using System.Text; using System.Threading.Tasks; @@ -18,6 +20,7 @@ namespace bts.perf { readonly string _version; readonly string _username; IDisposable _subs; + Ping _pinger = new Ping(); [ImportingConstructor] public ShellLatencyDaemon(ShellLatencyService service, [Import("username")] string user) { @@ -25,7 +28,12 @@ namespace bts.perf { _version = Configuration.get().version; _username = user; _hostname = Environment.MachineName; - _subs = _service.subscribei_clock().Subscribe(on_tick); + + _forever_ping_host(_pinger); + _subs = Disposable.Create( + _pinger, + _service.subscribei_clock().Subscribe(on_tick) + ); } void on_tick(Update update) { @@ -40,8 +48,60 @@ namespace bts.perf { } } + async void _forever_ping_host(Ping pinger) { + var config = Configuration.get(); + string server_ip = Configuration.get().config_ip; + int delay = config.workloop_milliseconds_interval; + int _current_missed_ping_count = 0; + + if (config.shell_ping_is_enabled && pinger != null) { + while (true) { + try { + await Task.Delay(delay); + var response = await pinger.SendPingAsync(server_ip, config.timeout_milliseconds_threshold); + if (response.Status == IPStatus.Success) { + _current_missed_ping_count = 0; + if (response.RoundtripTime > config.logging_milliseconds_threshold) { + Log.Ui.warn($"Ping response not recieved in a timely manner:\n" + + $"\tAddress:\t{response.Address}\n" + + $"\tRoundTrip:\t{response.RoundtripTime} ms"); + } + // potentially reset delay to initial work period (no-op otherwise) + delay = config.workloop_milliseconds_interval; + } else { + _current_missed_ping_count++; + delay = _get_exp_delay(_current_missed_ping_count, config.workloop_milliseconds_interval, config.maximum_backoff_milliseconds); + Log.Ui.warn($"Ping was not successful. Ping response: {response.Status}. Missed ping count: {_current_missed_ping_count}. Backing off for {delay} milliseconds."); + } + } catch (PingException e) { + _current_missed_ping_count++; + delay = _get_exp_delay(_current_missed_ping_count, config.workloop_milliseconds_interval, config.maximum_backoff_milliseconds); + Log.Ui.error($"Ping exception. {e}\nBacking off for {delay} milliseconds."); + } + } // end while + } + + // since we're returning milliseconds here up to a 1 minute delay + // we don't need to go crazy with the power, else we will get + // an ArgumentOutOfRangeExecption. + // Int.MaxValue = 2^31, so we need to be below that + // Also, even if we're under the 2^31 int.maxvalue, we don't want to be multed over it + // so that is why we have checked() here. + int _get_exp_delay(int retry_num, int min_interval, int max_delay) { + // add some jitter up to half the max delay so we don't + // have a ping spike from all clients at once + var jitter_amount = new Random().Next(max_delay / 2); + int power_factor = (int)Math.Pow(2, Math.Min(retry_num, 32) - 1); + try { + return Math.Min(checked(min_interval * power_factor + jitter_amount), max_delay + jitter_amount); + } catch (OverflowException) { + return max_delay + jitter_amount; + } + } + } + public void Dispose() { - throw new NotImplementedException(); + Disposable.dispose(ref _subs); } } } diff --git a/src/client/spark/view/Spark/SparkWindow.cs b/src/client/spark/view/Spark/SparkWindow.cs index a65b7d983..07774e7c7 100644 --- a/src/client/spark/view/Spark/SparkWindow.cs +++ b/src/client/spark/view/Spark/SparkWindow.cs @@ -19,6 +19,7 @@ namespace bts.spark { TITLE_PANEL_HEIGHT = 30, COLUMN_HEADER_HEIGHT = 27, SPARK_MENU_ITEM_HEIGHT = 45, + GRID_FOOTER_HEIGHT = 30, ABSOLUTE_MIN_HEIGHT = 230; protected static readonly TimeSpan CONTENT_MEASURE_WAIT_PERIOD = TimeSpan.FromSeconds(0.3); @@ -60,7 +61,7 @@ namespace bts.spark { public abstract UIElement Content { get; } public virtual IEnumerable Commands => Enumerable.Empty(); public virtual double MinWidth => 400; - public virtual double MinHeight => Math.Max(ABSOLUTE_MIN_HEIGHT, TITLE_PANEL_HEIGHT + COLUMN_HEADER_HEIGHT + Commands.Count() * SPARK_MENU_ITEM_HEIGHT); + public virtual double MinHeight => Math.Max(ABSOLUTE_MIN_HEIGHT, TITLE_PANEL_HEIGHT + COLUMN_HEADER_HEIGHT + GRID_FOOTER_HEIGHT + Commands.Count() * SPARK_MENU_ITEM_HEIGHT); public ShellWindowUI GetContent() => new ShellWindowUI(new SparkWindowView(_bus, Commands, Content)); diff --git a/src/lib/config/protos/UI.proto b/src/lib/config/protos/UI.proto index 489b55328..55ab87a4b 100644 --- a/src/lib/config/protos/UI.proto +++ b/src/lib/config/protos/UI.proto @@ -56,4 +56,14 @@ message LadderKnobs { optional uint32 seconds_to_recenter = 3 [default = 300]; optional uint32 max_open_ladders = 4 [default = 75]; optional uint32 security_timeout_secs = 5 [default = 60]; +} + +message ShellPingOptions { + optional bool shell_ping_is_enabled = 1 [default = true]; + optional int32 workloop_milliseconds_interval = 2 [default = 5000]; + // 4000 is default Windows ping timeout as specified here: + // https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-xp/bb490968(v=technet.10) + optional int32 timeout_milliseconds_threshold = 3 [default = 4000]; + optional int32 logging_milliseconds_threshold = 4 [default = 25]; + optional int32 maximum_backoff_milliseconds = 5 [default = 60000]; } \ No newline at end of file From 3df7156ba3696dff4f69d6cf3e269dd4a82815b0 Mon Sep 17 00:00:00 2001 From: Eric Harding Date: Wed, 31 Oct 2018 10:34:24 -0400 Subject: [PATCH 43/46] Disable entitlement checks in QueryServer --- src/lib/md/relay/QueryServer.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lib/md/relay/QueryServer.cpp b/src/lib/md/relay/QueryServer.cpp index 6cbfcfc08..91f1938cf 100644 --- a/src/lib/md/relay/QueryServer.cpp +++ b/src/lib/md/relay/QueryServer.cpp @@ -429,7 +429,8 @@ struct MDRelayServer : bts::co2::MarketDataRelayBase { api::user::TokenPayload payload; auto ents = _entitlements; const Entitlements* user_entitlement = nullptr; - bool bypass_entitlement = false; + // Temporarily bypass all entitlements + bool bypass_entitlement = true; if (api::auth::decode_token(request.token(), &payload)) { bypass_entitlement = payload.wheel(); if (!bypass_entitlement) { From df516fbd2d77c9acca9a8b82776b49bdf1501892 Mon Sep 17 00:00:00 2001 From: Jake Norbut Date: Wed, 31 Oct 2018 13:02:22 -0500 Subject: [PATCH 44/46] ICMP ping - increasing default logging threshold --- src/lib/config/protos/UI.proto | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/config/protos/UI.proto b/src/lib/config/protos/UI.proto index 55ab87a4b..7810d9cab 100644 --- a/src/lib/config/protos/UI.proto +++ b/src/lib/config/protos/UI.proto @@ -64,6 +64,6 @@ message ShellPingOptions { // 4000 is default Windows ping timeout as specified here: // https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-xp/bb490968(v=technet.10) optional int32 timeout_milliseconds_threshold = 3 [default = 4000]; - optional int32 logging_milliseconds_threshold = 4 [default = 25]; + optional int32 logging_milliseconds_threshold = 4 [default = 50]; optional int32 maximum_backoff_milliseconds = 5 [default = 60000]; } \ No newline at end of file From a418f47d1c17651f6b1e53136eb189e8d72698e5 Mon Sep 17 00:00:00 2001 From: Jake Norbut Date: Wed, 31 Oct 2018 14:36:57 -0500 Subject: [PATCH 45/46] Updating Sounds test --- .../settings/SoundAndNotificationsTest.txt | 72 ++++++------------- 1 file changed, 20 insertions(+), 52 deletions(-) diff --git a/testing/manual/settings/SoundAndNotificationsTest.txt b/testing/manual/settings/SoundAndNotificationsTest.txt index 0d9f61ab2..27a3bcf9e 100644 --- a/testing/manual/settings/SoundAndNotificationsTest.txt +++ b/testing/manual/settings/SoundAndNotificationsTest.txt @@ -14,21 +14,16 @@ Sound and Notifications Test (15 min)------------------------------------------- -------------------------------------------------------------------------------- 0) Prepare: + - Sounds are enabled by default, but we now have a key that can enable/disable them from the server as well. This was necessary to prevent crashing on systems without connected sound devices. If this key exists in your spark.conf, please make sure it's enabled = true (or simply remove it) +SoundNotifications { + enabled = true +} + - open an OrderBook, then click the Window Settings button in the Spark Menu to open the OWSD. - *add a sound trigger. -1) Sound Preview/Author Credentials: +1) Sound Preview: First: check "Play Sound" (if it is not already checked) - Perform the following steps for each trigger in the dropdown, the variable sound will be reffered to as 'X' - - select 'X' - - hover over the "?" help button next to the dropdown - - the tool tip should contain some basic info about 'X': - i) [Required] the official name (it may not exactly match the name in the dropdown, but it should be similar in subject matter, e.g. "Reload" vs "1911 Reload") - ii) [Required] the author's name or username (e.g. "zstewart09") - iii) [Optional] a url to the author's own site/account page (e.g. https://freesound.org/people/kianda/sounds/) - - the last line in the tool tip should say: "Click to preview, Ctrl + click to copy to clipboard" - - click the "?" button to confirm the preview works, - - ctrl + click the "?" button, then paste the result in a text editor, it should be identical to the tool tip (except the last line, with clicking instructions) 2) Duplicate sound triggers do not cause crash: - *delete the sound trigger you added in the previous step. @@ -85,47 +80,8 @@ Prepare: open a ladder for a security that has steady market data activity (in t - enter a limit order ACROSS the market to trigger an immediate fill. - this time, the "When order is filled" sound should play; the "When order is canceled" sound should not be heard. -7) Toast Notifications: - A) Simultaneously with sounds: - - in the OWSD, check "Show Notification" for both the "filled" and "canceled" sound-triggers, then click apply. - - enter a 1 lot limit order across the market to trigger an immediate fill. - - as soon as it is filled, you should see a notification AND hear the sound. - B) Without sounds: - - in the OWSD, uncheck "Play Sound" in the "When order is filled" trigger. - - you should be a pro by now, no more redundant baby talk: trigger an immediate fill. - - you should see the filled notification, but not hear the sound. - C) Rapid fire fills: - - open a Fills window. - - trigger a rapid succession of 1 lot limit order fills as you did earlier, by repeatedly clicking on a price well-across the market - - the toast notifications should keep pace with the fills, as they arrive in the Fills window. - - if it is hard to tell, most likely that means it is keeping up. - - if the notifications continue to arrive more than a few seconds after the fills window has finished updating. - D) Settings: - First: in the OWSD, check "Play Sounds," then click OK (both sounds and notifications should be enabled now). - ----Notes--------------------------------------------------------------------------------------------------------------- - - you will be opening the Notification Settings Dialog (NSD), there are only a couple of properties in this form, - so there is no "Apply" button, only an "OK" button which applies the changes and closes the window. - the steps will not tell you to open the NSD again each time you need to change a different setting, but yes, that is what you need to do. - If this is terribly annoying, tell John and he might add an "Apply" button for you ;) - - this dialog contains "application-level" settings, which override Window settings. - - if sounds/notifications are disabled here, any sounds/notifications in OrderBook Window Settings will be ignored. - - COMING SOON: if they are disabled, the Orderbook settings dialog will indicate this to the user, - to avoid potential frustration they might endure as they wonder why the notifications/sounds aren't working. - ------------------------------------------------------------------------------------------------------------------------- - - from the launcher: Settings -> Settings to open the Notification Settings dialog. - Note: this dialog also has 1 setting for sounds. - - disable sounds (this should be obvious, it's just a checkbox), then click OK. - - trigger a fill, and verify that the notification is shown, but the sound is not heard. - - in the notification settings dialog, disable toast notifications, the click OK. - - trigger a fill, and verify that neither the notification nor the sound are shown/played. - - enable both sounds and notifications again. - - trigger a fill, and verify that the sound is heard, and the notification shown. - - in the NSD, change the Toast Notification "Flash Duration" to 1 second (or any duration that is different enough from the current, so that the difference is easily detectable), click OK. - - trigger a fill, the notification box should disappear after thet appropriate time. - - in the NSD, chagne the Toast Notification "Mode" to "Manual" (the duration control should be disabled now). - - trigger a fill, the NSD should NOT dissappear. -8) Saved with workspace: +7) Saved with workspace: ----Note---------------------------------------------------------------------------------------------------------------------- - you have completed the sound-functionality testing, don't be alarmed by the 4 sound triggers you are about to add. - this step just requires comparing 2 images of the settings dialog to confirm that they were retained with the workspace. @@ -136,11 +92,23 @@ Prepare: open a ladder for a security that has steady market data activity (in t - you may replace sounds referred to as 'A' and 'B' with whatever you like; if such free will is too cumbersome, A=Game Sound Correct, B=Powerup) - the steps won't specifically say "check Play Sound," but if the trigger has a sound, check it, otherwise don't. i) "When order is filled", "(any order)", Sound 'A' - ii) "When order is filled", "(choose securities)" pick any 2 securities, Sound 'B', enable "Show Notifications" + ii) "When order is filled", "(choose securities)" pick any 2 securities, Sound 'B' iii) "When order is canceled", "(any order)", Sound 'B' - close the shell, then restart it with the "Where I left off on" workspace. - open the OWSD to verify that the sound triggers were saved with the workspace. +8) Test shell does not crash when no sound card present + - use a machine that does not have a sound card, such as eh-2 (check with Eric first) + - update config to turn off sounds + +SoundNotifications { + enabled = false +} + - restart your shell to pick up your config change + - Pressing the Preview Sound button should not crash the shell + - Having a sound notification triggered by the orderbook should not crash the shell + + TEST COMPLETE----------------------------------------------------------------- From 1966c8e0c678d6ff052ebdd2682b52cba5c4b3d1 Mon Sep 17 00:00:00 2001 From: Eric Harding Date: Wed, 31 Oct 2018 15:49:30 -0400 Subject: [PATCH 46/46] always bypass entitlement take 2 --- src/lib/md/relay/QueryServer.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib/md/relay/QueryServer.cpp b/src/lib/md/relay/QueryServer.cpp index 91f1938cf..7af0043e9 100644 --- a/src/lib/md/relay/QueryServer.cpp +++ b/src/lib/md/relay/QueryServer.cpp @@ -429,15 +429,15 @@ struct MDRelayServer : bts::co2::MarketDataRelayBase { api::user::TokenPayload payload; auto ents = _entitlements; const Entitlements* user_entitlement = nullptr; - // Temporarily bypass all entitlements - bool bypass_entitlement = true; + bool bypass_entitlement = false; if (api::auth::decode_token(request.token(), &payload)) { bypass_entitlement = payload.wheel(); if (!bypass_entitlement) { user_entitlement = get_entitlements(payload.id(), ents); } } - _db.query(request, preply, user_entitlement, bypass_entitlement); + // Temporarily bypass all entitlements + _db.query(request, preply, user_entitlement, true); reply(*preply); #ifndef _WIN32 auto elapsed = (TimeStamp::now() - start);