forked from influxdata/influxdb-client-csharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMeasurementMapper.cs
161 lines (149 loc) · 5.9 KB
/
MeasurementMapper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using InfluxDB.Client.Api.Domain;
using InfluxDB.Client.Core;
using InfluxDB.Client.Writes;
using NodaTime;
[assembly: InternalsVisibleTo("Client.Test, PublicKey=002400000480000094000000060200000024000052534131" +
"0004000001000100efaac865f88dd35c90dc548945405aae34056eedbe42cad60971f89a861a78437e86d" +
"95804a1aeeb0de18ac3728782f9dc8dbae2e806167a8bb64c0402278edcefd78c13dbe7f8d13de36eb362" +
"21ec215c66ee2dfe7943de97b869c5eea4d92f92d345ced67de5ac8fc3cd2f8dd7e3c0c53bdb0cc433af8" +
"59033d069cad397a7")]
namespace InfluxDB.Client.Internal
{
internal class PropertyInfoColumn
{
internal PropertyInfo Property;
internal Column Column;
}
internal class MeasurementMapper
{
private IDictionary<string, PropertyInfoColumn[]> CACHE = new ConcurrentDictionary<string, PropertyInfoColumn[]>();
internal PointData ToPoint<TM>(TM measurement, WritePrecision precision)
{
Arguments.CheckNotNull(measurement, nameof(measurement));
Arguments.CheckNotNull(precision, nameof(precision));
var measurementType = measurement.GetType();
CacheMeasurementClass(measurementType);
var measurementAttribute = (Measurement) measurementType.GetCustomAttribute(typeof(Measurement));
if (measurementAttribute == null)
{
throw new InvalidOperationException(
$"Measurement {measurement} does not have a {typeof(Measurement)} attribute.");
}
var point = PointData.Measurement(measurementAttribute.Name);
foreach (var propertyInfo in CACHE[measurementType.Name])
{
var value = propertyInfo.Property.GetValue(measurement);
if (value == null)
{
continue;
}
var name = !string.IsNullOrEmpty(propertyInfo.Column.Name) ? propertyInfo.Column.Name : propertyInfo.Property.Name;
if (propertyInfo.Column.IsTag)
{
point = point.Tag(name, value.ToString());
}
else if (propertyInfo.Column.IsTimestamp)
{
if (value is long l)
{
point = point.Timestamp(l, precision);
}
else if (value is TimeSpan span)
{
point = point.Timestamp(span, precision);
}
else if (value is DateTime date)
{
point = point.Timestamp(date, precision);
}
else if (value is DateTimeOffset offset)
{
point = point.Timestamp(offset, precision);
}
else if (value is Instant instant)
{
point = point.Timestamp(instant, precision);
}
else
{
Trace.WriteLine($"{value} is not supported as Timestamp");
}
}
else
{
if (value is bool b)
{
point = point.Field(name, b);
}
else if (value is double d)
{
point = point.Field(name, d);
}
else if (value is float f)
{
point = point.Field(name, f);
}
else if (value is decimal dec)
{
point = point.Field(name, dec);
}
else if (value is long lng)
{
point = point.Field(name, lng);
}
else if (value is ulong ulng)
{
point = point.Field(name, ulng);
}
else if (value is int i)
{
point = point.Field(name, i);
}
else if (value is byte bt)
{
point = point.Field(name, bt);
}
else if (value is sbyte sb)
{
point = point.Field(name, sb);
}
else if (value is short sh)
{
point = point.Field(name, sh);
}
else if (value is uint ui)
{
point = point.Field(name, ui);
}
else if (value is ushort us)
{
point = point.Field(name, us);
}
else
{
point = point.Field(name, value.ToString());
}
}
}
return point;
}
private void CacheMeasurementClass(Type measurementType)
{
if (CACHE.ContainsKey(measurementType.Name))
{
return;
}
CACHE[measurementType.Name] = measurementType.GetProperties()
.Select(property => new PropertyInfoColumn {Column = (Column) property.GetCustomAttribute(typeof(Column)), Property = property})
.Where(propertyInfo => propertyInfo.Column != null)
.ToArray();
}
}
}