Skip to content

Commit 4c8302a

Browse files
committed
Updated PostValuesAsync, added test
1 parent 4872780 commit 4c8302a

File tree

4 files changed

+35
-64
lines changed

4 files changed

+35
-64
lines changed

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using AdysTech.InfluxDB.Client.Net;
55
using System.Threading.Tasks;
6+
using System.Collections.Generic;
67

78
namespace InfluxDB.Client.Test
89
{
@@ -120,7 +121,7 @@ public async Task TestPostValueAsync()
120121
try
121122
{
122123
var client = new InfluxDBClient (influxUrl, dbUName, dbpwd);
123-
var r = await client.PostValueAsync (dbName, measurementName, DateTime.Now.ToEpoch(TimePrecision.Seconds),TimePrecision.Seconds,"testTag=testTagValue","Temp",33.05);
124+
var r = await client.PostValueAsync (dbName, measurementName, DateTime.UtcNow.ToEpoch(TimePrecision.Seconds),TimePrecision.Seconds,"testTag=testTagValue","Temp",33.05);
124125
Assert.IsTrue (r, "PostValueAsync retunred false");
125126
}
126127
catch ( Exception e )
@@ -132,5 +133,24 @@ public async Task TestPostValueAsync()
132133
}
133134
}
134135

136+
[TestMethod]
137+
public async Task TestPostValuesAsync()
138+
{
139+
try
140+
{
141+
var client = new InfluxDBClient (influxUrl, dbUName, dbpwd);
142+
var val = new Random ();
143+
var values = new Dictionary<string, double> () { { "filed1", val.NextDouble () * 10 }, { "filed2", val.NextDouble () * 10 }, { "filed3", val.NextDouble () * 10 } };
144+
var r = await client.PostValuesAsync (dbName, measurementName, DateTime.UtcNow.ToEpoch (TimePrecision.Seconds), TimePrecision.Seconds, "testTag=testTagValue",values);
145+
Assert.IsTrue (r, "PostValuesAsync retunred false");
146+
}
147+
catch ( Exception e )
148+
{
149+
150+
Assert.Fail ("Unexpected exception of type {0} caught: {1}",
151+
e.GetType (), e.Message);
152+
return;
153+
}
154+
}
135155
}
136156
}

AdysTech.InfluxDB.Client.Net/IInfluxDBClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public interface IInfluxDBClient
1212
Task<Dictionary<string, List<String>>> GetInfluxDBStructureAsync(string dbName);
1313
Task<bool> CreateDatabaseAsync(string dbName);
1414
Task<bool> PostValueAsync(string dbName, string measurement, long timestamp, TimePrecision precision, string tags, string field, double value);
15-
Task<bool> PostValuesAsync(string dbName, string measurement, long timestamp, TimePrecision precision, string tags, IEnumerable<string> values);
15+
Task<bool> PostValuesAsync(string dbName, string measurement, long timestamp, TimePrecision precision, string tags, IDictionary<string,double> values);
1616
Task<bool> PostRawValueAsync(string dbName, TimePrecision precision, string content);
1717
}
1818
}

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

Lines changed: 0 additions & 54 deletions
This file was deleted.

AdysTech.InfluxDB.Client.Net/InfluxDBClient.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ public enum TimePrecision
1919
Seconds = 3,
2020
Milliseconds = 4,
2121
Microseconds = 5,
22-
Nanoseconds = 6
22+
Nanoseconds = 6,
23+
H=1
2324
}
2425

2526
public class InfluxDBClient : IInfluxDBClient
@@ -253,7 +254,7 @@ public async Task<bool> PostValueAsync(string dbName, string measurement, long t
253254
new KeyValuePair<string, string>("precision", precisionLiterals[(int) precision])
254255
}).ReadAsStringAsync ();
255256

256-
var content = String.Format ("{0},{1} {2}={3:0.00} {4}", measurement, tags, field, value, timestamp);
257+
var content = String.Format ("{0},{1} {2}={3} {4}", measurement, tags, field, value, timestamp);
257258
ByteArrayContent requestContent = new ByteArrayContent (Encoding.UTF8.GetBytes (content));
258259
HttpResponseMessage response = await PostAsync (builder, requestContent);
259260

@@ -280,7 +281,7 @@ public async Task<bool> PostValueAsync(string dbName, string measurement, long t
280281
/// <returns>True:Success, False:Failure</returns>
281282
///<exception cref="UnauthorizedAccessException">When Influx needs authentication, and no user name password is supplied or auth fails</exception>
282283
///<exception cref="HttpRequestException">all other HTTP exceptions</exception>
283-
public async Task<bool> PostValuesAsync(string dbName, string measurement, long timestamp, TimePrecision precision, string tags, IEnumerable<string> values)
284+
public async Task<bool> PostValuesAsync(string dbName, string measurement, long timestamp, TimePrecision precision, string tags, IDictionary<string,double> values)
284285
{
285286
var influxAddress = new Uri (String.Format ("{0}/write?", InfluxUrl));
286287
var builder = new UriBuilder (influxAddress);
@@ -289,11 +290,13 @@ public async Task<bool> PostValuesAsync(string dbName, string measurement, long
289290
new KeyValuePair<string, string>("precision", precisionLiterals[(int) precision])
290291
}).ReadAsStringAsync ();
291292

292-
var content = new StringBuilder ();
293-
foreach ( var value in values )
294-
content.AppendFormat ("{0},{1} {2} {3}\n", measurement, tags, value, timestamp);
295-
//remove last \n
296-
content.Remove (content.Length - 1, 1);
293+
//var content = new StringBuilder ();
294+
//foreach ( var value in values )
295+
// content.AppendFormat ("{0},{1} {2} {3}\n", measurement, tags, value, timestamp);
296+
////remove last \n
297+
//content.Remove (content.Length - 1, 1);
298+
var valuesTxt=String.Join (",", values.Select (v => String.Format ("{0}={1}", v.Key, v.Value)));
299+
var content = String.Format ("{0},{1} {2} {3}", measurement, tags, valuesTxt, timestamp);
297300

298301
ByteArrayContent requestContent = new ByteArrayContent (Encoding.UTF8.GetBytes (content.ToString ()));
299302
HttpResponseMessage response = await PostAsync (builder, requestContent);
@@ -337,6 +340,8 @@ public async Task<bool> PostRawValueAsync(string dbName, TimePrecision precision
337340
else
338341
return false;
339342
}
343+
344+
340345
}
341346

342347
}

0 commit comments

Comments
 (0)