renderer.tsx 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. // Libraries
  2. import _ from 'lodash';
  3. import moment from 'moment';
  4. import React, { CSSProperties, ReactNode } from 'react';
  5. import { sanitize } from 'app/core/utils/text';
  6. // Types
  7. import kbn from 'app/core/utils/kbn';
  8. import { getValueFormat, getColorFromHexRgbOrName, GrafanaThemeType, InterpolateFunction, Column } from '@grafana/ui';
  9. import { Style } from './types';
  10. import { Index } from 'react-virtualized';
  11. type CellFormatter = (v: any, style: Style) => string;
  12. interface ColumnInfo {
  13. header: string;
  14. accessor: string; // the field name
  15. style?: Style;
  16. hidden?: boolean;
  17. formatter: CellFormatter;
  18. filterable?: boolean;
  19. }
  20. export class TableRenderer {
  21. isUTC: false; // TODO? get UTC from props?
  22. columns: ColumnInfo[];
  23. colorState: any;
  24. theme?: GrafanaThemeType;
  25. constructor(
  26. styles: Style[],
  27. schema: Column[],
  28. private rowGetter: (info: Index) => any[], // matches the table rowGetter
  29. private replaceVariables: InterpolateFunction
  30. ) {
  31. this.colorState = {};
  32. if (!schema) {
  33. this.columns = [];
  34. return;
  35. }
  36. this.columns = schema.map((col, index) => {
  37. let title = col.text;
  38. let style: Style = null;
  39. for (let i = 0; i < styles.length; i++) {
  40. const s = styles[i];
  41. const regex = kbn.stringToJsRegex(s.pattern);
  42. if (title.match(regex)) {
  43. style = s;
  44. if (s.alias) {
  45. title = title.replace(regex, s.alias);
  46. }
  47. break;
  48. }
  49. }
  50. return {
  51. header: title,
  52. accessor: col.text, // unique?
  53. style: style,
  54. formatter: this.createColumnFormatter(style, col),
  55. };
  56. });
  57. }
  58. setTheme(theme: GrafanaThemeType) {
  59. this.theme = theme;
  60. }
  61. getColorForValue(value, style: Style) {
  62. if (!style.thresholds) {
  63. return null;
  64. }
  65. for (let i = style.thresholds.length; i > 0; i--) {
  66. if (value >= style.thresholds[i - 1]) {
  67. return getColorFromHexRgbOrName(style.colors[i], this.theme);
  68. }
  69. }
  70. return getColorFromHexRgbOrName(_.first(style.colors), this.theme);
  71. }
  72. defaultCellFormatter(v: any, style: Style): string {
  73. if (v === null || v === void 0 || v === undefined) {
  74. return '';
  75. }
  76. if (_.isArray(v)) {
  77. v = v.join(', ');
  78. }
  79. if (style && style.sanitize) {
  80. return sanitize(v);
  81. } else {
  82. return _.escape(v);
  83. }
  84. }
  85. createColumnFormatter(style: Style, header: any): CellFormatter {
  86. if (!style) {
  87. return this.defaultCellFormatter;
  88. }
  89. if (style.type === 'hidden') {
  90. return v => {
  91. return undefined;
  92. };
  93. }
  94. if (style.type === 'date') {
  95. return v => {
  96. if (v === undefined || v === null) {
  97. return '-';
  98. }
  99. if (_.isArray(v)) {
  100. v = v[0];
  101. }
  102. let date = moment(v);
  103. if (this.isUTC) {
  104. date = date.utc();
  105. }
  106. return date.format(style.dateFormat);
  107. };
  108. }
  109. if (style.type === 'string') {
  110. return v => {
  111. if (_.isArray(v)) {
  112. v = v.join(', ');
  113. }
  114. const mappingType = style.mappingType || 0;
  115. if (mappingType === 1 && style.valueMaps) {
  116. for (let i = 0; i < style.valueMaps.length; i++) {
  117. const map = style.valueMaps[i];
  118. if (v === null) {
  119. if (map.value === 'null') {
  120. return map.text;
  121. }
  122. continue;
  123. }
  124. // Allow both numeric and string values to be mapped
  125. if ((!_.isString(v) && Number(map.value) === Number(v)) || map.value === v) {
  126. this.setColorState(v, style);
  127. return this.defaultCellFormatter(map.text, style);
  128. }
  129. }
  130. }
  131. if (mappingType === 2 && style.rangeMaps) {
  132. for (let i = 0; i < style.rangeMaps.length; i++) {
  133. const map = style.rangeMaps[i];
  134. if (v === null) {
  135. if (map.from === 'null' && map.to === 'null') {
  136. return map.text;
  137. }
  138. continue;
  139. }
  140. if (Number(map.from) <= Number(v) && Number(map.to) >= Number(v)) {
  141. this.setColorState(v, style);
  142. return this.defaultCellFormatter(map.text, style);
  143. }
  144. }
  145. }
  146. if (v === null || v === void 0) {
  147. return '-';
  148. }
  149. this.setColorState(v, style);
  150. return this.defaultCellFormatter(v, style);
  151. };
  152. }
  153. if (style.type === 'number') {
  154. const valueFormatter = getValueFormat(style.unit || header.unit);
  155. return v => {
  156. if (v === null || v === void 0) {
  157. return '-';
  158. }
  159. if (_.isString(v) || _.isArray(v)) {
  160. return this.defaultCellFormatter(v, style);
  161. }
  162. this.setColorState(v, style);
  163. return valueFormatter(v, style.decimals, null);
  164. };
  165. }
  166. return value => {
  167. return this.defaultCellFormatter(value, style);
  168. };
  169. }
  170. setColorState(value: any, style: Style) {
  171. if (!style.colorMode) {
  172. return;
  173. }
  174. if (value === null || value === void 0 || _.isArray(value)) {
  175. return;
  176. }
  177. if (_.isNaN(value)) {
  178. return;
  179. }
  180. const numericValue = Number(value);
  181. this.colorState[style.colorMode] = this.getColorForValue(numericValue, style);
  182. }
  183. renderRowVariables(rowIndex: number) {
  184. const scopedVars = {};
  185. const row = this.rowGetter({ index: rowIndex });
  186. for (let i = 0; i < row.length; i++) {
  187. scopedVars[`__cell_${i}`] = { value: row[i] };
  188. }
  189. return scopedVars;
  190. }
  191. renderCell(columnIndex: number, rowIndex: number, value: any): ReactNode {
  192. const column = this.columns[columnIndex];
  193. if (column.formatter) {
  194. value = column.formatter(value, column.style);
  195. }
  196. const style: CSSProperties = {};
  197. const cellClasses = [];
  198. let cellClass = '';
  199. if (this.colorState.cell) {
  200. style.backgroundColor = this.colorState.cell;
  201. style.color = 'white';
  202. this.colorState.cell = null;
  203. } else if (this.colorState.value) {
  204. style.color = this.colorState.value;
  205. this.colorState.value = null;
  206. }
  207. if (value === undefined) {
  208. style.display = 'none';
  209. column.hidden = true;
  210. } else {
  211. column.hidden = false;
  212. }
  213. if (column.style && column.style.preserveFormat) {
  214. cellClasses.push('table-panel-cell-pre');
  215. }
  216. let columnHtml: JSX.Element;
  217. if (column.style && column.style.link) {
  218. // Render cell as link
  219. const scopedVars = this.renderRowVariables(rowIndex);
  220. scopedVars['__cell'] = { value: value };
  221. const cellLink = this.replaceVariables(column.style.linkUrl, scopedVars, encodeURIComponent);
  222. const cellLinkTooltip = this.replaceVariables(column.style.linkTooltip, scopedVars);
  223. const cellTarget = column.style.linkTargetBlank ? '_blank' : '';
  224. cellClasses.push('table-panel-cell-link');
  225. columnHtml = (
  226. <a
  227. href={cellLink}
  228. target={cellTarget}
  229. data-link-tooltip
  230. data-original-title={cellLinkTooltip}
  231. data-placement="right"
  232. >
  233. {value}
  234. </a>
  235. );
  236. } else {
  237. columnHtml = <span>{value}</span>;
  238. }
  239. let filterLink: JSX.Element;
  240. if (column.filterable) {
  241. cellClasses.push('table-panel-cell-filterable');
  242. filterLink = (
  243. <span>
  244. <a
  245. className="table-panel-filter-link"
  246. data-link-tooltip
  247. data-original-title="Filter out value"
  248. data-placement="bottom"
  249. data-row={rowIndex}
  250. data-column={columnIndex}
  251. data-operator="!="
  252. >
  253. <i className="fa fa-search-minus" />
  254. </a>
  255. <a
  256. className="table-panel-filter-link"
  257. data-link-tooltip
  258. data-original-title="Filter for value"
  259. data-placement="bottom"
  260. data-row={rowIndex}
  261. data-column={columnIndex}
  262. data-operator="="
  263. >
  264. <i className="fa fa-search-plus" />
  265. </a>
  266. </span>
  267. );
  268. }
  269. if (cellClasses.length) {
  270. cellClass = cellClasses.join(' ');
  271. }
  272. style.width = '100%';
  273. style.height = '100%';
  274. columnHtml = (
  275. <div className={cellClass} style={style}>
  276. {columnHtml}
  277. {filterLink}
  278. </div>
  279. );
  280. return columnHtml;
  281. }
  282. }