TablePanel.tsx 9.3 KB

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