From fc2bceca48b1c5ac49dafc951f61acbf7500ff9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zolt=C3=A1n=20Bedi?= Date: Mon, 21 Oct 2024 15:50:59 +0200 Subject: [PATCH 1/3] chore: rename datasource --- src/DataSource.ts | 5 +++-- src/components/MetaInspector.tsx | 4 ++-- src/components/QueryEditor.tsx | 4 ++-- src/module.ts | 6 ++++-- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/DataSource.ts b/src/DataSource.ts index 72262d04..62313615 100644 --- a/src/DataSource.ts +++ b/src/DataSource.ts @@ -7,11 +7,12 @@ import { } from '@grafana/data'; import { DataSourceOptions } from '@grafana/google-sdk'; import { DataSourceWithBackend, getTemplateSrv } from '@grafana/runtime'; -import { SheetsQuery } from './types'; import { Observable } from 'rxjs'; import { trackRequest } from 'tracking'; +import { SheetsQuery } from './types'; +import { GoogleSheetsVariableSupport } from './variables'; -export class DataSource extends DataSourceWithBackend { +export class GoogleSheetsDataSource extends DataSourceWithBackend { authType: string; constructor(instanceSettings: DataSourceInstanceSettings) { super(instanceSettings); diff --git a/src/components/MetaInspector.tsx b/src/components/MetaInspector.tsx index e2878359..74903b38 100644 --- a/src/components/MetaInspector.tsx +++ b/src/components/MetaInspector.tsx @@ -1,10 +1,10 @@ import { DataFrame, MetadataInspectorProps } from '@grafana/data'; import { DataSourceOptions } from '@grafana/google-sdk'; import React, { PureComponent } from 'react'; -import { DataSource } from '../DataSource'; +import { GoogleSheetsDataSource } from '../datasource'; import { SheetResponseMeta, SheetsQuery } from '../types'; -export type Props = MetadataInspectorProps; +export type Props = MetadataInspectorProps; export class MetaInspector extends PureComponent { state = { index: 0 }; diff --git a/src/components/QueryEditor.tsx b/src/components/QueryEditor.tsx index 7cab9af7..02539dfb 100644 --- a/src/components/QueryEditor.tsx +++ b/src/components/QueryEditor.tsx @@ -2,12 +2,12 @@ import { QueryEditorProps } from '@grafana/data'; import { DataSourceOptions } from '@grafana/google-sdk'; import { InlineFieldRow, InlineFormLabel, InlineSwitch, Input, LinkButton, Segment, SegmentAsync } from '@grafana/ui'; import React, { ChangeEvent, PureComponent } from 'react'; -import { DataSource } from '../DataSource'; +import { GoogleSheetsDataSource } from '../datasource'; import { SheetsQuery } from '../types'; import { reportInteraction } from '@grafana/runtime'; import { css } from '@emotion/css'; -type Props = QueryEditorProps; +type Props = QueryEditorProps; export function getGoogleSheetRangeInfoFromURL(url: string): Partial { let idx = url?.indexOf('/d/'); diff --git a/src/module.ts b/src/module.ts index 5c16661c..3825e07b 100644 --- a/src/module.ts +++ b/src/module.ts @@ -1,10 +1,12 @@ import { DataSourcePlugin } from '@grafana/data'; import { DataSourceOptions } from '@grafana/google-sdk'; import { ConfigEditor, MetaInspector, QueryEditor } from './components'; -import { DataSource } from './DataSource'; +import { GoogleSheetsDataSource } from './datasource'; import { SheetsQuery } from './types'; -export const plugin = new DataSourcePlugin(DataSource) +export const plugin = new DataSourcePlugin( + GoogleSheetsDataSource +) .setConfigEditor(ConfigEditor) .setQueryEditor(QueryEditor) .setMetadataInspector(MetaInspector); From 11f235790b824e9a3d481424a971988981c8f34f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zolt=C3=A1n=20Bedi?= Date: Mon, 21 Oct 2024 15:51:22 +0200 Subject: [PATCH 2/3] Feat: Add variable editor --- src/DataSource.ts | 1 + src/variables.ts | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 src/variables.ts diff --git a/src/DataSource.ts b/src/DataSource.ts index 62313615..d052f865 100644 --- a/src/DataSource.ts +++ b/src/DataSource.ts @@ -17,6 +17,7 @@ export class GoogleSheetsDataSource extends DataSourceWithBackend) { super(instanceSettings); this.authType = instanceSettings.jsonData.authenticationType; + this.variables = new GoogleSheetsVariableSupport(this); } query(request: DataQueryRequest): Observable { diff --git a/src/variables.ts b/src/variables.ts new file mode 100644 index 00000000..e09af369 --- /dev/null +++ b/src/variables.ts @@ -0,0 +1,26 @@ +import { CustomVariableSupport, DataQueryRequest } from '@grafana/data'; +import { GoogleSheetsDataSource } from './datasource'; +import { QueryEditor } from './components/QueryEditor'; +import { SheetsQuery } from './types'; + +export class GoogleSheetsVariableSupport extends CustomVariableSupport { + editor = QueryEditor; + + constructor(private datasource: GoogleSheetsDataSource) { + super(); + } + + getDefaultQuery(): Partial { + return { + refId: 'tempvar', + }; + } + + query(request: DataQueryRequest) { + if (!this.datasource) { + throw new Error('Datasource not initialized'); + } + + return this.datasource.query(request); + } +} From e8bceda01909cd61758b9e20dd47260bf99f629d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zolt=C3=A1n=20Bedi?= Date: Mon, 21 Oct 2024 15:54:39 +0200 Subject: [PATCH 3/3] Change file name casing --- src/{DataSource.ts => datasource.ts} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/{DataSource.ts => datasource.ts} (100%) diff --git a/src/DataSource.ts b/src/datasource.ts similarity index 100% rename from src/DataSource.ts rename to src/datasource.ts