import React, { PureComponent, ChangeEvent } from 'react'; import { SelectableValue, QueryEditorProps } from '@grafana/data'; import { Input, Segment, SegmentAsync, ValidationEvents, EventsWithValidation, Switch } from '@grafana/ui'; import { CloudWatchQuery } from '../types'; import CloudWatchDatasource from '../datasource'; import { SelectableStrings } from '../types'; import { Stats, Dimensions, QueryInlineField, QueryField, Alias } from './'; export type Props = QueryEditorProps; interface State { regions: SelectableStrings; namespaces: SelectableStrings; metricNames: SelectableStrings; variableOptionGroup: SelectableValue; showMeta: boolean; } const idValidationEvents: ValidationEvents = { [EventsWithValidation.onBlur]: [ { rule: value => new RegExp(/^$|^[a-z][a-zA-Z0-9_]*$/).test(value), errorMessage: 'Invalid format. Only alphanumeric characters and underscores are allowed', }, ], }; export class QueryEditor extends PureComponent { state: State = { regions: [], namespaces: [], metricNames: [], variableOptionGroup: {}, showMeta: false }; componentWillMount() { const { query } = this.props; if (!query.namespace) { query.namespace = ''; } if (!query.metricName) { query.metricName = ''; } if (!query.expression) { query.expression = ''; } if (!query.dimensions) { query.dimensions = {}; } if (!query.region) { query.region = 'default'; } if (!query.id) { query.id = ''; } if (!query.alias) { query.alias = ''; } if (!query.statistics || !query.statistics.length) { query.statistics = ['Average']; } if (!query.hasOwnProperty('matchExact')) { query.matchExact = true; } } componentDidMount() { const { datasource } = this.props; const variableOptionGroup = { label: 'Template Variables', options: this.props.datasource.variables.map(this.toOption), }; Promise.all([datasource.metricFindQuery('regions()'), datasource.metricFindQuery('namespaces()')]).then( ([regions, namespaces]) => { this.setState({ ...this.state, regions: [...regions, variableOptionGroup], namespaces: [...namespaces, variableOptionGroup], variableOptionGroup, }); } ); } loadMetricNames = async () => { const { namespace, region } = this.props.query; return this.props.datasource.metricFindQuery(`metrics(${namespace},${region})`).then(this.appendTemplateVariables); }; appendTemplateVariables = (values: SelectableValue[]) => [ ...values, { label: 'Template Variables', options: this.props.datasource.variables.map(this.toOption) }, ]; toOption = (value: any) => ({ label: value, value }); onChange(query: CloudWatchQuery) { const { onChange, onRunQuery } = this.props; onChange(query); onRunQuery(); } render() { const { query, datasource, onChange, onRunQuery, data } = this.props; const { regions, namespaces, variableOptionGroup: variableOptionGroup, showMeta } = this.state; const metaDataExist = data && Object.values(data).length && data.state === 'Done'; return ( <> this.onChange({ ...query, region })} /> {query.expression.length === 0 && ( <> this.onChange({ ...query, namespace })} /> this.onChange({ ...query, metricName })} /> this.onChange({ ...query, statistics })} variableOptionGroup={variableOptionGroup} /> this.onChange({ ...query, dimensions })} loadKeys={() => datasource.getDimensionKeys(query.namespace, query.region).then(this.appendTemplateVariables) } loadValues={newKey => { const { [newKey]: value, ...newDimensions } = query.dimensions; return datasource .getDimensionValues(query.region, query.namespace, query.metricName, newKey, newDimensions) .then(values => (values.length ? [{ value: '*', text: '*', label: '*' }, ...values] : values)) .then(this.appendTemplateVariables); }} /> )} {query.statistics.length <= 1 && (
) => onChange({ ...query, id: event.target.value })} validationEvents={idValidationEvents} value={query.id || ''} />
) => onChange({ ...query, expression: event.target.value }) } />
)}
) => onChange({ ...query, period: event.target.value })} />
this.onChange({ ...query, alias: value })} /> this.onChange({ ...query, matchExact: !query.matchExact })} />
{showMeta && metaDataExist && ( {data.series[0].meta.gmdMeta.map(({ ID, Expression }: any) => ( ))}
Metric Data Query ID Metric Data Query Expression
{ID} {Expression}
)}
); } }