-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ML] Extract job selection resolver (#95394)
- Loading branch information
Showing
2 changed files
with
109 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
x-pack/plugins/ml/public/embeddables/common/resolve_job_selection.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { CoreStart } from 'kibana/public'; | ||
import moment from 'moment'; | ||
import { takeUntil } from 'rxjs/operators'; | ||
import { from } from 'rxjs'; | ||
import React from 'react'; | ||
import { getInitialGroupsMap } from '../../application/components/job_selector/job_selector'; | ||
import { | ||
KibanaContextProvider, | ||
toMountPoint, | ||
} from '../../../../../../src/plugins/kibana_react/public'; | ||
import { getMlGlobalServices } from '../../application/app'; | ||
import { JobSelectorFlyoutContent } from '../../application/components/job_selector/job_selector_flyout'; | ||
import { DashboardConstants } from '../../../../../../src/plugins/dashboard/public'; | ||
import { JobId } from '../../../common/types/anomaly_detection_jobs'; | ||
|
||
/** | ||
* Handles Anomaly detection jobs selection by a user. | ||
* Intended to use independently of the ML app context, | ||
* for instance on the dashboard for embeddables initialization. | ||
* | ||
* @param coreStart | ||
* @param selectedJobIds | ||
*/ | ||
export async function resolveJobSelection( | ||
coreStart: CoreStart, | ||
selectedJobIds?: JobId[] | ||
): Promise<{ jobIds: string[]; groups: Array<{ groupId: string; jobIds: string[] }> }> { | ||
const { | ||
http, | ||
uiSettings, | ||
application: { currentAppId$ }, | ||
} = coreStart; | ||
|
||
return new Promise(async (resolve, reject) => { | ||
const maps = { | ||
groupsMap: getInitialGroupsMap([]), | ||
jobsMap: {}, | ||
}; | ||
|
||
const tzConfig = uiSettings.get('dateFormat:tz'); | ||
const dateFormatTz = tzConfig !== 'Browser' ? tzConfig : moment.tz.guess(); | ||
|
||
const flyoutSession = coreStart.overlays.openFlyout( | ||
toMountPoint( | ||
<KibanaContextProvider services={{ ...coreStart, mlServices: getMlGlobalServices(http) }}> | ||
<JobSelectorFlyoutContent | ||
selectedIds={selectedJobIds} | ||
withTimeRangeSelector={false} | ||
dateFormatTz={dateFormatTz} | ||
singleSelection={false} | ||
timeseriesOnly={true} | ||
onFlyoutClose={() => { | ||
flyoutSession.close(); | ||
reject(); | ||
}} | ||
onSelectionConfirmed={async ({ jobIds, groups }) => { | ||
await flyoutSession.close(); | ||
resolve({ jobIds, groups }); | ||
}} | ||
maps={maps} | ||
/> | ||
</KibanaContextProvider> | ||
), | ||
{ | ||
'data-test-subj': 'mlFlyoutJobSelector', | ||
ownFocus: true, | ||
closeButtonAriaLabel: 'jobSelectorFlyout', | ||
} | ||
); | ||
|
||
// Close the flyout when user navigates out of the dashboard plugin | ||
currentAppId$.pipe(takeUntil(from(flyoutSession.onClose))).subscribe((appId) => { | ||
if (appId !== DashboardConstants.DASHBOARDS_ID) { | ||
flyoutSession.close(); | ||
} | ||
}); | ||
}); | ||
} |