Commit b5261505 by David Committed by GitHub

Merge pull request #12634 from grafana/davkal/explore-step

Explore: calculate query interval based on available width
parents e318489b 02427ef8
...@@ -2,16 +2,18 @@ import React from 'react'; ...@@ -2,16 +2,18 @@ import React from 'react';
import { hot } from 'react-hot-loader'; import { hot } from 'react-hot-loader';
import Select from 'react-select'; import Select from 'react-select';
import kbn from 'app/core/utils/kbn';
import colors from 'app/core/utils/colors'; import colors from 'app/core/utils/colors';
import TimeSeries from 'app/core/time_series2'; import TimeSeries from 'app/core/time_series2';
import { decodePathComponent } from 'app/core/utils/location_util'; import { decodePathComponent } from 'app/core/utils/location_util';
import { parse as parseDate } from 'app/core/utils/datemath';
import ElapsedTime from './ElapsedTime'; import ElapsedTime from './ElapsedTime';
import QueryRows from './QueryRows'; import QueryRows from './QueryRows';
import Graph from './Graph'; import Graph from './Graph';
import Table from './Table'; import Table from './Table';
import TimePicker, { DEFAULT_RANGE } from './TimePicker'; 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) { function makeTimeSeriesList(dataList, options) {
return dataList.map((seriesData, index) => { return dataList.map((seriesData, index) => {
...@@ -63,8 +65,9 @@ interface IExploreState { ...@@ -63,8 +65,9 @@ interface IExploreState {
tableResult: any; tableResult: any;
} }
// @observer
export class Explore extends React.Component<any, IExploreState> { export class Explore extends React.Component<any, IExploreState> {
el: any;
constructor(props) { constructor(props) {
super(props); super(props);
const { datasource, queries, range } = parseInitialState(props.routeParams.initial); const { datasource, queries, range } = parseInitialState(props.routeParams.initial);
...@@ -132,6 +135,10 @@ export class Explore extends React.Component<any, IExploreState> { ...@@ -132,6 +135,10 @@ export class Explore extends React.Component<any, IExploreState> {
} }
} }
getRef = el => {
this.el = el;
};
handleAddQueryRow = index => { handleAddQueryRow = index => {
const { queries } = this.state; const { queries } = this.state;
const nextQueries = [ const nextQueries = [
...@@ -214,20 +221,33 @@ export class Explore extends React.Component<any, IExploreState> { ...@@ -214,20 +221,33 @@ export class Explore extends React.Component<any, IExploreState> {
} }
}; };
async runGraphQuery() { buildQueryOptions(targetOptions: { format: string; instant: boolean }) {
const { datasource, queries, range } = this.state; 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)) { if (!hasQuery(queries)) {
return; return;
} }
this.setState({ latency: 0, loading: true, graphResult: null, queryError: null }); this.setState({ latency: 0, loading: true, graphResult: null, queryError: null });
const now = Date.now(); const now = Date.now();
const options = buildQueryOptions({ const options = this.buildQueryOptions({ format: 'time_series', instant: false });
format: 'time_series',
interval: datasource.interval,
instant: false,
range,
queries: queries.map(q => q.query),
});
try { try {
const res = await datasource.query(options); const res = await datasource.query(options);
const result = makeTimeSeriesList(res.data, options); const result = makeTimeSeriesList(res.data, options);
...@@ -241,18 +261,15 @@ export class Explore extends React.Component<any, IExploreState> { ...@@ -241,18 +261,15 @@ export class Explore extends React.Component<any, IExploreState> {
} }
async runTableQuery() { async runTableQuery() {
const { datasource, queries, range } = this.state; const { datasource, queries } = this.state;
if (!hasQuery(queries)) { if (!hasQuery(queries)) {
return; return;
} }
this.setState({ latency: 0, loading: true, queryError: null, tableResult: null }); this.setState({ latency: 0, loading: true, queryError: null, tableResult: null });
const now = Date.now(); const now = Date.now();
const options = buildQueryOptions({ const options = this.buildQueryOptions({
format: 'table', format: 'table',
interval: datasource.interval,
instant: true, instant: true,
range,
queries: queries.map(q => q.query),
}); });
try { try {
const res = await datasource.query(options); const res = await datasource.query(options);
...@@ -301,7 +318,7 @@ export class Explore extends React.Component<any, IExploreState> { ...@@ -301,7 +318,7 @@ export class Explore extends React.Component<any, IExploreState> {
const selectedDatasource = datasource ? datasource.name : undefined; const selectedDatasource = datasource ? datasource.name : undefined;
return ( return (
<div className={exploreClass}> <div className={exploreClass} ref={this.getRef}>
<div className="navbar"> <div className="navbar">
{position === 'left' ? ( {position === 'left' ? (
<div> <div>
......
export function buildQueryOptions({ format, interval, instant, range, queries }) {
return {
interval,
range,
targets: queries.map(expr => ({
expr,
format,
instant,
})),
};
}
export function generateQueryKey(index = 0) { export function generateQueryKey(index = 0) {
return `Q-${Date.now()}-${Math.random()}-${index}`; return `Q-${Date.now()}-${Math.random()}-${index}`;
} }
......
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