Skip to content

Commit 39bdac2

Browse files
authored
Serialize Epoch milliseconds as a string (#4996)
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.
1 parent 5eb6bb6 commit 39bdac2

File tree

5 files changed

+52
-14
lines changed

5 files changed

+52
-14
lines changed

src/Nest/CommonAbstractions/SerializationBehavior/JsonFormatters/DateTimeOffsetEpochMillisecondsFormatter.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ internal class DateTimeOffsetEpochMillisecondsFormatter : MachineLearningDateTim
1111
{
1212
public override void Serialize(ref JsonWriter writer, DateTimeOffset value, IJsonFormatterResolver formatterResolver)
1313
{
14-
var dateTimeOffsetDifference = (value - DateTimeUtil.Epoch).TotalMilliseconds;
15-
writer.WriteInt64((long)dateTimeOffsetDifference);
14+
writer.WriteQuotation();
15+
writer.WriteInt64(value.ToUnixTimeMilliseconds());
16+
writer.WriteQuotation();
1617
}
1718
}
1819

@@ -53,8 +54,9 @@ public void Serialize(ref JsonWriter writer, DateTimeOffset? value, IJsonFormatt
5354
return;
5455
}
5556

56-
var dateTimeOffsetDifference = (value.Value - DateTimeUtil.Epoch).TotalMilliseconds;
57-
writer.WriteInt64((long)dateTimeOffsetDifference);
57+
writer.WriteQuotation();
58+
writer.WriteInt64(value.Value.ToUnixTimeMilliseconds());
59+
writer.WriteQuotation();
5860
}
5961
}
6062
}

