فهرست منبع

add support for column aliases in table panel

Dan Cech 8 سال پیش
والد
کامیت
b1691f1cd1

+ 60 - 42
public/app/plugins/panel/table/editor.html

@@ -67,20 +67,38 @@
 						<select class="gf-form-input" ng-model="style.type" ng-options="c.value as c.text for c in editor.columnTypes" ng-change="editor.render()"></select>
 					</div>
 				</div>
-				<div class="gf-form" ng-if="style.type === 'date'">
-					<label class="gf-form-label">Format</label>
-					<metric-segment-model property="style.dateFormat" options="editor.dateFormats" on-change="editor.render()" custom="true"></metric-segment-model>
+				<div class="gf-form" ng-if="style.type !== 'hidden'">
+					<label class="gf-form-label">Title</label>
+					<input type="text" class="gf-form-input" ng-model="style.alias" ng-change="editor.render()" ng-model-onblur>
 				</div>
-				<gf-form-switch class="gf-form" label-class="width-8" ng-if="style.type === 'string'" label="Sanitize HTML" checked="style.sanitize" change="editor.render()"></gf-form-switch>
 				<div class="gf-form gf-form--grow">
 					<div class="gf-form-label gf-form-label--grow"></div>
 				</div>
 				<div class="gf-form">
-				  <label class="gf-form-label">
-				    <a class="pointer" ng-click="editor.removeColumnStyle(style)">
-				      <i class="fa fa-trash"></i>
-				    </a>
-				  </label>
+					<label class="gf-form-label">
+						<a class="pointer" ng-click="editor.removeColumnStyle(style)">
+							<i class="fa fa-trash"></i>
+						</a>
+					</label>
+				</div>
+			</div>
+
+			<div class="gf-form-inline" ng-if="style.type === 'date'">
+				<div class="gf-form offset-width-8">
+					<label class="gf-form-label width-8">Format</label>
+				</div>
+				<div class="gf-form">
+					<metric-segment-model property="style.dateFormat" options="editor.dateFormats" on-change="editor.render()" custom="true"></metric-segment-model>
+				</div>
+				<div class="gf-form gf-form--grow">
+					<div class="gf-form-label gf-form-label--grow"></div>
+				</div>
+			</div>
+
+			<div class="gf-form-inline" ng-if="style.type === 'string'">
+				<gf-form-switch class="gf-form offset-width-8" label-class="width-8" ng-if="style.type === 'string'" label="Sanitize HTML" checked="style.sanitize" change="editor.render()"></gf-form-switch>
+				<div class="gf-form gf-form--grow">
+					<div class="gf-form-label gf-form-label--grow"></div>
 				</div>
 			</div>
 
@@ -94,41 +112,41 @@
 				<div class="gf-form">
 					<label class="gf-form-label">Decimals</label>
 					<input type="number" class="gf-form-input width-4" data-placement="right" ng-model="style.decimals" ng-change="editor.render()" ng-model-onblur>
-        </div>
+				</div>
 				<div class="gf-form">
-          <label class="gf-form-label">Coloring</label>
-          <div class="gf-form-select-wrapper">
-            <select class="gf-form-input" ng-model="style.colorMode" ng-options="c.value as c.text for c in editor.colorModes" ng-change="editor.render()"></select>
-          </div>
-        </div>
-        <div class="gf-form gf-form--grow">
-          <div class="gf-form-label gf-form-label--grow"></div>
-        </div>
-      </div>
+					<label class="gf-form-label">Coloring</label>
+					<div class="gf-form-select-wrapper">
+						<select class="gf-form-input" ng-model="style.colorMode" ng-options="c.value as c.text for c in editor.colorModes" ng-change="editor.render()"></select>
+					</div>
+				</div>
+				<div class="gf-form gf-form--grow">
+					<div class="gf-form-label gf-form-label--grow"></div>
+				</div>
+			</div>
 
