11import { AppEvents , createDataFrame , DataFrameDTO , DataQueryRequest , DataSourceInstanceSettings , dateTime , FieldDTO , FieldType , LegacyMetricFindQueryOptions , MetricFindValue , QueryResultMetaNotice , ScopedVars , TimeRange } from "@grafana/data" ;
22import { DataFrameDataSourceBase } from "../../DataFrameDataSourceBase" ;
33import { BackendSrv , getBackendSrv , TemplateSrv , getTemplateSrv } from "@grafana/runtime" ;
4- import { Column , Option , DataFrameDataQuery , DataFrameDataSourceOptions , DataFrameQueryType , DataFrameQueryV2 , DataFrameVariableQuery , DataFrameVariableQueryType , DataTableProjectionLabelLookup , DataTableProjections , DataTableProperties , defaultQueryV2 , defaultVariableQueryV2 , FlattenedTableProperties , TableDataRows , TableProperties , TablePropertiesList , ValidDataFrameQueryV2 , ValidDataFrameVariableQuery , DataFrameQueryV1 , DecimatedDataRequest , UndecimatedDataRequest , ColumnFilter , CombinedFilters , QueryResultsResponse , ColumnOptions , ColumnType , TableColumnsData , ColumnWithDisplayName , ColumnDataType , metadataFieldOptions , DATA_TABLE_NAME_FIELD , DATA_TABLE_ID_FIELD , DATA_TABLE_NAME_LABEL , DATA_TABLE_ID_LABEL , CustomPropertyOptions , PropertySelections , ALL_STANDARD_PROPERTIES , PropertiesQueryCache } from "../../types" ;
5- import { COLUMN_OPTIONS_LIMIT , COLUMN_SELECTION_LIMIT , COLUMNS_GROUP , CUSTOM_PROPERTY_COLUMNS_LIMIT , DELAY_BETWEEN_REQUESTS_MS , FLOAT32_MAX , FLOAT32_MIN , FLOAT64_MAX , FLOAT64_MIN , INT32_MAX , INT32_MIN , INT64_MAX , INT64_MIN , X_COLUMN_RANGE_DECIMAL_PRECISION , INTEGER_DATA_TYPES , NUMERIC_DATA_TYPES , POSSIBLE_UNIT_CUSTOM_PROPERTY_KEYS , REQUESTS_PER_SECOND , RESULT_IDS_LIMIT , TAKE_LIMIT , MAXIMUM_DATA_POINTS , UNDECIMATED_RECORDS_LIMIT , CUSTOM_COLUMN_PROPERTIES_GROUP , CUSTOM_DATA_TABLE_PROPERTIES_GROUP , CUSTOM_PROPERTY_SUFFIX , propertiesCacheTTL } from "datasources/data-frame/constants" ;
4+ import { Column , Option , DataFrameDataQuery , DataFrameDataSourceOptions , DataFrameQueryType , DataFrameQueryV2 , DataFrameVariableQuery , DataFrameVariableQueryType , DataTableProjectionLabelLookup , DataTableProjections , DataTableProperties , defaultQueryV2 , defaultVariableQueryV2 , FlattenedTableProperties , TableDataRows , TableProperties , TablePropertiesList , ValidDataFrameQueryV2 , ValidDataFrameVariableQuery , DataFrameQueryV1 , DecimatedDataRequest , UndecimatedDataRequest , ColumnFilter , CombinedFilters , DataFrameQueryResultsResponse , ColumnOptions , ColumnType , TableColumnsData , ColumnWithDisplayName , ColumnDataType , metadataFieldOptions , DATA_TABLE_NAME_FIELD , DATA_TABLE_ID_FIELD , DATA_TABLE_NAME_LABEL , DATA_TABLE_ID_LABEL , CustomPropertyOptions , PropertySelections , ALL_STANDARD_PROPERTIES , PropertiesQueryCache , DataFrameResultsResponseProperties } from "../../types" ;
5+ import { COLUMN_OPTIONS_LIMIT , COLUMN_SELECTION_LIMIT , COLUMNS_GROUP , CUSTOM_PROPERTY_COLUMNS_LIMIT , DELAY_BETWEEN_REQUESTS_MS , FLOAT32_MAX , FLOAT32_MIN , FLOAT64_MAX , FLOAT64_MIN , INT32_MAX , INT32_MIN , INT64_MAX , INT64_MIN , X_COLUMN_RANGE_DECIMAL_PRECISION , INTEGER_DATA_TYPES , NUMERIC_DATA_TYPES , POSSIBLE_UNIT_CUSTOM_PROPERTY_KEYS , REQUESTS_PER_SECOND , RESULT_IDS_LIMIT , TAKE_LIMIT , MAXIMUM_DATA_POINTS , UNDECIMATED_RECORDS_LIMIT , CUSTOM_COLUMN_PROPERTIES_GROUP , CUSTOM_DATA_TABLE_PROPERTIES_GROUP , CUSTOM_PROPERTY_SUFFIX , propertiesCacheTTL , DATA_TABLES_IDS_LIMIT } from "datasources/data-frame/constants" ;
66import { ExpressionTransformFunction , listFieldsQuery , multipleValuesQuery , timeFieldsQuery , transformComputedFieldsQuery } from "core/query-builder.utils" ;
77import { LEGACY_METADATA_TYPE , Workspace } from "core/types" ;
88import { extractErrorInfo } from "core/errors" ;
@@ -196,28 +196,52 @@ export class DataFrameDataSourceV2 extends DataFrameDataSourceBase {
196196 take ?: number ,
197197 projections ?: DataTableProjections [ ]
198198 ) : Observable < TableProperties [ ] > {
199- if ( filters . resultFilter ) {
200- return this . queryResultIds$ ( filters . resultFilter ) . pipe (
201- switchMap ( resultIds => {
202- if ( resultIds . length === 0 ) {
203- return of ( [ ] ) ;
204- }
205- const resultFilter = this . buildResultIdFilter ( resultIds ) ;
206- const combinedFilter = this . buildCombinedFilter ( {
207- resultFilter,
208- dataTableFilter : filters . dataTableFilter ,
209- columnFilter : filters . columnFilter
210- } ) ;
211- return this . queryTablesInternal$ (
212- combinedFilter ,
213- take ,
214- projections ,
215- resultIds
216- ) ;
217- } )
218- ) ;
199+ const filterAndSubstitutions$ = this . buildQueryTablesFilterAndSubstitutions$ ( filters ) ;
200+ return filterAndSubstitutions$ . pipe (
201+ switchMap ( filterAndSubstitutions => {
202+ if ( ! filterAndSubstitutions ) {
203+ return of ( [ ] ) ;
204+ }
205+
206+ return this . queryTablesInternal$ (
207+ filterAndSubstitutions . filter ,
208+ take ,
209+ projections ,
210+ filterAndSubstitutions . substitutions
211+ ) ;
212+ } )
213+ ) ;
214+ }
215+
216+ private buildQueryTablesFilterAndSubstitutions$ (
217+ filters : CombinedFilters
218+ ) : Observable < { filter : string ; substitutions ?: string [ ] } | null > {
219+ if ( ! filters . resultFilter ) {
220+ return of ( {
221+ filter : filters . dataTableFilter || ''
222+ } ) ;
219223 }
220- return this . queryTablesInternal$ ( filters . dataTableFilter || '' , take , projections , undefined ) ;
224+
225+ const results$ = this . queryResults$ ( filters . resultFilter ) ;
226+ return results$ . pipe (
227+ map ( results => {
228+ if ( results . length === 0 ) {
229+ return null ;
230+ }
231+
232+ const {
233+ filter : resultFilter ,
234+ substitutions
235+ } = this . buildResultFilter ( results ) ;
236+ const filter = this . buildCombinedFilter ( {
237+ resultFilter,
238+ dataTableFilter : filters . dataTableFilter ,
239+ columnFilter : filters . columnFilter
240+ } ) ;
241+
242+ return { filter, substitutions } ;
243+ } )
244+ ) ;
221245 }
222246
223247 private queryTablesInternal$ (
@@ -2219,17 +2243,19 @@ export class DataFrameDataSourceV2 extends DataFrameDataSourceBase {
22192243 return propertyKeysSet ;
22202244 }
22212245
2222- private queryResultIds$ ( resultFilter : string ) : Observable < string [ ] > {
2246+ private queryResults$ (
2247+ resultFilter : string
2248+ ) : Observable < DataFrameResultsResponseProperties [ ] > {
22232249 const queryResultsUrl = `${ this . instanceSettings . url } /nitestmonitor/v2/query-results` ;
22242250 const requestBody = {
22252251 filter : resultFilter ,
2226- projection : [ 'id' ] ,
2252+ projection : [ 'id' , 'dataTableIds' ] ,
22272253 take : RESULT_IDS_LIMIT ,
22282254 orderBy : 'UPDATED_AT' ,
22292255 descending : true
22302256 } ;
22312257
2232- return this . post$ < QueryResultsResponse > (
2258+ return this . post$ < DataFrameQueryResultsResponse > (
22332259 queryResultsUrl ,
22342260 requestBody ,
22352261 { showErrorAlert : false }
@@ -2238,7 +2264,7 @@ export class DataFrameDataSourceV2 extends DataFrameDataSourceBase {
22382264 if ( ! response . results || response . results . length === 0 ) {
22392265 return [ ] ;
22402266 }
2241- return response . results . map ( result => result . id ) ;
2267+ return response . results ;
22422268 } ) ,
22432269 catchError ( error => {
22442270 const errorMessage = this . getErrorMessage ( error , 'results' ) ;
@@ -2251,13 +2277,51 @@ export class DataFrameDataSourceV2 extends DataFrameDataSourceBase {
22512277 ) ;
22522278 }
22532279
2254- private buildResultIdFilter ( resultIds : string [ ] ) : string {
2255- if ( resultIds . length === 0 ) {
2256- return '' ;
2280+ private buildResultFilter (
2281+ results : DataFrameResultsResponseProperties [ ]
2282+ ) : { filter : string ; substitutions ?: string [ ] } {
2283+ if ( results . length === 0 ) {
2284+ return { filter : '' } ;
22572285 }
2258- const placeholders = resultIds . map ( ( _ , index ) => `@${ index } ` ) . join ( ',' ) ;
2259- const resultFilter = `new[]{${ placeholders } }.Contains(testResultId)` ;
2260- return resultFilter ;
2286+
2287+ const {
2288+ resultIds,
2289+ dataTableIds
2290+ } = this . extractResultAndDataTableIds ( results ) ;
2291+
2292+ const resultIdFilter = this . buildPlaceholderContainsFilter (
2293+ 'testResultId' ,
2294+ resultIds . length ,
2295+ 0
2296+ ) ;
2297+ if ( dataTableIds . length === 0 ) {
2298+ return {
2299+ filter : resultIdFilter ,
2300+ substitutions : resultIds
2301+ } ;
2302+ }
2303+
2304+ const dataTableIdFilter = this . buildPlaceholderContainsFilter (
2305+ 'id' ,
2306+ dataTableIds . length ,
2307+ resultIds . length
2308+ ) ;
2309+ return {
2310+ filter : `(${ resultIdFilter } )||(${ dataTableIdFilter } )` ,
2311+ substitutions : [ ...resultIds , ...dataTableIds ]
2312+ } ;
2313+ }
2314+
2315+ private buildPlaceholderContainsFilter (
2316+ fieldName : string ,
2317+ count : number ,
2318+ startIndex : number
2319+ ) : string {
2320+ const placeholders = Array . from (
2321+ { length : count } ,
2322+ ( _ , index ) => `@${ startIndex + index } `
2323+ ) . join ( ',' ) ;
2324+ return `new[]{${ placeholders } }.Contains(${ fieldName } )` ;
22612325 }
22622326
22632327 private buildCombinedFilter ( filters : CombinedFilters ) : string {
@@ -2334,4 +2398,22 @@ export class DataFrameDataSourceV2 extends DataFrameDataSourceBase {
23342398
23352399 return defaultQueryV2 . filterXRangeOnZoomPan ;
23362400 }
2401+
2402+ private extractResultAndDataTableIds (
2403+ results : DataFrameResultsResponseProperties [ ]
2404+ ) : { resultIds : string [ ] ; dataTableIds : string [ ] } {
2405+ const resultIds : string [ ] = [ ] ;
2406+ const dataTableIdSet = new Set < string > ( ) ;
2407+
2408+ for ( const { id, dataTableIds } of results ) {
2409+ resultIds . push ( id ) ;
2410+ dataTableIds ?. forEach ( dataTableId => {
2411+ if ( dataTableIdSet . size < DATA_TABLES_IDS_LIMIT ) {
2412+ dataTableIdSet . add ( dataTableId ) ;
2413+ }
2414+ } ) ;
2415+ }
2416+
2417+ return { resultIds, dataTableIds : [ ...dataTableIdSet ] } ;
2418+ }
23372419}
0 commit comments