Esempio n. 1
0
        public static double[] ApplyDatasetStatus2(Array dataset, byte[] status)
        {
            var methodName  = nameof(BufferUtilities.ForwardApplyDatasetStatus);
            var flags       = BindingFlags.NonPublic | BindingFlags.Static;
            var genericType = dataset.GetType().GetElementType();
            var parameters  = new object[] { dataset, status };

            var result = NexusUtilities.InvokeGenericMethod(typeof(BufferUtilities), null, methodName, flags, genericType, parameters);

            return((double[])result);
        }
Esempio n. 2
0
        private void AggregateProject(ClaimsPrincipal user,
                                      string databaseFolderPath,
                                      string projectId,
                                      DateTime date,
                                      AggregationSetup setup,
                                      AggregationInstruction instruction,
                                      CancellationToken cancellationToken)
        {
            foreach (var(registration, aggregationChannels) in instruction.DataReaderToAggregationsMap)
            {
                using var dataReader = _databaseManager.GetDataReader(user, registration);

                // find reader configurations
                foreach (var configuration in setup.ReaderConfigurations
                         .Where(configuration => configuration.ProjectId == projectId))
                {
                    var tmpRegistration = new DataReaderRegistration()
                    {
                        RootPath     = configuration.DataReaderRootPath,
                        DataReaderId = configuration.DataReaderId
                    };

                    if (dataReader.Registration.Equals(tmpRegistration))
                    {
                        dataReader.OptionalParameters = configuration.Parameters;
                        break;
                    }
                }

                // get files
                if (!dataReader.IsDataOfDayAvailable(projectId, date))
                {
                    return;
                }

                // project
                var container = _databaseManager.Database.ProjectContainers.FirstOrDefault(container => container.Id == projectId);

                if (container == null)
                {
                    throw new Exception($"The requested project '{projectId}' could not be found.");
                }

                var targetDirectoryPath = Path.Combine(databaseFolderPath, "DATA", WebUtility.UrlEncode(container.Id), date.ToString("yyyy-MM"), date.ToString("dd"));

                // for each channel
                foreach (var aggregationChannel in aggregationChannels)
                {
                    cancellationToken.ThrowIfCancellationRequested();

                    try
                    {
                        var dataset = aggregationChannel.Channel.Datasets.First();

                        NexusUtilities.InvokeGenericMethod(this, nameof(this.OrchestrateAggregation),
                                                           BindingFlags.Instance | BindingFlags.NonPublic,
                                                           NexusUtilities.GetTypeFromNexusDataType(dataset.DataType),
                                                           new object[]
                        {
                            targetDirectoryPath,
                            dataReader,
                            dataset,
                            aggregationChannel.Aggregations,
                            date,
                            setup.Force,
                            cancellationToken
                        });
                    }
                    catch (TaskCanceledException)
                    {
                        throw;
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError(ex.GetFullMessage());
                    }
                }
            }
        }
