Explorar el Código

Minor refactoring of stats picker / shared singlestat code

Torkel Ödegaard hace 6 años
padre
commit
beba9676f6

+ 1 - 8
packages/grafana-ui/src/components/StatsPicker/StatsPicker.tsx

@@ -73,15 +73,8 @@ export class StatsPicker extends PureComponent<Props> {
       };
     });
 
-    const value: SelectOptionItem[] = [];
-    stats.forEach(s => {
-      const o = options.find(v => v.value === s);
-      if (o) {
-        value.push(o);
-      }
-    });
+    const value: SelectOptionItem[] = options.filter(option => stats.find(stat => option.value === stat));
 
-    //getStatsCalculators(stats);
     return (
       <Select
         width={width}

+ 4 - 2
packages/grafana-ui/src/utils/statsCalculator.ts

@@ -35,7 +35,6 @@ export interface StatCalculatorInfo {
   id: string;
   name: string;
   description: string;
-
   alias?: string; // optional secondary key.  'avg' vs 'mean', 'total' vs 'sum'
 
   // Internal details
@@ -54,6 +53,7 @@ export function getStatsCalculators(ids?: string[]): StatCalculatorInfo[] {
     }
     return listOfStats;
   }
+
   return ids.reduce((list, id) => {
     const stat = getById(id);
     if (stat) {
@@ -123,6 +123,7 @@ export function calculateStats(options: CalculateStatsOptions): ColumnStats {
 interface TableStatIndex {
   [id: string]: StatCalculatorInfo;
 }
+
 const listOfStats: StatCalculatorInfo[] = [];
 const index: TableStatIndex = {};
 let hasBuiltIndex = false;
@@ -166,7 +167,7 @@ function getById(id: string): StatCalculatorInfo | undefined {
       {
         id: StatID.delta,
         name: 'Delta',
-        description: 'Cumulative change in value (??? help not really sure ???)',
+        description: 'Cumulative change in value',
         standard: true,
       },
       {
@@ -217,6 +218,7 @@ function getById(id: string): StatCalculatorInfo | undefined {
     });
     hasBuiltIndex = true;
   }
+
   return index[id];
 }
 

+ 5 - 2
public/app/plugins/panel/singlestat2/SingleStatPanel.tsx

@@ -38,7 +38,7 @@ export const getSingleStatValues = (props: PanelProps<SingleStatBaseOptions>): D
       if (column.type === ColumnType.number) {
         const stats = calculateStats({
           table,
-          columnIndex: i, // Hardcoded for now!
+          columnIndex: i,
           stats: [stat], // The stats to calculate
           nullValueMode: NullValueMode.Null,
         });
@@ -49,7 +49,10 @@ export const getSingleStatValues = (props: PanelProps<SingleStatBaseOptions>): D
   }
 
   if (values.length === 0) {
-    throw { message: 'Could not find numeric data' };
+    values.push({
+      numeric: 0,
+      text: 'No data',
+    });
   }
 
   return values;

+ 4 - 4
public/app/plugins/panel/singlestat2/module.tsx

@@ -14,11 +14,11 @@ export const singleStatBaseOptionsCheck = (
   prevOptions?: any
 ) => {
   if (prevOptions) {
-    optionsToKeep.forEach(v => {
-      if (prevOptions.hasOwnProperty(v)) {
-        options[v] = cloneDeep(prevOptions.display);
+    for (const otk of optionsToKeep) {
+      if (prevOptions.hasOwnProperty(otk)) {
+        options[otk] = cloneDeep(prevOptions[otk]);
       }
-    });
+    }
   }
 
   // 6.1 renamed some stats, This makes sure they are up to date