Browse Source

Explore: Runs query when measurement/field and pairs are selected in logs mode for influx (#17523)

* Explore: Runs query when measurement/field and pairs are select in logs mode for influx
Closes #17500

* Explore: Modifies logic determining when to auto-run query during influx logs mode
Also adds test to validate this logic
kay delaney 6 years ago
parent
commit
0a3af385e1

+ 53 - 0
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);
+    });
+  });
+});

+ 18 - 0
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<Props, State> {
   templateSrv: TemplateSrv = new TemplateSrv();
   state: State = { measurements: [], measurement: null, field: null };
@@ -77,6 +90,11 @@ export class InfluxLogsQueryField extends React.PureComponent<Props, State> {
     );
 
     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() {