From af31ee9d0ebdc56e8e2bc3f40a720d7a670030bf Mon Sep 17 00:00:00 2001 From: Igor Toropov Date: Thu, 22 Feb 2018 15:09:45 -0500 Subject: [PATCH 1/3] Retention Policy support added. --- .../Client/LineProtocolClient.cs | 11 +++++++---- .../Client/MockLineProtocolClient.cs | 2 +- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/InfluxDB.LineProtocol/Client/LineProtocolClient.cs b/src/InfluxDB.LineProtocol/Client/LineProtocolClient.cs index 8737141..e3d157e 100644 --- a/src/InfluxDB.LineProtocol/Client/LineProtocolClient.cs +++ b/src/InfluxDB.LineProtocol/Client/LineProtocolClient.cs @@ -11,14 +11,14 @@ namespace InfluxDB.LineProtocol.Client public class LineProtocolClient { readonly HttpClient _httpClient; - readonly string _database, _username, _password; + readonly string _database, _retentionPolicy, _username, _password; - public LineProtocolClient(Uri serverBaseAddress, string database, string username = null, string password = null) - : this(new HttpClientHandler(), serverBaseAddress, database, username, password) + public LineProtocolClient(Uri serverBaseAddress, string database, string retentionPolicy = null, string username = null, string password = null) + : this(new HttpClientHandler(), serverBaseAddress, database, retentionPolicy, username, password) { } - protected LineProtocolClient(HttpMessageHandler handler, Uri serverBaseAddress, string database, string username, string password) + protected LineProtocolClient(HttpMessageHandler handler, Uri serverBaseAddress, string database, string retentionPolicy, string username, string password) { if (serverBaseAddress == null) throw new ArgumentNullException(nameof(serverBaseAddress)); if (string.IsNullOrEmpty(database)) throw new ArgumentException("A database must be specified"); @@ -26,6 +26,7 @@ protected LineProtocolClient(HttpMessageHandler handler, Uri serverBaseAddress, // Overload that allows injecting handler is protected to avoid HttpMessageHandler being part of our public api which would force clients to reference System.Net.Http when using the lib. _httpClient = new HttpClient(handler) { BaseAddress = serverBaseAddress }; _database = database; + _retentionPolicy = retentionPolicy; _username = username; _password = password; } @@ -47,6 +48,8 @@ protected LineProtocolClient(HttpMessageHandler handler, Uri serverBaseAddress, private async Task SendAsync(string payload, Precision precision, CancellationToken cancellationToken = default(CancellationToken)) { var endpoint = $"write?db={Uri.EscapeDataString(_database)}"; + if (!string.IsNullOrWhiteSpace(_retentionPolicy)) + endpoint += $"&rp={Uri.EscapeDataString(_retentionPolicy)}"; if (!string.IsNullOrEmpty(_username)) endpoint += $"&u={Uri.EscapeDataString(_username)}&p={Uri.EscapeDataString(_password)}"; diff --git a/test/InfluxDB.LineProtocol.Tests/Client/MockLineProtocolClient.cs b/test/InfluxDB.LineProtocol.Tests/Client/MockLineProtocolClient.cs index 47cab05..e28eab3 100644 --- a/test/InfluxDB.LineProtocol.Tests/Client/MockLineProtocolClient.cs +++ b/test/InfluxDB.LineProtocol.Tests/Client/MockLineProtocolClient.cs @@ -11,7 +11,7 @@ public class MockLineProtocolClient : LineProtocolClient { } - private MockLineProtocolClient(MockHttpMessageHandler handler, Uri serverBaseAddress, string database) : base(handler, serverBaseAddress, database, null, null) + private MockLineProtocolClient(MockHttpMessageHandler handler, Uri serverBaseAddress, string database) : base(handler, serverBaseAddress, database, null, null, null) { Handler = handler; BaseAddress = serverBaseAddress; From bb9ab0e0511b5b1fe935b4c0c7c9900e65cf5cce Mon Sep 17 00:00:00 2001 From: Igor Toropov Date: Thu, 22 Feb 2018 15:10:31 -0500 Subject: [PATCH 2/3] Allow precision to be specified along the LineProtocolPayload --- src/InfluxDB.LineProtocol/Client/LineProtocolClient.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/InfluxDB.LineProtocol/Client/LineProtocolClient.cs b/src/InfluxDB.LineProtocol/Client/LineProtocolClient.cs index e3d157e..13730f5 100644 --- a/src/InfluxDB.LineProtocol/Client/LineProtocolClient.cs +++ b/src/InfluxDB.LineProtocol/Client/LineProtocolClient.cs @@ -31,13 +31,13 @@ protected LineProtocolClient(HttpMessageHandler handler, Uri serverBaseAddress, _password = password; } - public Task WriteAsync(LineProtocolPayload payload, CancellationToken cancellationToken = default(CancellationToken)) + public Task WriteAsync(LineProtocolPayload payload, Precision precision = Precision.Nanoseconds, CancellationToken cancellationToken = default(CancellationToken)) { var stringWriter = new StringWriter(); payload.Format(stringWriter); - return SendAsync(stringWriter.ToString(), Precision.Nanoseconds, cancellationToken); + return SendAsync(stringWriter.ToString(), precision, cancellationToken); } public Task SendAsync(LineProtocolWriter lineProtocolWriter, CancellationToken cancellationToken = default(CancellationToken)) From 00e5ceec9b8296edb703dd7934ca41bd5d2ec493 Mon Sep 17 00:00:00 2001 From: Igor Toropov Date: Thu, 22 Feb 2018 16:20:08 -0500 Subject: [PATCH 3/3] Precision formatting for Payload. --- .../Client/LineProtocolClient.cs | 2 +- .../Payload/LineProtocolPayload.cs | 4 ++-- .../Payload/LineProtocolPoint.cs | 7 +++--- .../Payload/LineProtocolSyntax.cs | 24 +++++++++++++++---- 4 files changed, 26 insertions(+), 11 deletions(-) diff --git a/src/InfluxDB.LineProtocol/Client/LineProtocolClient.cs b/src/InfluxDB.LineProtocol/Client/LineProtocolClient.cs index 13730f5..6d69708 100644 --- a/src/InfluxDB.LineProtocol/Client/LineProtocolClient.cs +++ b/src/InfluxDB.LineProtocol/Client/LineProtocolClient.cs @@ -35,7 +35,7 @@ protected LineProtocolClient(HttpMessageHandler handler, Uri serverBaseAddress, { var stringWriter = new StringWriter(); - payload.Format(stringWriter); + payload.Format(stringWriter, precision); return SendAsync(stringWriter.ToString(), precision, cancellationToken); } diff --git a/src/InfluxDB.LineProtocol/Payload/LineProtocolPayload.cs b/src/InfluxDB.LineProtocol/Payload/LineProtocolPayload.cs index ef71b0d..747db21 100644 --- a/src/InfluxDB.LineProtocol/Payload/LineProtocolPayload.cs +++ b/src/InfluxDB.LineProtocol/Payload/LineProtocolPayload.cs @@ -14,13 +14,13 @@ public void Add(LineProtocolPoint point) _points.Add(point); } - public void Format(TextWriter textWriter) + public void Format(TextWriter textWriter, Precision precision) { if (textWriter == null) throw new ArgumentNullException(nameof(textWriter)); foreach (var point in _points) { - point.Format(textWriter); + point.Format(textWriter, precision); textWriter.Write('\n'); } } diff --git a/src/InfluxDB.LineProtocol/Payload/LineProtocolPoint.cs b/src/InfluxDB.LineProtocol/Payload/LineProtocolPoint.cs index 65f95e2..ec1a8da 100644 --- a/src/InfluxDB.LineProtocol/Payload/LineProtocolPoint.cs +++ b/src/InfluxDB.LineProtocol/Payload/LineProtocolPoint.cs @@ -37,7 +37,7 @@ public LineProtocolPoint( UtcTimestamp = utcTimestamp; } - public void Format(TextWriter textWriter) + public void Format(TextWriter textWriter, Precision precision) { if (textWriter == null) throw new ArgumentNullException(nameof(textWriter)); @@ -47,7 +47,7 @@ public void Format(TextWriter textWriter) { foreach (var t in Tags.OrderBy(t => t.Key)) { - if (t.Value == null || t.Value == "") + if (string.IsNullOrEmpty(t.Value)) continue; textWriter.Write(','); @@ -70,9 +70,8 @@ public void Format(TextWriter textWriter) if (UtcTimestamp != null) { textWriter.Write(' '); - textWriter.Write(LineProtocolSyntax.FormatTimestamp(UtcTimestamp.Value)); + textWriter.Write(LineProtocolSyntax.FormatTimestamp(UtcTimestamp.Value, precision)); } } } } - diff --git a/src/InfluxDB.LineProtocol/Payload/LineProtocolSyntax.cs b/src/InfluxDB.LineProtocol/Payload/LineProtocolSyntax.cs index 5a9ce7e..462ffa0 100644 --- a/src/InfluxDB.LineProtocol/Payload/LineProtocolSyntax.cs +++ b/src/InfluxDB.LineProtocol/Payload/LineProtocolSyntax.cs @@ -60,7 +60,7 @@ static string FormatTimespan(object ts) static string FormatBoolean(object b) { - return ((bool)b) ? "t" : "f"; + return (bool)b ? "t" : "f"; } static string FormatString(string s) @@ -68,10 +68,26 @@ static string FormatString(string s) return "\"" + s.Replace("\"", "\\\"") + "\""; } - public static string FormatTimestamp(DateTime utcTimestamp) + public static string FormatTimestamp(DateTime utcTimestamp, Precision precision) { - var t = utcTimestamp - Origin; - return (t.Ticks * 100L).ToString(CultureInfo.InvariantCulture); + TimeSpan ts = utcTimestamp - Origin; + switch (precision) + { + case Precision.Nanoseconds: + return (ts.Ticks * 100).ToString(CultureInfo.InvariantCulture); + case Precision.Microseconds: + return (ts.Ticks / 10).ToString(CultureInfo.InvariantCulture); + case Precision.Milliseconds: + return ((long)ts.TotalMilliseconds).ToString(CultureInfo.InvariantCulture); + case Precision.Seconds: + return ((long)ts.TotalSeconds).ToString(CultureInfo.InvariantCulture); + case Precision.Minutes: + return ((long)ts.TotalMinutes).ToString(CultureInfo.InvariantCulture); + case Precision.Hours: + return ((long)ts.TotalHours).ToString(CultureInfo.InvariantCulture); + default: + throw new NotSupportedException("Precision is unknown to the formatter."); + } } } }