11import { QueryEditorProps , SelectableValue } from '@grafana/data' ;
2- import { InlineFieldRow , VerticalGroup } from '@grafana/ui' ;
2+ import { Button , Icon , InlineFieldRow , Toggletip , VerticalGroup } from '@grafana/ui' ;
33import React from 'react' ;
44
55import { DataSource } from '../DataSource' ;
66import { Aggregation , GraphType , RequestSpec } from '../RequestSpec' ;
7- import { CmkQuery , DataSourceOptions } from '../types' ;
7+ import { CmkQuery , DataSourceOptions , LabelVariableNames } from '../types' ;
88import { aggregationToPresentation , updateQuery } from '../utils' ;
9- import { CheckMkSelect } from './components' ;
9+ import { CheckMkSelect , GenericField } from './components' ;
1010import { Filters } from './filters' ;
1111import { labelForRequestSpecKey } from './utils' ;
1212
@@ -19,6 +19,8 @@ export const QueryEditor = (props: Props): JSX.Element => {
1919 const [ qAggregation , setQAggregation ] = React . useState ( rs . aggregation || 'off' ) ;
2020 const [ qGraphType , setQGraphType ] = React . useState ( rs . graph_type || 'predefined_graph' ) ;
2121 const [ qGraph , setQGraph ] = React . useState ( rs . graph ) ;
22+ const [ qLabel , setQLabel ] = React . useState ( rs . label ) ;
23+
2224 const filters : Partial < RequestSpec > = {
2325 // make sure to only include keys filters should change, otherwise they could
2426 // overwrite other fields!
@@ -43,6 +45,7 @@ export const QueryEditor = (props: Props): JSX.Element => {
4345 graph_type : qGraphType ,
4446 graph : qGraph ,
4547 aggregation : qAggregation ,
48+ label : qLabel ,
4649 } ;
4750
4851 // TODO: not sure if this is a dirty hack or a great solution:
@@ -109,6 +112,55 @@ export const QueryEditor = (props: Props): JSX.Element => {
109112 />
110113 ) ;
111114
115+ const LabelsTooltip = (
116+ < Toggletip
117+ content = {
118+ < >
119+ In addition to the variables provided by Grafana, the following variables hold the values provided in the
120+ filters:
121+ < ul style = { { paddingLeft : '20px' } } >
122+ { [
123+ [ LabelVariableNames . SITE , 'Site name' ] ,
124+ [ LabelVariableNames . HOSTNAME , 'Host name' ] ,
125+ [ LabelVariableNames . HOST_IN_GROUP , 'Group containing the host' ] ,
126+ [ LabelVariableNames . SERVICE , 'Service name' ] ,
127+ [ LabelVariableNames . SERVICE_IN_GROUP , 'Group containing the service' ] ,
128+ ] . map ( ( lbl ) => (
129+ < li key = { lbl [ 0 ] } >
130+ < strong > { lbl [ 0 ] } </ strong > : { lbl [ 1 ] }
131+ </ li >
132+ ) ) }
133+ </ ul >
134+ < br />
135+ Also, the original label is available as
136+ < ul style = { { paddingLeft : '20px' } } >
137+ < li >
138+ < strong > { LabelVariableNames . ORIGINAL } </ strong >
139+ </ li >
140+ </ ul >
141+ </ >
142+ }
143+ closeButton = { false }
144+ placement = "right-end"
145+ >
146+ < Button type = "button" fill = "text" size = "sm" tooltip = { 'Variables support information' } >
147+ < Icon name = "question-circle" />
148+ </ Button >
149+ </ Toggletip >
150+ ) ;
151+
152+ const labelField = (
153+ < GenericField
154+ requestSpecKey = "label"
155+ label = { labelForRequestSpecKey ( 'label' , requestSpec ) }
156+ value = { qLabel }
157+ onChange = { setQLabel }
158+ dataTestId = "custom-label-field"
159+ placeholder = { LabelVariableNames . ORIGINAL }
160+ suffix = { LabelsTooltip }
161+ > </ GenericField >
162+ ) ;
163+
112164 if ( editionMode === 'RAW' ) {
113165 return (
114166 < VerticalGroup >
@@ -122,6 +174,7 @@ export const QueryEditor = (props: Props): JSX.Element => {
122174 />
123175 { graphTypeSelect }
124176 { graphSelect }
177+ { labelField }
125178 </ VerticalGroup >
126179 ) ;
127180 } else {
@@ -139,6 +192,7 @@ export const QueryEditor = (props: Props): JSX.Element => {
139192 />
140193 { graphTypeSelect }
141194 { graphSelect }
195+ { labelField }
142196 </ VerticalGroup >
143197 ) ;
144198 }
0 commit comments