-
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.
[8.10] [ML] Fix query bar autocompletion for ML and AIOps embeddables (…
…#164485) (#164647) # Backport This will backport the following commits from `main` to `8.10`: - [[ML] Fix query bar autocompletion for ML and AIOps embeddables (#164485)](#164485) <!--- Backport version: 8.9.7 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) <!--BACKPORT [{"author":{"name":"Dima Arnautov","email":"[email protected]"},"sourceCommit":{"committedDate":"2023-08-23T22:08:23Z","message":"[ML] Fix query bar autocompletion for ML and AIOps embeddables (#164485)","sha":"5d5ac37e9e7a36f359a78d1af7a89ce324c3194f","branchLabelMapping":{"^v8.11.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:fix",":ml","Feature:Anomaly Detection","Team:ML","v8.10.0","Feature:Embeddables","v8.11.0"],"number":164485,"url":"https://github.com/elastic/kibana/pull/164485","mergeCommit":{"message":"[ML] Fix query bar autocompletion for ML and AIOps embeddables (#164485)","sha":"5d5ac37e9e7a36f359a78d1af7a89ce324c3194f"}},"sourceBranch":"main","suggestedTargetBranches":["8.10"],"targetPullRequestStates":[{"branch":"8.10","label":"v8.10.0","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"main","label":"v8.11.0","labelRegex":"^v8.11.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/164485","number":164485,"mergeCommit":{"message":"[ML] Fix query bar autocompletion for ML and AIOps embeddables (#164485)","sha":"5d5ac37e9e7a36f359a78d1af7a89ce324c3194f"}}]}] BACKPORT--> Co-authored-by: Dima Arnautov <[email protected]>
- Loading branch information
1 parent
c94b0f8
commit 916b1eb
Showing
6 changed files
with
116 additions
and
73 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
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
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
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
84 changes: 84 additions & 0 deletions
84
x-pack/plugins/ml/public/embeddables/common/anomaly_detection_embeddable.ts
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,84 @@ | ||
/* | ||
* 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 { | ||
Embeddable, | ||
type EmbeddableInput, | ||
type EmbeddableOutput, | ||
type IContainer, | ||
} from '@kbn/embeddable-plugin/public'; | ||
import { type DataView } from '@kbn/data-views-plugin/common'; | ||
import { type DataViewsContract } from '@kbn/data-views-plugin/public'; | ||
import { firstValueFrom } from 'rxjs'; | ||
import { type AnomalyDetectorService } from '../../application/services/anomaly_detector_service'; | ||
|
||
export type CommonInput = { jobIds: string[] } & EmbeddableInput; | ||
|
||
export type CommonOutput = { indexPatterns?: DataView[] } & EmbeddableOutput; | ||
|
||
export abstract class AnomalyDetectionEmbeddable< | ||
Input extends CommonInput, | ||
Output extends CommonOutput | ||
> extends Embeddable<Input, Output> { | ||
// Need to defer embeddable load in order to resolve data views | ||
deferEmbeddableLoad = true; | ||
|
||
protected constructor( | ||
initialInput: Input, | ||
private anomalyDetectorService: AnomalyDetectorService, | ||
private dataViewsService: DataViewsContract, | ||
parent?: IContainer | ||
) { | ||
super( | ||
initialInput, | ||
{ | ||
defaultTitle: initialInput.title, | ||
defaultDescription: initialInput.description, | ||
} as Output, | ||
parent | ||
); | ||
|
||
this.initializeOutput(initialInput).finally(() => { | ||
this.setInitializationFinished(); | ||
}); | ||
} | ||
|
||
protected async initializeOutput(initialInput: CommonInput) { | ||
const { jobIds } = initialInput; | ||
|
||
try { | ||
const jobs = await firstValueFrom(this.anomalyDetectorService.getJobs$(jobIds)); | ||
|
||
// First get list of unique indices from the selected jobs | ||
const indices = new Set(jobs.map((j) => j.datafeed_config!.indices).flat()); | ||
// Then find the data view assuming the data view title matches the index name | ||
const indexPatterns: Record<string, DataView> = {}; | ||
for (const indexName of indices) { | ||
const response = await this.dataViewsService.find(`"${indexName}"`); | ||
const indexPattern = response.find((obj) => | ||
obj.getIndexPattern().toLowerCase().includes(indexName.toLowerCase()) | ||
); | ||
|
||
if (indexPattern !== undefined) { | ||
indexPatterns[indexPattern.id!] = indexPattern; | ||
} | ||
} | ||
|
||
this.updateOutput({ | ||
...this.getOutput(), | ||
indexPatterns: Object.values(indexPatterns), | ||
}); | ||
} catch (e) { | ||
// Unable to find and load data view but we can ignore the error | ||
// as we only load it to support the filter & query bar | ||
// the visualizations should still work correctly | ||
|
||
// eslint-disable-next-line no-console | ||
console.error(`Unable to load data views for ${jobIds}`, e); | ||
} | ||
} | ||
} |
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