diff --git a/public/app/plugins/datasource/influxdb/components/InfluxLogsQueryField.test.tsx b/public/app/plugins/datasource/influxdb/components/InfluxLogsQueryField.test.tsx new file mode 100644 index 00000000000..9f31f5b1286 --- /dev/null +++ b/public/app/plugins/datasource/influxdb/components/InfluxLogsQueryField.test.tsx @@ -0,0 +1,53 @@ +import { pairsAreValid } from './InfluxLogsQueryField'; + +describe('pairsAreValid()', () => { + describe('when all pairs are fully defined', () => { + it('should return true', () => { + const pairs = [ + { + key: 'a', + operator: '=', + value: '1', + }, + { + key: 'b', + operator: '!=', + value: '2', + }, + ]; + + expect(pairsAreValid(pairs as any)).toBe(true); + }); + }); + + describe('when no pairs are defined at all', () => { + it('should return true', () => { + expect(pairsAreValid([])).toBe(true); + }); + }); + + describe('when pairs are undefined', () => { + it('should return true', () => { + expect(pairsAreValid(undefined)).toBe(true); + }); + }); + + describe('when one or more pairs are only partially defined', () => { + it('should return false', () => { + const pairs = [ + { + key: 'a', + operator: undefined, + value: '1', + }, + { + key: 'b', + operator: '!=', + value: '2', + }, + ]; + + expect(pairsAreValid(pairs as any)).toBe(false); + }); + }); +}); diff --git a/public/app/plugins/datasource/influxdb/components/InfluxLogsQueryField.tsx b/public/app/plugins/datasource/influxdb/components/InfluxLogsQueryField.tsx index ef41ba4384e..70fd5a74da3 100644 --- a/public/app/plugins/datasource/influxdb/components/InfluxLogsQueryField.tsx +++ b/public/app/plugins/datasource/influxdb/components/InfluxLogsQueryField.tsx @@ -19,6 +19,19 @@ export interface State { field: string; } +// Helper function for determining if a collection of pairs are valid +// where a valid pair is either fully defined, or not defined at all, but not partially defined +export function pairsAreValid(pairs: KeyValuePair[]) { + return ( + !pairs || + pairs.every(pair => { + const allDefined = !!(pair.key && pair.operator && pair.value); + const allEmpty = pair.key === undefined && pair.operator === undefined && pair.value === undefined; + return allDefined || allEmpty; + }) + ); +} + export class InfluxLogsQueryField extends React.PureComponent { templateSrv: TemplateSrv = new TemplateSrv(); state: State = { measurements: [], measurement: null, field: null }; @@ -77,6 +90,11 @@ export class InfluxLogsQueryField extends React.PureComponent { ); this.props.onChange(queryModel.target); + + // Only run the query if measurement & field are set, and there are no invalid pairs + if (measurement && field && pairsAreValid(pairs)) { + this.props.onRunQuery(); + } }; render() {