mirror of
https://github.com/grafana/grafana.git
synced 2025-08-02 23:53:10 +08:00
more typings work around data query and data source
This commit is contained in:
@ -7,8 +7,26 @@ export interface DataQueryResponse {
|
||||
}
|
||||
|
||||
export interface DataQuery {
|
||||
/**
|
||||
* A - Z
|
||||
*/
|
||||
refId: string;
|
||||
|
||||
/**
|
||||
* true if query is disabled (ie not executed / sent to TSDB)
|
||||
*/
|
||||
hide?: boolean;
|
||||
|
||||
/**
|
||||
* Unique, guid like, string used in explore mode
|
||||
*/
|
||||
key?: string;
|
||||
|
||||
/**
|
||||
* For mixed data sources the selected datasource is on the query level.
|
||||
* For non mixed scenarios this is undefined.
|
||||
*/
|
||||
datasource?: string | null;
|
||||
}
|
||||
|
||||
export interface DataQueryOptions<TQuery extends DataQuery = DataQuery> {
|
||||
|
@ -203,7 +203,7 @@ export function ensureQueries(queries?: DataQuery[]): DataQuery[] {
|
||||
/**
|
||||
* A target is non-empty when it has keys (with non-empty values) other than refId and key.
|
||||
*/
|
||||
export function hasNonEmptyQuery(queries: DataQuery[]): boolean {
|
||||
export function hasNonEmptyQuery<TQuery extends DataQuery = any>(queries: TQuery[]): boolean {
|
||||
return (
|
||||
queries &&
|
||||
queries.some(
|
||||
@ -280,7 +280,11 @@ export function makeTimeSeriesList(dataList) {
|
||||
/**
|
||||
* Update the query history. Side-effect: store history in local storage
|
||||
*/
|
||||
export function updateHistory(history: HistoryItem[], datasourceId: string, queries: DataQuery[]): HistoryItem[] {
|
||||
export function updateHistory<T extends DataQuery = any>(
|
||||
history: Array<HistoryItem<T>>,
|
||||
datasourceId: string,
|
||||
queries: T[]
|
||||
): Array<HistoryItem<T>> {
|
||||
const ts = Date.now();
|
||||
queries.forEach(query => {
|
||||
history = [{ query, ts }, ...history];
|
||||
|
@ -1,16 +1,21 @@
|
||||
// Libraries
|
||||
import React from 'react';
|
||||
import Cascader from 'rc-cascader';
|
||||
import PluginPrism from 'slate-prism';
|
||||
import Prism from 'prismjs';
|
||||
|
||||
import { DataQuery } from '@grafana/ui/src/types';
|
||||
import { TypeaheadOutput } from 'app/types/explore';
|
||||
// Components
|
||||
import QueryField, { TypeaheadInput, QueryFieldState } from 'app/features/explore/QueryField';
|
||||
|
||||
// Utils & Services
|
||||
// dom also includes Element polyfills
|
||||
import { getNextCharacter, getPreviousCousin } from 'app/features/explore/utils/dom';
|
||||
import BracesPlugin from 'app/features/explore/slate-plugins/braces';
|
||||
import RunnerPlugin from 'app/features/explore/slate-plugins/runner';
|
||||
import QueryField, { TypeaheadInput, QueryFieldState } from 'app/features/explore/QueryField';
|
||||
|
||||
// Types
|
||||
import { LokiQuery } from '../types';
|
||||
import { TypeaheadOutput } from 'app/types/explore';
|
||||
|
||||
const PRISM_SYNTAX = 'promql';
|
||||
|
||||
@ -63,10 +68,10 @@ interface LokiQueryFieldProps {
|
||||
error?: string | JSX.Element;
|
||||
hint?: any;
|
||||
history?: any[];
|
||||
initialQuery?: DataQuery;
|
||||
initialQuery?: LokiQuery;
|
||||
onClickHintFix?: (action: any) => void;
|
||||
onPressEnter?: () => void;
|
||||
onQueryChange?: (value: DataQuery, override?: boolean) => void;
|
||||
onQueryChange?: (value: LokiQuery, override?: boolean) => void;
|
||||
}
|
||||
|
||||
interface LokiQueryFieldState {
|
||||
|
@ -1,5 +1,6 @@
|
||||
import moment from 'moment';
|
||||
import LokiDatasource from './datasource';
|
||||
import { LokiQuery } from './types';
|
||||
import { getQueryOptions } from 'test/helpers/getQueryOptions';
|
||||
|
||||
describe('LokiDatasource', () => {
|
||||
const instanceSettings: any = {
|
||||
@ -14,19 +15,13 @@ describe('LokiDatasource', () => {
|
||||
replace: a => a,
|
||||
};
|
||||
|
||||
const range = {
|
||||
from: moment(),
|
||||
to: moment(),
|
||||
raw: {
|
||||
from: 'now-6h',
|
||||
to: 'now'
|
||||
}
|
||||
};
|
||||
|
||||
test('should use default max lines when no limit given', () => {
|
||||
const ds = new LokiDatasource(instanceSettings, backendSrvMock, templateSrvMock);
|
||||
backendSrvMock.datasourceRequest = jest.fn();
|
||||
ds.query({ range, targets: [{ expr: 'foo', refId: 'B' }] });
|
||||
const options = getQueryOptions<LokiQuery>({ targets: [{ expr: 'foo', refId: 'B' }] });
|
||||
|
||||
ds.query(options);
|
||||
|
||||
expect(backendSrvMock.datasourceRequest.mock.calls.length).toBe(1);
|
||||
expect(backendSrvMock.datasourceRequest.mock.calls[0][0].url).toContain('limit=1000');
|
||||
});
|
||||
@ -36,7 +31,10 @@ describe('LokiDatasource', () => {
|
||||
const customSettings = { ...instanceSettings, jsonData: customData };
|
||||
const ds = new LokiDatasource(customSettings, backendSrvMock, templateSrvMock);
|
||||
backendSrvMock.datasourceRequest = jest.fn();
|
||||
ds.query({ range, targets: [{ expr: 'foo', refId: 'A' }] });
|
||||
|
||||
const options = getQueryOptions<LokiQuery>({ targets: [{ expr: 'foo', refId: 'B' }] });
|
||||
ds.query(options);
|
||||
|
||||
expect(backendSrvMock.datasourceRequest.mock.calls.length).toBe(1);
|
||||
expect(backendSrvMock.datasourceRequest.mock.calls[0][0].url).toContain('limit=20');
|
||||
});
|
||||
|
25
public/test/helpers/getQueryOptions.ts
Normal file
25
public/test/helpers/getQueryOptions.ts
Normal file
@ -0,0 +1,25 @@
|
||||
import { DataQueryOptions, DataQuery } from '@grafana/ui';
|
||||
import moment from 'moment';
|
||||
|
||||
|
||||
export function getQueryOptions<TQuery extends DataQuery>(options: Partial<DataQueryOptions<TQuery>>): DataQueryOptions<TQuery> {
|
||||
const raw = {from: 'now', to: 'now-1h'};
|
||||
const range = { from: moment(), to: moment(), raw: raw};
|
||||
|
||||
const defaults: DataQueryOptions<TQuery> = {
|
||||
range: range,
|
||||
rangeRaw: raw,
|
||||
targets: [],
|
||||
scopedVars: {},
|
||||
timezone: 'browser',
|
||||
panelId: 1,
|
||||
dashboardId: 1,
|
||||
interval: '60s',
|
||||
intervalMs: 60000,
|
||||
maxDataPoints: 500,
|
||||
};
|
||||
|
||||
Object.assign(defaults, options);
|
||||
|
||||
return defaults;
|
||||
}
|
Reference in New Issue
Block a user