From 02427ef88d3bff05c983314f4f7588485301d6ae Mon Sep 17 00:00:00 2001 From: David Kaltschmidt Date: Tue, 17 Jul 2018 15:13:44 +0200 Subject: [PATCH] Explore: calculate query interval based on available width - classic dashboard panels inject a dynamic query interval as part of the query options. Explore did not have that. - this PR adds the interval calculation to Explore - interval based on Explore container's width - ensure min interval if set in datasource --- public/app/containers/Explore/Explore.tsx | 49 +++++++++++++------- public/app/containers/Explore/utils/query.ts | 12 ----- 2 files changed, 33 insertions(+), 28 deletions(-) diff --git a/public/app/containers/Explore/Explore.tsx b/public/app/containers/Explore/Explore.tsx index 81e1922d2cd..5ebde6e853f 100644 --- a/public/app/containers/Explore/Explore.tsx +++ b/public/app/containers/Explore/Explore.tsx @@ -2,16 +2,18 @@ import React from 'react'; import { hot } from 'react-hot-loader'; import Select from 'react-select'; +import kbn from 'app/core/utils/kbn'; import colors from 'app/core/utils/colors'; import TimeSeries from 'app/core/time_series2'; import { decodePathComponent } from 'app/core/utils/location_util'; +import { parse as parseDate } from 'app/core/utils/datemath'; import ElapsedTime from './ElapsedTime'; import QueryRows from './QueryRows'; import Graph from './Graph'; import Table from './Table'; import TimePicker, { DEFAULT_RANGE } from './TimePicker'; -import { buildQueryOptions, ensureQueries, generateQueryKey, hasQuery } from './utils/query'; +import { ensureQueries, generateQueryKey, hasQuery } from './utils/query'; function makeTimeSeriesList(dataList, options) { return dataList.map((seriesData, index) => { @@ -63,8 +65,9 @@ interface IExploreState { tableResult: any; } -// @observer export class Explore extends React.Component { + el: any; + constructor(props) { super(props); const { datasource, queries, range } = parseInitialState(props.routeParams.initial); @@ -132,6 +135,10 @@ export class Explore extends React.Component { } } + getRef = el => { + this.el = el; + }; + handleAddQueryRow = index => { const { queries } = this.state; const nextQueries = [ @@ -214,20 +221,33 @@ export class Explore extends React.Component { } }; - async runGraphQuery() { + buildQueryOptions(targetOptions: { format: string; instant: boolean }) { const { datasource, queries, range } = this.state; + const resolution = this.el.offsetWidth; + const absoluteRange = { + from: parseDate(range.from, false), + to: parseDate(range.to, true), + }; + const { interval } = kbn.calculateInterval(absoluteRange, resolution, datasource.interval); + const targets = queries.map(q => ({ + ...targetOptions, + expr: q.query, + })); + return { + interval, + range, + targets, + }; + } + + async runGraphQuery() { + const { datasource, queries } = this.state; if (!hasQuery(queries)) { return; } this.setState({ latency: 0, loading: true, graphResult: null, queryError: null }); const now = Date.now(); - const options = buildQueryOptions({ - format: 'time_series', - interval: datasource.interval, - instant: false, - range, - queries: queries.map(q => q.query), - }); + const options = this.buildQueryOptions({ format: 'time_series', instant: false }); try { const res = await datasource.query(options); const result = makeTimeSeriesList(res.data, options); @@ -241,18 +261,15 @@ export class Explore extends React.Component { } async runTableQuery() { - const { datasource, queries, range } = this.state; + const { datasource, queries } = this.state; if (!hasQuery(queries)) { return; } this.setState({ latency: 0, loading: true, queryError: null, tableResult: null }); const now = Date.now(); - const options = buildQueryOptions({ + const options = this.buildQueryOptions({ format: 'table', - interval: datasource.interval, instant: true, - range, - queries: queries.map(q => q.query), }); try { const res = await datasource.query(options); @@ -301,7 +318,7 @@ export class Explore extends React.Component { const selectedDatasource = datasource ? datasource.name : undefined; return ( -
+
{position === 'left' ? (
diff --git a/public/app/containers/Explore/utils/query.ts b/public/app/containers/Explore/utils/query.ts index 3aa0cc5b357..d774f619a30 100644 --- a/public/app/containers/Explore/utils/query.ts +++ b/public/app/containers/Explore/utils/query.ts @@ -1,15 +1,3 @@ -export function buildQueryOptions({ format, interval, instant, range, queries }) { - return { - interval, - range, - targets: queries.map(expr => ({ - expr, - format, - instant, - })), - }; -} - export function generateQueryKey(index = 0) { return `Q-${Date.now()}-${Math.random()}-${index}`; }