Skip to content

[Backport 7.x] Serialize Epoch milliseconds as a string #5001

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 3, 2020
Merged
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
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