|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.elasticsearch.client.ml.datafeed; |
| 20 | + |
| 21 | +import org.elasticsearch.common.Nullable; |
| 22 | +import org.elasticsearch.common.ParseField; |
| 23 | +import org.elasticsearch.common.Strings; |
| 24 | +import org.elasticsearch.common.xcontent.ConstructingObjectParser; |
| 25 | +import org.elasticsearch.common.xcontent.ToXContent; |
| 26 | +import org.elasticsearch.common.xcontent.ToXContentObject; |
| 27 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 28 | + |
| 29 | +import java.io.IOException; |
| 30 | +import java.util.Objects; |
| 31 | + |
| 32 | +import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg; |
| 33 | +import static org.elasticsearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg; |
| 34 | + |
| 35 | +public class DatafeedTimingStats implements ToXContentObject { |
| 36 | + |
| 37 | + public static final ParseField JOB_ID = new ParseField("job_id"); |
| 38 | + public static final ParseField SEARCH_COUNT = new ParseField("search_count"); |
| 39 | + public static final ParseField TOTAL_SEARCH_TIME_MS = new ParseField("total_search_time_ms"); |
| 40 | + |
| 41 | + public static final ParseField TYPE = new ParseField("datafeed_timing_stats"); |
| 42 | + |
| 43 | + public static final ConstructingObjectParser<DatafeedTimingStats, Void> PARSER = createParser(); |
| 44 | + |
| 45 | + private static ConstructingObjectParser<DatafeedTimingStats, Void> createParser() { |
| 46 | + ConstructingObjectParser<DatafeedTimingStats, Void> parser = |
| 47 | + new ConstructingObjectParser<>( |
| 48 | + "datafeed_timing_stats", |
| 49 | + true, |
| 50 | + args -> { |
| 51 | + String jobId = (String) args[0]; |
| 52 | + Long searchCount = (Long) args[1]; |
| 53 | + Double totalSearchTimeMs = (Double) args[2]; |
| 54 | + return new DatafeedTimingStats(jobId, getOrDefault(searchCount, 0L), getOrDefault(totalSearchTimeMs, 0.0)); |
| 55 | + }); |
| 56 | + parser.declareString(constructorArg(), JOB_ID); |
| 57 | + parser.declareLong(optionalConstructorArg(), SEARCH_COUNT); |
| 58 | + parser.declareDouble(optionalConstructorArg(), TOTAL_SEARCH_TIME_MS); |
| 59 | + return parser; |
| 60 | + } |
| 61 | + |
| 62 | + private final String jobId; |
| 63 | + private long searchCount; |
| 64 | + private double totalSearchTimeMs; |
| 65 | + |
| 66 | + public DatafeedTimingStats(String jobId, long searchCount, double totalSearchTimeMs) { |
| 67 | + this.jobId = Objects.requireNonNull(jobId); |
| 68 | + this.searchCount = searchCount; |
| 69 | + this.totalSearchTimeMs = totalSearchTimeMs; |
| 70 | + } |
| 71 | + |
| 72 | + public String getJobId() { |
| 73 | + return jobId; |
| 74 | + } |
| 75 | + |
| 76 | + public long getSearchCount() { |
| 77 | + return searchCount; |
| 78 | + } |
| 79 | + |
| 80 | + public double getTotalSearchTimeMs() { |
| 81 | + return totalSearchTimeMs; |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException { |
| 86 | + builder.startObject(); |
| 87 | + builder.field(JOB_ID.getPreferredName(), jobId); |
| 88 | + builder.field(SEARCH_COUNT.getPreferredName(), searchCount); |
| 89 | + builder.field(TOTAL_SEARCH_TIME_MS.getPreferredName(), totalSearchTimeMs); |
| 90 | + builder.endObject(); |
| 91 | + return builder; |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public boolean equals(Object obj) { |
| 96 | + if (this == obj) { |
| 97 | + return true; |
| 98 | + } |
| 99 | + if (obj == null || getClass() != obj.getClass()) { |
| 100 | + return false; |
| 101 | + } |
| 102 | + |
| 103 | + DatafeedTimingStats other = (DatafeedTimingStats) obj; |
| 104 | + return Objects.equals(this.jobId, other.jobId) |
| 105 | + && this.searchCount == other.searchCount |
| 106 | + && this.totalSearchTimeMs == other.totalSearchTimeMs; |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public int hashCode() { |
| 111 | + return Objects.hash(jobId, searchCount, totalSearchTimeMs); |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public String toString() { |
| 116 | + return Strings.toString(this); |
| 117 | + } |
| 118 | + |
| 119 | + private static <T> T getOrDefault(@Nullable T value, T defaultValue) { |
| 120 | + return value != null ? value : defaultValue; |
| 121 | + } |
| 122 | +} |
0 commit comments