@@ -337,6 +337,46 @@ public void testUsage() throws Exception {
337
337
}
338
338
}
339
339
340
+ public void testUsageWithOrphanedTask () throws Exception {
341
+ when (licenseState .isAllowed (XPackLicenseState .Feature .MACHINE_LEARNING )).thenReturn (true );
342
+ Settings .Builder settings = Settings .builder ().put (commonSettings );
343
+ settings .put ("xpack.ml.enabled" , true );
344
+
345
+ Job opened1 = buildJob ("opened1" , Collections .singletonList (buildMinDetector ("foo" )),
346
+ Collections .singletonMap ("created_by" , randomFrom ("a-cool-module" , "a_cool_module" , "a cool module" )));
347
+ GetJobsStatsAction .Response .JobStats opened1JobStats = buildJobStats ("opened1" , JobState .OPENED , 100L , 3L );
348
+ // NB: we have JobStats but no Job for "opened2"
349
+ GetJobsStatsAction .Response .JobStats opened2JobStats = buildJobStats ("opened2" , JobState .OPENED , 200L , 8L );
350
+ Job closed1 = buildJob ("closed1" , Arrays .asList (buildMinDetector ("foo" ), buildMinDetector ("bar" ), buildMinDetector ("foobar" )));
351
+ GetJobsStatsAction .Response .JobStats closed1JobStats = buildJobStats ("closed1" , JobState .CLOSED , 300L , 0 );
352
+ givenJobs (Arrays .asList (opened1 , closed1 ), Arrays .asList (opened1JobStats , opened2JobStats , closed1JobStats ));
353
+
354
+ MachineLearningFeatureSet featureSet = new MachineLearningFeatureSet (TestEnvironment .newEnvironment (settings .build ()),
355
+ clusterService , client , licenseState , jobManagerHolder );
356
+ PlainActionFuture <Usage > future = new PlainActionFuture <>();
357
+ featureSet .usage (future );
358
+ XPackFeatureSet .Usage usage = future .get ();
359
+
360
+ XContentSource source ;
361
+ try (XContentBuilder builder = XContentFactory .jsonBuilder ()) {
362
+ usage .toXContent (builder , ToXContent .EMPTY_PARAMS );
363
+ source = new XContentSource (builder );
364
+ }
365
+
366
+ // The orphaned job should be excluded from the usage info
367
+ assertThat (source .getValue ("jobs._all.count" ), equalTo (2 ));
368
+ assertThat (source .getValue ("jobs._all.detectors.min" ), equalTo (1.0 ));
369
+ assertThat (source .getValue ("jobs._all.detectors.max" ), equalTo (3.0 ));
370
+ assertThat (source .getValue ("jobs._all.detectors.total" ), equalTo (4.0 ));
371
+ assertThat (source .getValue ("jobs._all.detectors.avg" ), equalTo (2.0 ));
372
+ assertThat (source .getValue ("jobs._all.model_size.min" ), equalTo (100.0 ));
373
+ assertThat (source .getValue ("jobs._all.model_size.max" ), equalTo (300.0 ));
374
+ assertThat (source .getValue ("jobs._all.model_size.total" ), equalTo (400.0 ));
375
+ assertThat (source .getValue ("jobs._all.model_size.avg" ), equalTo (200.0 ));
376
+ assertThat (source .getValue ("jobs._all.created_by.a_cool_module" ), equalTo (1 ));
377
+ assertThat (source .getValue ("jobs._all.created_by.unknown" ), equalTo (1 ));
378
+ }
379
+
340
380
public void testUsageDisabledML () throws Exception {
341
381
when (licenseState .isAllowed (XPackLicenseState .Feature .MACHINE_LEARNING )).thenReturn (true );
342
382
Settings .Builder settings = Settings .builder ().put (commonSettings );
0 commit comments