Elastic: Store index in jsonData (#62808)

* 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 <gabor.farkas@gmail.com>

* Update public/app/plugins/datasource/elasticsearch/configuration/ElasticDetails.tsx

Co-authored-by: Gábor Farkas <gabor.farkas@gmail.com>

* add indexChangeHandler function

* fix

* fix failing tests

* fix

* fix

---------

Co-authored-by: Gábor Farkas <gabor.farkas@gmail.com>
This commit is contained in:
Gareth Dawson
2023-04-19 09:30:09 +01:00
committed by GitHub
co-authored by Gábor Farkas
parent 98778289cb
commit 82a6c8a647
5 changed files with 26 additions and 15 deletions
+9 -1
View File
@@ -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,
@@ -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' }),
})
);
});
@@ -25,8 +25,8 @@ export const ElasticDetails = ({ value, onChange }: Props) => {
<InlineField label="Index name" labelWidth={26}>
<Input
id="es_config_indexName"
value={value.database || ''}
onChange={changeHandler('database', value, onChange)}
value={value.jsonData.index ?? (value.database || '')}
onChange={indexChangeHandler(value, onChange)}
width={24}
placeholder="es-index-name"
required
@@ -108,13 +108,16 @@ export const ElasticDetails = ({ value, onChange }: Props) => {
);
};
// TODO: Use change handlers from @grafana/data
const changeHandler =
(key: keyof DataSourceSettings<ElasticsearchOptions>, value: Props['value'], onChange: Props['onChange']) =>
const indexChangeHandler =
(value: Props['value'], onChange: Props['onChange']) =>
(event: React.SyntheticEvent<HTMLInputElement | HTMLSelectElement>) => {
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<Interval | 'none'>) => {
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,
},
});
@@ -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);
@@ -62,6 +62,7 @@ export interface ElasticsearchOptions extends DataSourceJsonData {
logLevelField?: string;
dataLinks?: DataLinkConfig[];
includeFrozen?: boolean;
index?: string;
}
interface MetricConfiguration<T extends MetricAggregationType> {