Esempio n. 3
0
        private RenderFragment CreateComponent() => builder =>
        {
            var properties = this.DataContext.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);

            var sequence = 0; // Autoincrement is not recommended for performance reasons (https://docs.microsoft.com/en-us/aspnet/core/blazor/advanced-scenarios?view=aspnetcore-5.0#the-problem-with-generating-sequence-numbers-programmatically)

            builder.OpenElement(sequence++, "div");
            builder.AddAttribute(sequence++, "class", "form-generator");

            foreach (var property in properties)
            {
                builder.OpenElement(sequence++, "div");
                builder.AddAttribute(sequence++, "class", "form-generator-property");

                var type       = this.DataContext.GetType();
                var constant   = Expression.Constant(this.DataContext, type);
                var expression = MemberExpression.Property(constant, property.Name);
                var value      = property.GetValue(this.DataContext);

                if (property.PropertyType == typeof(string))
                {
                    builder.OpenComponent(sequence++, typeof(MatTextField <string>));
                    builder.AddAttribute(sequence++, "Value", value);

                    builder.AddAttribute(sequence++, "ValueChanged",
                                         EventCallback.Factory.Create <string>(this, value =>
                    {
                        property.SetValue(this.DataContext, value);
                        this.OnChanged();
                    }));

                    builder.AddAttribute(sequence++, "Label", property.Name);
                    builder.CloseComponent();
                }
                else if (property.PropertyType == typeof(bool))
                {
                    builder.OpenComponent(sequence++, typeof(MatCheckbox <bool>));
                    builder.AddAttribute(sequence++, "Value", value);

                    builder.AddAttribute(sequence++, "ValueChanged",
                                         EventCallback.Factory.Create <bool>(this, value =>
                    {
                        property.SetValue(this.DataContext, value);
                        this.OnChanged();
                    }));

                    builder.AddAttribute(sequence++, "Label", property.Name);
                    builder.CloseComponent();
                }
                else if (property.PropertyType == typeof(DateTime))
                {
                    builder.OpenComponent(sequence++, typeof(MatDatePicker <DateTime>));
                    builder.AddAttribute(sequence++, "Value", value);

                    builder.AddAttribute(sequence++, "ValueChanged",
                                         EventCallback.Factory.Create <DateTime>(this, value =>
                    {
                        property.SetValue(this.DataContext, value);
                        this.OnChanged();
                    }));

                    builder.AddAttribute(sequence++, "Label", property.Name);
                    builder.AddAttribute(sequence++, "Format", "dd.MM.yyyy HH:mm");
                    builder.AddAttribute(sequence++, "EnableTime", true);
                    builder.AddAttribute(sequence++, "EnableSeconds", false);
                    builder.AddAttribute(sequence++, "Enable24hours", true);
                    builder.AddAttribute(sequence++, "AllowInput", true);
                    builder.CloseComponent();
                }
                // List
                else if (value is IList list)
                {
                    var elementType = list.GetType().GetGenericArguments().Single();

                    builder.OpenElement(sequence++, "div");
                    builder.AddAttribute(sequence++, "class", "form-generator-list");

                    builder.OpenElement(sequence++, "div");
                    builder.AddAttribute(sequence++, "class", "form-generator-header");

                    builder.OpenElement(sequence++, "span");
                    builder.AddAttribute(sequence++, "class", "form-generator-title");
                    builder.AddContent(sequence++, $"{property.Name} ({list.Count})");
                    builder.CloseComponent();

                    builder.OpenElement(sequence++, "span");
                    builder.AddAttribute(sequence++, "class", "form-generator-add-button");
                    builder.AddAttribute(sequence++, "onclick",
                                         EventCallback.Factory.Create <MouseEventArgs>(this, value =>
                    {
                        var element = this.Instantiate(elementType);
                        list.Add(element);
                        this.OnChanged();
                    }));
                    builder.OpenElement(sequence++, "i");
                    builder.AddAttribute(sequence++, "class", "fas fa-plus");
                    builder.CloseElement();
                    builder.CloseElement();

                    builder.CloseElement();

                    builder.OpenElement(sequence++, "div");
                    builder.AddAttribute(sequence++, "class", "form-generator-content");

                    if (list.Count > 0)
                    {
                        var index = 0;

                        foreach (var item in list)
                        {
                            builder.OpenElement(sequence++, "div");
                            builder.AddAttribute(sequence++, "class", "form-generator-list-item");

                            if (elementType.IsPrimitive || elementType.IsEnum || elementType == typeof(string) || elementType == typeof(decimal))
                            {
                                var localIndex = index;

                                Action <object> valueChanged = newValue =>
                                {
                                    list[localIndex] = newValue;
                                    this.OnChanged();
                                };

                                var onKeyDown = EventCallback.Factory.Create <KeyboardEventArgs>(this, e =>
                                {
                                    if (e.Key == "Delete")
                                    {
                                        list.RemoveAt(localIndex);
                                        this.OnChanged();
                                    }
                                });

                                sequence = (int)NexusUtilities.InvokeGenericMethod(
                                    this.GetType(),
                                    this,
                                    nameof(AddGenericPrimitive),
                                    BindingFlags.NonPublic | BindingFlags.Instance,
                                    elementType,
                                    new object[] { builder, sequence, "Value", item, valueChanged, onKeyDown });
                            }
                            else
                            {
                                builder.OpenComponent(sequence++, typeof(FormGenerator));
                                builder.AddAttribute(sequence++, "DataContext", item);
                                builder.AddAttribute(sequence++, "Changed",
                                                     EventCallback.Factory.Create(this, value => this.OnChanged()));
                                builder.CloseComponent();

                                var localIndex = index;
                                builder.OpenElement(sequence++, "span");
                                builder.AddAttribute(sequence++, "class", "form-generator-remove-button");
                                builder.AddAttribute(sequence++, "onclick",
                                                     EventCallback.Factory.Create <MouseEventArgs>(this, e =>
                                {
                                    var element = this.Instantiate(elementType);
                                    list.RemoveAt(localIndex);
                                    this.OnChanged();
                                }));
                                builder.OpenElement(sequence++, "i");
                                builder.AddAttribute(sequence++, "class", "fas fa-trash-alt");
                                builder.CloseElement();
                                builder.CloseElement();
                            }

                            builder.CloseElement();

                            index++;
                        }
                    }

                    builder.CloseElement();
                    builder.CloseElement();
                }
                // Dictionary
                else if (value is IDictionary dict)
                {
                    var elementTypes = dict.GetType().GetGenericArguments();

                    if (!(elementTypes[0].IsPrimitive || elementTypes[0].IsEnum || elementTypes[0] == typeof(string) || elementTypes[0] == typeof(decimal)))
                    {
                        throw new Exception("Only primitive keys are supported.");
                    }

                    builder.OpenElement(sequence++, "div");
                    builder.AddAttribute(sequence++, "class", "form-generator-dict");

                    builder.OpenElement(sequence++, "div");
                    builder.AddAttribute(sequence++, "class", "form-generator-header");

                    builder.OpenElement(sequence++, "span");
                    builder.AddAttribute(sequence++, "class", "form-generator-title");
                    builder.AddContent(sequence++, $"{property.Name} ({dict.Count})");
                    builder.CloseComponent();

                    builder.OpenElement(sequence++, "span");
                    builder.AddAttribute(sequence++, "class", "form-generator-add-button");
                    builder.AddAttribute(sequence++, "onclick",
                                         EventCallback.Factory.Create <MouseEventArgs>(this, value =>
                    {
                        var dictKey   = this.Instantiate(elementTypes[0]);
                        var dictValue = this.Instantiate(elementTypes[1]);
                        dict[dictKey] = dictValue;
                        this.OnChanged();
                    }));
                    builder.OpenElement(sequence++, "i");
                    builder.AddAttribute(sequence++, "class", "fas fa-plus");
                    builder.CloseElement();
                    builder.CloseElement();

                    builder.CloseElement();

                    builder.OpenElement(sequence++, "div");
                    builder.AddAttribute(sequence++, "class", "form-generator-content");

                    if (dict.Count > 0)
                    {
                        foreach (var key in dict.Keys)
                        {
                            builder.OpenElement(sequence++, "div");
                            builder.AddAttribute(sequence++, "class", "form-generator-dict-entry");

                            // key
                            Action <object> valueChanged = newKey =>
                            {
                                dict.Remove(key);
                                dict[newKey] = this.Instantiate(elementTypes[1]);
                                this.OnChanged();
                            };

                            var onKeyDown = EventCallback.Factory.Create <KeyboardEventArgs>(this, e =>
                            {
                                if (e.Key == "Delete")
                                {
                                    dict.Remove(key);
                                    this.OnChanged();
                                }
                            });

                            sequence = (int)NexusUtilities.InvokeGenericMethod(
                                this.GetType(),
                                this,
                                nameof(AddGenericPrimitive),
                                BindingFlags.NonPublic | BindingFlags.Instance,
                                elementTypes[0],
                                new object[] { builder, sequence, "Key", key, valueChanged, onKeyDown });

                            // space
                            builder.OpenElement(sequence++, "span");
                            builder.AddContent(sequence++, " ");
                            builder.CloseElement();

                            // value
                            if (elementTypes[1].IsPrimitive || elementTypes[1].IsEnum || elementTypes[1] == typeof(string) || elementTypes[1] == typeof(decimal))
                            {
                                Action <object> valueChanged2 = newValue =>
                                {
                                    dict[key] = newValue;
                                    this.OnChanged();
                                };

                                sequence = (int)NexusUtilities.InvokeGenericMethod(
                                    this.GetType(),
                                    this,
                                    nameof(AddGenericPrimitive),
                                    BindingFlags.NonPublic | BindingFlags.Instance,
                                    elementTypes[1],
                                    new object[] { builder, sequence, "Value", dict[key], valueChanged2, null });
                            }
                            else
                            {
                                builder.OpenComponent(sequence++, typeof(FormGenerator));
                                builder.AddAttribute(sequence++, "DataContext", dict[key]);
                                builder.AddAttribute(sequence++, "Changed",
                                                     EventCallback.Factory.Create(this, value => this.OnChanged()));
                                builder.CloseComponent();
                            }

                            builder.CloseElement();
                        }
                    }

                    builder.CloseElement();
                    builder.CloseElement();
                }

                builder.CloseElement();
            }

            builder.CloseComponent();
        };