Merge branch 'master' of bitbucket.org:bluetradesys/spark

This commit is contained in:
Taha Afzal committed 2018-08-21 21:41:32 -04:00
commit 7c19b7615d
30 files changed
+719 -195

No files matched your search

@@ -29,7 +29,7 @@ namespace bts.perf {
}
void on_tick(Update<TickImmutable, int> update) {
if (update.added.Count > 0) {
if (update.added.Any()) {
var tick = update.added.Last();
_service.report_async(new Report() {
HostName = _hostname,
@@ -69,13 +69,13 @@ namespace mbts.spark {
_dispose = Disposable.Create(
bus.subscribe(SparkTags.SecuritiesTag).OnDispatcher().Subscribe(on_securities),
model.on_orders.OnDispatcher().Subscribe(on_orders),
model.on_orders.OnDispatcher().AddRemoveOnly().Subscribe(on_orders),
grid_settings_manager,
columns);
void on_orders(TableUpdate<OrderRecordImmutable, uint> table) {
void on_orders(LegacyTableUpdate<OrderRecordImmutable, uint> table) {
// clear everything (most likely the server restarted)
if (table.removed.Count == _order_rows.Count && table.added.Count == 0) {
if (table.removed.Count() == _order_rows.Count && !table.added.Any()) {
_selected_rows.Clear();
_order_rows.DisposeAll();
_order_rows.Clear();
+5 -3
View File
@@ -53,7 +53,7 @@ namespace mbts.spark {
_dispose = Disposable.Create(
securities.OnDispatcher().Subscribe(_update_securities),
orders.OnDispatcher().Subscribe(_update_orders),
fills.OnDispatcher().Subscribe(_update_fills));
fills.OnDispatcher().AddRemoveOnly().Subscribe(_update_fills));
public IDisposable Subscribe(IObserver<IIncrementalUpdate<FillTuple, FillTupleKey>> observer) {
using (_rw_gate.read()) {
@@ -64,9 +64,11 @@ namespace mbts.spark {
public void Dispose() => Disposable.dispose(ref _dispose);
void _update_fills(TableUpdate<FillImmutable, string> fills) {
void _update_fills(LegacyTableUpdate<FillImmutable, string> fills) {
using (_rw_gate.write()) {
Dictionary<FillTupleKey, FillTuple> removed = new Dictionary<FillTupleKey, FillTuple>(), added = new Dictionary<FillTupleKey, FillTuple>();
var removed = new Dictionary<FillTupleKey, FillTuple>();
var added = new Dictionary<FillTupleKey, FillTuple>();
foreach (FillImmutable fill in fills.removed) {
FillTuple old_ft = new FillTuple(fill, _get_order(fill.OrderKey), _get_security_md(fill.SecurityKey));
removed.Add(old_ft.key, old_ft);
@@ -114,14 +114,7 @@ namespace bts.spark {
void orders_updated(TableUpdate<OrderRecordImmutable, uint> update) {
// Go though added and changed orders
var added = new HashSet<OrderRecordImmutable>(update.added, _id_comparer);
var changed = new HashSet<OrderRecordImmutable>(update.added, _id_comparer);
var removed = update.removed.ToDictionary(o => o.Key);
added.ExceptWith(update.removed);
changed.ExceptWith(added);
foreach (var order in added) {
foreach (var order in update.added) {
if (!string.IsNullOrEmpty(order.State.LastMsg) && order.State.Created.ToDateTime().Date == DateTime.Today) {
(var security, var spec) = get_security(order.Def.SecurityKey);
if (security.HasValue) {
@@ -130,8 +123,7 @@ namespace bts.spark {
}
}
foreach (var neworder in changed) {
var oldorder = removed[neworder.Key];
foreach (var (neworder, oldorder) in update.changes) {
(var security, var spec) = get_security(neworder.Def.SecurityKey);
//
// description
@@ -45,7 +45,7 @@ namespace mbts.spark {
OrderRecordTupleSource(IObservable<IReadOnlyDictionary<uint, SecurityMD>> securities, IObservable<TableUpdate<OrderRecordImmutable, uint>> orders) =>
_dispose = Disposable.Create(
securities.OnDispatcher().Subscribe(_update_securities),
orders.OnDispatcher().Subscribe(_update_orders));
orders.OnDispatcher().AddRemoveOnly().Subscribe(_update_orders));
public IDisposable Subscribe(IObserver<IIncrementalUpdate<OrderRecordTuple, uint>> observer) {
using (_rw_gate.read()) {
@@ -56,7 +56,7 @@ namespace mbts.spark {
public void Dispose() => Disposable.dispose(ref _dispose);
void _update_orders(TableUpdate<OrderRecordImmutable, uint> orders) {
void _update_orders(LegacyTableUpdate<OrderRecordImmutable, uint> orders) {
using (_rw_gate.write()) {
Dictionary<uint, OrderRecordTuple> removed = new Dictionary<uint, OrderRecordTuple>(), added = new Dictionary<uint, OrderRecordTuple>();
foreach (OrderRecordImmutable order in orders.removed) {
+28 -12
View File
@@ -8,9 +8,11 @@ using mbts.spark.utilities;
using mbts.utils;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Collections.Specialized;
using System.ComponentModel.Composition;
using System.Linq;
using System.Reactive.Concurrency;
using System.Reactive.Linq;
using System.Threading.Tasks;
@@ -36,20 +38,24 @@ namespace mbts.spark {
_position_service = position_service;
_spreader = spreader;
_error = error;
on_orders = _service.subscribei_orders().Table().Cache();
var scheduler = ThreadPoolScheduler.Instance;
var interval = TimeSpan.FromMilliseconds(100);
on_orders = _service.subscribei_orders().Naggle(interval, scheduler).Table().Cache();
on_positions = _position_service.subscribei_position_data().Table().Cache();
IReadOnlyDictionary<uint, OrderRecordImmutable> empty_orders = new Dictionary<uint, OrderRecordImmutable>();
working_orders =
on_orders.Scan(empty_orders, (worders, table) => _update_working_orders(worders, table)).Replay(1).RefCount();
on_orders.Scan(
ImmutableDictionary<uint,OrderRecordImmutable>.Empty,
(workers, table) => _update_working_orders(workers, table))
.Replay(1).RefCount();
on_order_tuples = OrderRecordTupleSource.create_observable(bus.subscribe(SparkTags.SecuritiesTag), on_orders);
on_fills = _service.subscribei_fills().Table().Cache();
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);
_security_orders_demuxer = new TableUpdateDemuxer<OrderRecordImmutable, uint, uint>(o => o.Key, o => o.security_key());
_security_positions_demuxer = new TableUpdateDemuxer<PositionUnitImmutable, int, uint>(o => o.RecordId, p => p.SecurityId);
_dispose = Disposable.Create(
on_orders.Subscribe(_security_orders_demuxer),
on_positions.Subscribe(_security_positions_demuxer),
on_orders.AddRemoveOnly().Subscribe(_security_orders_demuxer),
on_positions.AddRemoveOnly().Subscribe(_security_positions_demuxer),
_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()),
@@ -133,17 +139,27 @@ namespace mbts.spark {
public void Dispose() =>
Disposable.dispose(ref _dispose);
static IReadOnlyDictionary<uint, OrderRecordImmutable> _update_working_orders(IReadOnlyDictionary<uint, OrderRecordImmutable> target, TableUpdate<OrderRecordImmutable, uint> table) {
var clone = new Dictionary<uint, OrderRecordImmutable>((IDictionary<uint, OrderRecordImmutable>)target);
static ImmutableDictionary<uint, OrderRecordImmutable> _update_working_orders(ImmutableDictionary<uint, OrderRecordImmutable> target, TableUpdate<OrderRecordImmutable, uint> table) {
var clone = target.ToBuilder();
foreach (var o in table.removed) {
if (o.is_active())
if (o.is_active()) {
clone.Remove(o.Key);
}
}
foreach (var o in table.changed) {
if (o.is_active()) {
clone[o.Key] = o;
}
else {
clone.Remove(o.Key);
}
}
foreach (var o in table.added) {
if (o.is_active())
if (o.is_active()) {
clone.Add(o.Key, o);
}
}
return clone;
return clone.ToImmutableDictionary();
}
}
@@ -253,7 +269,7 @@ namespace mbts.spark {
}
// ** [continue comment at top of method] this call would otherwise be:
// rod.last_update = rod.last_update.update(removed_dict.Values.ToList(), added_dict.Values.ToList(), o => o.Key) **
rod.last_update = rod.last_update.update(removed_roots, added_roots, o => o.Key);
//rod.last_update = rod.last_update.update(removed_roots, added_roots, o => o.Key);
return rod;
}).Select(r => r.last_update);
@@ -4,6 +4,11 @@ using System.Collections.ObjectModel;
using System.Windows.Input;
namespace mbts.collections {
static class ObservableEditableCollection {
public static ObservableEditableCollection<T> create<T>() where T : new() =>
new ObservableEditableCollection<T>(() => new T());
}
class ObservableEditableCollection<T> : ObservableCollection<T> {
public ObservableEditableCollection(Func<T> factory, bool insert_new_item_at_front = false) {
AddItemCommand = insert_new_item_at_front ? new DelegateCommand(() => Insert(0, factory())) : new DelegateCommand(() => Add(factory()));
@@ -364,6 +364,19 @@ namespace System.Collections.Generic {
if (condition)
yield return get_value();
}
/// <summary>
/// Uses <see cref="ObjectHelper.try_convert{TResult}(object)"/> to convert the elements of an <see cref="IEnumerable"/> to the specified type.
/// </summary>
/// <typeparam name="TResult">The type to convert the elements of source to.</typeparam>
/// <param name="source">The <see cref="IEnumerable"/> that contains the elements to be cast to type <typeparamref name="TResult"/></param>
/// <returns>An <see cref="IEnumerable{TResult}"/> that contains each element of the source sequence cast to the specified type.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
public static IEnumerable<TResult> convert<TResult>(this IEnumerable source) {
ExceptionAssert.Argument.NotNull(source, nameof(source));
foreach (object obj in source)
yield return ObjectHelper.try_convert<TResult>(obj);
}
}
public static class CollectionExtensions
@@ -70,10 +70,6 @@ namespace mbts
}
#endregion
public static T choose<T>(this Random r, IEnumerable<T> range) {
var index = r.Next(0, range.Count());
return range.Skip(index).First();
}
// Iterate through a list 1,2,3,4 and return the items in pairwise tuples (1,2), (2,3), (3,4)
// This is better than using Zip+Skip because it iterates the collectiononly once
@@ -5,6 +5,7 @@ using mbts.ui.utility;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Reactive.Linq;
using System.Runtime.CompilerServices;
@@ -30,6 +31,10 @@ namespace mbts.spark {
void load_from(T value);
}
interface IMutableValueWrapperVM<T> : INotifyPropertyChanged {
T value { get; set; }
}
interface IViewValue {
object value { get; }
object view { get; }
@@ -70,6 +75,12 @@ namespace mbts.spark {
return vm;
}
public static IMutableValueWrapperVM<T> create_mutable_vm<T>(T init = default) =>
create_mutable_vm(EqualityComparer<T>.Default, init);
public static IMutableValueWrapperVM<T> create_mutable_vm<T>(IEqualityComparer<T> comparer, T init = default) =>
new MutableValueWrapperVM<T>(comparer, init);
public static ITemplatedPropertyWrapperVM<TSource> create_vm<TSource, TValue>(string template_key, Func<TSource, TValue> get, Action<TSource, TValue> set, Dictionary<string, IObservable<object>> named_params = null) =>
new DelegatePropertyWrapperVM<TSource, TValue>(template_key, get, set, named_params);
@@ -88,6 +99,20 @@ namespace mbts.spark {
public static IPropertyWrapperVM<TOutter> link<TOutter, TInner>(this IPropertyWrapperVM<TInner> inner, Func<TOutter, TInner> get, Action<TOutter, TInner> set) =>
new LinkedPropertyWrapperVM<TOutter, TInner>(inner, get, set);
class MutableValueWrapperVM<T> : ViewModelBase, IMutableValueWrapperVM<T> {
readonly IEqualityComparer<T> _comparer;
readonly Property<T> _value;
public MutableValueWrapperVM(IEqualityComparer<T> comparer, T init) {
_comparer = comparer;
_value = backing(nameof(value), comparer, init);
}
public T value { get => _value.Value; set => _value.set(value); }
public override string ToString() => value?.ToString();
}
class LinkedPropertyWrapperVM<TOutter, TInner> : ViewModelBase, IPropertyWrapperVM<TOutter> {
readonly Func<TOutter, TInner> _get;
readonly Action<TOutter, TInner> _set;
@@ -78,7 +78,7 @@ namespace mbts.utils {
protected override IDictionary<TKey, TValue> demux(IDictionary<TKey, TValue> input) => input;
}
class TableUpdateDemuxer<T, TKey, TGroupKey> : IObservableDemuxer<TGroupKey, TableUpdate<T, TKey>, ITableUpdateTransform<T, TKey>>
class TableUpdateDemuxer<T, TKey, TGroupKey> : IObservableDemuxer<TGroupKey, LegacyTableUpdate<T, TKey>, ITableUpdateTransform<T, TKey>>
where TKey : IEquatable<TKey>
where TGroupKey : IEquatable<TGroupKey> {
readonly ConcurrentDictionary<TGroupKey, Data> _map = new ConcurrentDictionary<TGroupKey, Data>();
@@ -101,7 +101,7 @@ namespace mbts.utils {
public void OnCompleted() { }
public void OnError(Exception error) { }
public void OnNext(TableUpdate<T, TKey> table) {
public void OnNext(LegacyTableUpdate<T, TKey> table) {
var changes = new Dictionary<TGroupKey, ChangeSet>();
foreach (T value in table.added)
changes.AddOrUpdate(_group_key(value), gk => ChangeSet.from_added(value), (gk, cs) => cs.added.Add(value));
@@ -14,8 +14,8 @@ namespace mbts.utils {
interface ITableUpdateTransform<T, TKey> where TKey : IEquatable<TKey> {
IReadOnlyDictionary<TKey, T> state { get; }
IReadOnlyList<T> added { get; }
IReadOnlyList<T> removed { get; }
IEnumerable<T> added { get; }
IEnumerable<T> removed { get; }
}
class IncrementalUpdate<T, TKey> : IIncrementalUpdate<T, TKey> where TKey : IEquatable<TKey> {
@@ -33,20 +33,20 @@ namespace mbts.utils {
class TableUpdateTransform<T, TKey> : ITableUpdateTransform<T, TKey> where TKey : IEquatable<TKey> {
readonly Dictionary<TKey, T> _state;
readonly IReadOnlyList<T> _added, _removed;
readonly IEnumerable<T> _added, _removed;
internal TableUpdateTransform() :
this(new Dictionary<TKey, T>(), new List<T>(0), new List<T>(0)) { }
internal TableUpdateTransform(Dictionary<TKey, T> init, IReadOnlyList<T> added, IReadOnlyList<T> removed) {
internal TableUpdateTransform(Dictionary<TKey, T> init, IEnumerable<T> added, IEnumerable<T> removed) {
_state = init;
_added = added;
_removed = removed;
}
public IReadOnlyDictionary<TKey, T> state => _state;
public IReadOnlyList<T> added => _added;
public IReadOnlyList<T> removed => _removed;
public IEnumerable<T> added => _added;
public IEnumerable<T> removed => _removed;
public TableUpdateTransform<T, TKey> update(Update<T, TKey> update) =>
this.update(update.removed, update.added, update.key);
@@ -54,7 +54,7 @@ namespace mbts.utils {
public TableUpdateTransform<T, TKey> update(TableUpdate<T, TKey> update, Func<T, TKey> key) =>
this.update(update.removed, update.added, key);
public TableUpdateTransform<T, TKey> update(IReadOnlyList<T> removed, IReadOnlyList<T> added, Func<T, TKey> key) {
public TableUpdateTransform<T, TKey> update(IEnumerable<T> removed, IEnumerable<T> added, Func<T, TKey> key) {
var table = new TableUpdateTransform<T, TKey>(new Dictionary<TKey, T>(_state), added, removed);
foreach (var item in removed)
table._state.Remove(key(item));
@@ -86,7 +86,7 @@ namespace mbts.utils {
public static IObservable<ITableUpdateTransform<T, TKey>> filter<T, TKey>(this IObservable<ITableUpdateTransform<T, TKey>> source, Func<T, bool> predicate)
where TKey : IEquatable<TKey> => source
.Select(u => new FilteredTableUpdateTransform<T, TKey>(u, predicate))
.Where(u => u.added.Count > 0 || u.removed.Count > 0);
.Where(u => u.added.Any() || u.removed.Any());
/// <summary>
/// Makes a 1:1 conversion to an IncrementalUpdate sequence of TResult (TKey remains the same).
@@ -143,8 +143,8 @@ namespace mbts.utils {
}
public IReadOnlyDictionary<TKey, T> state { get; }
public IReadOnlyList<T> added { get; }
public IReadOnlyList<T> removed { get; }
public IEnumerable<T> added { get; }
public IEnumerable<T> removed { get; }
}
class PivotIncrementalUpdate<T, TKey, TResult, TResultKey> : IIncrementalUpdate<TResult, TResultKey> where TKey : IEquatable<TKey> where TResultKey : IEquatable<TResultKey> {
@@ -208,7 +208,7 @@ namespace mbts.utils {
internal PivotTableUpdateTransform(Func<T, TResult> selector, Func<TResult, TResultKey> result_key, Func<T, TKey> key) :
this(new Dictionary<TResultKey, TResult>(), new List<TResult>(0), new List<TResult>(0), new SetMap<TResultKey, TKey>(), selector, result_key, key) { }
PivotTableUpdateTransform(Dictionary<TResultKey, TResult> state, IReadOnlyList<TResult> added, IReadOnlyList<TResult> removed,
PivotTableUpdateTransform(Dictionary<TResultKey, TResult> state, IEnumerable<TResult> added, IEnumerable<TResult> removed,
SetMap<TResultKey, TKey> key_map, Func<T, TResult> selector, Func<TResult, TResultKey> result_key, Func<T, TKey> key) {
//
_state = state;
@@ -221,8 +221,8 @@ namespace mbts.utils {
}
public IReadOnlyDictionary<TResultKey, TResult> state => _state;
public IReadOnlyList<TResult> added { get; }
public IReadOnlyList<TResult> removed { get; }
public IEnumerable<TResult> added { get; }
public IEnumerable<TResult> removed { get; }
public PivotTableUpdateTransform<T, TKey, TResult, TResultKey> update(ITableUpdateTransform<T, TKey> table) {
Dictionary<TResultKey, TResult> removed = new Dictionary<TResultKey, TResult>(), added = new Dictionary<TResultKey, TResult>();
+11 -3
View File
@@ -113,8 +113,11 @@ namespace mbts.ui.shell
protected delegate void PropertyChange<T>(T oldval, T newval);
// use nameof() to add compile time checks
protected Property<T> backing<T>(string name, T default_value = default(T)) {
return new Property<T>(this, name, default_value);
protected Property<T> backing<T>(string name, T default_value = default(T)) =>
backing(name, EqualityComparer<T>.Default, default_value);
protected Property<T> backing<T>(string name, IEqualityComparer<T> comparer, T default_value = default(T)) {
return new Property<T>(this, name, comparer, default_value);
}
protected Property<T> backing<T>(Expression<Func<T>> member, T default_value) {
@@ -134,12 +137,17 @@ namespace mbts.ui.shell
protected class Property<T> : IObservable<(T oldval,T newval)> {
readonly string _propname;
readonly IEqualityComparer<T> _comparer;
readonly ViewModelBase _owner;
T _value;
public Property(ViewModelBase owner, string name, T default_value) {
public Property(ViewModelBase owner, string name, T default_value)
: this(owner, name, EqualityComparer<T>.Default, default_value) { }
public Property(ViewModelBase owner, string name, IEqualityComparer<T> comparer, T default_value) {
_owner = owner;
_propname = name;
_comparer = comparer;
_value = default_value;
}
@@ -1,9 +1,7 @@
using System;
namespace mbts.ui.converters
{
public class EnumerableDoubleStringConverter : EnumerableStringConverter<double>
{
namespace mbts.ui.converters {
class EnumerableDoubleStringConverter : EnumerableStringConverter<double> {
protected override double convert_item_back(string item_str) {
double result;
if (Double.TryParse(item_str, out result))
@@ -1,26 +1,22 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Data;
namespace mbts.ui.converters
{
public abstract class EnumerableStringConverter<T> : IValueConverter
{
protected EnumerableStringConverter() {
namespace mbts.ui.converters {
abstract class EnumerableStringConverter<T> : IValueConverter {
protected EnumerableStringConverter() =>
Separator = " ";
}
public string Separator { get; set; }
protected virtual string convert_item(T item) { return item.ToString(); }
protected virtual T convert_item_back(string item_str) { throw new NotImplementedException(); }
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
if (value is IEnumerable<T>)
return ((IEnumerable<T>)value).Select(convert_item).list_string(Separator);
else
throw new ArgumentException("Expected 'Convert' input value to be {0}".fmt(typeof(IEnumerable<T>)));
if (value is IEnumerable<T> vals) {
return _format(vals);
} else if (value is IEnumerable ng_vals) {
return _format(ng_vals.convert<T>());
} else return value?.ToString();
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
@@ -33,9 +29,13 @@ namespace mbts.ui.converters
} else
throw new ArgumentException("Expected 'ConvertBack' input value to be {0}".fmt(typeof(string)));
}
protected virtual string convert_item(T item) { return item.ToString(); }
protected virtual T convert_item_back(string item_str) { throw new NotImplementedException(); }
string _format(IEnumerable<T> vals) =>
string.Join(Separator, vals.Select(convert_item));
}
public class EnumerableStringConverter : EnumerableStringConverter<object>
{
}
class EnumerableStringConverter : EnumerableStringConverter<object> { }
}
@@ -99,23 +99,6 @@ namespace mbts.spark {
public static bool try_get<T, TKey>(this TableUpdate<T, TKey> table, TKey key, out T result) where TKey : IEquatable<TKey> =>
table.state.TryGetValue(key, out result);
public static void detect_changed_keys<T, TKey>(this TableUpdate<T, TKey> table, Func<T, TKey> key_selector, out HashSet<TKey> removed_keys, out HashSet<TKey> added_keys, out HashSet<TKey> changed_keys)
where TKey : IEquatable<TKey> {
removed_keys = new HashSet<TKey>(table.removed.Select(key_selector));
added_keys = new HashSet<TKey>(table.added.Select(key_selector));
changed_keys = new HashSet<TKey>(removed_keys.Intersect(added_keys));
removed_keys.ExceptWith(changed_keys);
added_keys.ExceptWith(changed_keys);
}
public static void detect_changed_keys<T, TKey>(this ITableUpdateTransform<T, TKey> table, Func<T, TKey> key_selector, out HashSet<TKey> removed_keys, out HashSet<TKey> added_keys, out HashSet<TKey> changed_keys)
where TKey : IEquatable<TKey> {
removed_keys = new HashSet<TKey>(table.removed.Select(key_selector));
added_keys = new HashSet<TKey>(table.added.Select(key_selector));
changed_keys = new HashSet<TKey>(removed_keys.Intersect(added_keys));
removed_keys.ExceptWith(changed_keys);
added_keys.ExceptWith(changed_keys);
}
#endregion
public static class UI {
@@ -1,8 +1,8 @@
using bts.api;
using mbts.collections;
using mbts.ui;
using mbts.ui.controls.color_picker;
using mbts.ui.shell;
using mbts.ui.spark;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
@@ -32,6 +32,8 @@ namespace mbts.spark {
_session_range_fill = new LadderMarketBreakLineVM("Session Range", 0, 0, false);
public LadderSettingsVM() {
size_presets = new ObservableEditableCollection<IMutableValueWrapperVM<uint>>(_create_size_preset_vm);
size_presets.CollectionChanged += (s, e) => RaisePropertyChanged(nameof(size_presets_text));
_badge_style = backing<LadderSettings.BadgeStyle>(nameof(badge_style));
_badge_style.Changed += _badge_style_Changed;
_alternate_order_size = backing<uint>(nameof(alternate_order_size));
@@ -81,6 +83,8 @@ namespace mbts.spark {
public LadderColumnSettingsVM ask_column => _ask;
public LadderColumnSettingsVM last_column => _last;
public LadderMarketBreakLineVM session_range_fill => _session_range_fill;
public ObservableEditableCollection<IMutableValueWrapperVM<uint>> size_presets { get; }
public string size_presets_text => string.Join(", ", size_presets);
public IEnumerable<LadderMarketBreakLineVM> mkt_break_items {
get {
@@ -97,6 +101,7 @@ namespace mbts.spark {
_current = settings;
badge_style = settings.badge_style;
alternate_order_size = settings.alternate_order_size;
size_presets.SetItems(settings.calculator_buttons.empty_if_null().Select(s => _create_size_preset_vm((uint)s)));
_orders.assign_from(_current.orders_column);
_bid.assign_from(_current.bid_column);
_ask.assign_from(_current.ask_column);
@@ -114,6 +119,7 @@ namespace mbts.spark {
var clone = SparkUtils.clone_proto(_current);
clone.badge_style = badge_style;
clone.alternate_order_size = alternate_order_size;
clone.calculator_buttons = size_presets.Select(vm => (int)vm.value).ToArray();
clone.orders_column = _orders.get_settings();
clone.bid_column = _bid.get_settings();
clone.price_column = _price.get_settings();
@@ -128,6 +134,13 @@ namespace mbts.spark {
clone.row_height = LadderRender.measure_row_height(clone);
return clone;
}
IMutableValueWrapperVM<uint> _create_size_preset_vm() => _create_size_preset_vm(0);
IMutableValueWrapperVM<uint> _create_size_preset_vm(uint init) {
var vm = PropertyWrapperVMUtils.create_mutable_vm(init);
vm.PropertyChanged += (s, e) => RaisePropertyChanged(nameof(size_presets_text));
return vm;
}
public class LadderColumnSettingsVM : ViewModelBase {
readonly Property<double> _font_size;
@@ -9,9 +9,18 @@
xmlns:cv="clr-namespace:mbts.ui.converters"
xmlns:usp="clr-namespace:mbts.ui.spark"
xmlns:txt="clr-namespace:mbts.ui.utility.text"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
TextElement.FontSize="{x:Static u:FontSizes.Small}"
>
<UserControl.Resources>
<sys:Double x:Key="HorizontalPush">4</sys:Double>
<sys:Double x:Key="HorizontalSplit">8</sys:Double>
<sys:Double x:Key="GroupBoxOffsetLeft">8</sys:Double>
<sys:Double x:Key="GroupBoxOffsetRight">24</sys:Double>
<sys:Double x:Key="VerticalPush">4</sys:Double>
<sys:Double x:Key="VerticalSectionBreak">6</sys:Double>
<sys:Double x:Key="VerticalSplit">8</sys:Double>
<sys:Double x:Key="CollapsedSizePresetsHeight">24</sys:Double>
<cv:ValueMap x:Key="ColumnSpanToVisibilityConverter" Default="{u:Visibility Visible}">
<cv:KeyValue Key="{u:Int 0}" Value="{u:Visibility Collapsed}"/>
</cv:ValueMap>
@@ -62,20 +71,94 @@
<Grid Grid.IsSharedSizeScope="True">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="{Binding Source={StaticResource VerticalPush}}"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="{Binding Source={StaticResource VerticalPush}}"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="{Binding Source={StaticResource VerticalSectionBreak}}"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="{Binding Source={StaticResource VerticalSectionBreak}}"/>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="{Binding Source={StaticResource GroupBoxOffsetLeft}}"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
<ColumnDefinition Width="{Binding Source={StaticResource HorizontalSplit}}"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="{Binding Source={StaticResource GroupBoxOffsetRight}}"/>
</Grid.ColumnDefinitions>
<TextBlock VerticalAlignment="Center" Margin="8,0,0,4">Order Badge Style</TextBlock>
<ComboBox Grid.Column="1" Margin="8,0,24,4" ItemsSource="{u:EnumBindingSource {u:Nested sp:LadderSettings.BadgeStyle}}" SelectedItem="{Binding badge_style}"/>
<TextBlock Grid.Row="1" VerticalAlignment="Center" Margin="8,0,0,4" >Alternate Order Qty</TextBlock>
<TextBox Grid.Row="1" Grid.Column="1" Margin="8,0,24,4" Text="{Binding alternate_order_size}"
u:TextBoxBehavior.InputFilter="{u:RegexInputFilter Regex={x:Static Member=Integer, MemberType={u:Nested txt:TextUtils.RegexFilters}}}"/>
<GroupBox Grid.Row="2" Grid.ColumnSpan="2" Margin="0,6,0,0" Padding="8" Header="Fonts and Colors">
<TextBlock Grid.Column="1" VerticalAlignment="Center">Order Badge Style</TextBlock>
<ComboBox Grid.Column="3" ItemsSource="{u:EnumBindingSource {u:Nested sp:LadderSettings.BadgeStyle}}" SelectedItem="{Binding badge_style}"/>
<TextBlock Grid.Row="2" Grid.Column="1" VerticalAlignment="Center">Alternate Order Qty</TextBlock>
<TextBox Grid.Row="2" Grid.Column="3" Text="{Binding alternate_order_size}" u:TextBoxBehavior.InputFilter="{u:RegexInputFilter Regex={x:Static Member=Integer, MemberType={u:Nested txt:TextUtils.RegexFilters}}}"/>
<GroupBox Grid.Row="4" Grid.ColumnSpan="5" VerticalAlignment="Center">
<GroupBox.Style>
<Style TargetType="GroupBox" BasedOn="{StaticResource {x:Type GroupBox}}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsChecked, ElementName=SizePresetsExpanderToggle}" Value="{u:False}">
<Setter Property="Height" Value="{StaticResource CollapsedSizePresetsHeight}"/>
<Setter Property="BorderThickness" Value="0"/>
</DataTrigger>
</Style.Triggers>
</Style>
</GroupBox.Style>
<GroupBox.Header>
<c:ExpanderToggleButton x:Name="SizePresetsExpanderToggle">
<TextBlock>
<TextBlock.Inlines>
<Run Text="Size Presets"/>
<Run FontStyle="Italic" Text="{Binding size_presets_text, StringFormat='{}({0})', Mode=OneWay}">
<Run.Style>
<Style TargetType="Run">
<Style.Triggers>
<DataTrigger Binding="{Binding IsChecked, ElementName=SizePresetsExpanderToggle}" Value="{u:True}">
<Setter Property="Text" Value="{x:Static sys:String.Empty}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Run.Style>
</Run>
</TextBlock.Inlines>
</TextBlock>
</c:ExpanderToggleButton>
</GroupBox.Header>
<Grid HorizontalAlignment="Left">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="{Binding Source={StaticResource VerticalPush}}"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<ItemsControl ItemsSource="{Binding size_presets}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="2" Focusable="False"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="52"/>
</Grid.ColumnDefinitions>
<Button
Command="{Binding DataContext.size_presets.RemoveItemCommand, RelativeSource={RelativeSource AncestorType=ItemsControl}}"
CommandParameter="{Binding}"
Style="{StaticResource RoundCloseButton}"
/>
<c:TickUpDownUInt Grid.Column="1" Margin="2,0,0,0" Value="{Binding value}"/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<Button Grid.Row="2" Command="{Binding size_presets.AddItemCommand}">
Add Size
</Button>
</Grid>
</GroupBox>
<GroupBox Grid.Row="6" Grid.ColumnSpan="5" Padding="8" Header="Fonts and Colors">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="27"/>
@@ -148,7 +231,7 @@
</ItemsControl>
</Grid>
</GroupBox>
<GroupBox Grid.Row="3" Grid.ColumnSpan="2" Margin="0,6,0,0" Padding="8" Header="Market Break Lines">
<GroupBox Grid.Row="8" Grid.ColumnSpan="5" Padding="8" Header="Market Break Lines">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="27"/>
@@ -83,7 +83,7 @@ namespace mbts.spark {
// Here we manage N orders to 1 price slot in the ladder
Dictionary<double, HashSet<uint>> orders_used_in_this_update = new Dictionary<double, HashSet<uint>>(_equality);
var ladders_orders = working.Values.Where(o => o.Def.SecurityKey == security.Id);
var ladders_orders = working.Values.Where(o => o.Def.SecurityKey == security.Id && (o.HasState && o.State.DisplayStatus == OrderState.Types.DisplayStatus.Visible));
foreach (var wo in ladders_orders) {
var slot_price_target = wo.price();
var order_type = wo.order_type();
@@ -52,17 +52,6 @@ namespace mbts.ui.utility {
}
});
public static ITableUpdateCollection<T, TKey> CreateTableUpdate<T, TKey>(IObservable<TableUpdate<T, TKey>> source) where TKey : IEquatable<TKey> =>
CreateTableUpdate(source, _ => true);
public static ITableUpdateCollection<T, TKey> CreateTableUpdate<T, TKey>(IObservable<TableUpdate<T, TKey>> source, Func<T, bool> collection_filter) where TKey : IEquatable<TKey> =>
new TableUpdateCollection<T, TKey>(source, collection_filter ?? new Func<T, bool>(_ => true));
public static ITableUpdateCollection<T, TKey> CreateTableUpdate<T, TKey>(IObservable<ITableUpdateTransform<T, TKey>> source) where TKey : IEquatable<TKey> =>
CreateTableUpdate(source, _ => true);
public static ITableUpdateCollection<T, TKey> CreateTableUpdate<T, TKey>(IObservable<ITableUpdateTransform<T, TKey>> source, Func<T, bool> collection_filter) where TKey : IEquatable<TKey> =>
new TableUpdateTransformCollection<T, TKey>(source, collection_filter ?? new Func<T, bool>(_ => true));
public static IRXCollection<T> CreateIncrementalUpdate<T, TKey>(IObservable<IIncrementalUpdate<T, TKey>> source) where TKey : IEquatable<TKey> =>
new IncrementalUpdateCollection<T, TKey>(source);
@@ -83,24 +72,6 @@ namespace mbts.ui.utility {
public void Dispose() => Disposable.dispose(ref _sub);
}
class TableUpdateCollection<T, TKey> : ObservableCollection<T>, ITableUpdateCollection<T, TKey> where TKey : IEquatable<TKey> {
readonly Func<T, bool> _collection_filter;
IReadOnlyDictionary<TKey, T> _raw_state = new ReadOnlyDictionary<TKey, T>(new Dictionary<TKey, T>());
IDisposable _sub;
internal TableUpdateCollection(IObservable<TableUpdate<T, TKey>> source, Func<T, bool> collection_filter) {
_collection_filter = collection_filter;
_sub = source.OnDispatcher().Subscribe(u => {
_raw_state = u.state;
foreach (T val in u.removed.Where(_collection_filter)) Remove(val);
foreach (T val in u.added.Where(_collection_filter)) Add(val);
});
}
public bool TryGetValue(TKey key, out T result) => _raw_state.TryGetValue(key, out result);
public void Dispose() => Disposable.dispose(ref _sub);
}
class TableUpdateTransformCollection<T, TKey> : ObservableCollection<T>, ITableUpdateCollection<T, TKey> where TKey : IEquatable<TKey> {
readonly Func<T, bool> _collection_filter;
IReadOnlyDictionary<TKey, T> _raw_state = new ReadOnlyDictionary<TKey, T>(new Dictionary<TKey, T>());
@@ -57,30 +57,34 @@ namespace bts.api.core {
}
public unsafe override IObservable<RawUpdate<TOut>> subscribe<TParse,TOut>(string method_and_qualifier, MessageParser<TParse> parser, Func<TParse,TOut> converter) {
bool first_update = true;
var result = Observable.Create<RawUpdate<TOut>>(observer => {
bool first_update = true;
// Memory optimization. There is only one update per subscription.
RawUpdate<TOut> the_update = new RawUpdate<TOut>();
ApiNativeMethods.raw_subscription_callback cb = (event_type, changes, change_count, unused) => {
the_update.Clear();
try {
if (event_type == ApiNativeMethods.EventType.TABLE_UPDATED) {
var added_items = new List<TOut>(change_count);
var added_keys = new List<int>(change_count);
var removed_keys = new List<int>(change_count);
foreach (var change in changes) {
if (change.type != ApiNativeMethods.ChangeType.REMOVE) {
// create or change
added_items.Add( converter( parser.ParseFrom(new UnmanagedMemoryStream(change.buffer, change.buffer_len))));
added_keys.Add(change.key);
}
if (change.type != ApiNativeMethods.ChangeType.CREATE) {
// removed or changed
removed_keys.Add(change.key);
switch (change.type) {
case ApiNativeMethods.ChangeType.REMOVE:
the_update.removed_keys.Add(change.key);
break;
case ApiNativeMethods.ChangeType.REPLACE:
the_update.changed_items.Add(converter(parser.ParseFrom(new UnmanagedMemoryStream(change.buffer, change.buffer_len))));
the_update.changed_keys.Add(change.key);
break;
case ApiNativeMethods.ChangeType.CREATE:
the_update.added_items.Add(converter(parser.ParseFrom(new UnmanagedMemoryStream(change.buffer, change.buffer_len))));
the_update.added_keys.Add(change.key);
break;
}
}
observer.OnNext(new RawUpdate<TOut>(added_items, added_keys, removed_keys));
observer.OnNext(the_update);
}
else if (event_type == ApiNativeMethods.EventType.TABLE_CREATED) {
if (!first_update) {
observer.OnNext(RawUpdate<TOut>.Reset());
observer.OnNext(the_update.MarkAsReset());
}
first_update = false;
}
@@ -88,7 +92,7 @@ namespace bts.api.core {
catch (Exception e) {
ApiNativeMethods.api_client_log_error($"Exception in subscription callback for sub: '{method_and_qualifier}' \n--\n '{e}'");
// Sleep to let the log message get asyncronously written to disk
System.Threading.Thread.Sleep(100);
Thread.Sleep(100);
Environment.Exit(-1);
}
};
+197 -32
View File
@@ -1,84 +1,204 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using System.Reactive.Concurrency;
using System.Reactive.Disposables;
using System.Reactive.Linq;
namespace bts.api.core {
public struct RawUpdate<T> {
class RawUpdate<T> {
// RawUpdate uses List<T> instead of IReadonlyList to avoid virtual dispatch on a type not likely to be see in a public api surface
public RawUpdate(List<T> added_items, List<int> added_keys, List<int> removed_keys) : this() {
// There is a single stack allocated RawUpdate per subscription
public RawUpdate() {
is_reset = false;
this.added_items = added_items;
this.added_keys = added_keys;
this.removed_keys = removed_keys;
added_items = new List<T>();
added_keys = new List<int>();
changed_items = new List<T>();
changed_keys = new List<int>();
removed_keys = new List<int>();
}
public RawUpdate<T> MarkAsReset() {
Clear();
is_reset = true;
return this;
}
public static RawUpdate<T> Reset() {
return new RawUpdate<T>() { is_reset = true };
public void Clear() {
is_reset = false;
added_items.Clear();
added_keys.Clear();
changed_items.Clear();
changed_keys.Clear();
removed_keys.Clear();
}
public bool is_reset { get; private set; }
public List<T> added_items { get; }
public List<int> added_keys { get; }
public List<T> changed_items { get; }
public List<int> changed_keys { get; }
public List<int> removed_keys { get; }
public bool is_reset;
public List<T> added_items { get; private set; }
public List<int> added_keys { get; private set; }
public List<int> removed_keys { get; private set; }
#if DEBUG
public override string ToString() {
var added = string.Join(", ", added_items);
var changed = string.Join(", ", changed_items);
var removed = string.Join(", ", removed_keys);
return $"added [{added}] changed [{changed}] removed [{removed}]";
}
#endif
}
public class Update<T, TKey> where TKey : IEquatable<TKey> {
private Func<T, TKey> _key;
public Update(IReadOnlyCollection<T> all, IReadOnlyList<T> added, IReadOnlyList<T> removed, Func<T, TKey> key_extractor) {
internal Update(
IReadOnlyCollection<T> all,
IEnumerable<T> added,
IEnumerable<(T New,T Old)> changes,
IEnumerable<T> removed,
Func<T, TKey> key_extractor) {
this.added = added;
this.removed = removed;
this.changes = changes;
this.all = all;
_key = key_extractor;
}
public IReadOnlyCollection<T> all { get; private set; }
public IReadOnlyList<T> added { get; private set; }
public IReadOnlyList<T> removed { get; private set; }
public static Update<T, TKey> empty(Func<T, TKey> key_extractor) =>
new Update<T, TKey>(new T[0], new T[0], new (T,T)[0], new T[0], key_extractor);
public Update<T,TKey> merge(Update<T, TKey> next) {
var comp = new RowEqualityComparer<T, TKey>(_key);
var added = this.added as HashSet<T> ?? new HashSet<T>(this.added, comp);
var changes = this.changed as HashSet<(T New,T Old)> ?? new HashSet<(T,T)>(this.changes, comp);
var removed = this.removed as HashSet<T> ?? new HashSet<T>(this.removed, comp);
// Add
added.UnionWith(next.added);
// Change
foreach (var c in next.changes) {
if (added.Contains(c.Old)) {
added.Remove(c.Old);
added.Add(c.New);
}
else {
// Remove does not error if it was not present
changes.Remove(c);
changes.Add(c);
}
}
// Remove
foreach (var r in next.removed) {
if (!added.Remove(r)) {
removed.Add(r);
}
changes.Remove((r,r));
}
return new Update<T, TKey>(
next.all,
added, changes, removed,
_key
);
}
public IReadOnlyCollection<T> all { get; }
public IEnumerable<T> added { get; }
public IEnumerable<(T New,T Old)> changes { get; }
public IEnumerable<T> changed => changes.Select(c => c.New);
public IEnumerable<T> removed { get; }
public TKey key(T t) { return _key(t); }
}
struct RowEqualityComparer<T,TKey> : IEqualityComparer<T>, IEqualityComparer<(T,T)>
where TKey : IEquatable<TKey> {
readonly Func<T,TKey> _key;
public RowEqualityComparer(Func<T,TKey> key) {
_key = key;
}
public bool Equals(T x, T y) {
return _key(x).Equals(_key(y));
}
public bool Equals((T, T) x, (T, T) y) {
return Equals(x.Item1, y.Item1);
}
public int GetHashCode(T obj) {
return _key(obj).GetHashCode();
}
public int GetHashCode((T, T) obj) {
return GetHashCode(obj.Item1);
}
}
public class TableUpdate<T, TKey> where TKey : IEquatable<TKey> {
ImmutableDictionary<TKey, T> _state;
readonly ImmutableDictionary<TKey, T> _state;
internal TableUpdate(ImmutableDictionary<TKey, T> init, IReadOnlyList<T> added, IReadOnlyList<T> removed) {
internal TableUpdate(
ImmutableDictionary<TKey, T> init,
IEnumerable<T> added,
IEnumerable<(T,T)> changed,
IEnumerable<T> removed) {
_state = init;
this.added = added;
this.changes = changed;
this.removed = removed;
}
public IReadOnlyDictionary<TKey, T> state { get { return _state; } }
public IReadOnlyList<T> added { get; }
public IReadOnlyList<T> removed { get; }
public IEnumerable<T> added { get; }
public IEnumerable<(T New,T Old)> changes { get; }
public IEnumerable<T> changed => changes.Select(c => c.New);
internal TableUpdate<T, TKey> update(Update<T, TKey> update) =>
this.update(update.removed, update.added, update.key);
public IEnumerable<T> removed { get; }
internal TableUpdate<T, TKey> update(IReadOnlyList<T> removed, IReadOnlyList<T> added, Func<T, TKey> key) {
var builder = _state.ToBuilder();
builder.RemoveRange(removed.Select(i => key(i)));
builder.AddRange(added.Select(i => new KeyValuePair<TKey, T>(key(i), i)));
return new TableUpdate<T, TKey>(builder.ToImmutableDictionary(), added, removed);
internal static TableUpdate<T, TKey> update(TableUpdate<T,TKey> table, Update<T, TKey> update) {
var builder = table._state.ToBuilder();
builder.RemoveRange(update.removed.Select(i => update.key(i)));
builder.AddRange(update.added.Select(i => new KeyValuePair<TKey, T>(update.key(i), i)));
foreach (var i in update.changed) {
builder[update.key(i)] = i;
}
return new TableUpdate<T, TKey>(builder.ToImmutableDictionary(), update.added, update.changes, update.removed);
}
internal TableUpdate<T, TKey> as_snapshot() {
return new TableUpdate<T, TKey>(_state, _state.Values.ToList(), new T[0]);
return new TableUpdate<T, TKey>(_state, _state.Values.ToList(), new (T,T)[0], new T[0]);
}
}
internal class LegacyTableUpdate<T, TKey> where TKey : IEquatable<TKey> {
public LegacyTableUpdate(
TableUpdate<T, TKey> update) {
this.state = update.state;
this.added = update.added.Concat(update.changes.Select(t => t.New));
this.removed = update.removed.Concat(update.changes.Select(t => t.Old));
}
public IReadOnlyDictionary<TKey, T> state { get; }
public IEnumerable<T> added { get; }
public IEnumerable<T> removed { get; }
}
public static class ObservableUpdateExtensions {
public static IObservable<Update<T, TKey>> reconstitute<T, TKey>(this IObservable<RawUpdate<T>> source, Func<T, TKey> key_extractor) where TKey : IEquatable<TKey> {
internal static IObservable<Update<T, TKey>> reconstitute<T, TKey>(this IObservable<RawUpdate<T>> source, Func<T, TKey> key_extractor) where TKey : IEquatable<TKey> {
var state = new Dictionary<int, T>();
return source.Select(raw => {
if (raw.is_reset) {
var removed = state.Values.ToList();
state.Clear();
return new Update<T, TKey>(new T[0], new T[0], removed, key_extractor);
return new Update<T, TKey>(new T[0], new T[0], new (T,T)[0], removed, key_extractor);
}
else {
var removed = new List<T>(raw.removed_keys.Count);
@@ -92,15 +212,56 @@ namespace bts.api.core {
for (int x = 0; x < raw.added_items.Count; ++x) {
state[raw.added_keys[x]] = raw.added_items[x];
}
return new Update<T, TKey>(state.Values, raw.added_items, removed, key_extractor);
List<(T, T)> changes = new List<(T, T)>(raw.changed_items.Count);
for (int x = 0; x < raw.changed_items.Count; ++x) {
var old = state[raw.changed_keys[x]];
state[raw.changed_keys[x]] = raw.changed_items[x];
changes.Add((raw.changed_items[x], old));
}
return new Update<T, TKey>(state.Values.ToList(), raw.added_items.ToList(), changes, removed, key_extractor);
}
});
}
public static IObservable<TableUpdate<T,TKey>> Table<T,TKey>(this IObservable<Update<T,TKey>> source) where TKey : IEquatable<TKey> =>
source.Scan(
new TableUpdate<T,TKey>(ImmutableDictionary<TKey,T>.Empty, new List<T>(), new List<T>()),
(table, update) => table.update(update));
new TableUpdate<T,TKey>(ImmutableDictionary<TKey,T>.Empty, new T[0], new (T,T)[0], new T[0]),
(table, update) => TableUpdate<T,TKey>.update(table,update));
// Fold updates and emit at max 1/interval
public static IObservable<Update<T,TKey>> Naggle<T, TKey>(this IObservable<Update<T,TKey>> source, TimeSpan interval, IScheduler scheduler) where TKey : IEquatable<TKey> {
// fold updates together and emit on TimeSpan (max) interval. Emit on threadpool
return Observable.Create<Update<T,TKey>>(observer => {
object guard = new object();
Update<T, TKey> cache = null;
bool finished = false;
var d1 = source.Subscribe(update => {
// subscribe to the source, merge updates (Quickbus thread)
lock (guard) {
if (cache == null) cache = update;
else cache = cache.merge(update);
}
},
() => { finished = true; });
// publish on interval
var d2 = Observable.Interval(interval, scheduler).Subscribe(_ => {
Update<T, TKey> update;
lock (guard) {
update = cache;
cache = null;
}
if (update != null) {
observer.OnNext(update);
}
if (finished) {
observer.OnCompleted();
}
});
return new CompositeDisposable(d1, d2);
});
}
public static IObservable<TableUpdate<T,TKey>> Cache<T, TKey>(this IObservable<TableUpdate<T, TKey>> source) where TKey : IEquatable<TKey> {
var inner = source.Replay(1).RefCount();
@@ -118,5 +279,9 @@ namespace bts.api.core {
});
}
internal static IObservable<LegacyTableUpdate<T, TKey>> AddRemoveOnly<T, TKey>(this IObservable<TableUpdate<T, TKey>> source) where TKey : IEquatable<TKey> {
return source.Select(t => new LegacyTableUpdate<T, TKey>(t));
}
}
}
@@ -37,11 +37,11 @@ namespace bts.test {
result.Status = TestResult.Types.Step.Pass;
}
catch (AssertFailture af) {
result.Message = $"Assertion Failure: {af.Message}\nLocation:{af._filename}:{af._line}";
result.Message = $"Assertion Failure:\n{af.Message}\nLocation:{af._filename}:{af._line}";
}
catch (Exception ue) {
if (ue.InnerException is AssertFailture af) {
result.Message = $"Assertion Failure: {af.Message}\nLocation:{af._filename}:{af._line}";
result.Message = $"Assertion Failure:\n{af.Message}\nLocation:{af._filename}:{af._line}";
}
else {
result.Message = $"Unexpected error: {ue.Message}\n\n{ue.StackTrace}";
+10 -6
View File
@@ -18,28 +18,32 @@ namespace bts.test {
}
public static void IsTrue(bool test, string msg="", [CallerFilePath] string file="", [CallerLineNumber] int line=0) {
if (!test) {
throw new AssertFailture($"Expected 'true' got 'false'. {msg}", file, line);
throw new AssertFailture($"Expected: 'true' got 'false'. {msg}", file, line);
}
}
public static void That(bool test, string msg = "", [CallerFilePath] string file = "", [CallerLineNumber] int line = 0) {
Assert.IsTrue(test, msg, file, line);
}
public static void IsFalse(bool test, string msg="", [CallerFilePath] string file="", [CallerLineNumber] int line=0) {
if (test) {
throw new AssertFailture($"Expected 'false' got 'true'. {msg}", file, line);
throw new AssertFailture($"Expected: 'false' got 'true'. {msg}", file, line);
}
}
public static void AreEqual<T>(T a, T b, string msg="", [CallerFilePath] string file="", [CallerLineNumber] int line=0) {
if (!a.Equals(b)) {
throw new AssertFailture(
$"Expected '{a}' == '{b}'. {msg}", file, line);
$"Expected: '{a}' == '{b}'. {msg}", file, line);
}
}
public static void AreEqual<T>(IEnumerable<T> a, IEnumerable<T> b, string msg="", [CallerFilePath] string file="", [CallerLineNumber] int line=0) {
public static void SequenceEqual<T>(IEnumerable<T> a, IEnumerable<T> b, string msg="", [CallerFilePath] string file="", [CallerLineNumber] int line=0) {
if (!a.SequenceEqual(b)) {
var aa = string.Join(", ", a);
var bb = string.Join(", ", b);
throw new AssertFailture(
$"Expected [{aa}] == [{bb}]. {msg}", file, line);
$"Expected: [{aa}]\nGot: [{bb}]. {msg}", file, line);
}
}
public static void IsNotNull<T>(T a, string msg="", [CallerFilePath] string file="", [CallerLineNumber] int line=0)
where T : class
{
@@ -53,7 +57,7 @@ namespace bts.test {
if (!items.Contains(item)) {
var aa = string.Join(", ", items);
throw new AssertFailture(
$"Expected {item} in [{aa}]. {msg}", file, line);
$"Expected: {item} in [{aa}]. {msg}", file, line);
}
}
}
@@ -61,6 +61,7 @@ namespace bts.test {
if (!string.IsNullOrEmpty(type)) {
ConsoleEx.Write($" - {type}", ConsoleColor.DarkRed);
}
Console.WriteLine();
if (!string.IsNullOrEmpty(result.Message)) {
ConsoleEx.Write("==>", ConsoleColor.White, ConsoleColor.DarkRed);
Console.Write(" ");
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
@@ -17,6 +19,10 @@ namespace mbts {
}
return sb.ToString();
}
public static T choose<T>(this Random r, IEnumerable<T> range) {
var index = r.Next(0, range.Count());
return range.Skip(index).First();
}
[DllImport("libspark")]
private static extern void ll_to_parts(UInt64 ll_time, out int year, out int month, out int day, out int hour, out int minute, out int second, out int ms);
+8 -8
View File
@@ -77,16 +77,16 @@ namespace mbts.api.Test {
var response = h.say_hello(new HelloRequest() { Name = "UnitTest 1" });
Assert.AreEqual("Hello UnitTest 1", response.Message);
Assert.IsNotNull(last_update);
Assert.IsTrue(last_update.added.Count == 1);
Assert.IsTrue(last_update.removed.Count == 0);
Assert.IsTrue(last_update.added.Count() == 1);
Assert.IsTrue(last_update.removed.Count() == 0);
Assert.IsTrue(last_update.state.Count == 1);
Assert.IsTrue(last_update.state.Single().Value.Message == "Hello UnitTest 1");
last_update = null;
response = h.yell_hello(new HelloRequest() { Name = "UnitTest 2" });
Assert.AreEqual("HELLO UNITTEST 2", response.Message);
Assert.IsNotNull(last_update);
Assert.IsTrue(last_update.added.Count == 1);
Assert.IsTrue(last_update.removed.Count == 1);
Assert.IsTrue(last_update.added.Count() == 1);
Assert.IsTrue(last_update.removed.Count() == 1);
Assert.IsTrue(last_update.state.Count == 1);
Assert.IsTrue(last_update.state.Single().Value.Message == "HELLO UNITTEST 2");
last_update = null;
@@ -97,8 +97,8 @@ namespace mbts.api.Test {
response = h.yell_hello(new HelloRequest() { Name = "UnitTest 3" });
Assert.AreEqual("HELLO UNITTEST 3", response.Message);
Assert.IsNotNull(last_update);
Assert.IsTrue(last_update.added.Count == 1);
Assert.IsTrue(last_update.removed.Count == 1);
Assert.IsTrue(last_update.added.Count() == 1);
Assert.IsTrue(last_update.removed.Count() == 1);
Assert.IsTrue(last_update.state.Count == 1);
Assert.IsTrue(last_update.state.Single().Value.Message == "HELLO UNITTEST 3");
last_update = null;
@@ -108,8 +108,8 @@ namespace mbts.api.Test {
_ntest.run_shout_service(null);
response = h.yell_hello(new HelloRequest() { Name = "UnitTest 4" });
Assert.AreEqual("HELLO UNITTEST 4", response.Message);
Assert.IsTrue(last_update.added.Count == 1);
Assert.IsTrue(last_update.removed.Count == 1);
Assert.IsTrue(last_update.added.Count() == 1);
Assert.IsTrue(last_update.removed.Count() == 1);
Assert.IsTrue(last_update.state.Count == 1);
Assert.IsTrue(last_update.state.Single().Value.Message == "HELLO UNITTEST 4");
last_update = null;
+231
View File
@@ -0,0 +1,231 @@
using bts.api.core;
using System.Collections.Generic;
using bts.test;
using System;
using System.Reactive.Linq;
using System.Threading.Tasks;
using System.Linq;
using System.Reactive.Concurrency;
namespace mbts.api.Test {
[TestGroup]
public class ApiUpdateTests {
int _id = 0;
List<Item> _item_pool;
public ApiUpdateTests() {
// Generate a pool of items
_item_pool = Enumerable.Range(1, 50).Select(i => mkitem()).ToList();
}
class Item : IEquatable<Item> {
public Item(int id, string name) {
this.id = id;
this.name = name;
}
public int id { get; }
public string name { get; }
public override string ToString() => $"{id}-{name}";
public override bool Equals(object obj) {
if (obj is Item i) {
return this.Equals(i);
}
return false;
}
public override int GetHashCode() {
return ToString().GetHashCode();
}
public bool Equals(Item other) {
return id == other.id && name == other.name;
}
}
static int id(Item i) => i.id;
Item mkitem() {
return new Item(++_id, Guid.NewGuid().ToString().Substring(0,8));
}
IObservable<Update<Item, int>> validate_incremental(IObservable<Update<Item, int>> source) {
// strip the state from source and turn it back into a 'raw' update
// then use the (well tested) reconstitute function to re-build the state from the increments
return source.Select(u => {
var r = new RawUpdate<Item>();
r.added_items.AddRange(u.added);
r.added_keys.AddRange(u.added.Select(id));
r.changed_items.AddRange(u.changed);
r.changed_keys.AddRange(u.changed.Select(id));
r.removed_keys.AddRange(u.removed.Select(id));
return r;
}).reconstitute(id);
}
async Task validate_naggle(IObservable<RawUpdate<Item>> raw) {
var total = raw.ToEnumerable().Count();
// Validate that update is right at each point
// This will mostly be collapsing to add()
for (var count = 1; count < total; ++count) {
var orig = raw.Take(count).reconstitute(id).ToEnumerable().Last();
var nag = await
validate_incremental(
raw
.Take(count)
.reconstitute(i => i.id)
.Naggle(TimeSpan.FromMilliseconds(1), CurrentThreadScheduler.Instance)).LastAsync();
Assert.SequenceEqual(orig.all.OrderBy(id), nag.all.OrderBy(id));
}
// Do it again with TableUpdate
for (var count = 1; count < total; ++count) {
var orig = raw.Take(count).reconstitute(id).Table().ToEnumerable().Last();
var nag = await
validate_incremental(
raw
.Take(count)
.reconstitute(i => i.id)
.Naggle(TimeSpan.FromMilliseconds(1), CurrentThreadScheduler.Instance))
.Table()
.LastAsync();
Assert.SequenceEqual(orig.state.Values.OrderBy(id), nag.state.Values.OrderBy(id));
}
// collapse rolling window
var update_list = raw.reconstitute(id).ToEnumerable().ToList();
var window = 10;
for (int x = 0; x+window < total; x += 5) {
var start = update_list[x];
var end = update_list[x + window];
var super_update = update_list.Skip(x).Take(window+1).Aggregate((u1, u2) => u1.merge(u2));
start.merge(super_update);
Assert.SequenceEqual(end.all, super_update.all);
}
}
[Test]
public async Task test_add() {
// Add 1 item 10x
var raw = Observable.Create<RawUpdate<Item>>(observer => {
RawUpdate<Item> rup = new RawUpdate<Item>();
for (int x = 0; x < 50; ++x) {
rup.Clear();
var item = _item_pool[x];
rup.added_items.Add(item);
rup.added_keys.Add(item.id);
observer.OnNext(rup);
}
observer.OnCompleted();
return Disposable.empty;
});
await validate_naggle(raw);
}
[Test]
public Task test_remove() {
// Add 20 items, then remove 10 one at a time
var raw = Observable.Create<RawUpdate<Item>>(observer => {
RawUpdate<Item> rup = new RawUpdate<Item>();
// add 20 items
rup.added_items.AddRange(_item_pool.Take(20));
rup.added_keys.AddRange(_item_pool.Take(20).Select(id));
observer.OnNext(rup);
for (int x = 0; x < 50; x+=2) {
rup.Clear();
var item = _item_pool[x];
rup.removed_keys.Add(item.id);
observer.OnNext(rup);
}
observer.OnCompleted();
return Disposable.empty;
});
return validate_naggle(raw);
}
[Test]
public Task test_update() {
// Add 20 items, then change all of their names to Taha
var raw = Observable.Create<RawUpdate<Item>>(observer => {
RawUpdate<Item> rup = new RawUpdate<Item>();
// add 20 items
rup.added_items.AddRange(_item_pool.Take(20));
rup.added_keys.AddRange(_item_pool.Take(20).Select(id));
observer.OnNext(rup);
for (int x = 0; x < 20; x++) {
rup.Clear();
var item = _item_pool[x];
var item2 = new Item(item.id, "Taha");
rup.changed_items.Add(item2);
rup.changed_keys.Add(item2.id);
observer.OnNext(rup);
}
observer.OnCompleted();
return Disposable.empty;
});
return validate_naggle(raw);
}
[Test]
public async Task test_random_walk() {
// Split items in half.
// Permute one collection in the direction of the other
var raw = Observable.Create<RawUpdate<Item>>((observer) => {
Random r = new Random(42);
var even = _item_pool.Where(i => i.id % 2 == 0).Take(10).ToList();
var odd = _item_pool.Where(i => i.id % 2 == 1).ToList();
RawUpdate<Item> rup = new RawUpdate<Item>();
rup.added_items.AddRange(even);
rup.added_keys.AddRange(even.Select(i => i.id));
observer.OnNext(rup);
for (var x = 0; x < 200; ++x) {
rup = new RawUpdate<Item>();
switch (r.Next(0, 3)) {
case 0: // add
if (odd.Count > 0) {
even.Add(odd[0]);
rup.added_items.Add(odd[0]);
rup.added_keys.Add(odd[0].id);
odd.RemoveAt(0);
observer.OnNext(rup);
}
break;
case 1: // remove
if (even.Count > 0) {
var item = r.choose(even);
even.Remove(item);
rup.removed_keys.Add(item.id);
observer.OnNext(rup);
}
break;
case 2: // change
if (even.Count > 0) {
var item = r.choose(even);
var item2 = new Item(item.id, "Jake");
var index = even.IndexOf(item);
even[index] = item2;
rup.changed_items.Add(item2);
rup.changed_keys.Add(item2.id);
observer.OnNext(rup);
}
break;
}
}
observer.OnCompleted();
return Disposable.empty;
});
// Since this is deterministic randomness get the final collection
await validate_naggle(raw);
}
}
}
+9 -2
View File
@@ -6,8 +6,15 @@ using System.Threading.Tasks;
namespace bts.test {
//[TestGroup]
public class SomeTests {
// Tests for testing the tester
public class SomeTests : IDisposable {
SomeTests() {
// You can run common setup code here.
}
public void Dispose() {
// Implement dispose if you need to clean up
}
[Test]
public void test1() {
Assert.IsTrue(true);
+1
View File
@@ -3,6 +3,7 @@
<PropertyGroup>
<TargetFramework>net471</TargetFramework>
<Platform>x64</Platform>
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup>
<ItemGroup>