2018-12-13 07:28:44 -06:00
|
|
|
// Libraries
|
2019-07-30 08:49:32 -05:00
|
|
|
import React, { PureComponent, ChangeEvent, FocusEvent, ReactText } from 'react';
|
2018-12-13 07:28:44 -06:00
|
|
|
|
|
|
|
// Utils
|
2020-05-05 08:48:31 -05:00
|
|
|
import { rangeUtil, PanelData, DataSourceApi } from '@grafana/data';
|
2018-12-13 07:28:44 -06:00
|
|
|
|
|
|
|
// Components
|
2020-04-26 14:59:14 -05:00
|
|
|
import {
|
|
|
|
EventsWithValidation,
|
|
|
|
LegacyInputStatus,
|
|
|
|
LegacyForms,
|
|
|
|
ValidationEvents,
|
|
|
|
InlineFormLabel,
|
|
|
|
stylesFactory,
|
|
|
|
} from '@grafana/ui';
|
2020-04-27 11:29:41 -05:00
|
|
|
const { Switch, Input } = LegacyForms;
|
2018-12-13 07:28:44 -06:00
|
|
|
|
|
|
|
// Types
|
2019-03-18 09:41:46 -05:00
|
|
|
import { PanelModel } from '../state';
|
2020-04-26 14:59:14 -05:00
|
|
|
import { QueryOperationRow } from 'app/core/components/QueryOperationRow/QueryOperationRow';
|
|
|
|
import { config } from 'app/core/config';
|
|
|
|
import { css } from 'emotion';
|
2018-12-13 07:28:44 -06:00
|
|
|
|
|
|
|
const timeRangeValidationEvents: ValidationEvents = {
|
|
|
|
[EventsWithValidation.onBlur]: [
|
|
|
|
{
|
|
|
|
rule: value => {
|
|
|
|
if (!value) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-07-08 12:12:02 -05:00
|
|
|
return rangeUtil.isValidTimeSpan(value);
|
2018-12-13 07:28:44 -06:00
|
|
|
},
|
|
|
|
errorMessage: 'Not a valid timespan',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
const emptyToNull = (value: string) => {
|
|
|
|
return value === '' ? null : value;
|
|
|
|
};
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
panel: PanelModel;
|
2020-05-05 08:48:31 -05:00
|
|
|
dataSource: DataSourceApi;
|
2020-04-26 14:59:14 -05:00
|
|
|
data: PanelData;
|
2018-12-13 07:28:44 -06:00
|
|
|
}
|
|
|
|
|
2018-12-18 09:39:59 -06:00
|
|
|
interface State {
|
|
|
|
relativeTime: string;
|
|
|
|
timeShift: string;
|
2019-03-05 03:49:45 -06:00
|
|
|
cacheTimeout: string;
|
2019-07-30 08:49:32 -05:00
|
|
|
maxDataPoints: string | ReactText;
|
2019-03-05 03:49:45 -06:00
|
|
|
interval: string;
|
|
|
|
hideTimeOverride: boolean;
|
2020-04-26 14:59:14 -05:00
|
|
|
isOpen: boolean;
|
2018-12-18 09:39:59 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
export class QueryOptions extends PureComponent<Props, State> {
|
2019-07-30 08:49:32 -05:00
|
|
|
constructor(props: Props) {
|
2018-12-18 09:39:59 -06:00
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
relativeTime: props.panel.timeFrom || '',
|
|
|
|
timeShift: props.panel.timeShift || '',
|
2019-03-05 03:49:45 -06:00
|
|
|
cacheTimeout: props.panel.cacheTimeout || '',
|
|
|
|
maxDataPoints: props.panel.maxDataPoints || '',
|
|
|
|
interval: props.panel.interval || '',
|
|
|
|
hideTimeOverride: props.panel.hideTimeOverride || false,
|
2020-04-26 14:59:14 -05:00
|
|
|
isOpen: false,
|
2018-12-18 09:39:59 -06:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-03-05 03:49:45 -06:00
|
|
|
onRelativeTimeChange = (event: ChangeEvent<HTMLInputElement>) => {
|
2018-12-18 09:39:59 -06:00
|
|
|
this.setState({
|
|
|
|
relativeTime: event.target.value,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-03-05 03:49:45 -06:00
|
|
|
onTimeShiftChange = (event: ChangeEvent<HTMLInputElement>) => {
|
2018-12-18 09:39:59 -06:00
|
|
|
this.setState({
|
|
|
|
timeShift: event.target.value,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-04-02 09:18:06 -05:00
|
|
|
onOverrideTime = (event: FocusEvent<HTMLInputElement>, status: LegacyInputStatus) => {
|
2019-03-05 03:49:45 -06:00
|
|
|
const { value } = event.target;
|
2018-12-13 07:28:44 -06:00
|
|
|
const { panel } = this.props;
|
|
|
|
const emptyToNullValue = emptyToNull(value);
|
2020-04-27 11:29:41 -05:00
|
|
|
|
2020-04-02 09:18:06 -05:00
|
|
|
if (status === LegacyInputStatus.Valid && panel.timeFrom !== emptyToNullValue) {
|
2018-12-13 07:28:44 -06:00
|
|
|
panel.timeFrom = emptyToNullValue;
|
|
|
|
panel.refresh();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-04-02 09:18:06 -05:00
|
|
|
onTimeShift = (event: FocusEvent<HTMLInputElement>, status: LegacyInputStatus) => {
|
2019-03-05 03:49:45 -06:00
|
|
|
const { value } = event.target;
|
2018-12-13 07:28:44 -06:00
|
|
|
const { panel } = this.props;
|
|
|
|
const emptyToNullValue = emptyToNull(value);
|
2020-04-27 11:29:41 -05:00
|
|
|
|
2020-04-02 09:18:06 -05:00
|
|
|
if (status === LegacyInputStatus.Valid && panel.timeShift !== emptyToNullValue) {
|
2018-12-13 07:28:44 -06:00
|
|
|
panel.timeShift = emptyToNullValue;
|
|
|
|
panel.refresh();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
onToggleTimeOverride = () => {
|
|
|
|
const { panel } = this.props;
|
2019-03-05 03:49:45 -06:00
|
|
|
this.setState({ hideTimeOverride: !this.state.hideTimeOverride }, () => {
|
|
|
|
panel.hideTimeOverride = this.state.hideTimeOverride;
|
|
|
|
panel.refresh();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
onDataSourceOptionBlur = (panelKey: string) => () => {
|
|
|
|
const { panel } = this.props;
|
|
|
|
|
2019-07-30 08:49:32 -05:00
|
|
|
// @ts-ignore
|
2019-03-05 03:49:45 -06:00
|
|
|
panel[panelKey] = this.state[panelKey];
|
2018-12-13 07:28:44 -06:00
|
|
|
panel.refresh();
|
|
|
|
};
|
|
|
|
|
2019-03-05 03:49:45 -06:00
|
|
|
onDataSourceOptionChange = (panelKey: string) => (event: ChangeEvent<HTMLInputElement>) => {
|
|
|
|
this.setState({ ...this.state, [panelKey]: event.target.value });
|
|
|
|
};
|
|
|
|
|
2020-04-27 11:29:41 -05:00
|
|
|
renderCacheTimeoutOption() {
|
2020-05-05 08:48:31 -05:00
|
|
|
const { dataSource } = this.props;
|
2020-04-27 11:29:41 -05:00
|
|
|
const { cacheTimeout } = this.state;
|
|
|
|
const tooltip = `If your time series store has a query cache this option can override the default cache timeout. Specify a
|
|
|
|
numeric value in seconds.`;
|
|
|
|
|
2020-05-05 08:48:31 -05:00
|
|
|
if (!dataSource.meta.queryOptions?.cacheTimeout) {
|
2020-04-27 11:29:41 -05:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="gf-form-inline">
|
|
|
|
<div className="gf-form">
|
|
|
|
<InlineFormLabel width={9} tooltip={tooltip}>
|
|
|
|
Cache timeout
|
|
|
|
</InlineFormLabel>
|
|
|
|
<Input
|
|
|
|
type="text"
|
|
|
|
className="width-6"
|
|
|
|
placeholder="60"
|
|
|
|
name={name}
|
|
|
|
spellCheck={false}
|
|
|
|
onBlur={this.onDataSourceOptionBlur('maxDataPoints')}
|
|
|
|
onChange={this.onDataSourceOptionChange('maxDataPoints')}
|
|
|
|
value={cacheTimeout}
|
2019-09-11 00:45:08 -05:00
|
|
|
/>
|
2020-04-27 11:29:41 -05:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderMaxDataPointsOption() {
|
|
|
|
const { data } = this.props;
|
|
|
|
const { maxDataPoints } = this.state;
|
|
|
|
const realMd = data.request?.maxDataPoints;
|
|
|
|
const isAuto = maxDataPoints === '';
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="gf-form-inline">
|
|
|
|
<div className="gf-form">
|
|
|
|
<InlineFormLabel
|
|
|
|
width={9}
|
|
|
|
tooltip={
|
|
|
|
<>
|
|
|
|
The maximum data points per series. Used directly by some data sources and used in calculation of auto
|
|
|
|
interval. With streaming data this value is used for the rolling buffer.
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
Max data points
|
|
|
|
</InlineFormLabel>
|
|
|
|
<Input
|
|
|
|
type="text"
|
|
|
|
className="width-6"
|
|
|
|
placeholder={`${realMd}`}
|
|
|
|
name={name}
|
|
|
|
spellCheck={false}
|
|
|
|
onBlur={this.onDataSourceOptionBlur('maxDataPoints')}
|
|
|
|
onChange={this.onDataSourceOptionChange('maxDataPoints')}
|
|
|
|
value={maxDataPoints}
|
|
|
|
/>
|
|
|
|
{isAuto && (
|
|
|
|
<>
|
|
|
|
<div className="gf-form-label query-segment-operator">=</div>
|
|
|
|
<div className="gf-form-label">Width of panel</div>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderIntervalOption() {
|
2020-05-05 08:48:31 -05:00
|
|
|
const { data, dataSource } = this.props;
|
2020-04-27 11:29:41 -05:00
|
|
|
const { interval } = this.state;
|
|
|
|
const realInterval = data.request?.interval;
|
2020-05-05 08:48:31 -05:00
|
|
|
const minIntervalOnDs = dataSource.interval ?? 'No limit';
|
2020-04-27 11:29:41 -05:00
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<div className="gf-form-inline">
|
|
|
|
<div className="gf-form">
|
|
|
|
<InlineFormLabel
|
|
|
|
width={9}
|
|
|
|
tooltip={
|
|
|
|
<>
|
|
|
|
A lower limit for the interval. Recommended to be set to write frequency, for example <code>1m</code>{' '}
|
|
|
|
if your data is written every minute. Default value can be set in data source settings for most data
|
|
|
|
sources.
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
Min interval
|
|
|
|
</InlineFormLabel>
|
|
|
|
<Input
|
|
|
|
type="text"
|
|
|
|
className="width-6"
|
2020-05-05 08:48:31 -05:00
|
|
|
placeholder={`${minIntervalOnDs}`}
|
2020-04-27 11:29:41 -05:00
|
|
|
name={name}
|
|
|
|
spellCheck={false}
|
|
|
|
onBlur={this.onDataSourceOptionBlur('interval')}
|
|
|
|
onChange={this.onDataSourceOptionChange('interval')}
|
|
|
|
value={interval}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div className="gf-form-inline">
|
|
|
|
<div className="gf-form">
|
|
|
|
<InlineFormLabel
|
|
|
|
width={9}
|
|
|
|
tooltip={
|
|
|
|
<>
|
|
|
|
The evaluated Interval that is sent to data source and is used in <code>$__interval</code> and{' '}
|
|
|
|
<code>$__interval_ms</code>
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
Interval
|
|
|
|
</InlineFormLabel>
|
|
|
|
<InlineFormLabel width={6}>{realInterval}</InlineFormLabel>
|
|
|
|
<div className="gf-form-label query-segment-operator">=</div>
|
|
|
|
<div className="gf-form-label">Max data points / time range</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
2018-12-13 07:28:44 -06:00
|
|
|
|
2020-04-26 14:59:14 -05:00
|
|
|
onOpenOptions = () => {
|
|
|
|
this.setState({ isOpen: true });
|
|
|
|
};
|
|
|
|
|
|
|
|
onCloseOptions = () => {
|
|
|
|
this.setState({ isOpen: false });
|
|
|
|
};
|
|
|
|
|
|
|
|
renderCollapsedText(styles: StylesType): React.ReactNode | undefined {
|
|
|
|
const { data } = this.props;
|
|
|
|
const { isOpen, maxDataPoints, interval } = this.state;
|
|
|
|
|
|
|
|
if (isOpen) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mdDesc = maxDataPoints;
|
|
|
|
if (maxDataPoints === '' && data.request) {
|
|
|
|
mdDesc = `auto = ${data.request.maxDataPoints}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
let intervalDesc = interval;
|
2020-04-27 11:29:41 -05:00
|
|
|
if (data.request) {
|
|
|
|
intervalDesc = `${data.request.interval}`;
|
2020-04-26 14:59:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{<div className={styles.collapsedText}>MD = {mdDesc}</div>}
|
|
|
|
{<div className={styles.collapsedText}>Interval = {intervalDesc}</div>}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-12-18 09:39:59 -06:00
|
|
|
render() {
|
2019-03-05 03:49:45 -06:00
|
|
|
const { hideTimeOverride } = this.state;
|
2020-04-26 14:59:14 -05:00
|
|
|
const { relativeTime, timeShift, isOpen } = this.state;
|
|
|
|
const styles = getStyles();
|
|
|
|
|
2018-12-13 07:28:44 -06:00
|
|
|
return (
|
2020-04-26 14:59:14 -05:00
|
|
|
<QueryOperationRow
|
|
|
|
title="Options"
|
|
|
|
headerElement={this.renderCollapsedText(styles)}
|
|
|
|
isOpen={isOpen}
|
|
|
|
onOpen={this.onOpenOptions}
|
|
|
|
onClose={this.onCloseOptions}
|
|
|
|
>
|
2020-04-27 11:29:41 -05:00
|
|
|
{this.renderMaxDataPointsOption()}
|
|
|
|
{this.renderIntervalOption()}
|
|
|
|
{this.renderCacheTimeoutOption()}
|
2018-12-13 07:28:44 -06:00
|
|
|
|
|
|
|
<div className="gf-form">
|
2020-04-26 14:59:14 -05:00
|
|
|
<InlineFormLabel width={9}>Relative time</InlineFormLabel>
|
2018-12-13 07:28:44 -06:00
|
|
|
<Input
|
|
|
|
type="text"
|
|
|
|
className="width-6"
|
|
|
|
placeholder="1h"
|
2018-12-18 09:39:59 -06:00
|
|
|
onChange={this.onRelativeTimeChange}
|
2018-12-13 07:28:44 -06:00
|
|
|
onBlur={this.onOverrideTime}
|
|
|
|
validationEvents={timeRangeValidationEvents}
|
|
|
|
hideErrorMessage={true}
|
2018-12-18 09:39:59 -06:00
|
|
|
value={relativeTime}
|
2018-12-13 07:28:44 -06:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="gf-form">
|
2020-04-26 14:59:14 -05:00
|
|
|
<span className="gf-form-label width-9">Time shift</span>
|
2018-12-13 07:28:44 -06:00
|
|
|
<Input
|
|
|
|
type="text"
|
|
|
|
className="width-6"
|
|
|
|
placeholder="1h"
|
2018-12-18 09:39:59 -06:00
|
|
|
onChange={this.onTimeShiftChange}
|
2018-12-13 07:28:44 -06:00
|
|
|
onBlur={this.onTimeShift}
|
|
|
|
validationEvents={timeRangeValidationEvents}
|
|
|
|
hideErrorMessage={true}
|
2018-12-18 09:39:59 -06:00
|
|
|
value={timeShift}
|
2018-12-13 07:28:44 -06:00
|
|
|
/>
|
|
|
|
</div>
|
2019-03-05 04:43:25 -06:00
|
|
|
{(timeShift || relativeTime) && (
|
|
|
|
<div className="gf-form-inline">
|
2020-04-26 14:59:14 -05:00
|
|
|
<Switch
|
|
|
|
label="Hide time info"
|
|
|
|
labelClass="width-9"
|
|
|
|
checked={hideTimeOverride}
|
|
|
|
onChange={this.onToggleTimeOverride}
|
|
|
|
/>
|
2019-03-05 04:43:25 -06:00
|
|
|
</div>
|
|
|
|
)}
|
2020-04-26 14:59:14 -05:00
|
|
|
</QueryOperationRow>
|
2018-12-13 07:28:44 -06:00
|
|
|
);
|
2018-12-18 09:39:59 -06:00
|
|
|
}
|
2018-12-13 07:28:44 -06:00
|
|
|
}
|
2020-04-26 14:59:14 -05:00
|
|
|
|
|
|
|
const getStyles = stylesFactory(() => {
|
|
|
|
const { theme } = config;
|
|
|
|
|
|
|
|
return {
|
|
|
|
collapsedText: css`
|
|
|
|
margin-left: ${theme.spacing.md};
|
|
|
|
font-size: ${theme.typography.size.sm};
|
|
|
|
color: ${theme.colors.textWeak};
|
|
|
|
`,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
type StylesType = ReturnType<typeof getStyles>;
|