3939import org .elasticsearch .xpack .core .ml .job .config .Job ;
4040import org .elasticsearch .xpack .core .ml .job .config .JobState ;
4141import 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 ;
4245import org .elasticsearch .xpack .core .watcher .support .xcontent .XContentSource ;
4346import 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