|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | +package org.elasticsearch.xpack.core.ml.action; |
| 7 | + |
| 8 | +import org.elasticsearch.ElasticsearchException; |
| 9 | +import org.elasticsearch.action.ActionRequestBuilder; |
| 10 | +import org.elasticsearch.action.ActionType; |
| 11 | +import org.elasticsearch.client.ElasticsearchClient; |
| 12 | +import org.elasticsearch.common.ParseField; |
| 13 | +import org.elasticsearch.common.io.stream.StreamInput; |
| 14 | +import org.elasticsearch.common.io.stream.StreamOutput; |
| 15 | +import org.elasticsearch.common.io.stream.Writeable; |
| 16 | +import org.elasticsearch.common.xcontent.ToXContentObject; |
| 17 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 18 | +import org.elasticsearch.ingest.IngestStats; |
| 19 | +import org.elasticsearch.xpack.core.action.AbstractGetResourcesRequest; |
| 20 | +import org.elasticsearch.xpack.core.action.AbstractGetResourcesResponse; |
| 21 | +import org.elasticsearch.xpack.core.action.util.QueryPage; |
| 22 | +import org.elasticsearch.xpack.core.ml.inference.TrainedModelConfig; |
| 23 | + |
| 24 | +import java.io.IOException; |
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.Collections; |
| 27 | +import java.util.List; |
| 28 | +import java.util.Map; |
| 29 | +import java.util.Objects; |
| 30 | +import java.util.Set; |
| 31 | + |
| 32 | +public class GetTrainedModelsStatsAction extends ActionType<GetTrainedModelsStatsAction.Response> { |
| 33 | + |
| 34 | + public static final GetTrainedModelsStatsAction INSTANCE = new GetTrainedModelsStatsAction(); |
| 35 | + public static final String NAME = "cluster:monitor/xpack/ml/inference/stats/get"; |
| 36 | + |
| 37 | + public static final ParseField MODEL_ID = new ParseField("model_id"); |
| 38 | + public static final ParseField PIPELINE_COUNT = new ParseField("pipeline_count"); |
| 39 | + |
| 40 | + private GetTrainedModelsStatsAction() { |
| 41 | + super(NAME, GetTrainedModelsStatsAction.Response::new); |
| 42 | + } |
| 43 | + |
| 44 | + public static class Request extends AbstractGetResourcesRequest { |
| 45 | + |
| 46 | + public static final ParseField ALLOW_NO_MATCH = new ParseField("allow_no_match"); |
| 47 | + |
| 48 | + public Request() { |
| 49 | + setAllowNoResources(true); |
| 50 | + } |
| 51 | + |
| 52 | + public Request(String id) { |
| 53 | + setResourceId(id); |
| 54 | + setAllowNoResources(true); |
| 55 | + } |
| 56 | + |
| 57 | + public Request(StreamInput in) throws IOException { |
| 58 | + super(in); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public String getResourceIdField() { |
| 63 | + return TrainedModelConfig.MODEL_ID.getPreferredName(); |
| 64 | + } |
| 65 | + |
| 66 | + } |
| 67 | + |
| 68 | + public static class RequestBuilder extends ActionRequestBuilder<Request, Response> { |
| 69 | + |
| 70 | + public RequestBuilder(ElasticsearchClient client, GetTrainedModelsStatsAction action) { |
| 71 | + super(client, action, new Request()); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + public static class Response extends AbstractGetResourcesResponse<Response.TrainedModelStats> { |
| 76 | + |
| 77 | + public static class TrainedModelStats implements ToXContentObject, Writeable { |
| 78 | + private final String modelId; |
| 79 | + private final IngestStats ingestStats; |
| 80 | + private final int pipelineCount; |
| 81 | + |
| 82 | + private static final IngestStats EMPTY_INGEST_STATS = new IngestStats(new IngestStats.Stats(0, 0, 0, 0), |
| 83 | + Collections.emptyList(), |
| 84 | + Collections.emptyMap()); |
| 85 | + |
| 86 | + public TrainedModelStats(String modelId, IngestStats ingestStats, int pipelineCount) { |
| 87 | + this.modelId = Objects.requireNonNull(modelId); |
| 88 | + this.ingestStats = ingestStats == null ? EMPTY_INGEST_STATS : ingestStats; |
| 89 | + if (pipelineCount < 0) { |
| 90 | + throw new ElasticsearchException("[{}] must be a greater than or equal to 0", PIPELINE_COUNT.getPreferredName()); |
| 91 | + } |
| 92 | + this.pipelineCount = pipelineCount; |
| 93 | + } |
| 94 | + |
| 95 | + public TrainedModelStats(StreamInput in) throws IOException { |
| 96 | + modelId = in.readString(); |
| 97 | + ingestStats = new IngestStats(in); |
| 98 | + pipelineCount = in.readVInt(); |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 103 | + builder.startObject(); |
| 104 | + builder.field(MODEL_ID.getPreferredName(), modelId); |
| 105 | + builder.field(PIPELINE_COUNT.getPreferredName(), pipelineCount); |
| 106 | + if (pipelineCount > 0) { |
| 107 | + // Ingest stats is a fragment |
| 108 | + ingestStats.toXContent(builder, params); |
| 109 | + } |
| 110 | + builder.endObject(); |
| 111 | + return builder; |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public void writeTo(StreamOutput out) throws IOException { |
| 116 | + out.writeString(modelId); |
| 117 | + ingestStats.writeTo(out); |
| 118 | + out.writeVInt(pipelineCount); |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public int hashCode() { |
| 123 | + return Objects.hash(modelId, ingestStats, pipelineCount); |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public boolean equals(Object obj) { |
| 128 | + if (obj == null) { |
| 129 | + return false; |
| 130 | + } |
| 131 | + if (getClass() != obj.getClass()) { |
| 132 | + return false; |
| 133 | + } |
| 134 | + TrainedModelStats other = (TrainedModelStats) obj; |
| 135 | + return Objects.equals(this.modelId, other.modelId) |
| 136 | + && Objects.equals(this.ingestStats, other.ingestStats) |
| 137 | + && Objects.equals(this.pipelineCount, other.pipelineCount); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + public static final ParseField RESULTS_FIELD = new ParseField("trained_model_stats"); |
| 142 | + |
| 143 | + public Response(StreamInput in) throws IOException { |
| 144 | + super(in); |
| 145 | + } |
| 146 | + |
| 147 | + public Response(QueryPage<Response.TrainedModelStats> trainedModels) { |
| 148 | + super(trainedModels); |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + protected Reader<Response.TrainedModelStats> getReader() { |
| 153 | + return Response.TrainedModelStats::new; |
| 154 | + } |
| 155 | + |
| 156 | + public static class Builder { |
| 157 | + |
| 158 | + private long totalModelCount; |
| 159 | + private Set<String> expandedIds; |
| 160 | + private Map<String, IngestStats> ingestStatsMap; |
| 161 | + |
| 162 | + public Builder setTotalModelCount(long totalModelCount) { |
| 163 | + this.totalModelCount = totalModelCount; |
| 164 | + return this; |
| 165 | + } |
| 166 | + |
| 167 | + public Builder setExpandedIds(Set<String> expandedIds) { |
| 168 | + this.expandedIds = expandedIds; |
| 169 | + return this; |
| 170 | + } |
| 171 | + |
| 172 | + public Set<String> getExpandedIds() { |
| 173 | + return this.expandedIds; |
| 174 | + } |
| 175 | + |
| 176 | + public Builder setIngestStatsByModelId(Map<String, IngestStats> ingestStatsByModelId) { |
| 177 | + this.ingestStatsMap = ingestStatsByModelId; |
| 178 | + return this; |
| 179 | + } |
| 180 | + |
| 181 | + public Response build() { |
| 182 | + List<TrainedModelStats> trainedModelStats = new ArrayList<>(expandedIds.size()); |
| 183 | + expandedIds.forEach(id -> { |
| 184 | + IngestStats ingestStats = ingestStatsMap.get(id); |
| 185 | + trainedModelStats.add(new TrainedModelStats(id, ingestStats, ingestStats == null ? |
| 186 | + 0 : |
| 187 | + ingestStats.getPipelineStats().size())); |
| 188 | + }); |
| 189 | + return new Response(new QueryPage<>(trainedModelStats, totalModelCount, RESULTS_FIELD)); |
| 190 | + } |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | +} |
0 commit comments