forked from influxdata/influxdb-client-csharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueryLinqCloud.cs
More file actions
206 lines (177 loc) · 6.89 KB
/
QueryLinqCloud.cs
File metadata and controls
206 lines (177 loc) · 6.89 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using InfluxDB.Client;
using InfluxDB.Client.Api.Domain;
using InfluxDB.Client.Core.Flux.Domain;
using InfluxDB.Client.Linq;
using InfluxDB.Client.Writes;
namespace Examples
{
public static class QueryLinqCloud
{
/// <summary>
/// Define Domain Object
/// </summary>
private class DomainEntity
{
public Guid SeriesId { get; set; }
public double Value { get; set; }
public DateTimeOffset Timestamp { get; set; }
public ICollection<DomainEntityAttribute> Properties { get; set; }
public override string ToString()
{
return $"{Timestamp:MM/dd/yyyy hh:mm:ss.fff tt} {SeriesId} value: {Value}, " +
$"properties: {string.Join(", ", Properties)}.";
}
}
/// <summary>
/// Attributes of DomainObject
/// </summary>
private class DomainEntityAttribute
{
public string Name { get; set; }
public string Value { get; set; }
public override string ToString()
{
return $"{Name}={Value}";
}
}
/// <summary>
/// Define Custom Domain Object Converter
/// </summary>
private class DomainEntityConverter : IDomainObjectMapper, IMemberNameResolver
{
/// <summary>
/// Convert to DomainObject.
/// </summary>
public T ConvertToEntity<T>(FluxRecord fluxRecord)
{
return (T)ConvertToEntity(fluxRecord, typeof(T));
}
public object ConvertToEntity(FluxRecord fluxRecord, Type type)
{
if (type != typeof(DomainEntity))
{
throw new NotSupportedException($"This converter doesn't supports: {typeof(DomainEntity)}");
}
var customEntity = new DomainEntity
{
SeriesId = Guid.Parse(Convert.ToString(fluxRecord.GetValueByKey("series_id"))!),
Value = Convert.ToDouble(fluxRecord.GetValueByKey("data")),
Timestamp = fluxRecord.GetTime().GetValueOrDefault().ToDateTimeUtc(),
Properties = new List<DomainEntityAttribute>()
};
foreach (var (key, value) in fluxRecord.Values)
if (key.StartsWith("property_"))
{
var attribute = new DomainEntityAttribute
{
Name = key.Replace("property_", string.Empty), Value = Convert.ToString(value)
};
customEntity.Properties.Add(attribute);
}
return Convert.ChangeType(customEntity, type);
}
public PointData ConvertToPointData<T>(T entity, WritePrecision precision)
{
throw new NotImplementedException();
}
/// <summary>
/// How the Domain Object property is mapped into InfluxDB schema. Is it Timestamp, Tag, ...?
/// </summary>
public MemberType ResolveMemberType(MemberInfo memberInfo)
{
switch (memberInfo.Name)
{
case "Timestamp":
return MemberType.Timestamp;
case "Name":
return MemberType.NamedField;
case "Value":
return MemberType.NamedFieldValue;
case "SeriesId":
return MemberType.Tag;
default:
return MemberType.Field;
}
}
/// <summary>
/// How your property is named in InfluxDB.
/// </summary>
public string GetColumnName(MemberInfo memberInfo)
{
switch (memberInfo.Name)
{
case "SeriesId":
return "series_id";
case "Value":
return "data";
default:
return memberInfo.Name;
}
}
/// <summary>
/// Return name for flattened properties.
/// </summary>
public string GetNamedFieldName(MemberInfo memberInfo, object value)
{
return $"property_{Convert.ToString(value)}";
}
}
public static void Main(string[] args)
{
const string host = "https://us-west-2-1.aws.cloud2.influxdata.com";
const string token = "...";
const string bucket = "linq_bucket";
const string organization = "jakub_bednar";
var options = new InfluxDBClientOptions.Builder()
.Url(host)
.AuthenticateToken(token.ToCharArray())
.Org(organization)
.Bucket(bucket)
.Build();
var converter = new DomainEntityConverter();
var client = InfluxDBClientFactory.Create(options)
.EnableGzip();
//
// Query Data to Domain object
//
var queryApi = client.GetQueryApiSync(converter);
var stopWatch = new Stopwatch();
stopWatch.Start();
//
// Select Cloud
//
var query = from s in InfluxDBQueryable<DomainEntity>.Queryable(bucket, organization, queryApi, converter)
select s;
var entities = query.ToList();
Console.WriteLine("==== Cloud Query Results ====");
Console.WriteLine($"> count: {entities.Count}");
Console.WriteLine($"> first: {entities.First()}");
Console.WriteLine($"> elapsed seconds: {stopWatch.Elapsed.TotalSeconds}");
Console.WriteLine();
//
// Debug Query
//
Console.WriteLine("==== Debug LINQ Queryable Flux output ====");
var influxQuery = ((InfluxDBQueryable<DomainEntity>)query).ToDebugQuery();
Console.WriteLine("> variables:");
foreach (var statement in influxQuery.Extern.Body)
{
var os = statement as OptionStatement;
var va = os?.Assignment as VariableAssignment;
var name = va?.Id.Name;
var value = va?.Init.GetType().GetProperty("Value")?.GetValue(va.Init, null);
Console.WriteLine($"{name}={value}");
}
Console.WriteLine();
Console.WriteLine("> query:");
Console.WriteLine(influxQuery._Query);
client.Dispose();
}
}
}