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/DataSource.ts b/src/datasource.ts similarity index 86% rename from src/DataSource.ts rename to src/datasource.ts index 72262d04..d052f865 100644 --- a/src/DataSource.ts +++ b/src/datasource.ts @@ -7,15 +7,17 @@ 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); this.authType = instanceSettings.jsonData.authenticationType; + this.variables = new GoogleSheetsVariableSupport(this); } query(request: DataQueryRequest): Observable { 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); 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); + } +}