-
Notifications
You must be signed in to change notification settings - Fork 24.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[7.x] [ML] Add DatafeedTimingStats to datafeed GetDatafeedStatsAction…
- Loading branch information
1 parent
853ddb5
commit 44781e4
Showing
53 changed files
with
1,730 additions
and
239 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
...st-high-level/src/main/java/org/elasticsearch/client/ml/datafeed/DatafeedTimingStats.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.elasticsearch.client.ml.datafeed; | ||
|
||
import org.elasticsearch.common.Nullable; | ||
import org.elasticsearch.common.ParseField; | ||
import org.elasticsearch.common.Strings; | ||
import org.elasticsearch.common.xcontent.ConstructingObjectParser; | ||
import org.elasticsearch.common.xcontent.ToXContent; | ||
import org.elasticsearch.common.xcontent.ToXContentObject; | ||
import org.elasticsearch.common.xcontent.XContentBuilder; | ||
|
||
import java.io.IOException; | ||
import java.util.Objects; | ||
|
||
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg; | ||
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg; | ||
|
||
public class DatafeedTimingStats implements ToXContentObject { | ||
|
||
public static final ParseField JOB_ID = new ParseField("job_id"); | ||
public static final ParseField SEARCH_COUNT = new ParseField("search_count"); | ||
public static final ParseField TOTAL_SEARCH_TIME_MS = new ParseField("total_search_time_ms"); | ||
|
||
public static final ParseField TYPE = new ParseField("datafeed_timing_stats"); | ||
|
||
public static final ConstructingObjectParser<DatafeedTimingStats, Void> PARSER = createParser(); | ||
|
||
private static ConstructingObjectParser<DatafeedTimingStats, Void> createParser() { | ||
ConstructingObjectParser<DatafeedTimingStats, Void> parser = | ||
new ConstructingObjectParser<>( | ||
"datafeed_timing_stats", | ||
true, | ||
args -> { | ||
String jobId = (String) args[0]; | ||
Long searchCount = (Long) args[1]; | ||
Double totalSearchTimeMs = (Double) args[2]; | ||
return new DatafeedTimingStats(jobId, getOrDefault(searchCount, 0L), getOrDefault(totalSearchTimeMs, 0.0)); | ||
}); | ||
parser.declareString(constructorArg(), JOB_ID); | ||
parser.declareLong(optionalConstructorArg(), SEARCH_COUNT); | ||
parser.declareDouble(optionalConstructorArg(), TOTAL_SEARCH_TIME_MS); | ||
return parser; | ||
} | ||
|
||
private final String jobId; | ||
private long searchCount; | ||
private double totalSearchTimeMs; | ||
|
||
public DatafeedTimingStats(String jobId, long searchCount, double totalSearchTimeMs) { | ||
this.jobId = Objects.requireNonNull(jobId); | ||
this.searchCount = searchCount; | ||
this.totalSearchTimeMs = totalSearchTimeMs; | ||
} | ||
|
||
public String getJobId() { | ||
return jobId; | ||
} | ||
|
||
public long getSearchCount() { | ||
return searchCount; | ||
} | ||
|
||
public double getTotalSearchTimeMs() { | ||
return totalSearchTimeMs; | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException { | ||
builder.startObject(); | ||
builder.field(JOB_ID.getPreferredName(), jobId); | ||
builder.field(SEARCH_COUNT.getPreferredName(), searchCount); | ||
builder.field(TOTAL_SEARCH_TIME_MS.getPreferredName(), totalSearchTimeMs); | ||
builder.endObject(); | ||
return builder; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) { | ||
return true; | ||
} | ||
if (obj == null || getClass() != obj.getClass()) { | ||
return false; | ||
} | ||
|
||
DatafeedTimingStats other = (DatafeedTimingStats) obj; | ||
return Objects.equals(this.jobId, other.jobId) | ||
&& this.searchCount == other.searchCount | ||
&& this.totalSearchTimeMs == other.totalSearchTimeMs; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(jobId, searchCount, totalSearchTimeMs); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return Strings.toString(this); | ||
} | ||
|
||
private static <T> T getOrDefault(@Nullable T value, T defaultValue) { | ||
return value != null ? value : defaultValue; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
...gh-level/src/test/java/org/elasticsearch/client/ml/datafeed/DatafeedTimingStatsTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.elasticsearch.client.ml.datafeed; | ||
|
||
import org.elasticsearch.common.xcontent.DeprecationHandler; | ||
import org.elasticsearch.common.xcontent.XContentFactory; | ||
import org.elasticsearch.common.xcontent.XContentParser; | ||
import org.elasticsearch.common.xcontent.XContentType; | ||
import org.elasticsearch.test.AbstractXContentTestCase; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.hamcrest.Matchers.equalTo; | ||
|
||
public class DatafeedTimingStatsTests extends AbstractXContentTestCase<DatafeedTimingStats> { | ||
|
||
private static final String JOB_ID = "my-job-id"; | ||
|
||
public static DatafeedTimingStats createRandomInstance() { | ||
return new DatafeedTimingStats(randomAlphaOfLength(10), randomLong(), randomDouble()); | ||
} | ||
|
||
@Override | ||
protected DatafeedTimingStats createTestInstance() { | ||
return createRandomInstance(); | ||
} | ||
|
||
@Override | ||
protected DatafeedTimingStats doParseInstance(XContentParser parser) throws IOException { | ||
return DatafeedTimingStats.PARSER.apply(parser, null); | ||
} | ||
|
||
@Override | ||
protected boolean supportsUnknownFields() { | ||
return true; | ||
} | ||
|
||
public void testParse_OptionalFieldsAbsent() throws IOException { | ||
String json = "{\"job_id\": \"my-job-id\"}"; | ||
try (XContentParser parser = | ||
XContentFactory.xContent(XContentType.JSON).createParser( | ||
xContentRegistry(), DeprecationHandler.THROW_UNSUPPORTED_OPERATION, json)) { | ||
DatafeedTimingStats stats = DatafeedTimingStats.PARSER.apply(parser, null); | ||
assertThat(stats.getJobId(), equalTo(JOB_ID)); | ||
assertThat(stats.getSearchCount(), equalTo(0L)); | ||
assertThat(stats.getTotalSearchTimeMs(), equalTo(0.0)); | ||
} | ||
} | ||
|
||
public void testEquals() { | ||
DatafeedTimingStats stats1 = new DatafeedTimingStats(JOB_ID, 5, 100.0); | ||
DatafeedTimingStats stats2 = new DatafeedTimingStats(JOB_ID, 5, 100.0); | ||
DatafeedTimingStats stats3 = new DatafeedTimingStats(JOB_ID, 5, 200.0); | ||
|
||
assertTrue(stats1.equals(stats1)); | ||
assertTrue(stats1.equals(stats2)); | ||
assertFalse(stats2.equals(stats3)); | ||
} | ||
|
||
public void testHashCode() { | ||
DatafeedTimingStats stats1 = new DatafeedTimingStats(JOB_ID, 5, 100.0); | ||
DatafeedTimingStats stats2 = new DatafeedTimingStats(JOB_ID, 5, 100.0); | ||
DatafeedTimingStats stats3 = new DatafeedTimingStats(JOB_ID, 5, 200.0); | ||
|
||
assertEquals(stats1.hashCode(), stats1.hashCode()); | ||
assertEquals(stats1.hashCode(), stats2.hashCode()); | ||
assertNotEquals(stats2.hashCode(), stats3.hashCode()); | ||
} | ||
|
||
public void testConstructorAndGetters() { | ||
DatafeedTimingStats stats = new DatafeedTimingStats(JOB_ID, 5, 123.456); | ||
assertThat(stats.getJobId(), equalTo(JOB_ID)); | ||
assertThat(stats.getSearchCount(), equalTo(5L)); | ||
assertThat(stats.getTotalSearchTimeMs(), equalTo(123.456)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.