From 36c798eb2f3201b0a6877a2618baf96f289ad3a4 Mon Sep 17 00:00:00 2001 From: Ryan McKinley Date: Wed, 11 Aug 2021 00:23:41 -0700 Subject: [PATCH] Chore: define base panel type in @grafana/data (#37766) --- .../grafana-data/src/types/annotations.ts | 6 ++- packages/grafana-data/src/types/dashboard.ts | 35 +++++++++++++ packages/grafana-data/src/types/data.ts | 5 +- packages/grafana-data/src/types/dataLink.ts | 2 +- packages/grafana-data/src/types/datasource.ts | 44 ++-------------- packages/grafana-data/src/types/index.ts | 1 + packages/grafana-data/src/types/logs.ts | 2 +- packages/grafana-data/src/types/panel.ts | 14 +----- packages/grafana-data/src/types/query.ts | 50 +++++++++++++++++++ .../grafana-data/src/types/queryRunner.ts | 5 +- packages/grafana-data/src/types/variables.ts | 2 +- .../features/dashboard/state/PanelModel.ts | 6 ++- .../plugins/datasource/grafana/datasource.ts | 8 ++- .../panel/graph/GraphMigrations.test.ts | 6 +-- 14 files changed, 117 insertions(+), 69 deletions(-) create mode 100644 packages/grafana-data/src/types/query.ts diff --git a/packages/grafana-data/src/types/annotations.ts b/packages/grafana-data/src/types/annotations.ts index 7cfa10225a3..559504ee7b9 100644 --- a/packages/grafana-data/src/types/annotations.ts +++ b/packages/grafana-data/src/types/annotations.ts @@ -1,14 +1,16 @@ import { Observable } from 'rxjs'; import { ComponentType } from 'react'; -import { DataQuery, QueryEditorProps } from './datasource'; +import { QueryEditorProps } from './datasource'; import { DataFrame } from './dataFrame'; +import { DataQuery, DatasourceRef } from './query'; /** * This JSON object is stored in the dashboard json model. */ export interface AnnotationQuery { - datasource?: string | null; + datasource?: DatasourceRef | string | null; + enable: boolean; name: string; iconColor: string; diff --git a/packages/grafana-data/src/types/dashboard.ts b/packages/grafana-data/src/types/dashboard.ts index 85dbe95adab..df1af6165a7 100644 --- a/packages/grafana-data/src/types/dashboard.ts +++ b/packages/grafana-data/src/types/dashboard.ts @@ -1,5 +1,40 @@ +import { FieldConfigSource } from './fieldOverrides'; +import { DataQuery, DatasourceRef } from './query'; + export enum DashboardCursorSync { Off, Crosshair, Tooltip, } + +/** + * @public + */ +export interface PanelModel { + /** ID of the panel within the current dashboard */ + id: number; + + /** Panel title */ + title?: string; + + /** Description */ + description?: string; + + /** Panel options */ + options: TOptions; + + /** Field options configuration */ + fieldConfig: FieldConfigSource; + + /** Version of the panel plugin */ + pluginVersion?: string; + + /** The datasource used in all targets */ + datasource?: DatasourceRef | null; + + /** The queries in a panel */ + targets?: DataQuery[]; + + /** alerting v1 object */ + alert?: any; +} diff --git a/packages/grafana-data/src/types/data.ts b/packages/grafana-data/src/types/data.ts index 344c119b4fb..1722c44369e 100644 --- a/packages/grafana-data/src/types/data.ts +++ b/packages/grafana-data/src/types/data.ts @@ -2,6 +2,7 @@ import { FieldConfig } from './dataFrame'; import { DataTransformerConfig } from './transformations'; import { ApplyFieldOverrideOptions } from './fieldOverrides'; import { PanelPluginDataSupport } from '.'; +import { DataTopic } from './query'; export type KeyValue = Record; @@ -17,10 +18,6 @@ export enum LoadingState { Error = 'Error', } -export enum DataTopic { - Annotations = 'annotations', -} - // Should be kept in sync with grafana-plugin-sdk-go/data/frame_meta.go export type PreferredVisualisationType = 'graph' | 'table' | 'logs' | 'trace' | 'nodeGraph'; diff --git a/packages/grafana-data/src/types/dataLink.ts b/packages/grafana-data/src/types/dataLink.ts index e846f411bfd..2d69aedfc7a 100644 --- a/packages/grafana-data/src/types/dataLink.ts +++ b/packages/grafana-data/src/types/dataLink.ts @@ -1,4 +1,4 @@ -import { DataQuery } from './datasource'; +import { DataQuery } from './query'; import { InterpolateFunction } from './panel'; /** diff --git a/packages/grafana-data/src/types/datasource.ts b/packages/grafana-data/src/types/datasource.ts index a370e1a5913..9f3cef1700e 100644 --- a/packages/grafana-data/src/types/datasource.ts +++ b/packages/grafana-data/src/types/datasource.ts @@ -4,7 +4,7 @@ import { GrafanaPlugin, PluginMeta } from './plugin'; import { PanelData } from './panel'; import { LogRowModel } from './logs'; import { AnnotationEvent, AnnotationQuery, AnnotationSupport } from './annotations'; -import { DataTopic, KeyValue, LoadingState, TableData, TimeSeries } from './data'; +import { KeyValue, LoadingState, TableData, TimeSeries } from './data'; import { DataFrame, DataFrameDTO } from './dataFrame'; import { RawTimeRange, TimeRange } from './time'; import { ScopedVars } from './ScopedVars'; @@ -12,6 +12,7 @@ import { CoreApp } from './app'; import { LiveChannelSupport } from './live'; import { CustomVariableSupport, DataSourceVariableSupport, StandardVariableSupport } from './variables'; import { makeClassES5Compatible } from '../utils/makeClassES5Compatible'; +import { DataQuery } from './query'; export interface DataSourcePluginOptionsEditorProps { options: DataSourceSettings; @@ -205,6 +206,9 @@ abstract class DataSourceApi< this.type = instanceSettings.type; this.meta = {} as DataSourcePluginMeta; this.uid = instanceSettings.uid; + if (!this.uid) { + this.uid = this.name; // Internal datasources do not have a UID (-- Grafana --) + } } /** @@ -430,44 +434,6 @@ export interface DataQueryResponse { state?: LoadingState; } -/** - * These are the common properties available to all queries in all datasources - * Specific implementations will extend this interface adding the required properties - * for the given context - */ -export interface DataQuery { - /** - * A - Z - */ - refId: string; - - /** - * true if query is disabled (ie should not be returned to the dashboard) - */ - hide?: boolean; - - /** - * Unique, guid like, string used in explore mode - */ - key?: string; - - /** - * Specify the query flavor - */ - queryType?: string; - - /** - * The data topic results should be attached to - */ - dataTopic?: DataTopic; - - /** - * For mixed data sources the selected datasource is on the query level. - * For non mixed scenarios this is undefined. - */ - datasource?: string | null; -} - export enum DataQueryErrorType { Cancelled = 'cancelled', Timeout = 'timeout', diff --git a/packages/grafana-data/src/types/index.ts b/packages/grafana-data/src/types/index.ts index afc91463a8e..5cb9ed3df37 100644 --- a/packages/grafana-data/src/types/index.ts +++ b/packages/grafana-data/src/types/index.ts @@ -2,6 +2,7 @@ export * from './data'; export * from './dataFrame'; export * from './dataLink'; export * from './dashboard'; +export * from './query'; export * from './annotations'; export * from './logs'; export * from './navModel'; diff --git a/packages/grafana-data/src/types/logs.ts b/packages/grafana-data/src/types/logs.ts index 2d64eed4767..aec5de66df9 100644 --- a/packages/grafana-data/src/types/logs.ts +++ b/packages/grafana-data/src/types/logs.ts @@ -1,7 +1,7 @@ import { Labels } from './data'; import { DataFrame } from './dataFrame'; +import { DataQuery } from './query'; import { AbsoluteTimeRange } from './time'; -import { DataQuery } from './datasource'; /** * Mapping of log level abbreviation to canonical log level. diff --git a/packages/grafana-data/src/types/panel.ts b/packages/grafana-data/src/types/panel.ts index 8da91ebe6a2..e6fd6e1f729 100644 --- a/packages/grafana-data/src/types/panel.ts +++ b/packages/grafana-data/src/types/panel.ts @@ -11,6 +11,7 @@ import { StandardEditorProps } from '../field'; import { OptionsEditorItem } from './OptionsUIRegistryBuilder'; import { OptionEditorConfig } from './options'; import { AlertStateInfo } from './alerts'; +import { PanelModel } from './dashboard'; export type InterpolateFunction = (value: string, scopedVars?: ScopedVars, format?: string | Function) => string; @@ -121,19 +122,6 @@ export interface PanelEditorProps { data?: PanelData; } -export interface PanelModel { - /** ID of the panel within the current dashboard */ - id: number; - alert?: any; - /** Panel options */ - options: TOptions; - /** Field options configuration */ - fieldConfig: FieldConfigSource; - /** Version of the panel plugin */ - pluginVersion?: string; - scopedVars?: ScopedVars; -} - /** * Called when a panel is first loaded with current panel model */ diff --git a/packages/grafana-data/src/types/query.ts b/packages/grafana-data/src/types/query.ts new file mode 100644 index 00000000000..84025ce1bb0 --- /dev/null +++ b/packages/grafana-data/src/types/query.ts @@ -0,0 +1,50 @@ +/** + * Attached to query results (not persisted) + * + * @public + */ +export enum DataTopic { + Annotations = 'annotations', +} + +/** + * In 8.2, this will become an interface + * + * @public + */ +export type DatasourceRef = string; + +/** + * These are the common properties available to all queries in all datasources + * Specific implementations will *extend* this interface adding the required properties + * for the given context + * + * @public + */ +export interface DataQuery { + /** + * A - Z + */ + refId: string; + + /** + * true if query is disabled (ie should not be returned to the dashboard) + */ + hide?: boolean; + + /** + * Unique, guid like, string used in explore mode + */ + key?: string; + + /** + * Specify the query flavor + */ + queryType?: string; + + /** + * For mixed data sources the selected datasource is on the query level. + * For non mixed scenarios this is undefined. + */ + datasource?: DatasourceRef; +} diff --git a/packages/grafana-data/src/types/queryRunner.ts b/packages/grafana-data/src/types/queryRunner.ts index b2d0496e870..ff526eb8dbd 100644 --- a/packages/grafana-data/src/types/queryRunner.ts +++ b/packages/grafana-data/src/types/queryRunner.ts @@ -1,5 +1,6 @@ import { Observable } from 'rxjs'; -import { DataQuery, DataSourceApi } from './datasource'; +import { DataQuery, DatasourceRef } from './query'; +import { DataSourceApi } from './datasource'; import { PanelData } from './panel'; import { ScopedVars } from './ScopedVars'; import { TimeRange, TimeZone } from './time'; @@ -10,7 +11,7 @@ import { TimeRange, TimeZone } from './time'; * @internal */ export interface QueryRunnerOptions { - datasource: string | DataSourceApi | null; + datasource: DatasourceRef | DataSourceApi | null; queries: DataQuery[]; panelId?: number; dashboardId?: number; diff --git a/packages/grafana-data/src/types/variables.ts b/packages/grafana-data/src/types/variables.ts index 5c0aac20832..1c90dcd7b3e 100644 --- a/packages/grafana-data/src/types/variables.ts +++ b/packages/grafana-data/src/types/variables.ts @@ -1,8 +1,8 @@ import { ComponentType } from 'react'; import { Observable } from 'rxjs'; +import { DataQuery } from './query'; import { - DataQuery, DataQueryRequest, DataQueryResponse, DataSourceApi, diff --git a/public/app/features/dashboard/state/PanelModel.ts b/public/app/features/dashboard/state/PanelModel.ts index 1b9bb9db61e..05f2768a659 100644 --- a/public/app/features/dashboard/state/PanelModel.ts +++ b/public/app/features/dashboard/state/PanelModel.ts @@ -17,6 +17,8 @@ import { PanelPluginDataSupport, ScopedVars, urlUtil, + PanelModel as IPanelModel, + DatasourceRef, } from '@grafana/data'; import { EDIT_PANEL_ID } from 'app/core/constants'; import config from 'app/core/config'; @@ -124,7 +126,7 @@ const defaults: any = { title: '', }; -export class PanelModel implements DataConfigSource { +export class PanelModel implements DataConfigSource, IPanelModel { /* persisted id, used in URL to identify a panel */ id!: number; editSourceId?: number; @@ -144,7 +146,7 @@ export class PanelModel implements DataConfigSource { panels?: any; declare targets: DataQuery[]; transformations?: DataTransformerConfig[]; - datasource: string | null = null; + datasource: DatasourceRef | null = null; thresholds?: any; pluginVersion?: string; diff --git a/public/app/plugins/datasource/grafana/datasource.ts b/public/app/plugins/datasource/grafana/datasource.ts index 7750db46625..4a127e59079 100644 --- a/public/app/plugins/datasource/grafana/datasource.ts +++ b/public/app/plugins/datasource/grafana/datasource.ts @@ -8,6 +8,7 @@ import { DataQueryResponse, DataSourceApi, DataSourceInstanceSettings, + DatasourceRef, isValidLiveChannelAddress, parseLiveChannelAddress, StreamingFrameOptions, @@ -17,6 +18,7 @@ import { import { GrafanaAnnotationQuery, GrafanaAnnotationType, GrafanaQuery, GrafanaQueryType } from './types'; import AnnotationQueryEditor from './components/AnnotationQueryEditor'; import { getDashboardSrv } from '../../../features/dashboard/services/DashboardSrv'; +import { isString } from 'lodash'; let counter = 100; @@ -37,7 +39,11 @@ export class GrafanaDatasource extends DataSourceApi { return json; }, prepareQuery(anno: AnnotationQuery): GrafanaQuery { - return { ...anno, refId: anno.name, queryType: GrafanaQueryType.Annotations }; + let datasource: DatasourceRef | undefined | null = undefined; + if (isString(anno.datasource)) { + datasource = anno.datasource as DatasourceRef; + } + return { ...anno, refId: anno.name, queryType: GrafanaQueryType.Annotations, datasource }; }, }; } diff --git a/public/app/plugins/panel/graph/GraphMigrations.test.ts b/public/app/plugins/panel/graph/GraphMigrations.test.ts index e0651433334..b6570bccd13 100644 --- a/public/app/plugins/panel/graph/GraphMigrations.test.ts +++ b/public/app/plugins/panel/graph/GraphMigrations.test.ts @@ -1,4 +1,4 @@ -import { PanelModel, FieldConfigSource } from '@grafana/data'; +import { PanelModel, FieldConfigSource, DataQuery } from '@grafana/data'; import { graphPanelMigrationHandler } from './GraphMigrations'; describe('Graph Panel Migrations', () => { @@ -52,7 +52,7 @@ describe('Graph Panel Migrations', () => { spaceLength: 10, stack: false, steppedLine: false, - targets: [ + targets: ([ { alias: 'Foo datacenter', labels: 'datacenter=foo,region=us-east-1', @@ -71,7 +71,7 @@ describe('Graph Panel Migrations', () => { refId: 'C', scenarioId: 'random_walk', }, - ], + ] as unknown) as DataQuery[], thresholds: [], timeFrom: null, timeRegions: [],