Exemple #1
0
        internal PropertyExpressionInfo(string key, PropertyInfo property)
        {
            if (key != null)
            {
                if (key.Contains("\n"))
                {
                    throw new InfluxException(Errors.InvalidTagOrFieldName);
                }
                LineProtocolEscapedKey = LineProtocolEscape.EscapeKey(key);
                Key = key;
            }

            Property = property;

            // Instance type of target entity class
            ParameterExpression instanceParam = Expression.Parameter(typeof(TInfluxRow), "x");

            // Instance type of target entity class
            ParameterExpression valueParam = Expression.Parameter(typeof(object), "y");

            // instance.Property
            MemberExpression getProperty = Expression.Property(instanceParam, property);
            var getterLambda             = Expression.Lambda <Func <TInfluxRow, object> >(Expression.Convert(getProperty, typeof(object)), true, instanceParam);

            GetValue = getterLambda.Compile();

            // instance.Property = row[property]
            if (property.CanWrite)
            {
                BinaryExpression assignProperty = Expression.Assign(getProperty, Expression.Convert(valueParam, property.PropertyType));
                var setterLambda = Expression.Lambda <Action <TInfluxRow, object> >(assignProperty, true, instanceParam, valueParam);
                SetValue = setterLambda.Compile();
            }

            var type = property.PropertyType;

            RawType = type;

            if (type.GetTypeInfo().IsGenericType&& type.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                // unwrap the nullable type
                Type = type.GetGenericArguments()[0];
            }
            else
            {
                Type = type;
            }

            IsEnum           = Type.GetTypeInfo().IsEnum;
            IsDateTime       = Type == typeof(DateTime);
            IsDateTimeOffset = Type == typeof(DateTimeOffset);

            // ensure we can convert between string/enum
            if (IsEnum)
            {
                EnumToString = new Dictionary <Enum, string>();
                StringToEnum = new Dictionary <string, Enum>();

                var values = Enum.GetValues(Type);
                foreach (Enum value in values)
                {
                    string stringValue = value.ToString();
                    var    memberInfo  = Type.GetField(stringValue);
                    if (memberInfo != null)
                    {
                        var attribute = memberInfo.GetCustomAttribute <EnumMemberAttribute>();
                        if (attribute != null)
                        {
                            stringValue = attribute.Value;
                        }
                    }

                    EnumToString.Add(value, stringValue);
                    StringToEnum.Add(stringValue, value);
                }
            }

            var attr = Property.GetCustomAttribute <InfluxMeasurementAttribute>();

            if (attr != null)
            {
                IsMeasurementNameProperty = true;
            }
        }
Exemple #2
0
        protected override Task SerializeToStreamAsync(Stream stream, TransportContext context)
        {
            var writer = new StreamWriter(stream, UTF8);

            var precision          = _precision;
            var getMeasurementName = _getMeasurementName;

            if (ResultSetFactory.IsIInfluxRow <TInfluxRow>())
            {
                foreach (IInfluxRow dp in _dataPoints)
                {
                    // write measurement name
                    writer.Write(getMeasurementName((TInfluxRow)dp));

                    // write all tags
                    foreach (var kvp in dp.GetAllTags()) // Ensure tags are in correct order?
                    {
                        var value = kvp.Value;
                        if (value != null)
                        {
                            writer.Write(',');
                            writer.Write(LineProtocolEscape.EscapeKey(kvp.Key));
                            writer.Write('=');
                            writer.Write(LineProtocolEscape.EscapeTagValue(value));
                        }
                    }

                    // write tag to field seperator
                    writer.Write(' ');

                    // write all fields
                    using (var enumerator = dp.GetAllFields().GetEnumerator())
                    {
                        bool   hasMore  = enumerator.MoveNext();
                        bool   hasValue = false;
                        object value    = null;
                        KeyValuePair <string, object> current = default(KeyValuePair <string, object>);
                        while (hasMore && !hasValue)
                        {
                            current  = enumerator.Current;
                            value    = enumerator.Current.Value;
                            hasValue = value != null;

                            hasMore = enumerator.MoveNext();
                        }

                        while (hasValue)
                        {
                            writer.Write(LineProtocolEscape.EscapeKey(current.Key));
                            writer.Write('=');
                            writer.Write(LineProtocolEscape.EscapeFieldValue(value));

                            // get a hold of the next non-null value
                            hasValue = false;
                            while (hasMore && !hasValue)
                            {
                                current  = enumerator.Current;
                                value    = enumerator.Current.Value;
                                hasValue = value != null;

                                hasMore = enumerator.MoveNext();
                            }

                            // we have just written a value, and now we have the next non-null value
                            if (hasValue)
                            {
                                writer.Write(',');
                            }
                        }
                    }

                    // write timestamp, if exists
                    var ts = dp.GetTimestamp();
                    if (ts != null)
                    {
                        writer.Write(' ');
                        long ticks = ts.Value.ToPrecision(precision);
                        writer.Write(ticks);
                    }

                    writer.Write('\n');
                }
            }
            else
            {
                var cache     = MetadataCache.GetOrCreate <TInfluxRow>();
                var tags      = cache.Tags;
                var fields    = cache.Fields;
                var timestamp = cache.Timestamp;

                foreach (var dp in _dataPoints)
                {
                    // write measurement name
                    writer.Write(getMeasurementName(dp));

                    // write all tags
                    if (tags.Count > 0)
                    {
                        foreach (var tagProperty in tags)
                        {
                            var value = tagProperty.GetValue(dp);
                            if (value != null)
                            {
                                writer.Write(',');
                                writer.Write(tagProperty.LineProtocolEscapedKey);
                                writer.Write('=');
                                writer.Write(LineProtocolEscape.EscapeTagValue(tagProperty.GetStringValue(value)));
                            }
                        }
                    }

                    // write tag to fields seperator
                    writer.Write(' ');

                    // write all fields
                    using (var enumerator = fields.GetEnumerator())
                    {
                        bool   hasMore  = enumerator.MoveNext();
                        bool   hasValue = false;
                        object value    = null;
                        PropertyExpressionInfo <TInfluxRow> property = null;
                        while (hasMore && !hasValue)
                        {
                            property = enumerator.Current;
                            value    = property.GetValue(dp);
                            hasValue = value != null;

                            hasMore = enumerator.MoveNext();
                        }

                        while (hasValue)
                        {
                            writer.Write(property.LineProtocolEscapedKey);
                            writer.Write('=');
                            if (property.IsEnum)
                            {
                                writer.Write(LineProtocolEscape.EscapeFieldValue(property.GetStringValue(value)));
                            }
                            else
                            {
                                writer.Write(LineProtocolEscape.EscapeFieldValue(value));
                            }

                            // get a hold of the next non-null value
                            hasValue = false;
                            while (hasMore && !hasValue)
                            {
                                property = enumerator.Current;
                                value    = property.GetValue(dp);
                                hasValue = value != null;

                                hasMore = enumerator.MoveNext();
                            }

                            // we have just written a value, and now we have the next non-null value
                            if (hasValue)
                            {
                                writer.Write(',');
                            }
                        }
                    }

                    // write timestamp, if exists
                    if (timestamp != null)
                    {
                        var ts = timestamp.GetValue(dp);
                        if (ts != null)
                        {
                            writer.Write(' ');
                            long ticks = ((DateTime)ts).ToPrecision(precision);
                            writer.Write(ticks);
                        }
                    }

                    writer.Write('\n');
                }
            }

            writer.Flush();

            return(Task.FromResult(0));
        }