From 54a3e2d1d1d710dc9f7be4b831a31a7810c71186 Mon Sep 17 00:00:00 2001 From: David Kaltschmidt Date: Wed, 24 Oct 2018 14:55:56 +0200 Subject: [PATCH] Added types to query rows --- public/app/features/explore/QueryRows.tsx | 40 ++++++++++++++++--- .../datasource/prometheus/query_hints.ts | 4 +- public/app/types/explore.ts | 19 ++++++++- 3 files changed, 55 insertions(+), 8 deletions(-) diff --git a/public/app/features/explore/QueryRows.tsx b/public/app/features/explore/QueryRows.tsx index 0b0d7085d2d..4024022851e 100644 --- a/public/app/features/explore/QueryRows.tsx +++ b/public/app/features/explore/QueryRows.tsx @@ -1,12 +1,12 @@ import React, { PureComponent } from 'react'; -import { QueryTransaction } from 'app/types/explore'; +import { QueryTransaction, HistoryItem, Query, QueryHint } from 'app/types/explore'; // TODO make this datasource-plugin-dependent import QueryField from './PromQueryField'; import QueryTransactions from './QueryTransactions'; -function getFirstHintFromTransactions(transactions: QueryTransaction[]) { +function getFirstHintFromTransactions(transactions: QueryTransaction[]): QueryHint { const transaction = transactions.find(qt => qt.hints && qt.hints.length > 0); if (transaction) { return transaction.hints[0]; @@ -14,7 +14,30 @@ function getFirstHintFromTransactions(transactions: QueryTransaction[]) { return undefined; } -class QueryRow extends PureComponent { +interface QueryRowEventHandlers { + onAddQueryRow: (index: number) => void; + onChangeQuery: (value: string, index: number, override?: boolean) => void; + onClickHintFix: (action: object, index?: number) => void; + onExecuteQuery: () => void; + onRemoveQueryRow: (index: number) => void; +} + +interface QueryRowCommonProps { + className?: string; + history: HistoryItem[]; + request: (url: string) => Promise; + // Temporarily + supportsLogs?: boolean; + transactions: QueryTransaction[]; +} + +type QueryRowProps = QueryRowCommonProps & + QueryRowEventHandlers & { + index: number; + query: string; + }; + +class QueryRow extends PureComponent { onChangeQuery = (value, override?: boolean) => { const { index, onChangeQuery } = this.props; if (onChangeQuery) { @@ -56,7 +79,7 @@ class QueryRow extends PureComponent { render() { const { history, query, request, supportsLogs, transactions } = this.props; - const transactionWithError = transactions.find(t => t.error); + const transactionWithError = transactions.find(t => t.error !== undefined); const hint = getFirstHintFromTransactions(transactions); const queryError = transactionWithError ? transactionWithError.error : null; return ( @@ -93,9 +116,14 @@ class QueryRow extends PureComponent { } } -export default class QueryRows extends PureComponent { +type QueryRowsProps = QueryRowCommonProps & + QueryRowEventHandlers & { + queries: Query[]; + }; + +export default class QueryRows extends PureComponent { render() { - const { className = '', queries, queryHints, transactions, ...handlers } = this.props; + const { className = '', queries, transactions, ...handlers } = this.props; return (
{queries.map((q, index) => ( diff --git a/public/app/plugins/datasource/prometheus/query_hints.ts b/public/app/plugins/datasource/prometheus/query_hints.ts index cfd04c766ba..388a9be48d1 100644 --- a/public/app/plugins/datasource/prometheus/query_hints.ts +++ b/public/app/plugins/datasource/prometheus/query_hints.ts @@ -1,6 +1,8 @@ import _ from 'lodash'; -export function getQueryHints(query: string, series?: any[], datasource?: any): any[] { +import { QueryHint } from 'app/types/explore'; + +export function getQueryHints(query: string, series?: any[], datasource?: any): QueryHint[] { const hints = []; // ..._bucket metric needs a histogram_quantile() diff --git a/public/app/types/explore.ts b/public/app/types/explore.ts index 918dd4e4483..8746dd2edf6 100644 --- a/public/app/types/explore.ts +++ b/public/app/types/explore.ts @@ -18,11 +18,28 @@ export interface Query { key?: string; } +export interface QueryFix { + type: string; + label: string; + action?: QueryFixAction; +} + +export interface QueryFixAction { + type: string; + query?: string; +} + +export interface QueryHint { + type: string; + label: string; + fix?: QueryFix; +} + export interface QueryTransaction { id: string; done: boolean; error?: string; - hints?: any[]; + hints?: QueryHint[]; latency: number; options: any; query: string;