Skip to content

Commit 20a860f

Browse files
committed
Merge pull request #5 from AdysTech/wip
Added InfluxDataPoint, and PostPoint methods
2 parents a881cc3 + ff03b3e commit 20a860f

File tree

9 files changed

+739
-137
lines changed

9 files changed

+739
-137
lines changed

AdysTech.InfluxDB.Client.Net.Test/InfluxDBClientTest.cs

Lines changed: 238 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using Microsoft.VisualStudio.TestTools.UnitTesting;
33
using System.Dynamic;
4-
4+
using System.Linq;
55
using AdysTech.InfluxDB.Client.Net;
66
using System.Threading.Tasks;
77
using System.Collections.Generic;
@@ -134,14 +134,52 @@ public async Task TestCreateDatabaseAsync()
134134
}
135135
}
136136

137+
137138
[TestMethod]
138-
public async Task TestPostValueAsync()
139+
public async Task TestQueryAsync()
139140
{
140141
try
141142
{
143+
142144
var client = new InfluxDBClient (influxUrl, dbUName, dbpwd);
143-
var r = await client.PostValueAsync (dbName, measurementName, DateTime.UtcNow.ToEpoch (TimePrecision.Seconds), TimePrecision.Seconds, "testTag=testTagValue", "Temp", new Random ().NextDouble ());
144-
Assert.IsTrue (r, "PostValueAsync retunred false");
145+
var r = await client.QueryDBAsync ("stress", "select * from performance limit 10");
146+
DateTime d;
147+
Assert.IsTrue (r != null && DateTime.TryParse (r[0].time, out d), "QueryDBAsync retunred null or invalid data");
148+
}
149+
catch ( Exception e )
150+
{
151+
Assert.Fail ("Unexpected exception of type {0} caught: {1}",
152+
e.GetType (), e.Message);
153+
return;
154+
}
155+
}
156+
157+
[TestMethod]
158+
[ExpectedException (typeof (ArgumentException))]
159+
public async Task TestQueryAsync_MultiSeries()
160+
{
161+
var client = new InfluxDBClient (influxUrl, dbUName, dbpwd);
162+
var r = await client.QueryDBAsync ("_internal", "Show Series");
163+
}
164+
165+
[TestMethod]
166+
public async Task TestPostIntPointAsync()
167+
{
168+
try
169+
{
170+
var client = new InfluxDBClient (influxUrl, dbUName, dbpwd);
171+
var time = DateTime.Now;
172+
var rand = new Random ();
173+
var valInt = new InfluxDatapoint<int> ();
174+
valInt.UtcTimestamp = DateTime.UtcNow;
175+
valInt.Tags.Add ("TestDate", time.ToShortDateString ());
176+
valInt.Tags.Add ("TestTime", time.ToShortTimeString ());
177+
valInt.Fields.Add ("Intfield", rand.Next ());
178+
valInt.Fields.Add ("Intfield2", rand.Next ());
179+
valInt.MeasurementName = measurementName;
180+
valInt.Precision = TimePrecision.Seconds;
181+
var r = await client.PostPointAsync (dbName, valInt);
182+
Assert.IsTrue (r, "PostPointAsync retunred false");
145183
}
146184
catch ( Exception e )
147185
{
@@ -153,15 +191,23 @@ public async Task TestPostValueAsync()
153191
}
154192

155193
[TestMethod]
156-
public async Task TestPostValuesAsync()
194+
public async Task TestPostDoublePointAsync()
157195
{
158196
try
159197
{
160198
var client = new InfluxDBClient (influxUrl, dbUName, dbpwd);
161-
var val = new Random ();
162-
var values = new Dictionary<string, double> () { { "filed1", val.NextDouble () * 10 }, { "filed2", val.NextDouble () * 10 }, { "filed3", val.NextDouble () * 10 } };
163-
var r = await client.PostValuesAsync (dbName, measurementName, DateTime.UtcNow.ToEpoch (TimePrecision.Seconds), TimePrecision.Seconds, "testTag=testTagValue", values);
164-
Assert.IsTrue (r, "PostValuesAsync retunred false");
199+
var time = DateTime.Now;
200+
var rand = new Random ();
201+
var valDouble = new InfluxDatapoint<double> ();
202+
valDouble.UtcTimestamp = DateTime.UtcNow;
203+
valDouble.Tags.Add ("TestDate", time.ToShortDateString ());
204+
valDouble.Tags.Add ("TestTime", time.ToShortTimeString ());
205+
valDouble.Fields.Add ("Doublefield", rand.NextDouble ());
206+
valDouble.Fields.Add ("Doublefield2", rand.NextDouble ());
207+
valDouble.MeasurementName = measurementName;
208+
valDouble.Precision = TimePrecision.Seconds;
209+
var r = await client.PostPointAsync (dbName, valDouble);
210+
Assert.IsTrue (r, "PostPointAsync retunred false");
165211
}
166212
catch ( Exception e )
167213
{
@@ -172,31 +218,206 @@ public async Task TestPostValuesAsync()
172218
}
173219
}
174220

221+
private string GenerateRandomString()
222+
{
223+
var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 ,=/\\";
224+
var stringChars = new char[16];
225+
var random = new Random ();
226+
227+
for ( int i = 0; i < stringChars.Length; i++ )
228+
{
229+
stringChars[i] = chars[random.Next (chars.Length)];
230+
}
231+
return new String (stringChars);
232+
}
233+
175234
[TestMethod]
176-
public async Task TestQueryAsync()
235+
public async Task TestPostStringPointAsync()
236+
{
237+
try
238+
{
239+
var client = new InfluxDBClient (influxUrl, dbUName, dbpwd);
240+
var time = DateTime.Now;
241+
var valString = new InfluxDatapoint<string> ();
242+
valString.UtcTimestamp = DateTime.UtcNow;
243+
valString.Tags.Add ("TestDate", time.ToShortDateString ());
244+
valString.Tags.Add ("TestTime", time.ToShortTimeString ());
245+
valString.Fields.Add ("Stringfield", GenerateRandomString ());
246+
valString.MeasurementName = measurementName;
247+
valString.Precision = TimePrecision.Seconds;
248+
var r = await client.PostPointAsync (dbName, valString);
249+
Assert.IsTrue (r, "PostPointAsync retunred false");
250+
}
251+
catch ( Exception e )
252+
{
253+
254+
Assert.Fail ("Unexpected exception of type {0} caught: {1}",
255+
e.GetType (), e.Message);
256+
return;
257+
}
258+
}
259+
260+
[TestMethod]
261+
public async Task TestPostBooleanPointAsync()
177262
{
178263
try
179264
{
265+
var client = new InfluxDBClient (influxUrl, dbUName, dbpwd);
266+
var time = DateTime.Now;
180267

268+
var valBool = new InfluxDatapoint<bool> ();
269+
valBool.UtcTimestamp = DateTime.UtcNow;
270+
valBool.Tags.Add ("TestDate", time.ToShortDateString ());
271+
valBool.Tags.Add ("TestTime", time.ToShortTimeString ());
272+
valBool.Fields.Add ("Booleanfield", time.Ticks % 2 == 0);
273+
valBool.MeasurementName = measurementName;
274+
valBool.Precision = TimePrecision.Seconds;
275+
var r = await client.PostPointAsync (dbName, valBool);
276+
Assert.IsTrue (r, "PostPointAsync retunred false");
277+
}
278+
catch ( Exception e )
279+
{
280+
281+
Assert.Fail ("Unexpected exception of type {0} caught: {1}",
282+
e.GetType (), e.Message);
283+
return;
284+
}
285+
}
286+
287+
[TestMethod]
288+
public async Task TestPostPointsAsync()
289+
{
290+
try
291+
{
181292
var client = new InfluxDBClient (influxUrl, dbUName, dbpwd);
182-
var r = await client.QueryDBAsync ("stress", "select * from performance limit 10");
183-
DateTime d;
184-
Assert.IsTrue (r != null && DateTime.TryParse(r[0].time,out d), "QueryDBAsync retunred null or invalid data");
293+
var time = DateTime.Now;
294+
var rand = new Random ();
295+
296+
var valDouble = new InfluxDatapoint<double> ();
297+
valDouble.UtcTimestamp = DateTime.UtcNow;
298+
valDouble.Tags.Add ("TestDate", time.ToShortDateString ());
299+
valDouble.Tags.Add ("TestTime", time.ToShortTimeString ());
300+
valDouble.Fields.Add ("Doublefield", rand.NextDouble ());
301+
valDouble.Fields.Add ("Doublefield2", rand.NextDouble ());
302+
valDouble.MeasurementName = measurementName;
303+
valDouble.Precision = TimePrecision.Milliseconds;
304+
305+
var valInt = new InfluxDatapoint<int> ();
306+
valInt.UtcTimestamp = DateTime.UtcNow;
307+
valInt.Tags.Add ("TestDate", time.ToShortDateString ());
308+
valInt.Tags.Add ("TestTime", time.ToShortTimeString ());
309+
valInt.Fields.Add ("Intfield", rand.Next ());
310+
valInt.Fields.Add ("Intfield2", rand.Next ());
311+
valInt.MeasurementName = measurementName;
312+
valInt.Precision = TimePrecision.Seconds;
313+
314+
var valBool = new InfluxDatapoint<bool> ();
315+
valBool.UtcTimestamp = DateTime.UtcNow;
316+
valBool.Tags.Add ("TestDate", time.ToShortDateString ());
317+
valBool.Tags.Add ("TestTime", time.ToShortTimeString ());
318+
valBool.Fields.Add ("Booleanfield", time.Ticks % 2 == 0);
319+
valBool.MeasurementName = measurementName;
320+
valBool.Precision = TimePrecision.Minutes;
321+
322+
var valString = new InfluxDatapoint<string> ();
323+
valString.UtcTimestamp = DateTime.UtcNow;
324+
valString.Tags.Add ("TestDate", time.ToShortDateString ());
325+
valString.Tags.Add ("TestTime", time.ToShortTimeString ());
326+
valString.Fields.Add ("Stringfield", GenerateRandomString ());
327+
valString.MeasurementName = measurementName;
328+
valString.Precision = TimePrecision.Hours;
329+
330+
var points = new List<IInfluxDatapoint> ();
331+
points.Add (valString);
332+
points.Add (valInt);
333+
points.Add (valDouble);
334+
points.Add (valBool);
335+
var r = await client.PostPointsAsync (dbName, points);
336+
Assert.IsTrue (r, "PostPointsAsync retunred false");
185337
}
186338
catch ( Exception e )
187339
{
340+
188341
Assert.Fail ("Unexpected exception of type {0} caught: {1}",
189342
e.GetType (), e.Message);
190343
return;
191344
}
192345
}
193346

347+
/// <summary>
348+
/// This will create 2000 objects, and posts them to Influx. Since the precision is random, many points get overwritten
349+
/// (e.g. you can have only point per hour at hour precision.
350+
/// </summary>
351+
/// <returns></returns>
194352
[TestMethod]
195-
[ExpectedException (typeof (ArgumentException))]
196-
public async Task TestQueryAsync_MultiSeries()
353+
public async Task TestBachingPostPointsAsync()
197354
{
198-
var client = new InfluxDBClient (influxUrl, dbUName, dbpwd);
199-
var r = await client.QueryDBAsync ("_internal", "Show Series");
355+
try
356+
{
357+
var client = new InfluxDBClient (influxUrl, dbUName, dbpwd);
358+
359+
var rand = new Random ();
360+
var points = new List<IInfluxDatapoint> ();
361+
362+
var today = DateTime.Now.ToShortDateString ();
363+
var now = DateTime.Now.ToShortTimeString ();
364+
365+
for ( int i = 0; i < 500; i++ )
366+
{
367+
var utcTime = DateTime.UtcNow;
368+
var valDouble = new InfluxDatapoint<double> ();
369+
valDouble.UtcTimestamp = utcTime;
370+
valDouble.Tags.Add ("TestDate", today);
371+
valDouble.Tags.Add ("TestTime", now);
372+
valDouble.Fields.Add ("Doublefield", rand.NextDouble ());
373+
valDouble.Fields.Add ("Doublefield2", rand.NextDouble ());
374+
valDouble.MeasurementName = measurementName;
375+
valDouble.Precision = (TimePrecision) ( rand.Next () % 6 ) + 1;
376+
377+
var valInt = new InfluxDatapoint<int> ();
378+
valInt.UtcTimestamp = utcTime;
379+
valInt.Tags.Add ("TestDate", today);
380+
valInt.Tags.Add ("TestTime", now);
381+
valInt.Fields.Add ("Intfield", rand.Next ());
382+
valInt.Fields.Add ("Intfield2", rand.Next ());
383+
valInt.MeasurementName = measurementName;
384+
valInt.Precision = (TimePrecision) ( rand.Next () % 6 ) + 1;
385+
386+
var valBool = new InfluxDatapoint<bool> ();
387+
valBool.UtcTimestamp = utcTime;
388+
valBool.Tags.Add ("TestDate", today);
389+
valBool.Tags.Add ("TestTime", now);
390+
valBool.Fields.Add ("Booleanfield", DateTime.Now.Ticks % 2 == 0);
391+
valBool.MeasurementName = measurementName;
392+
valBool.Precision = (TimePrecision) ( rand.Next () % 6 ) + 1;
393+
394+
var valString = new InfluxDatapoint<string> ();
395+
valString.UtcTimestamp = utcTime;
396+
valString.Tags.Add ("TestDate", today);
397+
valString.Tags.Add ("TestTime", now);
398+
valString.Fields.Add ("Stringfield", GenerateRandomString ());
399+
valString.MeasurementName = measurementName;
400+
valString.Precision = (TimePrecision) ( rand.Next () % 6 ) + 1;
401+
402+
403+
points.Add (valString);
404+
points.Add (valInt);
405+
points.Add (valDouble);
406+
points.Add (valBool);
407+
}
408+
409+
var r = await client.PostPointsAsync (dbName, points);
410+
Assert.IsTrue (points.TrueForAll (p => p.Saved == true), "PostPointsAsync did not save all points");
411+
412+
}
413+
catch ( Exception e )
414+
{
415+
416+
Assert.Fail ("Unexpected exception of type {0} caught: {1}",
417+
e.GetType (), e.Message);
418+
return;
419+
}
200420
}
201421
}
422+
202423
}

