ryan 6 лет назад
Родитель
Сommit
9b4d2a5ffa

+ 16 - 5
packages/grafana-ui/src/components/StatsPicker/StatsPicker.story.tsx

@@ -6,6 +6,15 @@ import { withCenteredStory } from '../../utils/storybook/withCenteredStory';
 import { StatsPicker } from './StatsPicker';
 import { text, boolean } from '@storybook/addon-knobs';
 
+const getKnobs = () => {
+  return {
+    placeholder: text('Placeholder Text', ''),
+    defaultStat: text('Default Stat', ''),
+    allowMultiple: boolean('Allow Multiple', false),
+    initialStats: text('Initial Stats', ''),
+  };
+};
+
 interface State {
   stats: string[];
 }
@@ -19,12 +28,16 @@ export class WrapperWithState extends PureComponent<any, State> {
   }
 
   toStatsArray = (txt: string): string[] => {
+    if (!txt) {
+      return [];
+    }
     return txt.split(',').map(v => v.trim());
   };
 
   componentDidUpdate(prevProps: any) {
     const { initialReducers } = this.props;
     if (initialReducers !== prevProps.initialReducers) {
+      console.log('Changing initial reducers');
       this.setState({ stats: this.toStatsArray(initialReducers) });
     }
   }
@@ -48,13 +61,11 @@ export class WrapperWithState extends PureComponent<any, State> {
   }
 }
 
-const story = storiesOf('UI/TableReducePicker', module);
+const story = storiesOf('UI/StatsPicker', module);
 story.addDecorator(withCenteredStory);
 story.add('picker', () => {
-  const placeholder = text('Placeholder Text', '');
-  const defaultStat = text('Default Stat', '');
-  const allowMultiple = boolean('Allow Multiple', false);
-  const initialStats = text('Initial Stats', '');
+  const { placeholder, defaultStat, allowMultiple, initialStats } = getKnobs();
+
   return (
     <div>
       <WrapperWithState

+ 7 - 7
packages/grafana-ui/src/components/StatsPicker/StatsPicker.tsx

@@ -1,6 +1,7 @@
 import React, { PureComponent } from 'react';
 
 import isArray from 'lodash/isArray';
+import difference from 'lodash/difference';
 
 import { Select } from '../index';
 
@@ -33,12 +34,12 @@ export class StatsPicker extends PureComponent<Props> {
   checkInput = () => {
     const { stats, allowMultiple, defaultStat, onChange } = this.props;
 
-    // Check that the selected reducers are all real
-    const notFound: string[] = [];
-    const current = getStatsCalculators(stats, notFound);
-    if (notFound.length > 0) {
-      console.warn('Unknown reducers', notFound, stats);
-      onChange(current.map(reducer => reducer.value));
+    const current = getStatsCalculators(stats);
+    if (current.length !== stats.length) {
+      const found = current.map(v => v.value);
+      const notFound = difference(stats, found);
+      console.warn('Unknown stats', notFound, stats);
+      onChange(current.map(stat => stat.value));
     }
 
     // Make sure there is only one
@@ -65,7 +66,6 @@ export class StatsPicker extends PureComponent<Props> {
   render() {
     const { width, stats, allowMultiple, defaultStat, placeholder } = this.props;
     const current = getStatsCalculators(stats);
-
     return (
       <Select
         width={width}

+ 10 - 8
packages/grafana-ui/src/utils/statsCalculator.test.ts

@@ -1,6 +1,8 @@
 import { parseCSV } from './processTableData';
 import { getStatsCalculators, StatID, calculateStats } from './statsCalculator';
 
+import _ from 'lodash';
+
 describe('Stats Calculators', () => {
   const basicTable = parseCSV('a,b,c\n10,20,30\n20,30,40');
 
@@ -21,20 +23,20 @@ describe('Stats Calculators', () => {
       // StatID.allIsZero,
       // StatID.allIsNull,
     ];
-    const notFound: string[] = [];
-    const stats = getStatsCalculators(names, notFound);
-    stats.forEach((stat, index) => {
-      expect(stat ? stat.value : '<missing>').toEqual(names[index]);
-    });
-    expect(notFound.length).toBe(0);
+    const stats = getStatsCalculators(names);
+    expect(stats.length).toBe(names.length);
   });
 
   it('should fail to load unknown stats', () => {
     const names = ['not a stat', StatID.max, StatID.min, 'also not a stat'];
-    const notFound: string[] = [];
-    const stats = getStatsCalculators(names, notFound);
+    const stats = getStatsCalculators(names);
     expect(stats.length).toBe(2);
+
+    const found = stats.map(v => v.value);
+    const notFound = _.difference(names, found);
     expect(notFound.length).toBe(2);
+
+    expect(notFound[0]).toBe('not a stat');
   });
 
   it('should calculate stats', () => {

+ 12 - 6
packages/grafana-ui/src/utils/statsCalculator.ts

@@ -42,18 +42,18 @@ export interface StatCalculatorInfo {
 
 /**
  * @param ids list of stat names or null to get all of them
- * @param notFound optional error object that will be filled with the names on unknown stats
  */
-export function getStatsCalculators(ids?: string[], notFound?: string[]): StatCalculatorInfo[] {
+export function getStatsCalculators(ids?: string[]): StatCalculatorInfo[] {
   if (ids === null || ids === undefined) {
+    if (!hasBuiltIndex) {
+      getById(StatID.mean);
+    }
     return listOfStats;
   }
   return ids.reduce((list, id) => {
     const stat = getById(id);
     if (stat) {
       list.push(stat);
-    } else if (notFound && id) {
-      notFound.push(id);
     }
     return list;
   }, new Array<StatCalculatorInfo>());
@@ -146,7 +146,13 @@ function getById(id: string): StatCalculatorInfo | undefined {
         standard: true,
         alias: 'total',
       },
-      { value: StatID.count, label: 'Count', description: 'Value Count', emptyInputResult: 0, standard: true },
+      {
+        value: StatID.count,
+        label: 'Count',
+        description: 'Number of values in response',
+        emptyInputResult: 0,
+        standard: true,
+      },
       {
         value: StatID.range,
         label: 'Range',
@@ -156,7 +162,7 @@ function getById(id: string): StatCalculatorInfo | undefined {
       {
         value: StatID.delta,
         label: 'Delta',
-        description: 'Cumulative change in value', // HELP! not totally sure what this does
+        description: 'Cumulative change in value (??? help not really sure ???)',
         standard: true,
       },
       {