-      <div class="gf-form-inline" ng-if="style.type === 'number'">
-        <div class="gf-form offset-width-8">
-          <label class="gf-form-label width-8">Thresholds<tip>Comma separated values</tip></label>
-          <input type="text" class="gf-form-input width-10" ng-model="style.thresholds" placeholder="50,80" ng-blur="editor.render()" array-join ng-model-onblur>
-        </div>
-        <div class="gf-form">
-          <label class="gf-form-label width-5">Colors</label>
-          <span class="gf-form-label">
-            <spectrum-picker ng-model="style.colors[0]" ng-change="editor.render()"></spectrum-picker>
-          </span>
-          <span class="gf-form-label">
-            <spectrum-picker ng-model="style.colors[1]" ng-change="editor.render()"></spectrum-picker>
-          </span>
-          <span class="gf-form-label">
-            <spectrum-picker ng-model="style.colors[2]" ng-change="editor.render()"></spectrum-picker>
-          </span>
-        </div>
-        <div class="gf-form gf-form--grow">
-          <div class="gf-form-label gf-form-label--grow">
-            <a class="pointer" ng-click="editor.invertColorOrder($index)">Invert</a>
-          </div>
-        </div>
-      </div>
+			<div class="gf-form-inline" ng-if="style.type === 'number'">
+				<div class="gf-form offset-width-8">
+					<label class="gf-form-label width-8">Thresholds<tip>Comma separated values</tip></label>
+					<input type="text" class="gf-form-input width-10" ng-model="style.thresholds" placeholder="50,80" ng-blur="editor.render()" array-join ng-model-onblur>
+				</div>
+				<div class="gf-form">
+					<label class="gf-form-label width-5">Colors</label>
+					<span class="gf-form-label">
+						<spectrum-picker ng-model="style.colors[0]" ng-change="editor.render()"></spectrum-picker>
+					</span>
+					<span class="gf-form-label">
+						<spectrum-picker ng-model="style.colors[1]" ng-change="editor.render()"></spectrum-picker>
+					</span>
+					<span class="gf-form-label">
+						<spectrum-picker ng-model="style.colors[2]" ng-change="editor.render()"></spectrum-picker>
+					</span>
+				</div>
+				<div class="gf-form gf-form--grow">
+					<div class="gf-form-label gf-form-label--grow">
+						<a class="pointer" ng-click="editor.invertColorOrder($index)">Invert</a>
+					</div>
+				</div>
+			</div>
 
     </div>
   </div>

+ 1 - 0
public/app/plugins/panel/table/editor.ts