AdysTech.InfluxDB.Client.Net/AdysTech.InfluxDB.Client.Net.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@
4242
</ItemGroup>
4343
<ItemGroup>
4444
<Compile Include="ExtensionMethods.cs" />
45+
<Compile Include="IInfluxDatapoint.cs" />
4546
<Compile Include="IInfluxDBClient.cs" />
47+
<Compile Include="InfluxDatapoint.cs" />
4648
<Compile Include="InfluxDBClient.cs" />
4749
<Compile Include="DataContracts\InfluxJsonTypes.cs" />
4850
<Compile Include="Properties\AssemblyInfo.cs" />

AdysTech.InfluxDB.Client.Net/ExtensionMethods.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ public static long ToEpoch(this DateTime time, TimePrecision precision)
1919
case TimePrecision.Minutes: return (long) t.TotalMinutes;
2020
case TimePrecision.Seconds: return (long) t.TotalSeconds;
2121
case TimePrecision.Milliseconds: return (long) t.TotalMilliseconds;
22-
case TimePrecision.Microseconds: return (long) t.TotalMilliseconds * 1000;
23-
case TimePrecision.Nanoseconds: return (long) t.TotalMilliseconds * 1000 * 1000;
22+
case TimePrecision.Microseconds: return (long) t.Ticks * ( TimeSpan.TicksPerMillisecond / 1000 );
23+
case TimePrecision.Nanoseconds: return (long) t.Ticks * 100; //1 tick = 100 nano sec
2424
}
2525
return 0;
2626
}

0 commit comments

Comments
 (0)