forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ML] Fix query bar autocompletion for ML and AIOps embeddables (elast…
…ic#164485) (cherry picked from commit 5d5ac37)
- Loading branch information
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