Commit bf5d52c5 by Ivana Huckova Committed by GitHub

Loki: Send current time range when fetching labels and values (#26622)

* Send current time range when fetching labels and values in Explore

* Pass range to Editors, in the same way as it was in Angular editors

* Remove unused imports

* Remove unused imports, props

* Update

* Update

* Update refresh condition

* Add comment
parent d88864f3
...@@ -5,7 +5,7 @@ import { PanelData } from './panel'; ...@@ -5,7 +5,7 @@ import { PanelData } from './panel';
import { LogRowModel } from './logs'; import { LogRowModel } from './logs';
import { AnnotationEvent, KeyValue, LoadingState, TableData, TimeSeries } from './data'; import { AnnotationEvent, KeyValue, LoadingState, TableData, TimeSeries } from './data';
import { DataFrame, DataFrameDTO } from './dataFrame'; import { DataFrame, DataFrameDTO } from './dataFrame';
import { RawTimeRange, TimeRange, AbsoluteTimeRange } from './time'; import { RawTimeRange, TimeRange } from './time';
import { ScopedVars } from './ScopedVars'; import { ScopedVars } from './ScopedVars';
import { CoreApp } from './app'; import { CoreApp } from './app';
...@@ -300,6 +300,7 @@ export interface QueryEditorProps< ...@@ -300,6 +300,7 @@ export interface QueryEditorProps<
* Contains query response filtered by refId of QueryResultBase and possible query error * Contains query response filtered by refId of QueryResultBase and possible query error
*/ */
data?: PanelData; data?: PanelData;
range?: TimeRange;
exploreId?: any; exploreId?: any;
history?: HistoryItem[]; history?: HistoryItem[];
} }
...@@ -322,7 +323,6 @@ export interface ExploreQueryFieldProps< ...@@ -322,7 +323,6 @@ export interface ExploreQueryFieldProps<
> extends QueryEditorProps<DSType, TQuery, TOptions> { > extends QueryEditorProps<DSType, TQuery, TOptions> {
history: any[]; history: any[];
onBlur?: () => void; onBlur?: () => void;
absoluteRange?: AbsoluteTimeRange;
exploreId?: any; exploreId?: any;
} }
......
...@@ -178,6 +178,7 @@ export class QueryEditorRow extends PureComponent<Props, State> { ...@@ -178,6 +178,7 @@ export class QueryEditorRow extends PureComponent<Props, State> {
onChange={onChange} onChange={onChange}
onRunQuery={this.onRunQuery} onRunQuery={this.onRunQuery}
data={data} data={data}
range={getTimeSrv().timeRange()}
/> />
); );
} }
......
...@@ -113,16 +113,7 @@ export class QueryRow extends PureComponent<QueryRowProps, QueryRowState> { ...@@ -113,16 +113,7 @@ export class QueryRow extends PureComponent<QueryRowProps, QueryRowState> {
}; };
renderQueryEditor = () => { renderQueryEditor = () => {
const { const { datasourceInstance, history, query, exploreEvents, range, queryResponse, exploreId } = this.props;
datasourceInstance,
history,
query,
exploreEvents,
range,
absoluteRange,
queryResponse,
exploreId,
} = this.props;
const queryErrors = queryResponse.error && queryResponse.error.refId === query.refId ? [queryResponse.error] : []; const queryErrors = queryResponse.error && queryResponse.error.refId === query.refId ? [queryResponse.error] : [];
...@@ -138,7 +129,7 @@ export class QueryRow extends PureComponent<QueryRowProps, QueryRowState> { ...@@ -138,7 +129,7 @@ export class QueryRow extends PureComponent<QueryRowProps, QueryRowState> {
onBlur={noopOnBlur} onBlur={noopOnBlur}
onChange={this.onChange} onChange={this.onChange}
data={queryResponse} data={queryResponse}
absoluteRange={absoluteRange} range={range}
exploreId={exploreId} exploreId={exploreId}
/> />
); );
......
...@@ -5,7 +5,7 @@ import LokiExploreQueryEditor from './LokiExploreQueryEditor'; ...@@ -5,7 +5,7 @@ import LokiExploreQueryEditor from './LokiExploreQueryEditor';
import { LokiExploreExtraField } from './LokiExploreExtraField'; import { LokiExploreExtraField } from './LokiExploreExtraField';
import { LokiDatasource } from '../datasource'; import { LokiDatasource } from '../datasource';
import { LokiQuery } from '../types'; import { LokiQuery } from '../types';
import { ExploreMode, LoadingState, PanelData, toUtc } from '@grafana/data'; import { ExploreMode, LoadingState, PanelData, toUtc, TimeRange } from '@grafana/data';
import { makeMockLokiDatasource } from '../mocks'; import { makeMockLokiDatasource } from '../mocks';
import LokiLanguageProvider from '../language_provider'; import LokiLanguageProvider from '../language_provider';
...@@ -15,6 +15,14 @@ const setup = (renderMethod: any, propOverrides?: object) => { ...@@ -15,6 +15,14 @@ const setup = (renderMethod: any, propOverrides?: object) => {
const onRunQuery = jest.fn(); const onRunQuery = jest.fn();
const onChange = jest.fn(); const onChange = jest.fn();
const query: LokiQuery = { expr: '', refId: 'A', maxLines: 0 }; const query: LokiQuery = { expr: '', refId: 'A', maxLines: 0 };
const range: TimeRange = {
from: toUtc('2020-01-01', 'YYYY-MM-DD'),
to: toUtc('2020-01-02', 'YYYY-MM-DD'),
raw: {
from: toUtc('2020-01-01', 'YYYY-MM-DD'),
to: toUtc('2020-01-02', 'YYYY-MM-DD'),
},
};
const data: PanelData = { const data: PanelData = {
state: LoadingState.NotStarted, state: LoadingState.NotStarted,
series: [], series: [],
...@@ -53,6 +61,7 @@ const setup = (renderMethod: any, propOverrides?: object) => { ...@@ -53,6 +61,7 @@ const setup = (renderMethod: any, propOverrides?: object) => {
const props: any = { const props: any = {
query, query,
data, data,
range,
datasource, datasource,
exploreMode, exploreMode,
history, history,
......
...@@ -3,7 +3,7 @@ import React, { memo } from 'react'; ...@@ -3,7 +3,7 @@ import React, { memo } from 'react';
import _ from 'lodash'; import _ from 'lodash';
// Types // Types
import { AbsoluteTimeRange, ExploreQueryFieldProps } from '@grafana/data'; import { ExploreQueryFieldProps } from '@grafana/data';
import { LokiDatasource } from '../datasource'; import { LokiDatasource } from '../datasource';
import { LokiQuery, LokiOptions } from '../types'; import { LokiQuery, LokiOptions } from '../types';
import { LokiQueryField } from './LokiQueryField'; import { LokiQueryField } from './LokiQueryField';
...@@ -12,22 +12,7 @@ import LokiExploreExtraField from './LokiExploreExtraField'; ...@@ -12,22 +12,7 @@ import LokiExploreExtraField from './LokiExploreExtraField';
type Props = ExploreQueryFieldProps<LokiDatasource, LokiQuery, LokiOptions>; type Props = ExploreQueryFieldProps<LokiDatasource, LokiQuery, LokiOptions>;
export function LokiExploreQueryEditor(props: Props) { export function LokiExploreQueryEditor(props: Props) {
const { query, data, datasource, history, onChange, onRunQuery } = props; const { range, query, data, datasource, history, onChange, onRunQuery } = props;
let absolute: AbsoluteTimeRange;
if (data && data.request) {
const { range } = data.request;
absolute = {
from: range.from.valueOf(),
to: range.to.valueOf(),
};
} else {
absolute = {
from: Date.now() - 10000,
to: Date.now(),
};
}
function onChangeQueryLimit(value: string) { function onChangeQueryLimit(value: string) {
const { query, onChange } = props; const { query, onChange } = props;
...@@ -70,7 +55,7 @@ export function LokiExploreQueryEditor(props: Props) { ...@@ -70,7 +55,7 @@ export function LokiExploreQueryEditor(props: Props) {
onRunQuery={onRunQuery} onRunQuery={onRunQuery}
history={history} history={history}
data={data} data={data}
absoluteRange={absolute} range={range}
ExtraFieldElement={ ExtraFieldElement={
<LokiExploreExtraField <LokiExploreExtraField
label={'Line limit'} label={'Line limit'}
......
...@@ -8,12 +8,8 @@ import { LokiQuery } from '../types'; ...@@ -8,12 +8,8 @@ import { LokiQuery } from '../types';
const createMockRequestRange = (from: string, to: string) => { const createMockRequestRange = (from: string, to: string) => {
return { return {
request: { from: toUtc(from, 'YYYY-MM-DD'),
range: { to: toUtc(to, 'YYYY-MM-DD'),
from: toUtc(from, 'YYYY-MM-DD'),
to: toUtc(to, 'YYYY-MM-DD'),
},
},
}; };
}; };
...@@ -29,14 +25,14 @@ const setup = (propOverrides?: object) => { ...@@ -29,14 +25,14 @@ const setup = (propOverrides?: object) => {
legendFormat: 'My Legend', legendFormat: 'My Legend',
}; };
const data = createMockRequestRange('2020-01-01', '2020-01-02'); const range = createMockRequestRange('2020-01-01', '2020-01-02');
const props: any = { const props: any = {
datasource, datasource,
onChange, onChange,
onRunQuery, onRunQuery,
query, query,
data, range,
}; };
Object.assign(props, propOverrides); Object.assign(props, propOverrides);
...@@ -56,10 +52,10 @@ describe('Render LokiQueryEditor with legend', () => { ...@@ -56,10 +52,10 @@ describe('Render LokiQueryEditor with legend', () => {
expect(wrapper).toMatchSnapshot(); expect(wrapper).toMatchSnapshot();
}); });
it('should update absolute timerange', () => { it('should update timerange', () => {
const { wrapper } = setup(); const { wrapper } = setup();
wrapper.setProps({ wrapper.setProps({
data: createMockRequestRange('2019-01-01', '2020-01-02'), range: createMockRequestRange('2019-01-01', '2020-01-02'),
}); });
expect(wrapper).toMatchSnapshot(); expect(wrapper).toMatchSnapshot();
}); });
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
import React, { PureComponent } from 'react'; import React, { PureComponent } from 'react';
// Types // Types
import { AbsoluteTimeRange, QueryEditorProps, PanelData } from '@grafana/data'; import { QueryEditorProps } from '@grafana/data';
import { InlineFormLabel } from '@grafana/ui'; import { InlineFormLabel } from '@grafana/ui';
import { LokiDatasource } from '../datasource'; import { LokiDatasource } from '../datasource';
import { LokiQuery } from '../types'; import { LokiQuery } from '../types';
...@@ -31,21 +31,6 @@ export class LokiQueryEditor extends PureComponent<Props, State> { ...@@ -31,21 +31,6 @@ export class LokiQueryEditor extends PureComponent<Props, State> {
}; };
} }
calcAbsoluteRange = (data: PanelData | undefined): AbsoluteTimeRange => {
if (data && data.request) {
const { range } = data.request;
return {
from: range.from.valueOf(),
to: range.to.valueOf(),
};
}
return {
from: Date.now() - 10000,
to: Date.now(),
};
};
onFieldChange = (query: LokiQuery, override?: any) => { onFieldChange = (query: LokiQuery, override?: any) => {
this.query.expr = query.expr; this.query.expr = query.expr;
}; };
...@@ -63,7 +48,7 @@ export class LokiQueryEditor extends PureComponent<Props, State> { ...@@ -63,7 +48,7 @@ export class LokiQueryEditor extends PureComponent<Props, State> {
}; };
render() { render() {
const { datasource, query, data } = this.props; const { datasource, query, data, range } = this.props;
const { legendFormat } = this.state; const { legendFormat } = this.state;
return ( return (
...@@ -75,7 +60,7 @@ export class LokiQueryEditor extends PureComponent<Props, State> { ...@@ -75,7 +60,7 @@ export class LokiQueryEditor extends PureComponent<Props, State> {
onRunQuery={this.onRunQuery} onRunQuery={this.onRunQuery}
history={[]} history={[]}
data={data} data={data}
absoluteRange={this.calcAbsoluteRange(data)} range={range}
/> />
<div className="gf-form-inline"> <div className="gf-form-inline">
......
...@@ -5,14 +5,16 @@ import LokiLanguageProvider from '../language_provider'; ...@@ -5,14 +5,16 @@ import LokiLanguageProvider from '../language_provider';
type LokiQueryFieldProps = Omit< type LokiQueryFieldProps = Omit<
LokiQueryFieldFormProps, LokiQueryFieldFormProps,
'syntax' | 'syntaxLoaded' | 'onLoadOptions' | 'onLabelsRefresh' | 'logLabelOptions' 'syntax' | 'syntaxLoaded' | 'onLoadOptions' | 'onLabelsRefresh' | 'logLabelOptions' | 'absoluteRange'
>; >;
export const LokiQueryField: FunctionComponent<LokiQueryFieldProps> = props => { export const LokiQueryField: FunctionComponent<LokiQueryFieldProps> = props => {
const { datasource, absoluteRange, ...otherProps } = props; const { datasource, range, ...otherProps } = props;
const absoluteTimeRange = { from: range!.from!.valueOf(), to: range!.to!.valueOf() }; // Range here is never optional
const { isSyntaxReady, setActiveOption, refreshLabels, syntax, logLabelOptions } = useLokiSyntaxAndLabels( const { isSyntaxReady, setActiveOption, refreshLabels, syntax, logLabelOptions } = useLokiSyntaxAndLabels(
datasource.languageProvider as LokiLanguageProvider, datasource.languageProvider as LokiLanguageProvider,
absoluteRange absoluteTimeRange
); );
return ( return (
...@@ -26,7 +28,7 @@ export const LokiQueryField: FunctionComponent<LokiQueryFieldProps> = props => { ...@@ -26,7 +28,7 @@ export const LokiQueryField: FunctionComponent<LokiQueryFieldProps> = props => {
*/ */
onLoadOptions={setActiveOption} onLoadOptions={setActiveOption}
onLabelsRefresh={refreshLabels} onLabelsRefresh={refreshLabels}
absoluteRange={absoluteRange} absoluteRange={absoluteTimeRange}
syntax={syntax} syntax={syntax}
syntaxLoaded={isSyntaxReady} syntaxLoaded={isSyntaxReady}
logLabelOptions={logLabelOptions} logLabelOptions={logLabelOptions}
......
...@@ -128,9 +128,6 @@ export class LokiQueryFieldForm extends React.PureComponent<LokiQueryFieldFormPr ...@@ -128,9 +128,6 @@ export class LokiQueryFieldForm extends React.PureComponent<LokiQueryFieldFormPr
{ text, value, prefix, wrapperClasses, labelKey }, { text, value, prefix, wrapperClasses, labelKey },
{ history, absoluteRange } { history, absoluteRange }
); );
//console.log('handleTypeahead', wrapperClasses, text, prefix, nextChar, labelKey, result.context);
return result; return result;
}; };
......
...@@ -12,12 +12,6 @@ exports[`LokiExploreQueryEditor should render component 1`] = ` ...@@ -12,12 +12,6 @@ exports[`LokiExploreQueryEditor should render component 1`] = `
value="0" value="0"
/> />
} }
absoluteRange={
Object {
"from": 1577836800000,
"to": 1577923200000,
}
}
data={ data={
Object { Object {
"request": Object { "request": Object {
...@@ -112,5 +106,15 @@ exports[`LokiExploreQueryEditor should render component 1`] = ` ...@@ -112,5 +106,15 @@ exports[`LokiExploreQueryEditor should render component 1`] = `
"refId": "A", "refId": "A",
} }
} }
range={
Object {
"from": "2020-01-01T00:00:00.000Z",
"raw": Object {
"from": "2020-01-01T00:00:00.000Z",
"to": "2020-01-02T00:00:00.000Z",
},
"to": "2020-01-02T00:00:00.000Z",
}
}
/> />
`; `;
...@@ -3,22 +3,6 @@ ...@@ -3,22 +3,6 @@
exports[`Render LokiQueryEditor with legend should render 1`] = ` exports[`Render LokiQueryEditor with legend should render 1`] = `
<div> <div>
<Component <Component
absoluteRange={
Object {
"from": 1577836800000,
"to": 1577923200000,
}
}
data={
Object {
"request": Object {
"range": Object {
"from": "2020-01-01T00:00:00.000Z",
"to": "2020-01-02T00:00:00.000Z",
},
},
}
}
datasource={Object {}} datasource={Object {}}
history={Array []} history={Array []}
onChange={[Function]} onChange={[Function]}
...@@ -30,6 +14,12 @@ exports[`Render LokiQueryEditor with legend should render 1`] = ` ...@@ -30,6 +14,12 @@ exports[`Render LokiQueryEditor with legend should render 1`] = `
"refId": "A", "refId": "A",
} }
} }
range={
Object {
"from": "2020-01-01T00:00:00.000Z",
"to": "2020-01-02T00:00:00.000Z",
}
}
/> />
<div <div
className="gf-form-inline" className="gf-form-inline"
...@@ -57,25 +47,9 @@ exports[`Render LokiQueryEditor with legend should render 1`] = ` ...@@ -57,25 +47,9 @@ exports[`Render LokiQueryEditor with legend should render 1`] = `
</div> </div>
`; `;
exports[`Render LokiQueryEditor with legend should update absolute timerange 1`] = ` exports[`Render LokiQueryEditor with legend should update timerange 1`] = `
<div> <div>
<Component <Component
absoluteRange={
Object {
"from": 1546300800000,
"to": 1577923200000,
}
}
data={
Object {
"request": Object {
"range": Object {
"from": "2019-01-01T00:00:00.000Z",
"to": "2020-01-02T00:00:00.000Z",
},
},
}
}
datasource={Object {}} datasource={Object {}}
history={Array []} history={Array []}
onChange={[Function]} onChange={[Function]}
...@@ -87,6 +61,12 @@ exports[`Render LokiQueryEditor with legend should update absolute timerange 1`] ...@@ -87,6 +61,12 @@ exports[`Render LokiQueryEditor with legend should update absolute timerange 1`]
"refId": "A", "refId": "A",
} }
} }
range={
Object {
"from": "2019-01-01T00:00:00.000Z",
"to": "2020-01-02T00:00:00.000Z",
}
}
/> />
<div <div
className="gf-form-inline" className="gf-form-inline"
......
import { useState, useEffect } from 'react'; import { useState, useEffect } from 'react';
import { isEqual } from 'lodash';
import { AbsoluteTimeRange } from '@grafana/data'; import { AbsoluteTimeRange } from '@grafana/data';
import { CascaderOption } from '@grafana/ui'; import { CascaderOption } from '@grafana/ui';
...@@ -23,6 +24,7 @@ export const useLokiLabels = ( ...@@ -23,6 +24,7 @@ export const useLokiLabels = (
// State // State
const [logLabelOptions, setLogLabelOptions] = useState<any>([]); const [logLabelOptions, setLogLabelOptions] = useState<any>([]);
const [shouldTryRefreshLabels, setRefreshLabels] = useState(false); const [shouldTryRefreshLabels, setRefreshLabels] = useState(false);
const [prevAbsoluteRange, setPrevAbsoluteRange] = useState<AbsoluteTimeRange | null>(null);
/** /**
* Holds information about currently selected option from rc-cascader to perform effect * Holds information about currently selected option from rc-cascader to perform effect
* that loads option values not fetched yet. Based on that useLokiLabels hook decides whether or not * that loads option values not fetched yet. Based on that useLokiLabels hook decides whether or not
...@@ -39,7 +41,8 @@ export const useLokiLabels = ( ...@@ -39,7 +41,8 @@ export const useLokiLabels = (
}; };
const tryLabelsRefresh = async () => { const tryLabelsRefresh = async () => {
await languageProvider.refreshLogLabels(absoluteRange); await languageProvider.refreshLogLabels(absoluteRange, !isEqual(absoluteRange, prevAbsoluteRange));
setPrevAbsoluteRange(absoluteRange);
if (mounted.current) { if (mounted.current) {
setRefreshLabels(false); setRefreshLabels(false);
......
...@@ -269,7 +269,7 @@ export default class LokiLanguageProvider extends LanguageProvider { ...@@ -269,7 +269,7 @@ export default class LokiLanguageProvider extends LanguageProvider {
// Query labels for selector // Query labels for selector
if (selector) { if (selector) {
if (selector === EMPTY_SELECTOR && labelKey) { if (selector === EMPTY_SELECTOR && labelKey) {
const labelValuesForKey = await this.getLabelValues(labelKey); const labelValuesForKey = await this.getLabelValues(labelKey, absoluteRange);
labelValues = { [labelKey]: labelValuesForKey }; labelValues = { [labelKey]: labelValuesForKey };
} else { } else {
labelValues = await this.getSeriesLabels(selector, absoluteRange); labelValues = await this.getSeriesLabels(selector, absoluteRange);
...@@ -444,8 +444,8 @@ export default class LokiLanguageProvider extends LanguageProvider { ...@@ -444,8 +444,8 @@ export default class LokiLanguageProvider extends LanguageProvider {
return nanos ? Math.floor(nanos / NS_IN_MS / 1000 / 60 / 5) : 0; return nanos ? Math.floor(nanos / NS_IN_MS / 1000 / 60 / 5) : 0;
} }
async getLabelValues(key: string): Promise<string[]> { async getLabelValues(key: string, absoluteRange = this.initialRange): Promise<string[]> {
return await this.fetchLabelValues(key, this.initialRange); return await this.fetchLabelValues(key, absoluteRange);
} }
async fetchLabelValues(key: string, absoluteRange: AbsoluteTimeRange): Promise<string[]> { async fetchLabelValues(key: string, absoluteRange: AbsoluteTimeRange): Promise<string[]> {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment