@@ -10,7 +10,7 @@ import { replaceVariables } from 'utils';
1010
1111import { MetricFindQuery , RequestSpec } from './RequestSpec' ;
1212import RestApiBackend from './backend/rest' ;
13- import { Backend } from './backend/types' ;
13+ import { BACKEND_TYPE , Backend } from './backend/types' ;
1414import WebApiBackend from './backend/web' ;
1515import { Settings } from './settings' ;
1616import { Backend as BackendType , CmkQuery , DataSourceOptions , Edition , ResponseDataAutocomplete } from './types' ;
@@ -22,6 +22,7 @@ export class DataSource extends DataSourceApi<CmkQuery> {
2222 webBackend : WebApiBackend ;
2323 restBackend : RestApiBackend ;
2424 settings : Settings ;
25+ autocompleteBackend : BACKEND_TYPE | null = null ;
2526
2627 constructor ( private instanceSettings : DataSourceInstanceSettings < DataSourceOptions > ) {
2728 super ( instanceSettings ) ;
@@ -30,14 +31,18 @@ export class DataSource extends DataSourceApi<CmkQuery> {
3031 this . settings = new Settings ( instanceSettings . jsonData ) ;
3132 }
3233
34+ protected async setAutocompleteBackend ( ) {
35+ this . autocompleteBackend = await this . getBackend ( ) . getAutocompleteBackend ( ) ;
36+ }
37+
3338 async query ( dataQueryRequest : DataQueryRequest < CmkQuery > ) : Promise < DataQueryResponse > {
3439 for ( const target of dataQueryRequest . targets ) {
3540 target . requestSpec = replaceVariables ( target . requestSpec , dataQueryRequest . scopedVars ) ;
3641 }
3742 return this . getBackend ( ) . query ( dataQueryRequest ) ;
3843 }
3944
40- async metricFindQuery ( query : MetricFindQuery , options ?: any ) : Promise < MetricFindValue [ ] > {
45+ async metricFindQuery ( query : MetricFindQuery , options ?: unknown ) : Promise < MetricFindValue [ ] > {
4146 if ( query . objectType === 'site' ) {
4247 // rest-api site endpoint were added in 2.2.0 so we have to use the web-api here
4348 // TODO: clean up (remove filterSites from Backend) with end of 2.1.0
@@ -51,8 +56,17 @@ export class DataSource extends DataSourceApi<CmkQuery> {
5156 return this . getBackend ( ) . testDatasource ( ) ;
5257 }
5358
54- async autocompleterRequest < T > ( api_url : string , data : unknown ) : Promise < FetchResponse < WebApiResponse < T > > > {
55- return this . webBackend . autocompleterRequest ( api_url , data ) ;
59+ async autocompleterRequest (
60+ api_url : string ,
61+ data : unknown
62+ ) : Promise < FetchResponse < WebApiResponse < ResponseDataAutocomplete > > > {
63+ this . autocompleteBackend === null && ( await this . setAutocompleteBackend ( ) ) ;
64+
65+ if ( this . autocompleteBackend === BACKEND_TYPE . WEB ) {
66+ return this . webBackend . autocompleterRequest ( api_url , data ) ;
67+ }
68+
69+ return this . restBackend . autocompleterRequest ( api_url , data ) ;
5670 }
5771
5872 async contextAutocomplete (
@@ -64,10 +78,13 @@ export class DataSource extends DataSourceApi<CmkQuery> {
6478 if ( ident === 'label' && this . getBackendType ( ) === 'web' ) {
6579 // we have a 2.1.0 version without werk #15074 so label autocompleter is a special edge case
6680 // can be removed after we stop supporting 2.1.0
67- const response = await this . autocompleterRequest < Array < { value : string } > > ( 'ajax_autocomplete_labels.py' , {
68- world : params . world ,
69- search_label : prefix ,
70- } ) ;
81+ const response = await this . webBackend . autocompleterRequest < Array < { value : string } > > (
82+ 'ajax_autocomplete_labels.py' ,
83+ {
84+ world : params . world ,
85+ search_label : prefix ,
86+ }
87+ ) ;
7188 return response . data . result . map ( ( val : { value : string } ) => ( {
7289 value : val . value ,
7390 label : val . value ,
@@ -78,7 +95,7 @@ export class DataSource extends DataSourceApi<CmkQuery> {
7895 replaceVariables ( partialRequestSpec ) ,
7996 this . getBackendType ( ) === 'rest' ? 'latest' : '2.1.0'
8097 ) ;
81- const response = await this . autocompleterRequest < ResponseDataAutocomplete > ( 'ajax_vs_autocomplete.py' , {
98+ const response = await this . autocompleterRequest ( 'ajax_vs_autocomplete.py' , {
8299 ident,
83100 value : prefix ,
84101 params : {
0 commit comments