import * as React from 'react'; import { DataSourceSettings, SelectableValue } from '@grafana/data'; import { ConfigDescriptionLink, ConfigSubSection } from '@grafana/experimental'; import { InlineField, Input, Select, InlineSwitch } from '@grafana/ui'; import { ElasticsearchOptions, Interval } from '../types'; const indexPatternTypes: Array> = [ { label: 'No pattern', value: 'none' }, { label: 'Hourly', value: 'Hourly', example: '[logstash-]YYYY.MM.DD.HH' }, { label: 'Daily', value: 'Daily', example: '[logstash-]YYYY.MM.DD' }, { label: 'Weekly', value: 'Weekly', example: '[logstash-]GGGG.WW' }, { label: 'Monthly', value: 'Monthly', example: '[logstash-]YYYY.MM' }, { label: 'Yearly', value: 'Yearly', example: '[logstash-]YYYY' }, ]; type Props = { value: DataSourceSettings; onChange: (value: DataSourceSettings) => void; }; export const ElasticDetails = ({ value, onChange }: Props) => { return ( } > A lower limit for the auto group by time interval. Recommended to be set to write frequency, for example{' '} 1m if your data is written every minute. } error="Value is not valid, you can use number with time unit specifier: y, M, w, d, h, m, s" invalid={!!value.jsonData.timeInterval && !/^\d+(ms|[Mwdhmsy])$/.test(value.jsonData.timeInterval)} > ); }; const indexChangeHandler = (value: Props['value'], onChange: Props['onChange']) => (event: React.SyntheticEvent) => { onChange({ ...value, database: '', jsonData: { ...value.jsonData, index: event.currentTarget.value, }, }); }; // TODO: Use change handlers from @grafana/data const jsonDataChangeHandler = (key: keyof ElasticsearchOptions, value: Props['value'], onChange: Props['onChange']) => (event: React.SyntheticEvent) => { onChange({ ...value, jsonData: { ...value.jsonData, [key]: event.currentTarget.value, }, }); }; const jsonDataSwitchChangeHandler = (key: keyof ElasticsearchOptions, value: Props['value'], onChange: Props['onChange']) => (event: React.SyntheticEvent) => { onChange({ ...value, jsonData: { ...value.jsonData, [key]: event.currentTarget.checked, }, }); }; const intervalHandler = (value: Props['value'], onChange: Props['onChange']) => (option: SelectableValue) => { // 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; const currentIndex = value.jsonData.index ?? value.database; if (!currentIndex || currentIndex.length === 0 || currentIndex.startsWith('[logstash-]')) { let newDatabase = ''; if (newInterval !== undefined) { const pattern = indexPatternTypes.find((pattern) => pattern.value === newInterval); if (pattern) { newDatabase = pattern.example ?? ''; } } onChange({ ...value, database: '', jsonData: { ...value.jsonData, index: newDatabase, interval: newInterval, }, }); } else { onChange({ ...value, jsonData: { ...value.jsonData, interval: newInterval, }, }); } }; export function defaultMaxConcurrentShardRequests() { return 5; }