@@ -107,6 +107,7 @@ export class TablePanelEditorCtrl {
     var columnStyleDefaults = {
       unit: 'short',
       type: 'number',
+      alias: '',
       decimals: 2,
       colors: ["rgba(245, 54, 54, 0.9)", "rgba(237, 129, 40, 0.89)", "rgba(50, 172, 45, 0.97)"],
       colorMode: null,

+ 1 - 1
public/app/plugins/panel/table/module.html

@@ -7,7 +7,7 @@
 				<tr>
 					<th ng-repeat="col in ctrl.table.columns" ng-hide="col.hidden">
 						<div class="table-panel-table-header-inner pointer" ng-click="ctrl.toggleColumnSort(col, $index)">
-							{{col.text}}
+							{{col.title || col.text}}
 							<span class="table-panel-table-header-controls" ng-if="col.sort">
 								<i class="fa fa-caret-down" ng-show="col.desc"></i>
 								<i class="fa fa-caret-up" ng-hide="col.desc"></i>

+ 21 - 0
public/app/plugins/panel/table/module.ts

@@ -9,6 +9,7 @@ import {MetricsPanelCtrl} from 'app/plugins/sdk';
 import {transformDataToTable} from './transformers';
 import {tablePanelEditor} from './editor';
 import {TableRenderer} from './renderer';
+import kbn from 'app/core/utils/kbn';
 
 class TablePanelCtrl extends MetricsPanelCtrl {
   static templateUrl = 'module.html';
@@ -26,11 +27,13 @@ class TablePanelCtrl extends MetricsPanelCtrl {
       {
         type: 'date',
         pattern: 'Time',
+        alias: 'Time',
         dateFormat: 'YYYY-MM-DD HH:mm:ss',
       },
       {
         unit: 'short',
         type: 'number',
+        alias: '',
         decimals: 2,
         colors: ["rgba(245, 54, 54, 0.9)", "rgba(237, 129, 40, 0.89)", "rgba(50, 172, 45, 0.97)"],
         colorMode: null,
@@ -118,6 +121,24 @@ class TablePanelCtrl extends MetricsPanelCtrl {
   render() {
     this.table = transformDataToTable(this.dataRaw, this.panel);
     this.table.sort(this.panel.sort);
+
+    for (let colIndex = 0; colIndex < this.table.columns.length; colIndex++) {
+      let column = this.table.columns[colIndex];
+
+      for (let i = 0; i < this.panel.styles.length; i++) {
+        let style = this.panel.styles[i];
+        var regex = kbn.stringToJsRegex(style.pattern);
+        const matches = column.text.match(regex);
+        if (matches) {
+          column.style = style;
+          if (style.alias) {
+            column.title = column.text.replace(regex, style.alias);
+          }
+          break;
+        }
+      }
+    }
+
     return super.render(this.table);
   }
 

+ 31 - 25
public/app/plugins/panel/table/renderer.ts

@@ -24,7 +24,7 @@ export class TableRenderer {
     return _.first(style.colors);
   }
 
-  defaultCellFormater(v, style) {
+  defaultCellFormatter(v, style) {
     if (v === null || v === void 0 || v === undefined) {
       return '';
     }
@@ -40,18 +40,18 @@ export class TableRenderer {
     }
   }
 
-  createColumnFormater(style, column) {
-    if (!style) {
-      return this.defaultCellFormater;
+  createColumnFormatter(column) {
+    if (!column.style) {
+      return this.defaultCellFormatter;
     }
 
-    if (style.type === 'hidden') {
+    if (column.style.type === 'hidden') {
       return v => {
         return undefined;
       };
     }
 
-    if (style.type === 'date') {
+    if (column.style.type === 'date') {
       return v => {
         if (v === undefined || v === null) {
           return '-';
@@ -62,12 +62,12 @@ export class TableRenderer {
         if (this.isUtc) {
           date = date.utc();
         }
-        return date.format(style.dateFormat);
+        return date.format(column.style.dateFormat);
       };
     }
 
-    if (style.type === 'number') {
-      let valueFormater = kbn.valueFormats[column.unit || style.unit];
+    if (column.style.type === 'number') {
+      let valueFormatter = kbn.valueFormats[column.unit || column.style.unit];
 
       return v =>  {
         if (v === null || v === void 0) {
@@ -75,38 +75,44 @@ export class TableRenderer {
         }
 
         if (_.isString(v)) {
-          return this.defaultCellFormater(v, style);
+          return this.defaultCellFormatter(v, column.style);
         }
 
-        if (style.colorMode) {
-          this.colorState[style.colorMode] = this.getColorForValue(v, style);
+        if (column.style.colorMode) {
+          this.colorState[column.style.colorMode] = this.getColorForValue(v, column.style);
         }
 
-        return valueFormater(v, style.decimals, null);
+        return valueFormatter(v, column.style.decimals, null);
       };
     }
 
     return (value) => {
-      return this.defaultCellFormater(value, style);
+      return this.defaultCellFormatter(value, column.style);
     };
   }
 
   formatColumnValue(colIndex, value) {
-    if (this.formaters[colIndex]) {
-      return this.formaters[colIndex](value);
-    }
-
-    for (let i = 0; i < this.panel.styles.length; i++) {
-      let style = this.panel.styles[i];
+    if (!this.formaters[colIndex]) {
       let column = this.table.columns[colIndex];
-      var regex = kbn.stringToJsRegex(style.pattern);
-      if (column.text.match(regex)) {
-        this.formaters[colIndex] = this.createColumnFormater(style, column);
-        return this.formaters[colIndex](value);
+
+      if (!column.style) {
+        for (let i = 0; i < this.panel.styles.length; i++) {
+          let style = this.panel.styles[i];
+          var regex = kbn.stringToJsRegex(style.pattern);
+          const matches = column.text.match(regex);
+          if (matches) {
+            column.style = style;
+            if (style.alias) {
+              column.title = column.text.replace(regex, style.alias);
+            }
+            break;
+          }
+        }
       }
+
+      this.formaters[colIndex] = this.createColumnFormatter(column);
     }
 
-    this.formaters[colIndex] = this.defaultCellFormater;
     return this.formaters[colIndex](value);
   }