Skip to content
This repository has been archived by the owner on Jul 18, 2023. It is now read-only.

Added RetentionPolicy and Precision #51

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions src/InfluxDB.LineProtocol/Client/LineProtocolClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,33 @@ 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");

// 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;
}

public Task<LineProtocolWriteResult> WriteAsync(LineProtocolPayload payload, CancellationToken cancellationToken = default(CancellationToken))
public Task<LineProtocolWriteResult> WriteAsync(LineProtocolPayload payload, Precision precision = Precision.Nanoseconds, CancellationToken cancellationToken = default(CancellationToken))
{
var stringWriter = new StringWriter();

payload.Format(stringWriter);
payload.Format(stringWriter, precision);

return SendAsync(stringWriter.ToString(), Precision.Nanoseconds, cancellationToken);
return SendAsync(stringWriter.ToString(), precision, cancellationToken);
}

public Task<LineProtocolWriteResult> SendAsync(LineProtocolWriter lineProtocolWriter, CancellationToken cancellationToken = default(CancellationToken))
Expand All @@ -47,6 +48,8 @@ protected LineProtocolClient(HttpMessageHandler handler, Uri serverBaseAddress,
private async Task<LineProtocolWriteResult> 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)}";

Expand Down
4 changes: 2 additions & 2 deletions src/InfluxDB.LineProtocol/Payload/LineProtocolPayload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
Expand Down
7 changes: 3 additions & 4 deletions src/InfluxDB.LineProtocol/Payload/LineProtocolPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand All @@ -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(',');
Expand All @@ -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));
}
}
}
}

24 changes: 20 additions & 4 deletions src/InfluxDB.LineProtocol/Payload/LineProtocolSyntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,34 @@ 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)
{
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.");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down