@@ -4,20 +4,13 @@ import {
44 DataQueryRequest ,
55 DataFrame ,
66 Field ,
7- MetricFindValue ,
87 getDefaultTimeRange ,
9- FieldType ,
10- CustomVariableSupport ,
11- DataQueryResponse ,
12- QueryEditorProps ,
138} from '@grafana/data' ;
149import { DataSourceWithBackend , getTemplateSrv } from '@grafana/runtime' ;
1510
16- import { HaystackQuery , OpsQuery , HaystackDataSourceOptions , HaystackVariableQuery , QueryType } from './types' ;
17- import { firstValueFrom , map , Observable } from 'rxjs' ;
18- import { isRef , parseRef } from 'haystack' ;
19- import { ComponentType } from 'react' ;
20- import { VariableQueryEditor } from 'components/VariableQueryEditor' ;
11+ import { HaystackQuery , OpsQuery , HaystackDataSourceOptions , QueryType } from './types' ;
12+ import { firstValueFrom } from 'rxjs' ;
13+ import { HaystackVariableSupport } from 'HaystackVariableSupport' ;
2114
2215export const queryTypes : QueryType [ ] = [
2316 { label : 'Eval' , value : 'eval' , apiRequirements : [ 'eval' ] , description : 'Evaluate an Axon expression' } ,
@@ -34,9 +27,7 @@ export const queryTypes: QueryType[] = [
3427export class DataSource extends DataSourceWithBackend < HaystackQuery , HaystackDataSourceOptions > {
3528 constructor ( instanceSettings : DataSourceInstanceSettings < HaystackDataSourceOptions > ) {
3629 super ( instanceSettings ) ;
37- this . variables = new HaystackVariableSupport ( ( request ) => {
38- return this . query ( request )
39- } ) ;
30+ this . variables = new HaystackVariableSupport ( this ) ;
4031 }
4132
4233 // Queries the available ops from the datasource and returns the queryTypes that are supported.
@@ -115,82 +106,3 @@ export class DataSource extends DataSourceWithBackend<HaystackQuery, HaystackDat
115106 } ;
116107 }
117108}
118-
119- export class HaystackVariableSupport extends CustomVariableSupport < DataSource , HaystackVariableQuery , HaystackQuery , HaystackDataSourceOptions > {
120- editor : ComponentType < QueryEditorProps < DataSource , HaystackQuery , HaystackDataSourceOptions , HaystackVariableQuery > > ;
121-
122- // Requests data from the backend. This allows this class to reuse the DataSource.query method to get data.
123- onQuery : ( request : DataQueryRequest < HaystackVariableQuery > ) => Observable < DataQueryResponse > ;
124-
125- constructor ( onQuery : ( request : DataQueryRequest < HaystackVariableQuery > ) => Observable < DataQueryResponse > ) {
126- super ( ) ;
127- this . editor = VariableQueryEditor ;
128- this . onQuery = onQuery ;
129- }
130-
131- query ( request : DataQueryRequest < HaystackVariableQuery > ) : Observable < DataQueryResponse > {
132- let variableQuery = request . targets [ 0 ] ;
133- let observable = this . onQuery ( request ) ;
134- return observable . pipe (
135- map ( ( response ) => {
136- if ( response === undefined || response . errors !== undefined || response . data === undefined ) {
137- return response
138- }
139-
140- let variableValues = response . data . reduce ( ( acc : MetricFindValue [ ] , frame : DataFrame ) => {
141- // Default to the first field
142- let column = frame . fields [ 0 ] ;
143- if ( variableQuery . column !== undefined && variableQuery . column !== '' ) {
144- // If a column was input, match the column name
145- column = frame . fields . find ( ( field : Field ) => field . name === variableQuery . column ) ?? column ;
146- } else if ( frame . fields . some ( ( field : Field ) => field . name === 'id' ) ) {
147- // If there is an id column, use that
148- column = frame . fields . find ( ( field : Field ) => field . name === 'id' ) ?? column ;
149- }
150-
151- // Default to the selected column
152- let displayColumn = column ;
153- if ( variableQuery . displayColumn !== undefined && variableQuery . displayColumn !== '' ) {
154- // If a column was input, match the column name
155- displayColumn = frame . fields . find ( ( field : Field ) => {
156- return field . name === variableQuery . displayColumn
157- } ) ?? displayColumn ;
158- }
159-
160- let variableValues = column . values . map ( ( value , index ) => {
161- let variableValue = variableValueFromCell ( value , column . type ) ;
162-
163- let displayValue = displayColumn . values [ index ] ;
164- let variableText = variableTextFromCell ( displayValue , displayColumn . type ) ;
165-
166- return { text : variableText , value : variableValue } ;
167- } ) ;
168- return acc . concat ( variableValues ) ;
169- } , [ ] ) ;
170- return { ...response , data : variableValues } ;
171- } )
172- ) ;
173- }
174- }
175-
176-
177- function variableValueFromCell ( value : string , columnType : FieldType ) : string {
178- switch ( columnType ) {
179- case FieldType . string :
180- if ( isRef ( value ) ) {
181- return parseRef ( value ) . id ;
182- }
183- }
184- return value ;
185- }
186-
187- function variableTextFromCell ( value : string , columnType : FieldType ) : string {
188- switch ( columnType ) {
189- case FieldType . string :
190- if ( isRef ( value ) ) {
191- let ref = parseRef ( value ) ;
192- return ref . dis ?? ref . id ;
193- }
194- }
195- return value ;
196- }
0 commit comments