Example #1
0
        public RemoveEntitiesViewModel(ISourcesCacheProvider cacheProvider, IDockWindow window)
        {
            _cache  = cacheProvider.CurrentCache;
            _window = window;

            Title = "Remove Instances";

            RemoveSourceCommand   = ReactiveCommand.Create <Source>(RemoveSource);
            RemoveInstanceCommand = ReactiveCommand.Create <Instance>(RemoveInstance);
            RemoveMetricCommand   = ReactiveCommand.Create <Metric>(RemoveMetric);

            var filter = this.WhenAnyValue(x => x.FilterText)
                         .Throttle(TimeSpan.FromSeconds(.3))
                         .Publish();

            var dynamicFilterSource = filter
                                      .Select(Filters.CreateFilterSource);
            var dynamicFilterInstance = filter
                                        .Select(Filters.CreateFilterInstance);
            var dynamicFilterMetric = filter
                                      .Select(Filters.CreateFilterMetric);

            filter.Connect()
            .DisposeWith(Disposables);

            _cache.Sources
            .Connect()
            .Filter(dynamicFilterSource)
            .Sort(SortExpressionComparer <Source>
                  .Ascending(x => x.ID))
            .ObserveOnDispatcher()
            .Bind(out _sources)
            .Subscribe()
            .DisposeWith(Disposables);

            _cache.Instances
            .Connect()
            .Filter(dynamicFilterInstance)
            .Sort(SortExpressionComparer <Instance>
                  .Ascending(x => x.ID))
            .ObserveOnDispatcher()
            .Bind(out _instances)
            .Subscribe()
            .DisposeWith(Disposables);

            _cache.Metrics
            .Connect()
            .Filter(dynamicFilterMetric)
            .Sort(SortExpressionComparer <Metric>
                  .Ascending(x => x.ID))
            .ObserveOnDispatcher()
            .Bind(out _metrics)
            .Subscribe()
            .DisposeWith(Disposables);
        }
        public ManageGroupsViewModel(ISourcesCacheProvider cacheProvider, IDockWindow window)
        {
            _cache  = cacheProvider.CurrentCache;
            _window = window;

            Title = "Manage Groups";

            _cache.Groups
            .Connect()
            .Sort(SortExpressionComparer <Group>
                  .Ascending(x => x.ID))
            .ObserveOnDispatcher()
            .Bind(out _listGroups)
            .Subscribe()
            .DisposeWith(Disposables);

            _cache.InstancesWithoutGroup
            .Connect()
            .Sort(SortExpressionComparer <Instance>
                  .Ascending(x => x.ID))
            .ObserveOnDispatcher()
            .Bind(out _listWithoutGroup)
            .Subscribe()
            .DisposeWith(Disposables);

            var dynamicInListFilter = this
                                      .WhenAnyValue(x => x.SelectedGroup)
                                      .Select(g => (Func <Group, bool>)(x => g != null && x.ID == g.ID));

            _cache.Groups
            .Connect()
            .Filter(dynamicInListFilter)
            .TransformMany(x => x.Instances, x => x.ID)
            .Sort(SortExpressionComparer <Instance>
                  .Ascending(x => x.ID))
            .ObserveOnDispatcher()
            .Bind(out _listInGroup)
            .Subscribe()
            .DisposeWith(Disposables);

            SelectedGroup = ListGroups.FirstOrDefault();

            RemoveGroupCommand = ReactiveCommand.Create <Group>(RemoveGroup);
            CreateGroupCommand = ReactiveCommand.CreateFromTask(CreateGroup);
        }
        public KeepAliveViewModel(ISourcesCacheProvider cacheProvider)
        {
            KeepAliveSource = new SourceList <KeepAliveItem>();

            KeepAliveSource
            .Connect()
            .ObserveOnDispatcher()
            .Bind(out var keepAliveItems)
            .Subscribe()
            .DisposeWith(Disposables);
            KeepAliveList = keepAliveItems;

            _model = new KeepAliveModel
            {
                Caption = "KeepAlives",
                Cache   = cacheProvider.CurrentCache
            };
            Disposables.Add(_model);

            _model.WhenAnyValue(x => x.Caption, x => x.Online)
            .Subscribe(v => Title = v.Item1 + (v.Item2 ? " >" : " ||"));

            var canStart = _model.WhenAny(x => x.Online, x => !x.Value);

            StartCommand = ReactiveCommand.Create(OnStart, canStart);

            var canStop = _model.WhenAny(x => x.Online, x => x.Value);

            StopCommand = ReactiveCommand.Create(OnStop, canStop);

            UpdateCommand = ReactiveCommand.Create(OnUpdate, canStop);

            UpdateCommand.Subscribe(results =>
                                    KeepAliveSource.Edit(innerList =>
            {
                innerList.Clear();
                innerList.AddRange(results);
            }));
        }
        public MetricsViewModel(ISourcesCacheProvider cacheProvider)
        {
            _model = new MetricsModel
            {
                Caption = "Metrics",
                Cache   = cacheProvider.CurrentCache
            };
            Disposables.Add(_model);

            _model.WhenAnyValue(x => x.Caption, x => x.Online)
            .Subscribe(v => Title = v.Item1 + (v.Item2 ? " >" : " ||"));

            var dynamicFilter = _model.SelectedSourcesChanged
                                .Select(_ => Filters.CreateFilterMetricBySources(_model));

            var observable = _model.Cache.Metrics
                             .Connect()
                             .Filter(dynamicFilter)
                             .Publish();

            _metricsCache = observable
                            .AsObservableCache()
                            .DisposeWith(Disposables);

            observable
            .Transform(x => new MetricValueItem {
                Metric = x
            })
            .Sort(SortExpressionComparer <MetricValueItem>
                  .Ascending(x => x.Metric.Instance.Source.Name)
                  .ThenByAscending(x => x.Metric.Instance.Name)
                  .ThenByAscending(x => x.Metric.Name))
            .ObserveOnDispatcher()
            .Bind(out var metricValuesStatic)
            .Subscribe()
            .DisposeWith(Disposables);

            observable
            .Connect()
            .DisposeWith(Disposables);

            var metricValuesDynamicSource = new SourceList <MetricValueItem>()
                                            .DisposeWith(Disposables);

            metricValuesDynamicSource
            .Connect()
            .ObserveOnDispatcher()
            .Bind(out var metricValuesDynamic)
            .Subscribe()
            .DisposeWith(Disposables);

            _model.WhenAnyValue(x => x.MetricDiagramVisible)
            .Subscribe(x => MetricValuesList = x ? metricValuesStatic : metricValuesDynamic);

            _model.SelectedSourcesChanged.Subscribe(_ => UpdateSelectedMetrics());
            UpdateSelectedMetrics();

            var canStart = _model.WhenAny(x => x.Online, x => !x.Value);

            StartCommand = ReactiveCommand.Create(OnStart, canStart);

            var canStop = _model.WhenAny(x => x.Online, x => x.Value);

            StopCommand = ReactiveCommand.Create(OnStop, canStop);

            UpdateCommand = ReactiveCommand.Create(OnUpdate, canStop);

            var mapper = Mappers.Xy <MetricValueItem>()
                         .X(item => (double)item.Interval.Ticks / TimeSpan.FromMinutes(5).Ticks)
                         .Y(item => item.Value);

            //lets save the mapper globally.
            Charting.For <MetricValueItem>(mapper);

            SeriesCollection = new ChartValues <MetricValueItem>();

            //lets set how to display the X Labels
            DateTimeFormatter = value => new DateTime((long)value * TimeSpan.FromMinutes(5).Ticks).ToString("t");

            UpdateCommand.Subscribe(results =>
            {
                if (_model.MetricDiagramVisible)
                {
                    SeriesCollection.Clear();
                    if (results != null)
                    {
                        SeriesCollection.AddRange(results);
                    }
                }
                else
                {
                    metricValuesDynamicSource.Edit(innerList =>
                    {
                        innerList.Clear();
                        innerList.AddRange(results);
                    });
                }
            });
        }
        public StartupViewModel(IShell shell, ISourcesCacheProvider cacheProvider, IDockWindow window)
        {
            _shell         = shell;
            _cacheProvider = cacheProvider;
            _window        = window;

            var appConfigStorage = new AppConfigStorage();
            var config           = appConfigStorage.Load();

            Title = "App settings";

            // Title

            AppTitle = shell.Title;
            this.WhenAnyValue(x => x.AppTitle)
            .Subscribe(x => shell.Title = x);

            // Theme

            Accent = config.Accent ?? "Blue";
            IsDark = config.IsDark;
            UpdateTheme();

            this.ObservableForProperty(x => x.Accent)
            .Subscribe(x =>
            {
                config.Accent = x.Value;
                appConfigStorage.Save(config);
                UpdateTheme();
            });
            this.ObservableForProperty(x => x.IsDark)
            .Subscribe(x =>
            {
                config.IsDark = x.Value;
                appConfigStorage.Save(config);
                UpdateTheme();
            });

            // Server Urls

            var urls = config.ServerUrl?
                       .Split(';')
                       .Select(x => Uri.TryCreate(x, UriKind.Absolute, out var result) ? result : null as Uri)
                       .Where(x => x != null)
                       .ToArray();

            ServerUrlsSource = new SourceList <Uri>();
            if (urls != null)
            {
                ServerUrlsSource.AddRange(urls);
                ServerUrl = urls.First();
            }

            ServerUrlsSource
            .Connect()
            .ObserveOnDispatcher()
            .Bind(out _serverUrls)
            .ToCollection()
            .Subscribe(items =>
            {
                config.ServerUrl = string.Join(";", items);
                appConfigStorage.Save(config);
            });

            this.WhenAnyValue(x => x.ServerUrl)
            .Skip(1)
            .Subscribe(_ => UpdateSourcesCache());

            // Authorization Tokens

            var tokens = config.AuthToken?
                         .Split(';')
                         .Where(x => !string.IsNullOrEmpty(x))
                         .ToArray();

            AuthTokensSource = new SourceList <string>();
            if (tokens != null)
            {
                AuthTokensSource.AddRange(tokens);
                AuthToken = tokens.First();
            }

            AuthTokensSource
            .Connect()
            .ObserveOnDispatcher()
            .Bind(out _authTokens)
            .ToCollection()
            .Subscribe(items =>
            {
                config.AuthToken = string.Join(";", items);
                appConfigStorage.Save(config);
            });

            this.WhenAnyValue(x => x.AuthToken)
            .Skip(1)
            .Subscribe(_ => UpdateSourcesCache());

            // Create Commands

            var hasUrl = this.WhenAny(x => x.ServerUrl, x => x.Value != null);

            NewLogCommand       = CreateCommandWithInit(NewLog, hasUrl);
            NewKeepAliveCommand = CreateCommandWithInit(NewKeepAlive, hasUrl);
            NewMetricsCommand   = CreateCommandWithInit(NewMetrics, hasUrl);

            RemoveEntitiesCommand = CreateCommandWithInit(RemoveEntities, hasUrl);
            ManageGroupsCommand   = CreateCommandWithInit(ManageGroups, hasUrl);

            RemoveUrlCommand       = ReactiveCommand.Create <Uri>(url => ServerUrlsSource.Remove(url));
            RemoveAuthTokenCommand = ReactiveCommand.Create <string>(token => AuthTokensSource.Remove(token));

            RefreshCommand = ReactiveCommand.CreateFromTask(Refresh, hasUrl);

            UpdateSourcesCache();
        }
