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