renderer.tsx 8.3 KB

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