Skip to content

Commit 8d2796d

Browse files
Do not omit zero values
1 parent 6e76c14 commit 8d2796d

File tree

4 files changed

+158
-121
lines changed

4 files changed

+158
-121
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/inference/usage/ModelStats.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,7 @@ public void addXContentFragment(XContentBuilder builder, Params params) throws I
9090
builder.field("service", service);
9191
builder.field("task_type", taskType.name());
9292
builder.field("count", count);
93-
if (semanticTextStats.isEmpty() == false) {
94-
builder.field("semantic_text", semanticTextStats);
95-
}
93+
builder.field("semantic_text", semanticTextStats);
9694
}
9795

9896
@Override

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/action/TransportInferenceUsageAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ private InferenceFeatureSetUsage collectUsage(List<ModelConfigurations> endpoint
9797
Map<String, ModelStats> endpointStats = new TreeMap<>();
9898
addStatsByServiceAndTask(inferenceFieldsByIndexServiceAndTask, endpoints, endpointStats);
9999
addStatsForDefaultModels(inferenceFieldsByIndexServiceAndTask, endpoints, endpointStats);
100-
return new InferenceFeatureSetUsage(endpointStats.values().stream().filter(stats -> stats.count() > 0).toList());
100+
return new InferenceFeatureSetUsage(endpointStats.values());
101101
}
102102

