Skip to content

Commit a0f6ed5

Browse files
author
Hendrik Muhs
committed
add a test for forecast by status for the usage endpoint
1 parent b10eb4c commit a0f6ed5

File tree

4 files changed

+30
-9
lines changed

4 files changed

+30
-9
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/stats/ForecastStats.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,18 @@ public ForecastStats(StreamInput in) throws IOException {
6969
this.statusCounts = new CountAccumulator(in);
7070
}
7171

72-
public void merge(ForecastStats other) {
72+
public ForecastStats merge(ForecastStats other) {
7373
if (other == null) {
74-
return;
74+
return this;
7575
}
7676
total += other.total;
7777
forecastedJobs += other.forecastedJobs;
7878
memoryStats.merge(other.memoryStats);
7979
recordStats.merge(other.recordStats);
8080
runtimeStats.merge(other.runtimeStats);
8181
statusCounts.merge(other.statusCounts);
82+
83+
return this;
8284
}
8385

8486
@Override

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/stats/ForecastStatsTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ protected Reader<ForecastStats> instanceReader() {
236236
return ForecastStats::new;
237237
}
238238

239-
private ForecastStats createForecastStats(long minTotal, long maxTotal) {
239+
public ForecastStats createForecastStats(long minTotal, long maxTotal) {
240240
ForecastStats forecastStats = new ForecastStats(randomLongBetween(minTotal, maxTotal), createStatsAccumulator(),
241241
createStatsAccumulator(), createStatsAccumulator(), createCountAccumulator());
242242

x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MachineLearningFeatureSet.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,7 @@ private void addJobsUsage(GetJobsStatsAction.Response response) {
219219
js -> new StatsAccumulator()).add(detectorsCount);
220220
modelSizeStatsByState.computeIfAbsent(jobState,
221221
js -> new StatsAccumulator()).add(modelSize);
222-
forecastStatsByState.computeIfAbsent(jobState,
223-
js -> new ForecastStats()).merge(jobStats.getForecastStats());
222+
forecastStatsByState.merge(jobState, jobStats.getForecastStats(), (f1, f2) -> f1.merge(f2));
224223
}
225224

226225
jobsUsage.put(MachineLearningFeatureSetUsage.ALL, createJobUsageEntry(jobs.size(), allJobsDetectorsStats,

x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/MachineLearningFeatureSetTests.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
import org.elasticsearch.xpack.core.ml.job.config.Job;
4040
import org.elasticsearch.xpack.core.ml.job.config.JobState;
4141
import org.elasticsearch.xpack.core.ml.job.process.autodetect.state.ModelSizeStats;
42+
import org.elasticsearch.xpack.core.ml.stats.ForecastStats;
43+
import org.elasticsearch.xpack.core.ml.stats.ForecastStatsTests;
44+
import org.elasticsearch.xpack.core.ml.stats.StatsAccumulatorTests;
4245
import org.elasticsearch.xpack.core.watcher.support.xcontent.XContentSource;
4346
import org.junit.Before;
4447

@@ -138,11 +141,11 @@ public void testUsage() throws Exception {
138141
settings.put("xpack.ml.enabled", true);
139142

140143
Job opened1 = buildJob("opened1", Arrays.asList(buildMinDetector("foo")));
141-
GetJobsStatsAction.Response.JobStats opened1JobStats = buildJobStats("opened1", JobState.OPENED, 100L);
144+
GetJobsStatsAction.Response.JobStats opened1JobStats = buildJobStats("opened1", JobState.OPENED, 100L, 3L);
142145
Job opened2 = buildJob("opened2", Arrays.asList(buildMinDetector("foo"), buildMinDetector("bar")));
143-
GetJobsStatsAction.Response.JobStats opened2JobStats = buildJobStats("opened2", JobState.OPENED, 200L);
146+
GetJobsStatsAction.Response.JobStats opened2JobStats = buildJobStats("opened2", JobState.OPENED, 200L, 8L);
144147
Job closed1 = buildJob("closed1", Arrays.asList(buildMinDetector("foo"), buildMinDetector("bar"), buildMinDetector("foobar")));
145-
GetJobsStatsAction.Response.JobStats closed1JobStats = buildJobStats("closed1", JobState.CLOSED, 300L);
148+
GetJobsStatsAction.Response.JobStats closed1JobStats = buildJobStats("closed1", JobState.CLOSED, 300L, 0);
146149
givenJobs(Arrays.asList(opened1, opened2, closed1),
147150
Arrays.asList(opened1JobStats, opened2JobStats, closed1JobStats));
148151

@@ -210,6 +213,15 @@ public void testUsage() throws Exception {
210213
assertThat(source.getValue("datafeeds._all.count"), equalTo(3));
211214
assertThat(source.getValue("datafeeds.started.count"), equalTo(2));
212215
assertThat(source.getValue("datafeeds.stopped.count"), equalTo(1));
216+
217+
assertThat(source.getValue("jobs._all.forecasts.total"), equalTo(11));
218+
assertThat(source.getValue("jobs._all.forecasts.forecasted_jobs"), equalTo(2));
219+
220+
assertThat(source.getValue("jobs.closed.forecasts.total"), equalTo(0));
221+
assertThat(source.getValue("jobs.closed.forecasts.forecasted_jobs"), equalTo(0));
222+
223+
assertThat(source.getValue("jobs.opened.forecasts.total"), equalTo(11));
224+
assertThat(source.getValue("jobs.opened.forecasts.forecasted_jobs"), equalTo(2));
213225
}
214226
}
215227

@@ -301,12 +313,16 @@ private static Job buildJob(String jobId, List<Detector> detectors) {
301313
.build(new Date(randomNonNegativeLong()));
302314
}
303315

304-
private static GetJobsStatsAction.Response.JobStats buildJobStats(String jobId, JobState state, long modelBytes) {
316+
private static GetJobsStatsAction.Response.JobStats buildJobStats(String jobId, JobState state, long modelBytes,
317+
long numberOfForecasts) {
305318
ModelSizeStats.Builder modelSizeStats = new ModelSizeStats.Builder(jobId);
306319
modelSizeStats.setModelBytes(modelBytes);
307320
GetJobsStatsAction.Response.JobStats jobStats = mock(GetJobsStatsAction.Response.JobStats.class);
321+
ForecastStats forecastStats = buildForecastStats(numberOfForecasts);
322+
308323
when(jobStats.getJobId()).thenReturn(jobId);
309324
when(jobStats.getModelSizeStats()).thenReturn(modelSizeStats.build());
325+
when(jobStats.getForecastStats()).thenReturn(forecastStats);
310326
when(jobStats.getState()).thenReturn(state);
311327
return jobStats;
312328
}
@@ -316,4 +332,8 @@ private static GetDatafeedsStatsAction.Response.DatafeedStats buildDatafeedStats
316332
when(stats.getDatafeedState()).thenReturn(state);
317333
return stats;
318334
}
335+
336+
private static ForecastStats buildForecastStats(long numberOfForecasts) {
337+
return new ForecastStatsTests().createForecastStats(numberOfForecasts, numberOfForecasts);
338+
}
319339
}

0 commit comments

Comments
 (0)