mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
wip: typings
This commit is contained in:
@@ -8,14 +8,14 @@ export interface DataQueryResponse {
|
|||||||
|
|
||||||
export interface DataQuery {
|
export interface DataQuery {
|
||||||
refId: string;
|
refId: string;
|
||||||
[key: string]: any;
|
hide?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface DataQueryOptions {
|
export interface DataQueryOptions<TQuery extends DataQuery = DataQuery> {
|
||||||
timezone: string;
|
timezone: string;
|
||||||
range: TimeRange;
|
range: TimeRange;
|
||||||
rangeRaw: RawTimeRange;
|
rangeRaw: RawTimeRange;
|
||||||
targets: DataQuery[];
|
targets: TQuery[];
|
||||||
panelId: number;
|
panelId: number;
|
||||||
dashboardId: number;
|
dashboardId: number;
|
||||||
cacheTimeout?: string;
|
cacheTimeout?: string;
|
||||||
|
|||||||
@@ -2,10 +2,11 @@ import { ComponentClass } from 'react';
|
|||||||
import { PanelProps, PanelOptionsProps } from './panel';
|
import { PanelProps, PanelOptionsProps } from './panel';
|
||||||
import { DataQueryOptions, DataQuery, DataQueryResponse, QueryHint } from './datasource';
|
import { DataQueryOptions, DataQuery, DataQueryResponse, QueryHint } from './datasource';
|
||||||
|
|
||||||
export interface DataSourceApi {
|
export interface DataSourceApi<TQuery extends DataQuery = DataQuery> {
|
||||||
name: string;
|
// set externally by grafana
|
||||||
meta: PluginMeta;
|
name?: string;
|
||||||
pluginExports: PluginExports;
|
meta?: PluginMeta;
|
||||||
|
pluginExports?: PluginExports;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* min interval range
|
* min interval range
|
||||||
@@ -15,7 +16,7 @@ export interface DataSourceApi {
|
|||||||
/**
|
/**
|
||||||
* Imports queries from a different datasource
|
* Imports queries from a different datasource
|
||||||
*/
|
*/
|
||||||
importQueries?(queries: DataQuery[], originMeta: PluginMeta): Promise<DataQuery[]>;
|
importQueries?(queries: TQuery[], originMeta: PluginMeta): Promise<TQuery[]>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes a datasource after instantiation
|
* Initializes a datasource after instantiation
|
||||||
@@ -25,7 +26,7 @@ export interface DataSourceApi {
|
|||||||
/**
|
/**
|
||||||
* Main metrics / data query action
|
* Main metrics / data query action
|
||||||
*/
|
*/
|
||||||
query(options: DataQueryOptions): Promise<DataQueryResponse>;
|
query(options: DataQueryOptions<TQuery>): Promise<DataQueryResponse>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test & verify datasource settings & connection details
|
* Test & verify datasource settings & connection details
|
||||||
@@ -35,12 +36,12 @@ export interface DataSourceApi {
|
|||||||
/**
|
/**
|
||||||
* Get hints for query improvements
|
* Get hints for query improvements
|
||||||
*/
|
*/
|
||||||
getQueryHints(query: DataQuery, results: any[], ...rest: any): QueryHint[];
|
getQueryHints?(query: TQuery, results: any[], ...rest: any): QueryHint[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface QueryEditorProps {
|
export interface QueryEditorProps<DSType extends DataSourceApi = DataSourceApi, TQuery extends DataQuery = DataQuery> {
|
||||||
datasource: DataSourceApi;
|
datasource: DSType;
|
||||||
query: DataQuery;
|
query: TQuery;
|
||||||
onExecuteQuery?: () => void;
|
onExecuteQuery?: () => void;
|
||||||
onQueryChange?: (value: DataQuery) => void;
|
onQueryChange?: (value: DataQuery) => void;
|
||||||
}
|
}
|
||||||
|
|||||||
14
public/app/features/.all.ts@neomake_22624_74.ts
Normal file
14
public/app/features/.all.ts@neomake_22624_74.ts
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
import './annotations/all';
|
||||||
|
import './templating/all';
|
||||||
|
import './plugins/all';
|
||||||
|
import './dashboard/all';
|
||||||
|
import './playlist/all';
|
||||||
|
import './panel/all';
|
||||||
|
import './org/all';
|
||||||
|
import './admin';
|
||||||
|
import './alerting/NotificationsEditCtrl';
|
||||||
|
import './alerting/NotificationsListCtrl';
|
||||||
|
import './manage-dashboards';
|
||||||
|
import './teams/CreateTeamCtrl';
|
||||||
|
import './profile/all';
|
||||||
|
import './datasources/settings/dsHttpSettings';
|
||||||
@@ -11,7 +11,7 @@ import { getNextCharacter, getPreviousCousin } from 'app/features/explore/utils/
|
|||||||
import BracesPlugin from 'app/features/explore/slate-plugins/braces';
|
import BracesPlugin from 'app/features/explore/slate-plugins/braces';
|
||||||
import RunnerPlugin from 'app/features/explore/slate-plugins/runner';
|
import RunnerPlugin from 'app/features/explore/slate-plugins/runner';
|
||||||
import QueryField, { TypeaheadInput, QueryFieldState } from 'app/features/explore/QueryField';
|
import QueryField, { TypeaheadInput, QueryFieldState } from 'app/features/explore/QueryField';
|
||||||
import { DataQuery } from '@grafana/ui/src/types';
|
import { PromQuery } from '../types';
|
||||||
|
|
||||||
const HISTOGRAM_GROUP = '__histograms__';
|
const HISTOGRAM_GROUP = '__histograms__';
|
||||||
const METRIC_MARK = 'metric';
|
const METRIC_MARK = 'metric';
|
||||||
@@ -88,13 +88,13 @@ interface CascaderOption {
|
|||||||
interface PromQueryFieldProps {
|
interface PromQueryFieldProps {
|
||||||
datasource: any;
|
datasource: any;
|
||||||
error?: string | JSX.Element;
|
error?: string | JSX.Element;
|
||||||
initialQuery: DataQuery;
|
initialQuery: PromQuery;
|
||||||
hint?: any;
|
hint?: any;
|
||||||
history?: any[];
|
history?: any[];
|
||||||
metricsByPrefix?: CascaderOption[];
|
metricsByPrefix?: CascaderOption[];
|
||||||
onClickHintFix?: (action: any) => void;
|
onClickHintFix?: (action: any) => void;
|
||||||
onPressEnter?: () => void;
|
onPressEnter?: () => void;
|
||||||
onQueryChange?: (value: DataQuery, override?: boolean) => void;
|
onQueryChange?: (value: PromQuery, override?: boolean) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface PromQueryFieldState {
|
interface PromQueryFieldState {
|
||||||
@@ -166,7 +166,7 @@ class PromQueryField extends React.PureComponent<PromQueryFieldProps, PromQueryF
|
|||||||
// Send text change to parent
|
// Send text change to parent
|
||||||
const { initialQuery, onQueryChange } = this.props;
|
const { initialQuery, onQueryChange } = this.props;
|
||||||
if (onQueryChange) {
|
if (onQueryChange) {
|
||||||
const query: DataQuery = {
|
const query: PromQuery = {
|
||||||
...initialQuery,
|
...initialQuery,
|
||||||
expr: value,
|
expr: value,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,57 +1,24 @@
|
|||||||
|
// Libraries
|
||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
|
|
||||||
import $ from 'jquery';
|
import $ from 'jquery';
|
||||||
|
|
||||||
|
// Services & Utils
|
||||||
import kbn from 'app/core/utils/kbn';
|
import kbn from 'app/core/utils/kbn';
|
||||||
import * as dateMath from 'app/core/utils/datemath';
|
import * as dateMath from 'app/core/utils/datemath';
|
||||||
import PrometheusMetricFindQuery from './metric_find_query';
|
import PrometheusMetricFindQuery from './metric_find_query';
|
||||||
import { ResultTransformer } from './result_transformer';
|
import { ResultTransformer } from './result_transformer';
|
||||||
import PrometheusLanguageProvider from './language_provider';
|
import PrometheusLanguageProvider from './language_provider';
|
||||||
import { BackendSrv } from 'app/core/services/backend_srv';
|
import { BackendSrv } from 'app/core/services/backend_srv';
|
||||||
|
|
||||||
import addLabelToQuery from './add_label_to_query';
|
import addLabelToQuery from './add_label_to_query';
|
||||||
import { getQueryHints } from './query_hints';
|
import { getQueryHints } from './query_hints';
|
||||||
import { expandRecordingRules } from './language_utils';
|
import { expandRecordingRules } from './language_utils';
|
||||||
import { DataQuery } from '@grafana/ui/src/types';
|
|
||||||
|
// Types
|
||||||
|
import { PromQuery } from './types';
|
||||||
|
import { DataQueryOptions, DataSourceApi } from '@grafana/ui/src/types';
|
||||||
import { ExploreUrlState } from 'app/types/explore';
|
import { ExploreUrlState } from 'app/types/explore';
|
||||||
|
|
||||||
export function alignRange(start, end, step) {
|
export class PrometheusDatasource implements DataSourceApi<PromQuery> {
|
||||||
const alignedEnd = Math.ceil(end / step) * step;
|
|
||||||
const alignedStart = Math.floor(start / step) * step;
|
|
||||||
return {
|
|
||||||
end: alignedEnd,
|
|
||||||
start: alignedStart,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export function extractRuleMappingFromGroups(groups: any[]) {
|
|
||||||
return groups.reduce(
|
|
||||||
(mapping, group) =>
|
|
||||||
group.rules.filter(rule => rule.type === 'recording').reduce(
|
|
||||||
(acc, rule) => ({
|
|
||||||
...acc,
|
|
||||||
[rule.name]: rule.query,
|
|
||||||
}),
|
|
||||||
mapping
|
|
||||||
),
|
|
||||||
{}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function prometheusRegularEscape(value) {
|
|
||||||
if (typeof value === 'string') {
|
|
||||||
return value.replace(/'/g, "\\\\'");
|
|
||||||
}
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function prometheusSpecialRegexEscape(value) {
|
|
||||||
if (typeof value === 'string') {
|
|
||||||
return prometheusRegularEscape(value.replace(/\\/g, '\\\\\\\\').replace(/[$^*{}\[\]+?.()]/g, '\\\\$&'));
|
|
||||||
}
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
export class PrometheusDatasource {
|
|
||||||
type: string;
|
type: string;
|
||||||
editorSrc: string;
|
editorSrc: string;
|
||||||
name: string;
|
name: string;
|
||||||
@@ -149,7 +116,7 @@ export class PrometheusDatasource {
|
|||||||
return this.templateSrv.variableExists(target.expr);
|
return this.templateSrv.variableExists(target.expr);
|
||||||
}
|
}
|
||||||
|
|
||||||
query(options) {
|
query(options: DataQueryOptions<PromQuery>) {
|
||||||
const start = this.getPrometheusTime(options.range.from, false);
|
const start = this.getPrometheusTime(options.range.from, false);
|
||||||
const end = this.getPrometheusTime(options.range.to, true);
|
const end = this.getPrometheusTime(options.range.to, true);
|
||||||
|
|
||||||
@@ -423,7 +390,7 @@ export class PrometheusDatasource {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
getExploreState(queries: DataQuery[]): Partial<ExploreUrlState> {
|
getExploreState(queries: PromQuery[]): Partial<ExploreUrlState> {
|
||||||
let state: Partial<ExploreUrlState> = { datasource: this.name };
|
let state: Partial<ExploreUrlState> = { datasource: this.name };
|
||||||
if (queries && queries.length > 0) {
|
if (queries && queries.length > 0) {
|
||||||
const expandedQueries = queries.map(query => ({
|
const expandedQueries = queries.map(query => ({
|
||||||
@@ -438,7 +405,7 @@ export class PrometheusDatasource {
|
|||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
||||||
getQueryHints(query: DataQuery, result: any[]) {
|
getQueryHints(query: PromQuery, result: any[]) {
|
||||||
return getQueryHints(query.expr || '', result, this);
|
return getQueryHints(query.expr || '', result, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -457,7 +424,7 @@ export class PrometheusDatasource {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
modifyQuery(query: DataQuery, action: any): DataQuery {
|
modifyQuery(query: PromQuery, action: any): PromQuery {
|
||||||
let expression = query.expr || '';
|
let expression = query.expr || '';
|
||||||
switch (action.type) {
|
switch (action.type) {
|
||||||
case 'ADD_FILTER': {
|
case 'ADD_FILTER': {
|
||||||
@@ -507,3 +474,40 @@ export class PrometheusDatasource {
|
|||||||
return this.resultTransformer.getOriginalMetricName(labelData);
|
return this.resultTransformer.getOriginalMetricName(labelData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function alignRange(start, end, step) {
|
||||||
|
const alignedEnd = Math.ceil(end / step) * step;
|
||||||
|
const alignedStart = Math.floor(start / step) * step;
|
||||||
|
return {
|
||||||
|
end: alignedEnd,
|
||||||
|
start: alignedStart,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function extractRuleMappingFromGroups(groups: any[]) {
|
||||||
|
return groups.reduce(
|
||||||
|
(mapping, group) =>
|
||||||
|
group.rules.filter(rule => rule.type === 'recording').reduce(
|
||||||
|
(acc, rule) => ({
|
||||||
|
...acc,
|
||||||
|
[rule.name]: rule.query,
|
||||||
|
}),
|
||||||
|
mapping
|
||||||
|
),
|
||||||
|
{}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function prometheusRegularEscape(value) {
|
||||||
|
if (typeof value === 'string') {
|
||||||
|
return value.replace(/'/g, "\\\\'");
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function prometheusSpecialRegexEscape(value) {
|
||||||
|
if (typeof value === 'string') {
|
||||||
|
return prometheusRegularEscape(value.replace(/\\/g, '\\\\\\\\').replace(/[$^*{}\[\]+?.()]/g, '\\\\$&'));
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|||||||
6
public/app/plugins/datasource/prometheus/types.ts
Normal file
6
public/app/plugins/datasource/prometheus/types.ts
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
import { DataQuery } from '@grafana/ui/src/types';
|
||||||
|
|
||||||
|
export interface PromQuery extends DataQuery {
|
||||||
|
expr: string;
|
||||||
|
}
|
||||||
|
|
||||||
@@ -10,18 +10,17 @@ import { FormLabel, Select, SelectOptionItem } from '@grafana/ui';
|
|||||||
|
|
||||||
// Types
|
// Types
|
||||||
import { QueryEditorProps } from '@grafana/ui/src/types';
|
import { QueryEditorProps } from '@grafana/ui/src/types';
|
||||||
|
import { TestDataDatasource } from './datasource';
|
||||||
interface Scenario {
|
import { TestDataQuery, Scenario } from './types';
|
||||||
id: string;
|
|
||||||
name: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface State {
|
interface State {
|
||||||
scenarioList: Scenario[];
|
scenarioList: Scenario[];
|
||||||
current: Scenario | null;
|
current: Scenario | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class QueryEditor extends PureComponent<QueryEditorProps> {
|
type Props = QueryEditorProps<TestDataDatasource, TestDataQuery>;
|
||||||
|
|
||||||
|
export class QueryEditor extends PureComponent<Props> {
|
||||||
backendSrv: BackendSrv = getBackendSrv();
|
backendSrv: BackendSrv = getBackendSrv();
|
||||||
|
|
||||||
state: State = {
|
state: State = {
|
||||||
|
|||||||
@@ -1,15 +1,17 @@
|
|||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
import TableModel from 'app/core/table_model';
|
import TableModel from 'app/core/table_model';
|
||||||
|
import { DataSourceApi, DataQueryOptions } from '@grafana/ui';
|
||||||
|
import { TestDataQuery } from './types';
|
||||||
|
|
||||||
class TestDataDatasource {
|
export class TestDataDatasource implements DataSourceApi<TestDataQuery> {
|
||||||
id: any;
|
id: number;
|
||||||
|
|
||||||
/** @ngInject */
|
/** @ngInject */
|
||||||
constructor(instanceSettings, private backendSrv, private $q) {
|
constructor(instanceSettings, private backendSrv, private $q) {
|
||||||
this.id = instanceSettings.id;
|
this.id = instanceSettings.id;
|
||||||
}
|
}
|
||||||
|
|
||||||
query(options) {
|
query(options: DataQueryOptions<TestDataQuery>) {
|
||||||
const queries = _.filter(options.targets, item => {
|
const queries = _.filter(options.targets, item => {
|
||||||
return item.hide !== true;
|
return item.hide !== true;
|
||||||
}).map(item => {
|
}).map(item => {
|
||||||
@@ -93,4 +95,3 @@ class TestDataDatasource {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export { TestDataDatasource };
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { TestDataDatasource } from './datasource';
|
import { TestDataDatasource } from './datasource';
|
||||||
import { TestDataQueryCtrl } from './query_ctrl';
|
// import { TestDataQueryCtrl } from './query_ctrl';
|
||||||
// import { QueryEditor } from './QueryEditor';
|
import { QueryEditor } from './QueryEditor';
|
||||||
|
|
||||||
class TestDataAnnotationsQueryCtrl {
|
class TestDataAnnotationsQueryCtrl {
|
||||||
annotation: any;
|
annotation: any;
|
||||||
@@ -11,8 +11,8 @@ class TestDataAnnotationsQueryCtrl {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export {
|
export {
|
||||||
// QueryEditor,
|
QueryEditor,
|
||||||
TestDataDatasource as Datasource,
|
TestDataDatasource as Datasource,
|
||||||
TestDataQueryCtrl as QueryCtrl,
|
// TestDataQueryCtrl as QueryCtrl,
|
||||||
TestDataAnnotationsQueryCtrl as AnnotationsQueryCtrl,
|
TestDataAnnotationsQueryCtrl as AnnotationsQueryCtrl,
|
||||||
};
|
};
|
||||||
|
|||||||
11
public/app/plugins/datasource/testdata/types.ts
vendored
Normal file
11
public/app/plugins/datasource/testdata/types.ts
vendored
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
import { DataQuery } from '@grafana/ui/src/types';
|
||||||
|
|
||||||
|
export interface TestDataQuery extends DataQuery {
|
||||||
|
scenarioId: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface Scenario {
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user