Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Variable-editor #280

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/components/MetaInspector.tsx
Original file line number Diff line number Diff line change
@@ -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<DataSource, SheetsQuery, DataSourceOptions>;
export type Props = MetadataInspectorProps<GoogleSheetsDataSource, SheetsQuery, DataSourceOptions>;

export class MetaInspector extends PureComponent<Props> {
state = { index: 0 };
Expand Down
4 changes: 2 additions & 2 deletions src/components/QueryEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<DataSource, SheetsQuery, DataSourceOptions>;
type Props = QueryEditorProps<GoogleSheetsDataSource, SheetsQuery, DataSourceOptions>;

export function getGoogleSheetRangeInfoFromURL(url: string): Partial<SheetsQuery> {
let idx = url?.indexOf('/d/');
Expand Down
6 changes: 4 additions & 2 deletions src/DataSource.ts → src/datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<SheetsQuery, DataSourceOptions> {
export class GoogleSheetsDataSource extends DataSourceWithBackend<SheetsQuery, DataSourceOptions> {
authType: string;
constructor(instanceSettings: DataSourceInstanceSettings<DataSourceOptions>) {
super(instanceSettings);
this.authType = instanceSettings.jsonData.authenticationType;
this.variables = new GoogleSheetsVariableSupport(this);
}

query(request: DataQueryRequest<SheetsQuery>): Observable<DataQueryResponse> {
Expand Down
6 changes: 4 additions & 2 deletions src/module.ts
Original file line number Diff line number Diff line change
@@ -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, SheetsQuery, DataSourceOptions>(DataSource)
export const plugin = new DataSourcePlugin<GoogleSheetsDataSource, SheetsQuery, DataSourceOptions>(
GoogleSheetsDataSource
)
.setConfigEditor(ConfigEditor)
.setQueryEditor(QueryEditor)
.setMetadataInspector(MetaInspector);
26 changes: 26 additions & 0 deletions src/variables.ts
Original file line number Diff line number Diff line change
@@ -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<GoogleSheetsDataSource, SheetsQuery> {
editor = QueryEditor;

constructor(private datasource: GoogleSheetsDataSource) {
super();
}

getDefaultQuery(): Partial<SheetsQuery> {
return {
refId: 'tempvar',
};
}

query(request: DataQueryRequest<SheetsQuery>) {
if (!this.datasource) {
throw new Error('Datasource not initialized');
}

return this.datasource.query(request);
}
}
Loading