From c49c2389741b09356382e9a964b7172a86c00c3f Mon Sep 17 00:00:00 2001 From: Ryan McKinley Date: Fri, 9 Sep 2022 13:16:24 -0700 Subject: [PATCH] Snapshots: Use the grafana datasource to render snapshot (#54870) --- .../dashboard/components/DebugWizard/utils.ts | 56 +++++++------------ .../grafana/components/QueryEditor.tsx | 32 ++++++++++- .../plugins/datasource/grafana/datasource.ts | 11 ++++ .../app/plugins/datasource/grafana/types.ts | 4 +- 4 files changed, 63 insertions(+), 40 deletions(-) diff --git a/public/app/features/dashboard/components/DebugWizard/utils.ts b/public/app/features/dashboard/components/DebugWizard/utils.ts index 401e00d2193..5eef71659f7 100644 --- a/public/app/features/dashboard/components/DebugWizard/utils.ts +++ b/public/app/features/dashboard/components/DebugWizard/utils.ts @@ -7,8 +7,6 @@ import { DataQuery, PanelData, DataTransformerConfig, - getValueFormat, - formattedValueToString, DataFrameJSON, LoadingState, dataFrameToJSON, @@ -16,6 +14,7 @@ import { } from '@grafana/data'; import { config } from '@grafana/runtime'; import { PanelModel } from 'app/features/dashboard/state'; +import { GrafanaQueryType } from 'app/plugins/datasource/grafana/types'; import { Randomize, randomizeData } from './randomizer'; @@ -74,8 +73,8 @@ export async function getDebugDashboard(panel: PanelModel, rand: Randomize, time withTransforms: false, }) ); + const dsref = panel.datasource; const frames = randomizeData(getPanelDataFrames(data), rand); - const rawFrameContent = JSON.stringify(frames); const grafanaVersion = `${config.buildInfo.version} (${config.buildInfo.commit})`; const queries = saveModel?.targets ?? []; const html = ` @@ -84,15 +83,16 @@ export async function getDebugDashboard(panel: PanelModel, rand: Randomize, time - + ${getTransformsRow(saveModel)} - ${getDataRow(data, rawFrameContent)} + ${getDataRow(data, frames)} ${getAnnotationsRow(data)} @@ -108,11 +108,11 @@ export async function getDebugDashboard(panel: PanelModel, rand: Randomize, time { refId: 'A', datasource: { - type: 'testdata', - uid: '${testdata}', + type: 'grafana', + uid: 'grafana', }, - rawFrameContent, - scenarioId: 'raw_frame', + queryType: GrafanaQueryType.Snapshot, + snapshot: frames, }, ], }; @@ -137,8 +137,8 @@ export async function getDebugDashboard(panel: PanelModel, rand: Randomize, time type: 'table', title: 'Annotations', datasource: { - type: 'testdata', - uid: '${testdata}', + type: 'grafana', + uid: 'grafana', }, targets: [ { @@ -174,19 +174,22 @@ function getTransformsRow(saveModel: any): string { `; } -function getDataRow(data: PanelData, raw: string): string { +function getDataRow(data: PanelData, frames: DataFrameJSON[]): string { let frameCount = data.series.length ?? 0; let fieldCount = 0; + let rowCount = 0; for (const frame of data.series) { fieldCount += frame.fields.length; + rowCount += frame.length; } return ( '' + '' + '' + '' ); @@ -211,8 +214,8 @@ const embeddedDataTemplate: any = { id: 2, title: 'Reproduced with embedded data', datasource: { - type: 'testdata', - uid: '${testdata}', + type: 'grafana', + uid: 'grafana', }, gridPos: { h: 13, @@ -283,23 +286,4 @@ const embeddedDataTemplate: any = { }, ], schemaVersion: 37, - templating: { - list: [ - { - current: { - selected: true, - text: 'gdev-testdata', - value: 'gdev-testdata', - }, - hide: 0, - includeAll: false, - multi: false, - name: 'testdata', - options: [], - query: 'testdata', - skipUrlSync: false, - type: 'datasource', - }, - ], - }, }; diff --git a/public/app/plugins/datasource/grafana/components/QueryEditor.tsx b/public/app/plugins/datasource/grafana/components/QueryEditor.tsx index a72bd5bd881..aae44eac03b 100644 --- a/public/app/plugins/datasource/grafana/components/QueryEditor.tsx +++ b/public/app/plugins/datasource/grafana/components/QueryEditor.tsx @@ -1,3 +1,4 @@ +import pluralize from 'pluralize'; import React, { PureComponent } from 'react'; import { @@ -9,7 +10,7 @@ import { DataFrame, } from '@grafana/data'; import { config, getBackendSrv, getDataSourceSrv } from '@grafana/runtime'; -import { InlineField, Select, Alert, Input, InlineFieldRow } from '@grafana/ui'; +import { InlineField, Select, Alert, Input, InlineFieldRow, InlineLabel } from '@grafana/ui'; import { hasAlphaPanels } from 'app/core/config'; import { SearchQuery } from 'app/features/search/service'; @@ -344,6 +345,18 @@ export class QueryEditor extends PureComponent { ); } + renderSnapshotQuery() { + const { query } = this.props; + + return ( + + + {pluralize('frame', query.snapshot?.length ?? 0, true)} + + + ); + } + onSearchChange = (search: SearchQuery) => { const { query, onChange, onRunQuery } = this.props; @@ -362,6 +375,18 @@ export class QueryEditor extends PureComponent { const { queryType } = query; + // Only show "snapshot" when it already exists + let queryTypes = this.queryTypes; + if (queryType === GrafanaQueryType.Snapshot) { + queryTypes = [ + ...this.queryTypes, + { + label: 'Snapshot', + value: queryType, + }, + ]; + } + return ( <> {queryType === GrafanaQueryType.Search && ( @@ -373,14 +398,15 @@ export class QueryEditor extends PureComponent {
${info.panelType} @ ${saveModel.pluginVersion ?? grafanaVersion}
Queries (${queries.length})Queries ${queries .map((t: DataQuery) => { - return `${t.refId}[${t.datasource?.type}]`; + const ds = t.datasource ?? dsref; + return `${t.refId}[${ds?.type}]`; }) .join(', ')}
Grafana
Data' + `${data.state !== LoadingState.Done ? data.state : ''} ` + - `${frameCount} frames, ${fieldCount} fields` + - `(${formattedValueToString(getValueFormat('decbytes')(raw?.length))} JSON)` + + `${frameCount} frames, ${fieldCount} fields, ` + + `${rowCount} rows ` + + // `(${formattedValueToString(getValueFormat('decbytes')(raw?.length))} JSON)` + '