Example #1
0
        public void Update(bool skipJson = false)
        {
            // analysis
            var instructions = AggregationService.ComputeInstructions(this.AggregationSetup, _state, NullLogger.Instance);
            var sb           = new StringBuilder();

            foreach (var instruction in instructions)
            {
                sb.AppendLine($"Project '{instruction.Container.Id}'");

                foreach (var(registration, aggregationChannels) in instruction.DataReaderToAggregationsMap)
                {
                    if (aggregationChannels.Any())
                    {
                        sb.AppendLine();
                        sb.AppendLine($"\tData Reader '{registration.DataReaderId}' ({registration.RootPath})");

                        foreach (var aggregationChannel in aggregationChannels)
                        {
                            sb.AppendLine();
                            sb.AppendLine($"\t\t{aggregationChannel.Channel.Name} / {aggregationChannel.Channel.Group} / {aggregationChannel.Channel.Unit}");

                            foreach (var aggregation in aggregationChannel.Aggregations)
                            {
                                foreach (var period in aggregation.Periods)
                                {
                                    sb.Append($"\t\t\tPeriod: {period} s, ");

                                    foreach (var method in aggregation.Methods)
                                    {
                                        sb.Append($" {method.Key}");
                                    }

                                    sb.AppendLine();
                                }
                            }
                        }
                    }
                }

                sb.AppendLine();
            }

            this.Analysis = sb.ToString();

            // json
            if (!skipJson)
            {
                var options = new JsonSerializerOptions()
                {
                    WriteIndented = true
                };
                _jsonString = JsonSerializer.Serialize(this.AggregationSetup, options);
            }
        }
Example #2
0
        public static List <AggregationInstruction> ComputeInstructions(AggregationSetup setup, DatabaseManagerState state, ILogger logger)
        {
            var projectIds = setup.Aggregations
                             .Select(aggregation => aggregation.ProjectId)
                             .Distinct().ToList();

            return(projectIds.Select(projectId =>
            {
                var container = state.Database.ProjectContainers.FirstOrDefault(container => container.Id == projectId);

                if (container is null)
                {
                    return null;
                }

                var dataReaderRegistrations = container
                                              .Project
                                              .Channels
                                              .SelectMany(channel => channel.Datasets.Select(dataset => dataset.Registration))
                                              .Distinct()
                                              .Where(registration => registration != state.AggregationRegistration)
                                              .ToList();

                return new AggregationInstruction(container, dataReaderRegistrations.ToDictionary(registration => registration, registration =>
                {
                    // find aggregations for project ID
                    var potentialAggregations = setup.Aggregations
                                                .Where(parameters => parameters.ProjectId == container.Project.Id)
                                                .ToList();

                    // create channel to aggregations map
                    var aggregationChannels = container.Project.Channels
                                              // find all channels for current reader registration
                                              .Where(channel => channel.Datasets.Any(dataset => dataset.Registration == registration))
                                              // find all aggregations for current channel
                                              .Select(channel =>
                    {
                        var channelMeta = container.ProjectMeta.Channels
                                          .First(current => current.Id == channel.Id);

                        return new AggregationChannel()
                        {
                            Channel = channel,
                            Aggregations = potentialAggregations.Where(current => AggregationService.ApplyAggregationFilter(channel, channelMeta, current.Filters, logger)).ToList()
                        };
                    })
                                              // take all channels with aggregations
                                              .Where(aggregationChannel => aggregationChannel.Aggregations.Any());

                    return aggregationChannels.ToList();
                }));
            }).Where(instruction => instruction != null).ToList());
        }