Example #6
0
        public LogsViewModel(IShell aShell, ISourcesCacheProvider cacheProvider)
        {
            ScrollTo = new Interaction <LogItem, Unit>();

            LogsSource = new SourceList <LogItem>();

            LogsSource
            .Connect()
            .ObserveOnDispatcher()
            .Bind(out _logsList)
            .Subscribe()
            .DisposeWith(Disposables);

            _model = new LogsModel
            {
                Caption = "Logs",
                Cache   = cacheProvider.CurrentCache
            }.DisposeWith(Disposables);

            _model.WhenAnyValue(x => x.Caption, x => x.Online)
            .ObserveOnDispatcher()
            .Subscribe(v => Title = v.Item1 + (v.Item2 ? " >" : " ||"))
            .DisposeWith(Disposables);

            _model.ObservableForProperty(x => x.Online)
            .Subscribe(p =>
            {
                if (p.Value)
                {
                    LastId = null;
                    LogsSource.Clear();

                    var interval    = TimeSpan.FromSeconds(_model.RefreshSec);
                    _updateExecutor = Observable
                                      .Timer(interval, interval)
                                      .Select(_ => Unit.Default)
                                      .InvokeCommand(UpdateCommand);
                }
                else
                {
                    _updateExecutor?.Dispose();
                }
            })
            .DisposeWith(Disposables);

            var canStart = _model.WhenAny(x => x.Online, x => !x.Value);

            StartCommand = ReactiveCommand.Create(() =>
            {
                _model.Online = true;
                return(Unit.Default);
            }, canStart);

            var canStop = _model.WhenAny(x => x.Online, x => x.Value);

            StopCommand = ReactiveCommand.Create(() =>
            {
                _model.Online = false;
                return(Unit.Default);
            }, canStop);

            UpdateCommand = ReactiveCommand.Create(OnUpdate);
            UpdateCommand.Subscribe(result =>
            {
                LogsSource.AddRange(result);
            });

            Observable.FromEventPattern <NotifyCollectionChangedEventHandler, NotifyCollectionChangedEventArgs>(
                h => ((INotifyCollectionChanged)_logsList).CollectionChanged += h,
                h => ((INotifyCollectionChanged)_logsList).CollectionChanged -= h)
            .Where(x => x.EventArgs.Action == NotifyCollectionChangedAction.Add)
            .Throttle(TimeSpan.FromSeconds(.1), RxApp.MainThreadScheduler)
            .Where(_ => _model.AutoScroll)
            .Subscribe(_ =>
            {
                var last = LogsSource.Items.LastOrDefault();
                if (last != null)
                {
                    ScrollTo.Handle(last).Wait();
                }
            })
            .DisposeWith(Disposables);
        }