grafana/public/app/features/expressions/components/ClassicConditions.tsx
Peter Holmberg 0006765a40
Alerting: Run queries (#33423)
* alertingqueryrunner first edition

* added so we always set name and uid when changing datasource.

* wip.

* wip

* added support for canceling requests.

* util for getting time ranges for expression queries

* remove logs, store data in state

* added structure for marble testing.

* change so the expression buttons doesnt submit form.

* fixed run button.

* replaced mocks with implementation that will set default query + expression.

* fixed so we set a datasource name for the default expression rule.

* improving expression guard.

* Update public/app/features/alerting/components/AlertingQueryEditor.tsx

Co-authored-by: Hugo Häggmark <hugo.haggmark@grafana.com>

* fixed some nits.

* some refactoring after feedback.

* use grafanthemev2

* rename grafanatheme

* fixing so we convert to correct relative time range.

* added some more tests.

* fixing so duplicating query works.

* added some more tests without marbles.

* small refactoring to share code between runRequest and alerting query runner.

Co-authored-by: Marcus Andersson <marcus.andersson@grafana.com>
Co-authored-by: Hugo Häggmark <hugo.haggmark@grafana.com>
2021-05-04 16:31:25 +02:00

85 lines
2.3 KiB
TypeScript

import React, { FC } from 'react';
import { SelectableValue } from '@grafana/data';
import { Button, Icon, InlineField, InlineFieldRow } from '@grafana/ui';
import { Condition } from './Condition';
import { ClassicCondition, ExpressionQuery } from '../types';
import { defaultCondition } from '../utils/expressionTypes';
interface Props {
query: ExpressionQuery;
refIds: Array<SelectableValue<string>>;
onChange: (query: ExpressionQuery) => void;
}
export const ClassicConditions: FC<Props> = ({ onChange, query, refIds }) => {
const onConditionChange = (condition: ClassicCondition, index: number) => {
if (query.conditions) {
onChange({
...query,
conditions: [...query.conditions.slice(0, index), condition, ...query.conditions.slice(index + 1)],
});
}
};
const onAddCondition = () => {
if (query.conditions) {
onChange({
...query,
conditions: query.conditions.length > 0 ? [...query.conditions, defaultCondition] : [defaultCondition],
});
}
};
const onRemoveCondition = (index: number) => {
if (query.conditions) {
const condition = query.conditions[index];
const conditions = query.conditions
.filter((c) => c !== condition)
.map((c, index) => {
if (index === 0) {
return {
...c,
operator: {
type: 'when',
},
};
}
return c;
});
onChange({
...query,
conditions,
});
}
};
return (
<div>
<InlineFieldRow>
<InlineField label="Conditions" labelWidth={14}>
<div>
{query.conditions?.map((condition, index) => {
if (!condition) {
return;
}
return (
<Condition
key={index}
index={index}
condition={condition}
onChange={(condition: ClassicCondition) => onConditionChange(condition, index)}
onRemoveCondition={onRemoveCondition}
refIds={refIds}
/>
);
})}
</div>
</InlineField>
</InlineFieldRow>
<Button variant="secondary" type="button" onClick={onAddCondition}>
<Icon name="plus-circle" />
</Button>
</div>
);
};