2021-03-16 13:58:51 +01:00
import React , { memo , FC , useEffect } from 'react' ;
2019-12-23 11:42:31 +01:00
// Types
import { ExploreQueryFieldProps } from '@grafana/data' ;
import { PrometheusDatasource } from '../datasource' ;
import { PromQuery , PromOptions } from '../types' ;
import PromQueryField from './PromQueryField' ;
2020-02-06 12:37:30 +00:00
import { PromExploreExtraField } from './PromExploreExtraField' ;
2019-12-23 11:42:31 +01:00
2020-02-06 12:37:30 +00:00
export type Props = ExploreQueryFieldProps < PrometheusDatasource , PromQuery , PromOptions > ;
2019-12-23 11:42:31 +01:00
2020-07-06 21:16:27 +02:00
export const PromExploreQueryEditor : FC < Props > = ( props : Props ) = > {
2020-09-29 12:05:57 +03:00
const { range , query , data , datasource , history , onChange , onRunQuery } = props ;
2019-12-23 11:42:31 +01:00
2021-03-16 13:58:51 +01:00
useEffect ( ( ) = > {
if ( query . exemplar === undefined ) {
onChange ( { . . . query , exemplar : true } ) ;
}
2021-03-25 12:42:14 +01:00
} , [ onChange , query ] ) ;
2021-03-16 13:58:51 +01:00
2020-02-06 12:37:30 +00:00
function onChangeQueryStep ( value : string ) {
const { query , onChange } = props ;
const nextQuery = { . . . query , interval : value } ;
onChange ( nextQuery ) ;
2019-12-23 11:42:31 +01:00
}
2020-02-06 12:37:30 +00:00
function onStepChange ( e : React.SyntheticEvent < HTMLInputElement > ) {
if ( e . currentTarget . value !== query . interval ) {
onChangeQueryStep ( e . currentTarget . value ) ;
}
}
2019-12-23 11:42:31 +01:00
2020-09-28 21:25:16 +02:00
function onReturnKeyDown ( e : React.KeyboardEvent < HTMLInputElement > ) {
if ( e . key === 'Enter' && ( e . shiftKey || e . ctrlKey ) ) {
onRunQuery ( ) ;
}
}
2020-09-22 17:31:42 +02:00
function onQueryTypeChange ( value : string ) {
const { query , onChange } = props ;
let nextQuery ;
if ( value === 'instant' ) {
nextQuery = { . . . query , instant : true , range : false } ;
} else if ( value === 'range' ) {
nextQuery = { . . . query , instant : false , range : true } ;
} else {
nextQuery = { . . . query , instant : true , range : true } ;
}
onChange ( nextQuery ) ;
}
return (
2020-09-28 21:25:16 +02:00
< PromQueryField
datasource = { datasource }
query = { query }
2020-09-29 12:05:57 +03:00
range = { range }
2020-09-28 21:25:16 +02:00
onRunQuery = { onRunQuery }
onChange = { onChange }
onBlur = { ( ) = > { } }
history = { history }
data = { data }
ExtraFieldElement = {
< PromExploreExtraField
2020-11-20 12:08:10 +01:00
// Select "both" as default option when Explore is opened. In legacy requests, range and instant can be undefined. In this case, we want to run queries with "both".
queryType = { query . range === query . instant ? 'both' : query . instant ? 'instant' : 'range' }
2020-09-28 21:25:16 +02:00
stepValue = { query . interval || '' }
onQueryTypeChange = { onQueryTypeChange }
onStepChange = { onStepChange }
onKeyDownFunc = { onReturnKeyDown }
2021-01-15 16:20:20 +01:00
query = { query }
onChange = { onChange }
2021-03-16 13:58:51 +01:00
datasource = { datasource }
2020-09-28 21:25:16 +02:00
/ >
}
/ >
2020-02-06 12:37:30 +00:00
) ;
2020-07-06 21:16:27 +02:00
} ;
2020-02-06 12:37:30 +00:00
export default memo ( PromExploreQueryEditor ) ;