-
Notifications
You must be signed in to change notification settings - Fork 29
feature: add default spreadsheet ID to config editor #370
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
base: main
Are you sure you want to change the base?
Changes from all commits
b046693
6b6a39d
5e7a1b2
e8d836e
f744aa7
7100c1a
d284235
7a9ecfd
d2e3f60
5541156
0e4f483
222b030
17190b8
b5491be
1a690b3
8df391d
e293d60
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| 'grafana-google-sheets-datasource': minor | ||
| --- | ||
|
|
||
| Add default spreadsheet ID to config editor |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| 'grafana-google-sheets-datasource': minor | ||
| --- | ||
|
|
||
| show title in selectors with spreadsheet ID in description |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ import { | |
| DataSourceInstanceSettings, | ||
| ScopedVars, | ||
| SelectableValue, | ||
| CoreApp, | ||
| } from '@grafana/data'; | ||
| import { DataSourceOptions } from '@grafana/google-sdk'; | ||
| import { DataSourceWithBackend, getTemplateSrv, TemplateSrv } from '@grafana/runtime'; | ||
|
|
@@ -15,7 +16,7 @@ import { SheetsVariableSupport } from 'variables'; | |
| export class DataSource extends DataSourceWithBackend<SheetsQuery, DataSourceOptions> { | ||
| authType: string; | ||
| constructor( | ||
| instanceSettings: DataSourceInstanceSettings<DataSourceOptions>, | ||
| private instanceSettings: DataSourceInstanceSettings<DataSourceOptions>, | ||
| private readonly templateSrv: TemplateSrv = getTemplateSrv() | ||
| ) { | ||
| super(instanceSettings); | ||
|
|
@@ -63,8 +64,16 @@ export class DataSource extends DataSourceWithBackend<SheetsQuery, DataSourceOpt | |
| async getSpreadSheets(): Promise<Array<SelectableValue<string>>> { | ||
| return this.getResource('spreadsheets').then(({ spreadsheets }) => | ||
| spreadsheets | ||
| ? Object.entries(spreadsheets).map(([value, label]) => ({ label, value }) as SelectableValue<string>) | ||
| ? Object.entries(spreadsheets).map(([value, label]) => ({ | ||
| label, | ||
| value, | ||
| description: value, | ||
| }) as SelectableValue<string>) | ||
| : [] | ||
| ); | ||
| } | ||
|
|
||
| getDefaultQuery(app: CoreApp): Partial<SheetsQuery> { | ||
| return { spreadsheet: (this.instanceSettings.jsonData as any).defaultSheetID || '' }; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should set the base class to |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| import { QueryEditorProps } from '@grafana/data'; | ||
| import { QueryEditorProps, SelectableValue } 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'; | ||
|
|
@@ -51,15 +51,45 @@ export const formatCacheTimeLabel = (s: number = defaultCacheDuration) => { | |
| }; | ||
|
|
||
| export class QueryEditor extends PureComponent<Props> { | ||
| state = { | ||
| selectedSheetOption: undefined as SelectableValue<string> | string | undefined, | ||
| }; | ||
|
|
||
| componentDidMount() { | ||
| if (!this.props.query.hasOwnProperty('cacheDurationSeconds')) { | ||
| this.props.onChange({ | ||
| ...this.props.query, | ||
| cacheDurationSeconds: defaultCacheDuration, // um :( | ||
| }); | ||
| } | ||
| this.updateSelectedSheetOption(); | ||
| } | ||
|
|
||
| componentDidUpdate(prevProps: Props) { | ||
| if (prevProps.query.spreadsheet !== this.props.query.spreadsheet) { | ||
| this.updateSelectedSheetOption(); | ||
| } | ||
| } | ||
|
|
||
| updateSelectedSheetOption = async () => { | ||
| const { query, datasource } = this.props; | ||
| if (!query.spreadsheet) { | ||
| this.setState({ selectedSheetOption: undefined }); | ||
| return; | ||
| } | ||
| try { | ||
| const sheetOptions = await datasource.getSpreadSheets(); | ||
| const matchingOption = sheetOptions.find((opt) => opt.value === query.spreadsheet); | ||
| if (matchingOption) { | ||
| this.setState({ selectedSheetOption: matchingOption }); | ||
| } else { | ||
| this.setState({ selectedSheetOption: query.spreadsheet }); | ||
| } | ||
| } catch { | ||
| this.setState({ selectedSheetOption: query.spreadsheet }); | ||
| } | ||
| }; | ||
|
|
||
| onRangeChange = (event: ChangeEvent<HTMLInputElement>) => { | ||
| this.props.onChange({ | ||
| ...this.props.query, | ||
|
|
@@ -71,10 +101,12 @@ export class QueryEditor extends PureComponent<Props> { | |
| const { query, onRunQuery, onChange } = this.props; | ||
|
|
||
| if (!item.value) { | ||
| this.setState({ selectedSheetOption: undefined }); | ||
| return; // ignore delete? | ||
| } | ||
|
|
||
| const v = item.value; | ||
| this.setState({ selectedSheetOption: item }); | ||
| // Check for pasted full URLs | ||
| if (/(.*)\/spreadsheets\/d\/(.*)/.test(v)) { | ||
| onChange({ ...query, ...getGoogleSheetRangeInfoFromURL(v) }); | ||
|
|
@@ -118,9 +150,19 @@ export class QueryEditor extends PureComponent<Props> { | |
| Spreadsheet ID | ||
| </InlineFormLabel> | ||
| <SegmentAsync | ||
| loadOptions={() => datasource.getSpreadSheets()} | ||
| loadOptions={async () => { | ||
| const options = await datasource.getSpreadSheets(); | ||
| const { query } = this.props; | ||
| if (query.spreadsheet) { | ||
| const matchingOption = options.find((opt) => opt.value === query.spreadsheet); | ||
| if (matchingOption && this.state.selectedSheetOption !== matchingOption) { | ||
| this.setState({ selectedSheetOption: matchingOption }); | ||
| } | ||
| } | ||
| return options; | ||
|
Comment on lines
+154
to
+162
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems we are duplicating the code here. Can we extract into function reuse along with |
||
| }} | ||
| placeholder="Enter SpreadsheetID" | ||
| value={query.spreadsheet} | ||
| value={this.state.selectedSheetOption ?? query.spreadsheet} | ||
| allowCustomValue={true} | ||
| onChange={this.onSpreadsheetIDChange} | ||
| /> | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes. let's remove this.