2021-04-09 07:46:24 -05:00
|
|
|
import React, { PureComponent } from 'react';
|
|
|
|
import { SelectableValue, QueryEditorProps } from '@grafana/data';
|
|
|
|
import { InlineField, Select } from '@grafana/ui';
|
2019-10-30 13:38:28 -05:00
|
|
|
import { ExpressionDatasourceApi } from './ExpressionDatasource';
|
2021-04-09 07:46:24 -05:00
|
|
|
import { Resample } from './components/Resample';
|
|
|
|
import { Reduce } from './components/Reduce';
|
|
|
|
import { Math } from './components/Math';
|
|
|
|
import { ClassicConditions } from './components/ClassicConditions';
|
|
|
|
import { getDefaults } from './utils/expressionTypes';
|
|
|
|
import { ExpressionQuery, ExpressionQueryType, gelTypes } from './types';
|
2019-10-30 13:38:28 -05:00
|
|
|
|
|
|
|
type Props = QueryEditorProps<ExpressionDatasourceApi, ExpressionQuery>;
|
|
|
|
|
2021-04-09 07:46:24 -05:00
|
|
|
const labelWidth = 14;
|
|
|
|
export class ExpressionQueryEditor extends PureComponent<Props> {
|
|
|
|
onSelectExpressionType = (item: SelectableValue<ExpressionQueryType>) => {
|
2019-10-30 13:38:28 -05:00
|
|
|
const { query, onChange } = this.props;
|
|
|
|
|
2021-04-09 07:46:24 -05:00
|
|
|
onChange(getDefaults({ ...query, type: item.value! }));
|
2019-10-30 13:38:28 -05:00
|
|
|
};
|
|
|
|
|
2021-04-09 07:46:24 -05:00
|
|
|
renderExpressionType() {
|
|
|
|
const { onChange, query, queries } = this.props;
|
|
|
|
const refIds = queries!.filter((q) => query.refId !== q.refId).map((q) => ({ value: q.refId, label: q.refId }));
|
2019-10-30 13:38:28 -05:00
|
|
|
|
2021-04-09 07:46:24 -05:00
|
|
|
switch (query.type) {
|
|
|
|
case ExpressionQueryType.math:
|
|
|
|
return <Math onChange={onChange} query={query} labelWidth={labelWidth} />;
|
2019-10-30 13:38:28 -05:00
|
|
|
|
2021-04-09 07:46:24 -05:00
|
|
|
case ExpressionQueryType.reduce:
|
|
|
|
return <Reduce refIds={refIds} onChange={onChange} labelWidth={labelWidth} query={query} />;
|
2020-12-17 06:56:42 -06:00
|
|
|
|
2021-04-09 07:46:24 -05:00
|
|
|
case ExpressionQueryType.resample:
|
|
|
|
return <Resample query={query} labelWidth={labelWidth} onChange={onChange} refIds={refIds} />;
|
2019-10-30 13:38:28 -05:00
|
|
|
|
2021-04-09 07:46:24 -05:00
|
|
|
case ExpressionQueryType.classic:
|
|
|
|
return <ClassicConditions onChange={onChange} query={query} refIds={refIds} />;
|
|
|
|
}
|
|
|
|
}
|
2019-10-30 13:38:28 -05:00
|
|
|
|
|
|
|
render() {
|
2021-04-09 07:46:24 -05:00
|
|
|
const { query } = this.props;
|
2021-01-20 00:59:48 -06:00
|
|
|
const selected = gelTypes.find((o) => o.value === query.type);
|
2020-12-17 06:56:42 -06:00
|
|
|
|
2019-10-30 13:38:28 -05:00
|
|
|
return (
|
|
|
|
<div>
|
2020-12-11 08:19:24 -06:00
|
|
|
<InlineField label="Operation" labelWidth={labelWidth}>
|
2021-08-04 09:47:53 -05:00
|
|
|
<Select
|
|
|
|
menuShouldPortal
|
|
|
|
options={gelTypes}
|
|
|
|
value={selected}
|
|
|
|
onChange={this.onSelectExpressionType}
|
|
|
|
width={25}
|
|
|
|
/>
|
2020-09-28 09:44:52 -05:00
|
|
|
</InlineField>
|
2021-04-09 07:46:24 -05:00
|
|
|
{this.renderExpressionType()}
|
2019-10-30 13:38:28 -05:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|