Skip to content

Commit

Permalink
Serialize Epoch milliseconds as a string (#4996)
Browse files Browse the repository at this point in the history
This commit updates the DateTimeOffsetEpochMillisecondsFormatter
and NullableDateTimeOffsetEpochMillisecondsFormatter to serialize
epoch milliseconds as a string instead of a number. Add additional
parameters to integration tests to assert the change is valid
where used.
  • Loading branch information
russcam authored Sep 3, 2020
1 parent 5eb6bb6 commit 39bdac2
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ internal class DateTimeOffsetEpochMillisecondsFormatter : MachineLearningDateTim
{
public override void Serialize(ref JsonWriter writer, DateTimeOffset value, IJsonFormatterResolver formatterResolver)
{
var dateTimeOffsetDifference = (value - DateTimeUtil.Epoch).TotalMilliseconds;
writer.WriteInt64((long)dateTimeOffsetDifference);
writer.WriteQuotation();
writer.WriteInt64(value.ToUnixTimeMilliseconds());
writer.WriteQuotation();
}
}

Expand Down Expand Up @@ -53,8 +54,9 @@ public void Serialize(ref JsonWriter writer, DateTimeOffset? value, IJsonFormatt
return;
}

var dateTimeOffsetDifference = (value.Value - DateTimeUtil.Epoch).TotalMilliseconds;
writer.WriteInt64((long)dateTimeOffsetDifference);
writer.WriteQuotation();
writer.WriteInt64(value.Value.ToUnixTimeMilliseconds());
writer.WriteQuotation();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,24 @@ public class GetAnomalyRecordsApiTests
: MachineLearningIntegrationTestBase<GetAnomalyRecordsResponse, IGetAnomalyRecordsRequest, GetAnomalyRecordsDescriptor,
GetAnomalyRecordsRequest>
{
private static readonly DateTimeOffset Timestamp = new DateTimeOffset(2016, 6, 2, 00, 00, 00, TimeSpan.Zero);

public GetAnomalyRecordsApiTests(MachineLearningCluster cluster, EndpointUsage usage) : base(cluster, usage) { }

protected override bool ExpectIsValid => true;
protected override object ExpectJson => null;
protected override int ExpectStatusCode => 200;
protected override Func<GetAnomalyRecordsDescriptor, IGetAnomalyRecordsRequest> Fluent => f => f;
protected override Func<GetAnomalyRecordsDescriptor, IGetAnomalyRecordsRequest> Fluent => f => f
.Start(Timestamp.AddHours(-1))
.End(Timestamp.AddHours(1));

protected override HttpMethod HttpMethod => HttpMethod.POST;
protected override GetAnomalyRecordsRequest Initializer => new GetAnomalyRecordsRequest(CallIsolatedValue);

protected override GetAnomalyRecordsRequest Initializer => new GetAnomalyRecordsRequest(CallIsolatedValue)
{
Start = Timestamp.AddHours(-1), End = Timestamp.AddHours(1)
};

protected override string UrlPath => $"/_ml/anomaly_detectors/{CallIsolatedValue}/results/records";

protected override GetAnomalyRecordsDescriptor NewDescriptor() => new GetAnomalyRecordsDescriptor(CallIsolatedValue);
Expand All @@ -33,7 +43,7 @@ protected override void IntegrationSetup(IElasticClient client, CallUniqueValues
foreach (var callUniqueValue in values)
{
PutJob(client, callUniqueValue.Value);
IndexAnomalyRecord(client, callUniqueValue.Value, new DateTimeOffset(2016, 6, 2, 00, 00, 00, TimeSpan.Zero));
IndexAnomalyRecord(client, callUniqueValue.Value, Timestamp);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,15 @@ public GetInfluencersApiTests(MachineLearningCluster cluster, EndpointUsage usag
protected override int ExpectStatusCode => 200;
protected override Func<GetInfluencersDescriptor, IGetInfluencersRequest> Fluent => f => f;
protected override HttpMethod HttpMethod => HttpMethod.POST;
protected override GetInfluencersRequest Initializer => new GetInfluencersRequest(CallIsolatedValue);
protected override GetInfluencersRequest Initializer => new GetInfluencersRequest(CallIsolatedValue)
{
End = new DateTimeOffset(2016, 6, 2, 01, 00, 00, TimeSpan.Zero)
};

protected override string UrlPath => $"/_ml/anomaly_detectors/{CallIsolatedValue}/results/influencers";

protected override GetInfluencersDescriptor NewDescriptor() => new GetInfluencersDescriptor(CallIsolatedValue);
protected override GetInfluencersDescriptor NewDescriptor() => new GetInfluencersDescriptor(CallIsolatedValue)
.End(new DateTimeOffset(2016, 6, 2, 01, 00, 00, TimeSpan.Zero));

protected override void IntegrationSetup(IElasticClient client, CallUniqueValues values)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,24 @@ public class GetModelSnapshotsApiTests
: MachineLearningIntegrationTestBase<GetModelSnapshotsResponse, IGetModelSnapshotsRequest, GetModelSnapshotsDescriptor,
GetModelSnapshotsRequest>
{
private static readonly DateTimeOffset Timestamp = new DateTimeOffset(2016, 6, 2, 00, 00, 00, TimeSpan.Zero);

public GetModelSnapshotsApiTests(MachineLearningCluster cluster, EndpointUsage usage) : base(cluster, usage) { }

protected override bool ExpectIsValid => true;
protected override object ExpectJson => null;
protected override int ExpectStatusCode => 200;
protected override Func<GetModelSnapshotsDescriptor, IGetModelSnapshotsRequest> Fluent => f => f;
protected override Func<GetModelSnapshotsDescriptor, IGetModelSnapshotsRequest> Fluent => f => f
.Start(Timestamp.AddHours(-1))
.End(Timestamp.AddHours(1));

protected override HttpMethod HttpMethod => HttpMethod.POST;
protected override GetModelSnapshotsRequest Initializer => new GetModelSnapshotsRequest(CallIsolatedValue);
protected override GetModelSnapshotsRequest Initializer => new GetModelSnapshotsRequest(CallIsolatedValue)
{
Start = Timestamp.AddHours(-1),
End = Timestamp.AddHours(1)
};

protected override string UrlPath => $"/_ml/anomaly_detectors/{CallIsolatedValue}/model_snapshots";

protected override void IntegrationSetup(IElasticClient client, CallUniqueValues values)
Expand Down Expand Up @@ -52,7 +62,7 @@ protected override void ExpectResponse(GetModelSnapshotsResponse response)

var modelSnapshot = response.ModelSnapshots.First();
modelSnapshot.JobId.Should().Be(CallIsolatedValue);
modelSnapshot.Timestamp.Should().Be(new DateTimeOffset(2016, 6, 2, 00, 00, 00, TimeSpan.Zero));
modelSnapshot.Timestamp.Should().Be(Timestamp);
modelSnapshot.SnapshotId.Should().Be("1");
modelSnapshot.SnapshotDocCount.Should().Be(1);
modelSnapshot.Retain.Should().Be(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,23 @@ public class StartDatafeedApiTests
{
public StartDatafeedApiTests(MachineLearningCluster cluster, EndpointUsage usage) : base(cluster, usage) { }

private DateTimeOffset Now => DateTimeOffset.Now;

protected override bool ExpectIsValid => true;
protected override object ExpectJson => null;
protected override int ExpectStatusCode => 200;
protected override Func<StartDatafeedDescriptor, IStartDatafeedRequest> Fluent => f => f;

protected override Func<StartDatafeedDescriptor, IStartDatafeedRequest> Fluent => f => f
.Start(Now)
.End(Now.AddSeconds(10));

protected override HttpMethod HttpMethod => HttpMethod.POST;
protected override StartDatafeedRequest Initializer => new StartDatafeedRequest(CallIsolatedValue + "-datafeed");
protected override StartDatafeedRequest Initializer => new StartDatafeedRequest(CallIsolatedValue + "-datafeed")
{
Start = Now,
End = Now.AddSeconds(10)
};

protected override string UrlPath => $"_ml/datafeeds/{CallIsolatedValue}-datafeed/_start";

protected override void IntegrationSetup(IElasticClient client, CallUniqueValues values)
Expand Down

0 comments on commit 39bdac2

Please sign in to comment.