From 82a6c8a647620831d1bc56afcad8c2e211517598 Mon Sep 17 00:00:00 2001 From: Gareth Dawson Date: Wed, 19 Apr 2023 09:30:09 +0100 Subject: [PATCH] Elastic: Store index in jsonData (#62808) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(typescript): read and write index to jsonData * feat(go): read index from jsonData * clear database column when index-name is updated * Update public/app/plugins/datasource/elasticsearch/datasource.ts Co-authored-by: Gábor Farkas * Update public/app/plugins/datasource/elasticsearch/configuration/ElasticDetails.tsx Co-authored-by: Gábor Farkas * add indexChangeHandler function * fix * fix failing tests * fix * fix --------- Co-authored-by: Gábor Farkas --- pkg/tsdb/elasticsearch/elasticsearch.go | 10 ++++++++- .../configuration/ElasticDetails.test.tsx | 6 ++--- .../configuration/ElasticDetails.tsx | 22 +++++++++++-------- .../datasource/elasticsearch/datasource.ts | 2 +- .../plugins/datasource/elasticsearch/types.ts | 1 + 5 files changed, 26 insertions(+), 15 deletions(-) diff --git a/pkg/tsdb/elasticsearch/elasticsearch.go b/pkg/tsdb/elasticsearch/elasticsearch.go index 5b445cd6b1a..ccdf101e555 100644 --- a/pkg/tsdb/elasticsearch/elasticsearch.go +++ b/pkg/tsdb/elasticsearch/elasticsearch.go @@ -108,6 +108,14 @@ func newInstanceSettings(httpClientProvider httpclient.Provider) datasource.Inst timeInterval = "" } + index, ok := jsonData["index"].(string) + if !ok { + index = "" + } + if index == "" { + index = settings.Database + } + var maxConcurrentShardRequests float64 switch v := jsonData["maxConcurrentShardRequests"].(type) { @@ -142,7 +150,7 @@ func newInstanceSettings(httpClientProvider httpclient.Provider) datasource.Inst ID: settings.ID, URL: settings.URL, HTTPClient: httpCli, - Database: settings.Database, + Database: index, MaxConcurrentShardRequests: int64(maxConcurrentShardRequests), ConfiguredFields: configuredFields, Interval: interval, diff --git a/public/app/plugins/datasource/elasticsearch/configuration/ElasticDetails.test.tsx b/public/app/plugins/datasource/elasticsearch/configuration/ElasticDetails.test.tsx index 470128711bd..42c69cd9cd9 100644 --- a/public/app/plugins/datasource/elasticsearch/configuration/ElasticDetails.test.tsx +++ b/public/app/plugins/datasource/elasticsearch/configuration/ElasticDetails.test.tsx @@ -22,8 +22,7 @@ describe('ElasticDetails', () => { expect(onChangeMock).toHaveBeenLastCalledWith( expect.objectContaining({ - database: '[logstash-]YYYY.MM.DD', - jsonData: expect.objectContaining({ interval: 'Daily' }), + jsonData: expect.objectContaining({ interval: 'Daily', index: '[logstash-]YYYY.MM.DD' }), }) ); }); @@ -39,8 +38,7 @@ describe('ElasticDetails', () => { expect(onChangeMock).toHaveBeenLastCalledWith( expect.objectContaining({ - database: '[logstash-]YYYY.MM', - jsonData: expect.objectContaining({ interval: 'Monthly' }), + jsonData: expect.objectContaining({ interval: 'Monthly', index: '[logstash-]YYYY.MM' }), }) ); }); diff --git a/public/app/plugins/datasource/elasticsearch/configuration/ElasticDetails.tsx b/public/app/plugins/datasource/elasticsearch/configuration/ElasticDetails.tsx index 0f0639f9238..687891ed154 100644 --- a/public/app/plugins/datasource/elasticsearch/configuration/ElasticDetails.tsx +++ b/public/app/plugins/datasource/elasticsearch/configuration/ElasticDetails.tsx @@ -25,8 +25,8 @@ export const ElasticDetails = ({ value, onChange }: Props) => { { ); }; -// TODO: Use change handlers from @grafana/data -const changeHandler = - (key: keyof DataSourceSettings, value: Props['value'], onChange: Props['onChange']) => +const indexChangeHandler = + (value: Props['value'], onChange: Props['onChange']) => (event: React.SyntheticEvent) => { onChange({ ...value, - [key]: event.currentTarget.value, + database: '', + jsonData: { + ...value.jsonData, + index: event.currentTarget.value, + }, }); }; @@ -145,11 +148,11 @@ const jsonDataSwitchChangeHandler = const intervalHandler = (value: Props['value'], onChange: Props['onChange']) => (option: SelectableValue) => { - const { database } = value; // If option value is undefined it will send its label instead so we have to convert made up value to undefined here. const newInterval = option.value === 'none' ? undefined : option.value; - if (!database || database.length === 0 || database.startsWith('[logstash-]')) { + const currentIndex = value.jsonData.index ?? value.database; + if (!currentIndex || currentIndex.length === 0 || currentIndex.startsWith('[logstash-]')) { let newDatabase = ''; if (newInterval !== undefined) { @@ -162,9 +165,10 @@ const intervalHandler = onChange({ ...value, - database: newDatabase, + database: '', jsonData: { ...value.jsonData, + index: newDatabase, interval: newInterval, }, }); diff --git a/public/app/plugins/datasource/elasticsearch/datasource.ts b/public/app/plugins/datasource/elasticsearch/datasource.ts index d296ae50a56..059d99f2e86 100644 --- a/public/app/plugins/datasource/elasticsearch/datasource.ts +++ b/public/app/plugins/datasource/elasticsearch/datasource.ts @@ -115,10 +115,10 @@ export class ElasticDatasource this.withCredentials = instanceSettings.withCredentials; this.url = instanceSettings.url!; this.name = instanceSettings.name; - this.index = instanceSettings.database ?? ''; this.isProxyAccess = instanceSettings.access === 'proxy'; const settingsData = instanceSettings.jsonData || ({} as ElasticsearchOptions); + this.index = settingsData.index ?? instanceSettings.database ?? ''; this.timeField = settingsData.timeField; this.xpack = Boolean(settingsData.xpack); this.indexPattern = new IndexPattern(this.index, settingsData.interval); diff --git a/public/app/plugins/datasource/elasticsearch/types.ts b/public/app/plugins/datasource/elasticsearch/types.ts index 1226818adda..ecb44373041 100644 --- a/public/app/plugins/datasource/elasticsearch/types.ts +++ b/public/app/plugins/datasource/elasticsearch/types.ts @@ -62,6 +62,7 @@ export interface ElasticsearchOptions extends DataSourceJsonData { logLevelField?: string; dataLinks?: DataLinkConfig[]; includeFrozen?: boolean; + index?: string; } interface MetricConfiguration {