From 6f69f1c2e20a04e58fb0090000ffb71068f76a5e Mon Sep 17 00:00:00 2001 From: John Colvin Date: Sun, 14 Aug 2022 16:10:35 -0400 Subject: [PATCH] fix (preemptive): Apply Day Fills settings migration rules This is a preemptive fix bc as of now (8/14/2022), there are no Day Fills migration rules --- .../spark/Model/Settings/UserSettingsHub.cs | 377 +++++++++--------- 1 file changed, 189 insertions(+), 188 deletions(-) diff --git a/src/client/spark/Model/Settings/UserSettingsHub.cs b/src/client/spark/Model/Settings/UserSettingsHub.cs index 327d180ae..2953c3f0a 100644 --- a/src/client/spark/Model/Settings/UserSettingsHub.cs +++ b/src/client/spark/Model/Settings/UserSettingsHub.cs @@ -1,188 +1,189 @@ -using bts; -using bts.settings; -using bts.shell.pnl; -using bts.utils; - -using mbts.spark; -using mbts.ui.settings; - -using System; -using System.Collections.Generic; -using System.Composition; -using System.Linq; -using System.Reactive.Linq; -using System.Reactive.Subjects; -using System.Threading.Tasks; - -namespace mbts.persist { - /// - /// Represents the top level of the persisted settings hierarchy. - /// One UserSettings object, which contains the defaults for every SparkWindow, can be read/written on the server by UISettingsHub. - /// - [Export(typeof(UserSettingsHub))] - class UserSettingsHub { - // do NOT CHANGE these consts, they represent a file path on the server - const string NAME_SPACE = "settings", DEFAULT_KEY = "default"; - - static readonly IImmutableProperty - _fills_propi = ImmutableProperty.create(us => us.fills_grid, (us, gs) => us.with(fills: gs)), - _day_fills_propi = ImmutableProperty.create(us => us.day_fills_grid, (us, gs) => us.with(day_fills: gs)), - _mview_propi = ImmutableProperty.create(us => us.market_view_grid, (us, gs) => us.with(market_view: gs)), - _synth_propi = ImmutableProperty.create(us => us.synthetic_management_grid, (us, gs) => us.with(synthetic_management: gs)), - _reconcile_report_propi = ImmutableProperty.create(us => us.reconcile_report_grid, (us, gs) => us.with(reconcile_report: gs)); - - static readonly IImmutableProperty - _pnl_propi = ImmutableProperty.create(us => us.pnl, (us, pnl) => us.with(pnl: pnl)); - - readonly object _default_settings_lock = new object(); - readonly BehaviorSubject _default_settings_subject = new BehaviorSubject(UserSettings.factory_defaults); - readonly IEnumerable _fills_migration_rules; - readonly IEnumerable _day_fills_migration_rules; - readonly IEnumerable _mview_migration_rules; - readonly IEnumerable _pnl_migration_rules; - readonly IEnumerable _synth_migration_rules; - readonly IEnumerable _reconcile_report_migration_rules; - readonly IUserPersist _persist; - - UserSettings _default_settings; - - [ImportingConstructor] - public UserSettingsHub( - IUserPersist persist, - [ImportMany] IEnumerable fills_migration_rules, - [ImportMany] IEnumerable day_fills_migration_rules, - [ImportMany] IEnumerable mview_migration_rules, - [ImportMany] IEnumerable pandl_migration_rules, - [ImportMany] IEnumerable pnl_migration_rules, - [ImportMany] IEnumerable synth_migration_rules, - [ImportMany] IEnumerable reconcile_report_migration_rules) { - // - _persist = persist; - _fills_migration_rules = fills_migration_rules ?? Enumerable.Empty(); - _day_fills_migration_rules = day_fills_migration_rules ?? Enumerable.Empty(); - _mview_migration_rules = mview_migration_rules ?? Enumerable.Empty(); - _pnl_migration_rules = pnl_migration_rules ?? Enumerable.Empty(); - _synth_migration_rules = synth_migration_rules ?? Enumerable.Empty(); - _reconcile_report_migration_rules = reconcile_report_migration_rules.empty_if_null(); - init(); - async void init() { - UserSettings s; - try { - s = await _persist.get(NAME_SPACE, DEFAULT_KEY); - if (s != null) { - var wrapped_rules = new[] { - _fills_migration_rules.merge().wrap(_fills_propi), - _mview_migration_rules.merge().wrap(_mview_propi), - _pnl_migration_rules.merge().wrap(_pnl_propi), - _synth_migration_rules.merge().wrap(_synth_propi), - _reconcile_report_migration_rules.merge().wrap(_reconcile_report_propi) - }; - SettingsMigrationHelper.apply_quiet(ref s, wrapped_rules); - // TODO: move ladder sanitization logic into ISettingsMigrationRule - s.ladder.sanitize(); - // ensure valid named ladder set (w/ distinct, non-zero ids) - var ladder_profile_map = new Dictionary(); - var unidentified_lps = new List(); - foreach (var lp in s.ladder_profiles) { - lp.value.sanitize(); - if (lp.id == 0) { - unidentified_lps.Add(lp); - } else ladder_profile_map[lp.id] = lp; - } - uint id = s.next_ladder_profile_id; - foreach (var lp in unidentified_lps) { - ladder_profile_map[id] = lp.with(id); - id++; - } - var sorted_lps = ladder_profile_map.Values.ToArray(); - Array.Sort(sorted_lps, Comparer.Default.wrap(lp => lp.id)); - if (!sorted_lps.SequenceEqual(s.ladder_profiles)) - await _persist.set(NAME_SPACE, DEFAULT_KEY, s = s.with(ladder_profiles: sorted_lps)); - } - } catch (Exception ex) { - Log.Persist.warn($"Error loading {typeof(UserSettings)} from namespace '{NAME_SPACE}', will restore factory defaults and continue as usual: {ex}"); - await save_default(s = UserSettings.factory_defaults); - } - lock (_default_settings_lock) { - initialized = true; - if (s != null) - _default_settings_subject.OnNext(_default_settings = s); - } - } - } - - // indicates whether or not the settings have been loaded from the server (this happens once, on startup - see the constructor for details) - public bool initialized { get; private set; } - - internal UserSettings current { - get { - lock (_default_settings_lock) - return _default_settings ?? UserSettings.factory_defaults; - } - } - - /// Subscribing via _persist.subscribe may be cleaner in theory, however using a local event allows observers to be notified immediately. - /// Assuming there aren't any rogue agents saving UserSettings by themselves, there should never be any discrepancies. - public IObservable subscribe_default_settings() => _default_settings_subject.AsObservable(); - - public IWindowSettingsManager create_fills_settings_manager(GridSettings instance_settings) => - _create_settings_manager( - instance_settings, - _fills_migration_rules, - _fills_propi); - - public IWindowSettingsManager create_mview_settings_manager(GridSettings instance_settings) => - _create_settings_manager( - instance_settings, - _mview_migration_rules, - _mview_propi); - - public IWindowSettingsManager create_pnl_settings_manager(PNLSettings instance_settings) => - _create_settings_manager( - instance_settings, - _pnl_migration_rules, - _pnl_propi); - - public IWindowSettingsManager create_synth_settings_manager(GridSettings instance_settings) => - _create_settings_manager( - instance_settings, - _synth_migration_rules, - _synth_propi); - - public IWindowSettingsManager create_reconcile_report_settings_manager( - GridSettings instance_settings) => - _create_settings_manager( - instance_settings, - _reconcile_report_migration_rules, - _reconcile_report_propi); - - public IWindowSettingsManager create_day_fills_settings_manager( - GridSettings instance_settings) => - _create_settings_manager( - instance_settings, - _day_fills_migration_rules, - _day_fills_propi); - - public async Task save_default(UserSettings value) { - // notify observers before persisting so the changes are reflected immediately in the ui - lock (_default_settings_lock) { - _default_settings_subject.OnNext(_default_settings = value); - } - await _persist_async(); - } - - IWindowSettingsManager _create_settings_manager(T instance_settings, IEnumerable> migration_rules, IImmutableProperty propi) where T : class { - if (instance_settings != null && migration_rules.merge().apply(instance_settings, out var new_settings)) - instance_settings = new_settings; - return new WindowSettingsManager(this, instance_settings, propi.get, propi.set); - } - - async Task _persist_async() { - try { - await _persist.set(NAME_SPACE, DEFAULT_KEY, _default_settings); - } catch (Exception ex) { - Log.Persist.warn($"Error persisting {typeof(UserSettings)} in namespace '{NAME_SPACE}': {ex}"); - } - } - } -} +using bts; +using bts.settings; +using bts.shell.pnl; +using bts.utils; + +using mbts.spark; +using mbts.ui.settings; + +using System; +using System.Collections.Generic; +using System.Composition; +using System.Linq; +using System.Reactive.Linq; +using System.Reactive.Subjects; +using System.Threading.Tasks; + +namespace mbts.persist { + /// + /// Represents the top level of the persisted settings hierarchy. + /// One UserSettings object, which contains the defaults for every SparkWindow, can be read/written on the server by UISettingsHub. + /// + [Export(typeof(UserSettingsHub))] + class UserSettingsHub { + // do NOT CHANGE these consts, they represent a file path on the server + const string NAME_SPACE = "settings", DEFAULT_KEY = "default"; + + static readonly IImmutableProperty + _fills_propi = ImmutableProperty.create(us => us.fills_grid, (us, gs) => us.with(fills: gs)), + _day_fills_propi = ImmutableProperty.create(us => us.day_fills_grid, (us, gs) => us.with(day_fills: gs)), + _mview_propi = ImmutableProperty.create(us => us.market_view_grid, (us, gs) => us.with(market_view: gs)), + _synth_propi = ImmutableProperty.create(us => us.synthetic_management_grid, (us, gs) => us.with(synthetic_management: gs)), + _reconcile_report_propi = ImmutableProperty.create(us => us.reconcile_report_grid, (us, gs) => us.with(reconcile_report: gs)); + + static readonly IImmutableProperty + _pnl_propi = ImmutableProperty.create(us => us.pnl, (us, pnl) => us.with(pnl: pnl)); + + readonly object _default_settings_lock = new object(); + readonly BehaviorSubject _default_settings_subject = new BehaviorSubject(UserSettings.factory_defaults); + readonly IEnumerable _fills_migration_rules; + readonly IEnumerable _day_fills_migration_rules; + readonly IEnumerable _mview_migration_rules; + readonly IEnumerable _pnl_migration_rules; + readonly IEnumerable _synth_migration_rules; + readonly IEnumerable _reconcile_report_migration_rules; + readonly IUserPersist _persist; + + UserSettings _default_settings; + + [ImportingConstructor] + public UserSettingsHub( + IUserPersist persist, + [ImportMany] IEnumerable fills_migration_rules, + [ImportMany] IEnumerable day_fills_migration_rules, + [ImportMany] IEnumerable mview_migration_rules, + [ImportMany] IEnumerable pandl_migration_rules, + [ImportMany] IEnumerable pnl_migration_rules, + [ImportMany] IEnumerable synth_migration_rules, + [ImportMany] IEnumerable reconcile_report_migration_rules) { + // + _persist = persist; + _fills_migration_rules = fills_migration_rules ?? Enumerable.Empty(); + _day_fills_migration_rules = day_fills_migration_rules ?? Enumerable.Empty(); + _mview_migration_rules = mview_migration_rules ?? Enumerable.Empty(); + _pnl_migration_rules = pnl_migration_rules ?? Enumerable.Empty(); + _synth_migration_rules = synth_migration_rules ?? Enumerable.Empty(); + _reconcile_report_migration_rules = reconcile_report_migration_rules.empty_if_null(); + init(); + async void init() { + UserSettings s; + try { + s = await _persist.get(NAME_SPACE, DEFAULT_KEY); + if (s != null) { + var wrapped_rules = new[] { + _fills_migration_rules.merge().wrap(_fills_propi), + _day_fills_migration_rules.merge().wrap(_day_fills_propi), + _mview_migration_rules.merge().wrap(_mview_propi), + _pnl_migration_rules.merge().wrap(_pnl_propi), + _synth_migration_rules.merge().wrap(_synth_propi), + _reconcile_report_migration_rules.merge().wrap(_reconcile_report_propi) + }; + SettingsMigrationHelper.apply_quiet(ref s, wrapped_rules); + // TODO: move ladder sanitization logic into ISettingsMigrationRule + s.ladder.sanitize(); + // ensure valid named ladder set (w/ distinct, non-zero ids) + var ladder_profile_map = new Dictionary(); + var unidentified_lps = new List(); + foreach (var lp in s.ladder_profiles) { + lp.value.sanitize(); + if (lp.id == 0) { + unidentified_lps.Add(lp); + } else ladder_profile_map[lp.id] = lp; + } + uint id = s.next_ladder_profile_id; + foreach (var lp in unidentified_lps) { + ladder_profile_map[id] = lp.with(id); + id++; + } + var sorted_lps = ladder_profile_map.Values.ToArray(); + Array.Sort(sorted_lps, Comparer.Default.wrap(lp => lp.id)); + if (!sorted_lps.SequenceEqual(s.ladder_profiles)) + await _persist.set(NAME_SPACE, DEFAULT_KEY, s = s.with(ladder_profiles: sorted_lps)); + } + } catch (Exception ex) { + Log.Persist.warn($"Error loading {typeof(UserSettings)} from namespace '{NAME_SPACE}', will restore factory defaults and continue as usual: {ex}"); + await save_default(s = UserSettings.factory_defaults); + } + lock (_default_settings_lock) { + initialized = true; + if (s != null) + _default_settings_subject.OnNext(_default_settings = s); + } + } + } + + // indicates whether or not the settings have been loaded from the server (this happens once, on startup - see the constructor for details) + public bool initialized { get; private set; } + + internal UserSettings current { + get { + lock (_default_settings_lock) + return _default_settings ?? UserSettings.factory_defaults; + } + } + + /// Subscribing via _persist.subscribe may be cleaner in theory, however using a local event allows observers to be notified immediately. + /// Assuming there aren't any rogue agents saving UserSettings by themselves, there should never be any discrepancies. + public IObservable subscribe_default_settings() => _default_settings_subject.AsObservable(); + + public IWindowSettingsManager create_fills_settings_manager(GridSettings instance_settings) => + _create_settings_manager( + instance_settings, + _fills_migration_rules, + _fills_propi); + + public IWindowSettingsManager create_mview_settings_manager(GridSettings instance_settings) => + _create_settings_manager( + instance_settings, + _mview_migration_rules, + _mview_propi); + + public IWindowSettingsManager create_pnl_settings_manager(PNLSettings instance_settings) => + _create_settings_manager( + instance_settings, + _pnl_migration_rules, + _pnl_propi); + + public IWindowSettingsManager create_synth_settings_manager(GridSettings instance_settings) => + _create_settings_manager( + instance_settings, + _synth_migration_rules, + _synth_propi); + + public IWindowSettingsManager create_reconcile_report_settings_manager( + GridSettings instance_settings) => + _create_settings_manager( + instance_settings, + _reconcile_report_migration_rules, + _reconcile_report_propi); + + public IWindowSettingsManager create_day_fills_settings_manager( + GridSettings instance_settings) => + _create_settings_manager( + instance_settings, + _day_fills_migration_rules, + _day_fills_propi); + + public async Task save_default(UserSettings value) { + // notify observers before persisting so the changes are reflected immediately in the ui + lock (_default_settings_lock) { + _default_settings_subject.OnNext(_default_settings = value); + } + await _persist_async(); + } + + IWindowSettingsManager _create_settings_manager(T instance_settings, IEnumerable> migration_rules, IImmutableProperty propi) where T : class { + if (instance_settings != null && migration_rules.merge().apply(instance_settings, out var new_settings)) + instance_settings = new_settings; + return new WindowSettingsManager(this, instance_settings, propi.get, propi.set); + } + + async Task _persist_async() { + try { + await _persist.set(NAME_SPACE, DEFAULT_KEY, _default_settings); + } catch (Exception ex) { + Log.Persist.warn($"Error persisting {typeof(UserSettings)} in namespace '{NAME_SPACE}': {ex}"); + } + } + } +}