Skip to content

Commit

Permalink
Add full support for data point status codes (#371)
Browse files Browse the repository at this point in the history
  • Loading branch information
einarmo authored Jan 30, 2024
1 parent 09886ef commit 093822b
Show file tree
Hide file tree
Showing 7 changed files with 1,523 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Cognite.Extensions/Cognite.Extensions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.Identity.Client" Version="4.59.0" />
<PackageReference Include="System.Text.Json" Version="8.0.1" />
<PackageReference Include="CogniteSdk" Version="3.19.0" />
<PackageReference Include="CogniteSdk" Version="3.20.0" />
<PackageReference Include="prometheus-net" Version="8.2.1" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="8.0.1" />
Expand Down
44 changes: 32 additions & 12 deletions Cognite.Extensions/CogniteUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging.Abstractions;
using Com.Cognite.V1.Timeseries.Proto.Alpha;
using Cognite.Extensions.Alpha;

namespace Cognite.Extensions
{
Expand All @@ -26,12 +28,12 @@ public static class CogniteUtils
/// Cognite min double value
/// </summary>
public const double NumericValueMin = -1e+100;

/// <summary>
/// Cognite max double value
/// </summary>
public const double NumericValueMax = 1e+100;

/// <summary>
/// Cognite max string length
/// </summary>
Expand Down Expand Up @@ -256,7 +258,7 @@ public static IDictionary<Identity, IEnumerable<Datapoint>> ReadDatapoints(Strea
total++;
dps.Add(dp);
}

if (!ret.TryGetValue(id, out var datapoints))
{
ret[id] = dps;
Expand Down Expand Up @@ -570,7 +572,7 @@ public static IAsyncPolicy<HttpResponseMessage> GetRetryPolicy(ILogger? logger,
retry => TimeSpan.FromMilliseconds(Math.Min(125 * Math.Pow(2, Math.Min(retry - 1, numRetries)), delay)),
GetRetryHandler(logger));
}

}
/// <summary>
/// Get a polly timeout policy with a timeout set to <paramref name="timeout"/> milliseconds
Expand Down Expand Up @@ -611,7 +613,8 @@ public class Datapoint
private readonly long _timestamp;
private readonly double? _numericValue;
private readonly string? _stringValue;

private readonly StatusCode _statusCode;

/// <summary>
/// Timestamp in Unix time milliseconds
/// </summary>
Expand All @@ -632,16 +635,24 @@ public class Datapoint
/// </summary>
public bool IsString => _numericValue == null;

/// <summary>
/// Datapoint status code.
/// </summary>
public StatusCode Status => _statusCode;

/// <summary>
/// Creates a numeric data point
/// </summary>
/// <param name="timestamp">Timestamp</param>
/// <param name="numericValue">double value</param>
public Datapoint(DateTime timestamp, double numericValue)
/// <param name="statusCode">ALPHA: set the data point status code.
/// This is only used if the alpha datapoints endpoint is used.</param>
public Datapoint(DateTime timestamp, double numericValue, StatusCode? statusCode = null)
{
_timestamp = timestamp.ToUnixTimeMilliseconds();
_numericValue = numericValue;
_stringValue = null;
_statusCode = statusCode ?? new StatusCode(0);
}

/// <summary>
Expand All @@ -654,17 +665,21 @@ public Datapoint(DateTime timestamp, string? stringValue)
_timestamp = timestamp.ToUnixTimeMilliseconds();
_numericValue = null;
_stringValue = stringValue;
_statusCode = new StatusCode(0);
}
/// <summary>
/// Creates a numeric data point
/// </summary>
/// <param name="timestamp">Timestamp</param>
/// <param name="numericValue">double value</param>
public Datapoint(long timestamp, double numericValue)
/// <param name="statusCode">ALPHA: set the data point status code.
/// This is only used if the alpha datapoints endpoint is used.</param>
public Datapoint(long timestamp, double numericValue, StatusCode? statusCode = null)
{
_timestamp = timestamp;
_numericValue = numericValue;
_stringValue = null;
_statusCode = statusCode ?? new StatusCode(0);
}

/// <summary>
Expand All @@ -677,6 +692,7 @@ public Datapoint(long timestamp, string? stringValue)
_timestamp = timestamp;
_numericValue = null;
_stringValue = stringValue;
_statusCode = new StatusCode(0);
}
/// <summary>
/// Convert datapoint into an array of bytes on the form
Expand All @@ -685,7 +701,7 @@ public Datapoint(long timestamp, string? stringValue)
/// <returns></returns>
public byte[] ToStorableBytes()
{
ushort size = sizeof(long) + sizeof(bool);
ushort size = sizeof(long) + sizeof(bool) + sizeof(ulong);

byte[] valBytes;

Expand All @@ -705,6 +721,8 @@ public byte[] ToStorableBytes()
pos += sizeof(long);
Buffer.BlockCopy(BitConverter.GetBytes(IsString), 0, bytes, pos, sizeof(bool));
pos += sizeof(bool);
Buffer.BlockCopy(BitConverter.GetBytes(_statusCode.Code), 0, bytes, pos, sizeof(ulong));
pos += sizeof(ulong);

Buffer.BlockCopy(valBytes, 0, bytes, pos, valBytes.Length);

Expand All @@ -720,12 +738,14 @@ public byte[] ToStorableBytes()
{
throw new ArgumentNullException(nameof(stream));
}
var baseBytes = new byte[sizeof(long) + sizeof(bool)];
int read = stream.Read(baseBytes, 0, sizeof(long) + sizeof(bool));
if (read < sizeof(long) + sizeof(bool)) return null;
var readLength = sizeof(long) + sizeof(bool) + sizeof(ulong);
var baseBytes = new byte[readLength];
int read = stream.Read(baseBytes, 0, readLength);
if (read < readLength) return null;

var timestamp = BitConverter.ToInt64(baseBytes, 0);
var isString = BitConverter.ToBoolean(baseBytes, sizeof(long));
var statusCode = BitConverter.ToUInt64(baseBytes, sizeof(long) + sizeof(bool));

if (isString)
{
Expand All @@ -737,7 +757,7 @@ public byte[] ToStorableBytes()
var valueBytes = new byte[sizeof(double)];
if (stream.Read(valueBytes, 0, sizeof(double)) < sizeof(double)) return null;
double value = BitConverter.ToDouble(valueBytes, 0);
return new Datapoint(timestamp, value);
return new Datapoint(timestamp, value, new StatusCode(statusCode));
}
}
}
Expand Down
Loading

0 comments on commit 093822b

Please sign in to comment.