예제 #1
0
        private void LoadCurrentState()
        {
            var sources   = _service.GetSources();
            var instances = _service.GetInstances();
            var metrics   = _service.GetMetrics();
            var groups    = _service.GetGroups();


            Sources.Edit(innerCache =>
            {
                var items = sources.Select(x => new Source
                {
                    ID        = x.ID,
                    Name      = x.Name,
                    Instances = new ObservableCollection <Instance>()
                });

                innerCache.Clear();
                innerCache.AddOrUpdate(items);
                innerCache.AddOrUpdate(_unknownSource);
            });

            Instances.Edit(innerCache =>
            {
                var items = instances.Select(x =>
                {
                    var source   = Sources.Lookup((short)x.SourceID).ValueOr(() => _unknownSource);
                    var instance = new Instance
                    {
                        ID      = x.ID,
                        Name    = x.Name,
                        Source  = source,
                        Metrics = new ObservableCollection <Metric>()
                    };
                    source.Instances.Add(instance);
                    return(instance);
                });

                innerCache.Clear();
                innerCache.AddOrUpdate(items);
                innerCache.AddOrUpdate(_unknownInstance);
            });

            InstancesWithoutGroup.Edit(innerCache =>
            {
                var idsInGroup = groups
                                 .SelectMany(x => x.Instances)
                                 .Distinct();
                var idsWithoutGroup = instances
                                      .Select(x => x.ID)
                                      .Except(idsInGroup);

                var items = idsWithoutGroup.Select(id => Instances.Lookup(id).Value);

                innerCache.Clear();
                innerCache.AddOrUpdate(items);
            });

            Metrics.Edit(innerCache =>
            {
                var items = metrics.Select(x =>
                {
                    var instance = Instances.Lookup(x.InstanceID).ValueOr(() => _unknownInstance);
                    var metric   = new Metric
                    {
                        ID          = x.ID,
                        Name        = x.Name,
                        Instance    = instance,
                        Aggregation = x.Aggregation
                    };
                    instance.Metrics.Add(metric);
                    return(metric);
                });

                innerCache.Clear();
                innerCache.AddOrUpdate(items);
            });

            Groups.Edit(innerCache =>
            {
                var items = groups.Select(x => new Group
                {
                    ID        = x.ID,
                    IsDefault = x.IsDefault,
                    Name      = x.Name,
                    Instances = new ObservableCollection <Instance>(x.Instances
                                                                    .Select(v => Instances.Lookup(v).ValueOrDefault())
                                                                    .Where(v => v != null))
                });

                innerCache.Clear();
                innerCache.AddOrUpdate(items);
            });

            SourceItems.Edit(innerCache =>
            {
                var itemsInGroup = Groups.Items
                                   .SelectMany(
                    x => x.Instances,
                    (g, i) => CreateSourceItem(i, g))
                                   .ToList();
                var itemsWithoutGroup = InstancesWithoutGroup.Items
                                        .Select(i => CreateSourceItem(i));

                innerCache.Clear();
                innerCache.AddOrUpdate(itemsInGroup);
                innerCache.AddOrUpdate(itemsWithoutGroup);
            });
        }