@@ -96,6 +96,7 @@ import {
96
96
getUISettings ,
97
97
getUiActions ,
98
98
getQueryService ,
99
+ getSavedObjectsClient ,
99
100
} from '../../../../public/services' ;
100
101
import { prettifyErrorMessage } from '../../../../server/utils/helpers' ;
101
102
import {
@@ -112,6 +113,12 @@ import { FLYOUT_MODES } from '../AnywhereParentFlyout/constants';
112
113
import { DetectorListItem } from '../../../../public/models/interfaces' ;
113
114
import { VisualizeEmbeddable } from '../../../../../../src/plugins/visualizations/public' ;
114
115
116
+ interface References {
117
+ id : string ;
118
+ name : string ;
119
+ type : string ;
120
+ }
121
+
115
122
function AddAnomalyDetector ( {
116
123
embeddable,
117
124
closeFlyout,
@@ -126,24 +133,42 @@ function AddAnomalyDetector({
126
133
VisualizeEmbeddable | ErrorEmbeddable
127
134
> ( ) ;
128
135
136
+ const indexPatternId = embeddable . vis . data . aggs . indexPattern . id ;
137
+ const [ dataSourceId , setDataSourceId ] = useState < string | undefined > ( undefined ) ;
138
+
139
+ async function getDataSourceId ( ) {
140
+ try {
141
+ const indexPattern = await getSavedObjectsClient ( ) . get ( 'index-pattern' , indexPatternId ) ;
142
+ const refs = indexPattern . references as References [ ] ;
143
+ const foundDataSourceId = refs . find ( ref => ref . type === 'data-source' ) ?. id ;
144
+ setDataSourceId ( foundDataSourceId ) ;
145
+ } catch ( error ) {
146
+ console . error ( "Error fetching index pattern:" , error ) ;
147
+ }
148
+ }
149
+
150
+ // useEffect to dispatch actions once dataSourceId fetch is complete
129
151
useEffect ( ( ) => {
130
- const getInitialIndices = async ( ) => {
131
- await dispatch ( getIndices ( queryText ) ) ;
132
- } ;
133
- getInitialIndices ( ) ;
134
- dispatch ( getMappings ( embeddable . vis . data . aggs . indexPattern . title ) ) ;
152
+ async function fetchData ( ) {
153
+ await getDataSourceId ( ) ;
135
154
136
- const createEmbeddable = async ( ) => {
155
+ const getIndicesDispatchCall = dispatch ( getIndices ( queryText , dataSourceId ) ) ;
156
+ const getMappingDispatchCall = dispatch ( getMappings ( embeddable . vis . data . aggs . indexPattern . title , dataSourceId ) ) ;
157
+ await Promise . all ( [ getIndicesDispatchCall , getMappingDispatchCall ] ) ;
158
+ }
159
+
160
+ async function createEmbeddable ( ) {
137
161
const visEmbeddable = await fetchVisEmbeddable (
138
162
embeddable . vis . id ,
139
163
getEmbeddable ( ) ,
140
164
getQueryService ( )
141
165
) ;
142
166
setGeneratedEmbeddable ( visEmbeddable ) ;
143
- } ;
144
-
167
+ }
168
+ fetchData ( ) ;
145
169
createEmbeddable ( ) ;
146
- } , [ ] ) ;
170
+ } , [ dataSourceId ] ) ;
171
+
147
172
const [ isShowVis , setIsShowVis ] = useState ( false ) ;
148
173
const [ accordionsOpen , setAccordionsOpen ] = useState ( { modelFeatures : true } ) ;
149
174
const [ detectorNameFromVis , setDetectorNameFromVis ] = useState (
@@ -310,6 +335,7 @@ function AddAnomalyDetector({
310
335
name : OVERLAY_ANOMALIES ,
311
336
args : {
312
337
detectorId : detectorId ,
338
+ dataSourceId : dataSourceId
313
339
} ,
314
340
} as VisLayerExpressionFn ;
315
341
@@ -338,7 +364,7 @@ function AddAnomalyDetector({
338
364
formikProps . setSubmitting ( true ) ;
339
365
try {
340
366
const detectorToCreate = formikToDetector ( formikProps . values ) ;
341
- await dispatch ( createDetector ( detectorToCreate ) )
367
+ await dispatch ( createDetector ( detectorToCreate , dataSourceId ) )
342
368
. then ( async ( response ) => {
343
369
dispatch ( startDetector ( response . response . id ) )
344
370
. then ( ( startDetectorResponse ) => { } )
@@ -410,7 +436,7 @@ function AddAnomalyDetector({
410
436
} ) ;
411
437
} )
412
438
. catch ( ( err : any ) => {
413
- dispatch ( getDetectorCount ( ) ) . then ( ( response : any ) => {
439
+ dispatch ( getDetectorCount ( dataSourceId ) ) . then ( ( response : any ) => {
414
440
const totalDetectors = get ( response , 'response.count' , 0 ) ;
415
441
if ( totalDetectors === MAX_DETECTORS ) {
416
442
notifications . toasts . addDanger (
@@ -517,7 +543,7 @@ function AddAnomalyDetector({
517
543
if ( error ) {
518
544
return error ;
519
545
}
520
- const resp = await dispatch ( matchDetector ( detectorName ) ) ;
546
+ const resp = await dispatch ( matchDetector ( detectorName , dataSourceId ) ) ;
521
547
const match = get ( resp , 'response.match' , false ) ;
522
548
if ( ! match ) {
523
549
return undefined ;
@@ -625,6 +651,7 @@ function AddAnomalyDetector({
625
651
embeddableVisId = { embeddable . vis . id }
626
652
selectedDetector = { selectedDetector }
627
653
setSelectedDetector = { setSelectedDetector }
654
+ dataSourceId = { dataSourceId }
628
655
> </ AssociateExisting >
629
656
) }
630
657
{ mode === FLYOUT_MODES . create && (
0 commit comments