tests/Tests/XPack/MachineLearning/GetAnomalyRecords/GetAnomalyRecordsApiTests.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,24 @@ public class GetAnomalyRecordsApiTests
1616
: MachineLearningIntegrationTestBase<GetAnomalyRecordsResponse, IGetAnomalyRecordsRequest, GetAnomalyRecordsDescriptor,
1717
GetAnomalyRecordsRequest>
1818
{
19+
private static readonly DateTimeOffset Timestamp = new DateTimeOffset(2016, 6, 2, 00, 00, 00, TimeSpan.Zero);
20+
1921
public GetAnomalyRecordsApiTests(MachineLearningCluster cluster, EndpointUsage usage) : base(cluster, usage) { }
2022

2123
protected override bool ExpectIsValid => true;
2224
protected override object ExpectJson => null;
2325
protected override int ExpectStatusCode => 200;
24-
protected override Func<GetAnomalyRecordsDescriptor, IGetAnomalyRecordsRequest> Fluent => f => f;
26+
protected override Func<GetAnomalyRecordsDescriptor, IGetAnomalyRecordsRequest> Fluent => f => f
27+
.Start(Timestamp.AddHours(-1))
28+
.End(Timestamp.AddHours(1));
29+
2530
protected override HttpMethod HttpMethod => HttpMethod.POST;
26-
protected override GetAnomalyRecordsRequest Initializer => new GetAnomalyRecordsRequest(CallIsolatedValue);
31+
32+
protected override GetAnomalyRecordsRequest Initializer => new GetAnomalyRecordsRequest(CallIsolatedValue)
33+
{
34+
Start = Timestamp.AddHours(-1), End = Timestamp.AddHours(1)
35+
};
36+
2737
protected override string UrlPath => $"/_ml/anomaly_detectors/{CallIsolatedValue}/results/records";
2838

2939
protected override GetAnomalyRecordsDescriptor NewDescriptor() => new GetAnomalyRecordsDescriptor(CallIsolatedValue);
@@ -33,7 +43,7 @@ protected override void IntegrationSetup(IElasticClient client, CallUniqueValues
3343
foreach (var callUniqueValue in values)
3444
{
3545
PutJob(client, callUniqueValue.Value);
36-
IndexAnomalyRecord(client, callUniqueValue.Value, new DateTimeOffset(2016, 6, 2, 00, 00, 00, TimeSpan.Zero));
46+
IndexAnomalyRecord(client, callUniqueValue.Value, Timestamp);
3747
}
3848
}
3949

tests/Tests/XPack/MachineLearning/GetInfluencers/GetInfluencersApiTests.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,15 @@ public GetInfluencersApiTests(MachineLearningCluster cluster, EndpointUsage usag
2222
protected override int ExpectStatusCode => 200;
2323
protected override Func<GetInfluencersDescriptor, IGetInfluencersRequest> Fluent => f => f;
2424
protected override HttpMethod HttpMethod => HttpMethod.POST;
25-
protected override GetInfluencersRequest Initializer => new GetInfluencersRequest(CallIsolatedValue);
25+
protected override GetInfluencersRequest Initializer => new GetInfluencersRequest(CallIsolatedValue)
26+
{
27+
End = new DateTimeOffset(2016, 6, 2, 01, 00, 00, TimeSpan.Zero)
28+
};
29+
2630
protected override string UrlPath => $"/_ml/anomaly_detectors/{CallIsolatedValue}/results/influencers";
2731

28-
protected override GetInfluencersDescriptor NewDescriptor() => new GetInfluencersDescriptor(CallIsolatedValue);
32+
protected override GetInfluencersDescriptor NewDescriptor() => new GetInfluencersDescriptor(CallIsolatedValue)
33+
.End(new DateTimeOffset(2016, 6, 2, 01, 00, 00, TimeSpan.Zero));
2934

3035
protected override void IntegrationSetup(IElasticClient client, CallUniqueValues values)
3136
{

tests/Tests/XPack/MachineLearning/GetModelSnapshots/GetModelSnapshotsApiTests.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,24 @@ public class GetModelSnapshotsApiTests
1616
: MachineLearningIntegrationTestBase<GetModelSnapshotsResponse, IGetModelSnapshotsRequest, GetModelSnapshotsDescriptor,
1717
GetModelSnapshotsRequest>
1818
{
19+
private static readonly DateTimeOffset Timestamp = new DateTimeOffset(2016, 6, 2, 00, 00, 00, TimeSpan.Zero);
20+
1921
public GetModelSnapshotsApiTests(MachineLearningCluster cluster, EndpointUsage usage) : base(cluster, usage) { }
2022

2123
protected override bool ExpectIsValid => true;
2224
protected override object ExpectJson => null;
2325
protected override int ExpectStatusCode => 200;
24-
protected override Func<GetModelSnapshotsDescriptor, IGetModelSnapshotsRequest> Fluent => f => f;
26+
protected override Func<GetModelSnapshotsDescriptor, IGetModelSnapshotsRequest> Fluent => f => f
27+
.Start(Timestamp.AddHours(-1))
28+
.End(Timestamp.AddHours(1));
29+
2530
protected override HttpMethod HttpMethod => HttpMethod.POST;
26-
protected override GetModelSnapshotsRequest Initializer => new GetModelSnapshotsRequest(CallIsolatedValue);
31+
protected override GetModelSnapshotsRequest Initializer => new GetModelSnapshotsRequest(CallIsolatedValue)
32+
{
33+
Start = Timestamp.AddHours(-1),
34+
End = Timestamp.AddHours(1)
35+
};
36+
2737
protected override string UrlPath => $"/_ml/anomaly_detectors/{CallIsolatedValue}/model_snapshots";
2838

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

5363
var modelSnapshot = response.ModelSnapshots.First();
5464
modelSnapshot.JobId.Should().Be(CallIsolatedValue);
55-
modelSnapshot.Timestamp.Should().Be(new DateTimeOffset(2016, 6, 2, 00, 00, 00, TimeSpan.Zero));
65+
modelSnapshot.Timestamp.Should().Be(Timestamp);
5666
modelSnapshot.SnapshotId.Should().Be("1");
5767
modelSnapshot.SnapshotDocCount.Should().Be(1);
5868
modelSnapshot.Retain.Should().Be(false);

tests/Tests/XPack/MachineLearning/StartDatafeed/StartDatafeedApiTests.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,23 @@ public class StartDatafeedApiTests
1717
{
1818
public StartDatafeedApiTests(MachineLearningCluster cluster, EndpointUsage usage) : base(cluster, usage) { }
1919

20+
private DateTimeOffset Now => DateTimeOffset.Now;
21+
2022
protected override bool ExpectIsValid => true;
2123
protected override object ExpectJson => null;
2224
protected override int ExpectStatusCode => 200;
23-
protected override Func<StartDatafeedDescriptor, IStartDatafeedRequest> Fluent => f => f;
25+
26+
protected override Func<StartDatafeedDescriptor, IStartDatafeedRequest> Fluent => f => f
27+
.Start(Now)
28+
.End(Now.AddSeconds(10));
29+
2430
protected override HttpMethod HttpMethod => HttpMethod.POST;
25-
protected override StartDatafeedRequest Initializer => new StartDatafeedRequest(CallIsolatedValue + "-datafeed");
31+
protected override StartDatafeedRequest Initializer => new StartDatafeedRequest(CallIsolatedValue + "-datafeed")
32+
{
33+
Start = Now,
34+
End = Now.AddSeconds(10)
35+
};
36+
2637
protected override string UrlPath => $"_ml/datafeeds/{CallIsolatedValue}-datafeed/_start";
2738

2839
protected override void IntegrationSetup(IElasticClient client, CallUniqueValues values)

0 commit comments

Comments
 (0)