@@ -4,20 +4,13 @@ import {
4
4
DataQueryRequest ,
5
5
DataFrame ,
6
6
Field ,
7
- MetricFindValue ,
8
7
getDefaultTimeRange ,
9
- FieldType ,
10
- CustomVariableSupport ,
11
- DataQueryResponse ,
12
- QueryEditorProps ,
13
8
} from '@grafana/data' ;
14
9
import { DataSourceWithBackend , getTemplateSrv } from '@grafana/runtime' ;
15
10
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' ;
21
14
22
15
export const queryTypes : QueryType [ ] = [
23
16
{ label : 'Eval' , value : 'eval' , apiRequirements : [ 'eval' ] , description : 'Evaluate an Axon expression' } ,
@@ -34,9 +27,7 @@ export const queryTypes: QueryType[] = [
34
27
export class DataSource extends DataSourceWithBackend < HaystackQuery , HaystackDataSourceOptions > {
35
28
constructor ( instanceSettings : DataSourceInstanceSettings < HaystackDataSourceOptions > ) {
36
29
super ( instanceSettings ) ;
37
- this . variables = new HaystackVariableSupport ( ( request ) => {
38
- return this . query ( request ) ;
39
- } ) ;
30
+ this . variables = new HaystackVariableSupport ( this ) ;
40
31
}
41
32
42
33
// Queries the available ops from the datasource and returns the queryTypes that are supported.
@@ -115,89 +106,3 @@ export class DataSource extends DataSourceWithBackend<HaystackQuery, HaystackDat
115
106
} ;
116
107
}
117
108
}
118
-
119
- export class HaystackVariableSupport extends CustomVariableSupport <
120
- DataSource ,
121
- HaystackVariableQuery ,
122
- HaystackQuery ,
123
- HaystackDataSourceOptions
124
- > {
125
- editor : ComponentType < QueryEditorProps < DataSource , HaystackQuery , HaystackDataSourceOptions , HaystackVariableQuery > > ;
126
-
127
- // Requests data from the backend. This allows this class to reuse the DataSource.query method to get data.
128
- onQuery : ( request : DataQueryRequest < HaystackVariableQuery > ) => Observable < DataQueryResponse > ;
129
-
130
- constructor ( onQuery : ( request : DataQueryRequest < HaystackVariableQuery > ) => Observable < DataQueryResponse > ) {
131
- super ( ) ;
132
- this . editor = VariableQueryEditor ;
133
- this . onQuery = onQuery ;
134
- }
135
-
136
- query ( request : DataQueryRequest < HaystackVariableQuery > ) : Observable < DataQueryResponse > {
137
- let variableQuery = request . targets [ 0 ] ;
138
- // Setting the refId is required for Grafana to associate the response with the request.
139
- variableQuery . refId = 'HaystackVariableQuery' ;
140
- let observable = this . onQuery ( request ) ;
141
- return observable . pipe (
142
- map ( ( response ) => {
143
- if ( response === undefined || response . errors !== undefined || response . data === undefined ) {
144
- return response ;
145
- }
146
-
147
- let variableValues = response . data . reduce ( ( acc : MetricFindValue [ ] , frame : DataFrame ) => {
148
- // Default to the first field
149
- let column = frame . fields [ 0 ] ;
150
- if ( variableQuery . column !== undefined && variableQuery . column !== '' ) {
151
- // If a column was input, match the column name
152
- column = frame . fields . find ( ( field : Field ) => field . name === variableQuery . column ) ?? column ;
153
- } else if ( frame . fields . some ( ( field : Field ) => field . name === 'id' ) ) {
154
- // If there is an id column, use that
155
- column = frame . fields . find ( ( field : Field ) => field . name === 'id' ) ?? column ;
156
- }
157
-
158
- // Default to the selected column
159
- let displayColumn = column ;
160
- if ( variableQuery . displayColumn !== undefined && variableQuery . displayColumn !== '' ) {
161
- // If a column was input, match the column name
162
- displayColumn =
163
- frame . fields . find ( ( field : Field ) => {
164
- return field . name === variableQuery . displayColumn ;
165
- } ) ?? displayColumn ;
166
- }
167
-
168
- let variableValues = column . values . map ( ( value , index ) => {
169
- let variableValue = variableValueFromCell ( value , column . type ) ;
170
-
171
- let displayValue = displayColumn . values [ index ] ;
172
- let variableText = variableTextFromCell ( displayValue , displayColumn . type ) ;
173
-
174
- return { text : variableText , value : variableValue } ;
175
- } ) ;
176
- return acc . concat ( variableValues ) ;
177
- } , [ ] ) ;
178
- return { ...response , data : variableValues } ;
179
- } )
180
- ) ;
181
- }
182
- }
183
-
184
- function variableValueFromCell ( value : string , columnType : FieldType ) : string {
185
- switch ( columnType ) {
186
- case FieldType . string :
187
- if ( isRef ( value ) ) {
188
- return parseRef ( value ) . id ;
189
- }
190
- }
191
- return value ;
192
- }
193
-
194
- function variableTextFromCell ( value : string , columnType : FieldType ) : string {
195
- switch ( columnType ) {
196
- case FieldType . string :
197
- if ( isRef ( value ) ) {
198
- let ref = parseRef ( value ) ;
199
- return ref . dis ?? ref . id ;
200
- }
201
- }
202
- return value ;
203
- }
0 commit comments