103103
private static Map<ServiceAndTaskType, Map<String, List<InferenceFieldMetadata>>> mapInferenceFieldsByIndexServiceAndTask(

x-pack/plugin/inference/src/test/java/org/elasticsearch/xpack/inference/action/TransportInferenceUsageActionTests.java

Lines changed: 87 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,15 @@ public void testGivenServices_NoInferenceFields() throws Exception {
109109

110110
XContentSource response = executeAction();
111111

112-
assertThat(response.getValue("models"), hasSize(5));
113-
assertStats(response, 0, new ModelStats("_all", TaskType.SPARSE_EMBEDDING, 3, new SemanticTextStats()));
114-
assertStats(response, 1, new ModelStats("_all", TaskType.TEXT_EMBEDDING, 3, new SemanticTextStats()));
115-
assertStats(response, 2, new ModelStats("hugging_face_elser", TaskType.SPARSE_EMBEDDING, 2, new SemanticTextStats()));
116-
assertStats(response, 3, new ModelStats("openai", TaskType.SPARSE_EMBEDDING, 1, new SemanticTextStats()));
117-
assertStats(response, 4, new ModelStats("openai", TaskType.TEXT_EMBEDDING, 3, new SemanticTextStats()));
112+
assertThat(response.getValue("models"), hasSize(8));
113+
assertStats(response, 0, new ModelStats("_all", TaskType.CHAT_COMPLETION, 0, new SemanticTextStats()));
114+
assertStats(response, 1, new ModelStats("_all", TaskType.COMPLETION, 0, new SemanticTextStats()));
115+
assertStats(response, 2, new ModelStats("_all", TaskType.RERANK, 0, new SemanticTextStats()));
116+
assertStats(response, 3, new ModelStats("_all", TaskType.SPARSE_EMBEDDING, 3, new SemanticTextStats()));
117+
assertStats(response, 4, new ModelStats("_all", TaskType.TEXT_EMBEDDING, 3, new SemanticTextStats()));
118+
assertStats(response, 5, new ModelStats("hugging_face_elser", TaskType.SPARSE_EMBEDDING, 2, new SemanticTextStats()));
119+
assertStats(response, 6, new ModelStats("openai", TaskType.SPARSE_EMBEDDING, 1, new SemanticTextStats()));
120+
assertStats(response, 7, new ModelStats("openai", TaskType.TEXT_EMBEDDING, 3, new SemanticTextStats()));
118121
}
119122

120123
public void testGivenFieldRefersToMissingInferenceEndpoint() throws Exception {
@@ -123,7 +126,12 @@ public void testGivenFieldRefersToMissingInferenceEndpoint() throws Exception {
123126

124127
XContentSource response = executeAction();
125128

126-
assertThat(response.getValue("models"), hasSize(0));
129+
assertThat(response.getValue("models"), hasSize(5));
130+
assertStats(response, 0, new ModelStats("_all", TaskType.CHAT_COMPLETION, 0, new SemanticTextStats()));
131+
assertStats(response, 1, new ModelStats("_all", TaskType.COMPLETION, 0, new SemanticTextStats()));
132+
assertStats(response, 2, new ModelStats("_all", TaskType.RERANK, 0, new SemanticTextStats()));
133+
assertStats(response, 3, new ModelStats("_all", TaskType.SPARSE_EMBEDDING, 0, new SemanticTextStats()));
134+
assertStats(response, 4, new ModelStats("_all", TaskType.TEXT_EMBEDDING, 0, new SemanticTextStats()));
127135
}
128136

129137
public void testGivenVariousServicesAndInferenceFields() throws Exception {
@@ -166,12 +174,15 @@ public void testGivenVariousServicesAndInferenceFields() throws Exception {
166174

167175
XContentSource response = executeAction();
168176

169-
assertThat(response.getValue("models"), hasSize(5));
170-
assertStats(response, 0, new ModelStats("_all", TaskType.SPARSE_EMBEDDING, 2, new SemanticTextStats(6, 2, 2)));
171-
assertStats(response, 1, new ModelStats("_all", TaskType.TEXT_EMBEDDING, 5, new SemanticTextStats(9, 3, 4)));
172-
assertStats(response, 2, new ModelStats("eis", TaskType.TEXT_EMBEDDING, 3, new SemanticTextStats(3, 3, 2)));
173-
assertStats(response, 3, new ModelStats("openai", TaskType.SPARSE_EMBEDDING, 2, new SemanticTextStats(6, 2, 2)));
174-
assertStats(response, 4, new ModelStats("openai", TaskType.TEXT_EMBEDDING, 2, new SemanticTextStats(6, 2, 2)));
177+
assertThat(response.getValue("models"), hasSize(8));
178+
assertStats(response, 0, new ModelStats("_all", TaskType.CHAT_COMPLETION, 0, new SemanticTextStats()));
179+
assertStats(response, 1, new ModelStats("_all", TaskType.COMPLETION, 0, new SemanticTextStats()));
180+
assertStats(response, 2, new ModelStats("_all", TaskType.RERANK, 0, new SemanticTextStats()));
181+
assertStats(response, 3, new ModelStats("_all", TaskType.SPARSE_EMBEDDING, 2, new SemanticTextStats(6, 2, 2)));
182+
assertStats(response, 4, new ModelStats("_all", TaskType.TEXT_EMBEDDING, 5, new SemanticTextStats(9, 3, 4)));
183+
assertStats(response, 5, new ModelStats("eis", TaskType.TEXT_EMBEDDING, 3, new SemanticTextStats(3, 3, 2)));
184+
assertStats(response, 6, new ModelStats("openai", TaskType.SPARSE_EMBEDDING, 2, new SemanticTextStats(6, 2, 2)));
185+
assertStats(response, 7, new ModelStats("openai", TaskType.TEXT_EMBEDDING, 2, new SemanticTextStats(6, 2, 2)));
175186
}
176187

177188
public void testGivenServices_InferenceFieldsReferencingDefaultModels() throws Exception {
@@ -208,16 +219,19 @@ public void testGivenServices_InferenceFieldsReferencingDefaultModels() throws E
208219

209220
XContentSource response = executeAction();
210221

211-
assertThat(response.getValue("models"), hasSize(9));
212-
assertStats(response, 0, new ModelStats("_all", TaskType.SPARSE_EMBEDDING, 2, new SemanticTextStats(4, 2, 2)));
213-
assertStats(response, 1, new ModelStats("_all", TaskType.TEXT_EMBEDDING, 3, new SemanticTextStats(6, 2, 2)));
214-
assertStats(response, 2, new ModelStats("_eis__model-id-001", TaskType.TEXT_EMBEDDING, 2, new SemanticTextStats(6, 2, 2)));
215-
assertStats(response, 3, new ModelStats("_eis__model-id-002", TaskType.SPARSE_EMBEDDING, 1, new SemanticTextStats(1, 1, 1)));
216-
assertStats(response, 4, new ModelStats("_openai_model-id-004", TaskType.SPARSE_EMBEDDING, 1, new SemanticTextStats(3, 2, 1)));
217-
assertStats(response, 5, new ModelStats("eis", TaskType.SPARSE_EMBEDDING, 1, new SemanticTextStats(1, 1, 1)));
218-
assertStats(response, 6, new ModelStats("eis", TaskType.TEXT_EMBEDDING, 2, new SemanticTextStats(6, 2, 2)));
219-
assertStats(response, 7, new ModelStats("openai", TaskType.SPARSE_EMBEDDING, 1, new SemanticTextStats(3, 2, 1)));
220-
assertStats(response, 8, new ModelStats("openai", TaskType.TEXT_EMBEDDING, 1, new SemanticTextStats()));
222+
assertThat(response.getValue("models"), hasSize(12));
223+
assertStats(response, 0, new ModelStats("_all", TaskType.CHAT_COMPLETION, 0, new SemanticTextStats()));
224+
assertStats(response, 1, new ModelStats("_all", TaskType.COMPLETION, 0, new SemanticTextStats()));
225+
assertStats(response, 2, new ModelStats("_all", TaskType.RERANK, 0, new SemanticTextStats()));
226+
assertStats(response, 3, new ModelStats("_all", TaskType.SPARSE_EMBEDDING, 2, new SemanticTextStats(4, 2, 2)));
227+
assertStats(response, 4, new ModelStats("_all", TaskType.TEXT_EMBEDDING, 3, new SemanticTextStats(6, 2, 2)));
228+
assertStats(response, 5, new ModelStats("_eis__model-id-001", TaskType.TEXT_EMBEDDING, 2, new SemanticTextStats(6, 2, 2)));
229+
assertStats(response, 6, new ModelStats("_eis__model-id-002", TaskType.SPARSE_EMBEDDING, 1, new SemanticTextStats(1, 1, 1)));
230+
assertStats(response, 7, new ModelStats("_openai_model-id-004", TaskType.SPARSE_EMBEDDING, 1, new SemanticTextStats(3, 2, 1)));
231+
assertStats(response, 8, new ModelStats("eis", TaskType.SPARSE_EMBEDDING, 1, new SemanticTextStats(1, 1, 1)));
232+
assertStats(response, 9, new ModelStats("eis", TaskType.TEXT_EMBEDDING, 2, new SemanticTextStats(6, 2, 2)));
233+
assertStats(response, 10, new ModelStats("openai", TaskType.SPARSE_EMBEDDING, 1, new SemanticTextStats(3, 2, 1)));
234+
assertStats(response, 11, new ModelStats("openai", TaskType.TEXT_EMBEDDING, 1, new SemanticTextStats()));
221235
}
222236

223237
public void testGivenDefaultModelWithLinuxSuffix() throws Exception {
@@ -247,10 +261,14 @@ public void testGivenDefaultModelWithLinuxSuffix() throws Exception {
247261

248262
XContentSource response = executeAction();
249263

250-
assertThat(response.getValue("models"), hasSize(3));
251-
assertStats(response, 0, new ModelStats("_all", TaskType.TEXT_EMBEDDING, 2, new SemanticTextStats(6, 2, 2)));
252-
assertStats(response, 1, new ModelStats("_eis__model-id-001", TaskType.TEXT_EMBEDDING, 2, new SemanticTextStats(6, 2, 2)));
253-
assertStats(response, 2, new ModelStats("eis", TaskType.TEXT_EMBEDDING, 2, new SemanticTextStats(6, 2, 2)));
264+
assertThat(response.getValue("models"), hasSize(7));
265+
assertStats(response, 0, new ModelStats("_all", TaskType.CHAT_COMPLETION, 0, new SemanticTextStats()));
266+
assertStats(response, 1, new ModelStats("_all", TaskType.COMPLETION, 0, new SemanticTextStats()));
267+
assertStats(response, 2, new ModelStats("_all", TaskType.RERANK, 0, new SemanticTextStats()));
268+
assertStats(response, 3, new ModelStats("_all", TaskType.SPARSE_EMBEDDING, 0, new SemanticTextStats()));
269+
assertStats(response, 4, new ModelStats("_all", TaskType.TEXT_EMBEDDING, 2, new SemanticTextStats(6, 2, 2)));
270+
assertStats(response, 5, new ModelStats("_eis__model-id-001", TaskType.TEXT_EMBEDDING, 2, new SemanticTextStats(6, 2, 2)));
271+
assertStats(response, 6, new ModelStats("eis", TaskType.TEXT_EMBEDDING, 2, new SemanticTextStats(6, 2, 2)));
254272
}
255273

256274
public void testGivenSameDefaultModelWithAndWithoutLinuxSuffix() throws Exception {
@@ -280,10 +298,14 @@ public void testGivenSameDefaultModelWithAndWithoutLinuxSuffix() throws Exceptio
280298

281299
XContentSource response = executeAction();
282300

283-
assertThat(response.getValue("models"), hasSize(3));
284-
assertStats(response, 0, new ModelStats("_all", TaskType.TEXT_EMBEDDING, 2, new SemanticTextStats(6, 2, 2)));
285-
assertStats(response, 1, new ModelStats("_eis__model-id-001", TaskType.TEXT_EMBEDDING, 2, new SemanticTextStats(6, 2, 2)));
286-
assertStats(response, 2, new ModelStats("eis", TaskType.TEXT_EMBEDDING, 2, new SemanticTextStats(6, 2, 2)));
301+
assertThat(response.getValue("models"), hasSize(7));
302+
assertStats(response, 0, new ModelStats("_all", TaskType.CHAT_COMPLETION, 0, new SemanticTextStats()));
303+
assertStats(response, 1, new ModelStats("_all", TaskType.COMPLETION, 0, new SemanticTextStats()));
304+
assertStats(response, 2, new ModelStats("_all", TaskType.RERANK, 0, new SemanticTextStats()));
305+
assertStats(response, 3, new ModelStats("_all", TaskType.SPARSE_EMBEDDING, 0, new SemanticTextStats()));
306+
assertStats(response, 4, new ModelStats("_all", TaskType.TEXT_EMBEDDING, 2, new SemanticTextStats(6, 2, 2)));
307+
assertStats(response, 5, new ModelStats("_eis__model-id-001", TaskType.TEXT_EMBEDDING, 2, new SemanticTextStats(6, 2, 2)));
308+
assertStats(response, 6, new ModelStats("eis", TaskType.TEXT_EMBEDDING, 2, new SemanticTextStats(6, 2, 2)));
287309
}
288310

289311
public void testGivenExternalServiceModelIsNull() throws Exception {
@@ -292,9 +314,13 @@ public void testGivenExternalServiceModelIsNull() throws Exception {
292314

293315
XContentSource response = executeAction();
294316

295-
assertThat(response.getValue("models"), hasSize(2));
296-
assertStats(response, 0, new ModelStats("_all", TaskType.TEXT_EMBEDDING, 1, new SemanticTextStats(1, 1, 1)));
297-
assertStats(response, 1, new ModelStats("openai", TaskType.TEXT_EMBEDDING, 1, new SemanticTextStats(1, 1, 1)));
317+
assertThat(response.getValue("models"), hasSize(6));
318+
assertStats(response, 0, new ModelStats("_all", TaskType.CHAT_COMPLETION, 0, new SemanticTextStats()));
319+
assertStats(response, 1, new ModelStats("_all", TaskType.COMPLETION, 0, new SemanticTextStats()));
320+
assertStats(response, 2, new ModelStats("_all", TaskType.RERANK, 0, new SemanticTextStats()));
321+
assertStats(response, 3, new ModelStats("_all", TaskType.SPARSE_EMBEDDING, 0, new SemanticTextStats()));
322+
assertStats(response, 4, new ModelStats("_all", TaskType.TEXT_EMBEDDING, 1, new SemanticTextStats(1, 1, 1)));
323+
assertStats(response, 5, new ModelStats("openai", TaskType.TEXT_EMBEDDING, 1, new SemanticTextStats(1, 1, 1)));
298324
}
299325

300326
public void testGivenDuplicateServices() throws Exception {
@@ -320,9 +346,13 @@ public void testGivenDuplicateServices() throws Exception {
320346

321347
XContentSource response = executeAction();
322348

323-
assertThat(response.getValue("models"), hasSize(2));
324-
assertStats(response, 0, new ModelStats("_all", TaskType.TEXT_EMBEDDING, 2, new SemanticTextStats(5, 2, 2)));
325-
assertStats(response, 1, new ModelStats("openai", TaskType.TEXT_EMBEDDING, 2, new SemanticTextStats(5, 2, 2)));
349+
assertThat(response.getValue("models"), hasSize(6));
350+
assertStats(response, 0, new ModelStats("_all", TaskType.CHAT_COMPLETION, 0, new SemanticTextStats()));
351+
assertStats(response, 1, new ModelStats("_all", TaskType.COMPLETION, 0, new SemanticTextStats()));
352+
assertStats(response, 2, new ModelStats("_all", TaskType.RERANK, 0, new SemanticTextStats()));
353+
assertStats(response, 3, new ModelStats("_all", TaskType.SPARSE_EMBEDDING, 0, new SemanticTextStats()));
354+
assertStats(response, 4, new ModelStats("_all", TaskType.TEXT_EMBEDDING, 2, new SemanticTextStats(5, 2, 2)));
355+
assertStats(response, 5, new ModelStats("openai", TaskType.TEXT_EMBEDDING, 2, new SemanticTextStats(5, 2, 2)));
326356
}
327357

328358
public void testShouldExcludeSystemIndexFields() throws Exception {
@@ -349,10 +379,14 @@ public void testShouldExcludeSystemIndexFields() throws Exception {
349379

350380
XContentSource response = executeAction();
351381

352-
assertThat(response.getValue("models"), hasSize(3));
353-
assertStats(response, 0, new ModelStats("_all", TaskType.TEXT_EMBEDDING, 2, new SemanticTextStats(2, 1, 2)));
354-
assertStats(response, 1, new ModelStats("eis", TaskType.TEXT_EMBEDDING, 1, new SemanticTextStats(1, 1, 1)));
355-
assertStats(response, 2, new ModelStats("openai", TaskType.TEXT_EMBEDDING, 1, new SemanticTextStats(1, 1, 1)));
382+
assertThat(response.getValue("models"), hasSize(7));
383+
assertStats(response, 0, new ModelStats("_all", TaskType.CHAT_COMPLETION, 0, new SemanticTextStats()));
384+
assertStats(response, 1, new ModelStats("_all", TaskType.COMPLETION, 0, new SemanticTextStats()));
385+
assertStats(response, 2, new ModelStats("_all", TaskType.RERANK, 0, new SemanticTextStats()));
386+
assertStats(response, 3, new ModelStats("_all", TaskType.SPARSE_EMBEDDING, 0, new SemanticTextStats()));
387+
assertStats(response, 4, new ModelStats("_all", TaskType.TEXT_EMBEDDING, 2, new SemanticTextStats(2, 1, 2)));
388+
assertStats(response, 5, new ModelStats("eis", TaskType.TEXT_EMBEDDING, 1, new SemanticTextStats(1, 1, 1)));
389+
assertStats(response, 6, new ModelStats("openai", TaskType.TEXT_EMBEDDING, 1, new SemanticTextStats(1, 1, 1)));
356390
}
357391

358392
public void testFailureReturnsEmptyUsage() {
@@ -437,21 +471,17 @@ private static void assertStats(XContentSource source, int index, ModelStats sta
437471
assertThat(source.getValue("models." + index + ".service"), is(stats.service()));
438472
assertThat(source.getValue("models." + index + ".task_type"), is(stats.taskType().name()));
439473
assertThat(((Integer) source.getValue("models." + index + ".count")).longValue(), equalTo(stats.count()));
440-
if (stats.semanticTextStats().isEmpty()) {
441-
assertThat(source.getValue("models." + index + ".semantic_text"), is(nullValue()));
442-
} else {
443-
assertThat(
444-
((Integer) source.getValue("models." + index + ".semantic_text.field_count")).longValue(),
445-
equalTo(stats.semanticTextStats().getFieldCount())
446-
);
447-
assertThat(
448-
((Integer) source.getValue("models." + index + ".semantic_text.indices_count")).longValue(),
449-
equalTo(stats.semanticTextStats().getIndicesCount())
450-
);
451-
assertThat(
452-
((Integer) source.getValue("models." + index + ".semantic_text.inference_id_count")).longValue(),
453-
equalTo(stats.semanticTextStats().getInferenceIdCount())
454-
);
455-
}
474+
assertThat(
475+
((Integer) source.getValue("models." + index + ".semantic_text.field_count")).longValue(),
476+
equalTo(stats.semanticTextStats().getFieldCount())
477+
);
478+
assertThat(
479+
((Integer) source.getValue("models." + index + ".semantic_text.indices_count")).longValue(),
480+
equalTo(stats.semanticTextStats().getIndicesCount())
481+
);
482+
assertThat(
483+
((Integer) source.getValue("models." + index + ".semantic_text.inference_id_count")).longValue(),
484+
equalTo(stats.semanticTextStats().getInferenceIdCount())
485+
);
456486
}
457487
}

0 commit comments

Comments
 (0)