Exemplo n.º 1
0
        private static async Task AddValuesToInfluxSeriesByInterfaceAsync <TInfluxRow, TTimestamp>(
            InfluxSeries <TInfluxRow> influxSerie,
            SeriesResult series,
            InfluxClient client,
            string db,
            bool allowMetadataQuerying,
            InfluxRowTypeInfo <TInfluxRow> propertyMap,
            InfluxQueryOptions options,
            ITimestampParser <TTimestamp> timestampParser)
            where TInfluxRow : IInfluxRow <TTimestamp>, new()
        {
            // Values will be null, if there are no entries in the result set
            if (series.Values != null)
            {
                var precision  = options.Precision;
                var name       = series.Name;
                var columns    = series.Columns;
                var setters    = new Action <TInfluxRow, string, object> [columns.Count];
                var dataPoints = new List <TInfluxRow>();
                // Get metadata information about the measurement we are querying, as we dont know
                // which columns are tags/fields otherwise

                DatabaseMeasurementInfo meta = null;
                if (allowMetadataQuerying)
                {
                    meta = await client.GetMetaInformationAsync(db, name, options.MetadataExpiration).ConfigureAwait(false);
                }

                for (int i = 0; i < columns.Count; i++)
                {
                    var columnName = columns[i];

                    if (!allowMetadataQuerying)
                    {
                        setters[i] = (row, fieldName, value) => row.SetField(fieldName, value);
                    }
                    else if (columnName == InfluxConstants.TimeColumn)
                    {
                        setters[i] = (row, timeName, value) => row.SetTimestamp(timestampParser.ToTimestamp(options.Precision, value));
                    }
                    else if (meta.Tags.Contains(columnName))
                    {
                        setters[i] = (row, tagName, value) => row.SetTag(tagName, (string)value);
                    }
                    else
                    {
                        setters[i] = (row, fieldName, value) => row.SetField(fieldName, value);
                    }
                }

                // constructs the IInfluxRows using the IInfluxRow interface
                foreach (var values in series.Values)
                {
                    var dataPoint = new TInfluxRow();
                    propertyMap.SetMeasurementName(name, dataPoint);

                    // go through all values that are stored as a List<List<object>>
                    for (int i = 0; i < values.Count; i++)
                    {
                        var value = values[i]; // TODO: What about NULL values? Are they treated as empty strings or actual nulls?
                        if (value != null)
                        {
                            setters[i](dataPoint, columns[i], value);
                        }
                    }

                    dataPoints.Add(dataPoint);
                }

                influxSerie.Rows.AddRange(dataPoints);
            }
        }
Exemplo n.º 2
0
        private static void AddValuesToInfluxSeriesByAttributes <TInfluxRow, TTimestamp>(
            InfluxSeries <TInfluxRow> influxSerie,
            SeriesResult series,
            InfluxRowTypeInfo <TInfluxRow> propertyMap,
            InfluxQueryOptions options,
            ITimestampParser <TTimestamp> timestampParser)
            where TInfluxRow : new()
        {
            // Values will be null, if there are no entries in the result set
            if (series.Values != null)
            {
                var dataPoints = new List <TInfluxRow>();

                var precision = options.Precision;
                var columns   = series.Columns;
                var name      = series.Name;

                // construct an array of properties based on the same indexing as the columns returned by the query
                var properties = new PropertyExpressionInfo <TInfluxRow> [columns.Count];
                for (int i = 0; i < columns.Count; i++)
                {
                    PropertyExpressionInfo <TInfluxRow> propertyInfo;
                    if (propertyMap.All.TryGetValue(columns[i], out propertyInfo))
                    {
                        properties[i] = propertyInfo;
                    }
                }

                foreach (var values in series.Values)
                {
                    // construct the data points based on the attributes
                    var dataPoint = new TInfluxRow();
                    propertyMap.SetMeasurementName(name, dataPoint);

                    for (int i = 0; i < values.Count; i++)
                    {
                        var value    = values[i]; // TODO: What about NULL values? Are they treated as empty strings or actual nulls?
                        var property = properties[i];

                        // set the value based on the property, if both the value and property is not null
                        if (property != null)
                        {
                            if (value != null)
                            {
                                if (property.Key == InfluxConstants.TimeColumn)
                                {
                                    property.SetValue(dataPoint, timestampParser.ToTimestamp(options.Precision, value));
                                }
                                else if (property.IsDateTime)
                                {
                                    property.SetValue(dataPoint, DateTime.Parse((string)value, CultureInfo.InvariantCulture, OnlyUtcStyles));
                                }
                                else if (property.IsEnum)
                                {
                                    property.SetValue(dataPoint, property.GetEnumValue(value));
                                }
                                else
                                {
                                    // will throw exception if types does not match up
                                    property.SetValue(dataPoint, Convert.ChangeType(value, property.Type, CultureInfo.InvariantCulture));
                                }
                            }
                        }
                    }

                    dataPoints.Add(dataPoint);
                }

                influxSerie.Rows.AddRange(dataPoints);
            }
        }