TablePanel.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. // Libraries
  2. import _ from 'lodash';
  3. import moment from 'moment';
  4. import React, { Component, CSSProperties, ReactNode } from 'react';
  5. import { sanitize } from 'app/core/utils/text';
  6. // Types
  7. import { PanelProps } from '@grafana/ui/src/types';
  8. import { Options, Style, CellFormatter, ColumnInfo } from './types';
  9. import kbn from 'app/core/utils/kbn';
  10. import { Table, SortDirectionType, SortIndicator, Column, TableHeaderProps, TableCellProps } from 'react-virtualized';
  11. interface Props extends PanelProps<Options> {}
  12. interface State {
  13. sortBy: string;
  14. sortDirection: SortDirectionType;
  15. sortedList: any[];
  16. }
  17. export class TablePanel extends Component<Props, State> {
  18. isUTC: false; // TODO? get UTC from props?
  19. columns: ColumnInfo[];
  20. colorState: any;
  21. constructor(props: Props) {
  22. super(props);
  23. this.state = {
  24. sortBy: 'index',
  25. sortDirection: 'ASC',
  26. sortedList: [],
  27. };
  28. }
  29. initColumns() {
  30. this.colorState = {};
  31. const { panelData, options } = this.props;
  32. if (!panelData.tableData) {
  33. this.columns = [];
  34. return;
  35. }
  36. const { styles } = options;
  37. this.columns = panelData.tableData.columns.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. getColorForValue(value: any, style: Style) {
  60. if (!style.thresholds) {
  61. return null;
  62. }
  63. for (let i = style.thresholds.length; i > 0; i--) {
  64. if (value >= style.thresholds[i - 1]) {
  65. return style.colors[i];
  66. }
  67. }
  68. return _.first(style.colors);
  69. }
  70. defaultCellFormatter(v: any, style: Style): string {
  71. if (v === null || v === void 0 || v === undefined) {
  72. return '';
  73. }
  74. if (_.isArray(v)) {
  75. v = v.join(', ');
  76. }
  77. if (style && style.sanitize) {
  78. return sanitize(v);
  79. } else {
  80. return _.escape(v);
  81. }
  82. }
  83. createColumnFormatter(style: Style, header: any): CellFormatter {
  84. if (!style) {
  85. return this.defaultCellFormatter;
  86. }
  87. if (style.type === 'hidden') {
  88. return v => {
  89. return undefined;
  90. };
  91. }
  92. if (style.type === 'date') {
  93. return v => {
  94. if (v === undefined || v === null) {
  95. return '-';
  96. }
  97. if (_.isArray(v)) {
  98. v = v[0];
  99. }
  100. let date = moment(v);
  101. if (this.isUTC) {
  102. date = date.utc();
  103. }
  104. return date.format(style.dateFormat);
  105. };
  106. }
  107. if (style.type === 'string') {
  108. return v => {
  109. if (_.isArray(v)) {
  110. v = v.join(', ');
  111. }
  112. const mappingType = style.mappingType || 0;
  113. if (mappingType === 1 && style.valueMaps) {
  114. for (let i = 0; i < style.valueMaps.length; i++) {
  115. const map = style.valueMaps[i];
  116. if (v === null) {
  117. if (map.value === 'null') {
  118. return map.text;
  119. }
  120. continue;
  121. }
  122. // Allow both numeric and string values to be mapped
  123. if ((!_.isString(v) && Number(map.value) === Number(v)) || map.value === v) {
  124. this.setColorState(v, style);
  125. return this.defaultCellFormatter(map.text, style);
  126. }
  127. }
  128. }
  129. if (mappingType === 2 && style.rangeMaps) {
  130. for (let i = 0; i < style.rangeMaps.length; i++) {
  131. const map = style.rangeMaps[i];
  132. if (v === null) {
  133. if (map.from === 'null' && map.to === 'null') {
  134. return map.text;
  135. }
  136. continue;
  137. }
  138. if (Number(map.from) <= Number(v) && Number(map.to) >= Number(v)) {
  139. this.setColorState(v, style);
  140. return this.defaultCellFormatter(map.text, style);
  141. }
  142. }
  143. }
  144. if (v === null || v === void 0) {
  145. return '-';
  146. }
  147. this.setColorState(v, style);
  148. return this.defaultCellFormatter(v, style);
  149. };
  150. }
  151. if (style.type === 'number') {
  152. const valueFormatter = kbn.valueFormats[style.unit || header.unit];
  153. return v => {
  154. if (v === null || v === void 0) {
  155. return '-';
  156. }
  157. if (_.isString(v) || _.isArray(v)) {
  158. return this.defaultCellFormatter(v, style);
  159. }
  160. this.setColorState(v, style);
  161. return valueFormatter(v, style.decimals, null);
  162. };
  163. }
  164. return value => {
  165. return this.defaultCellFormatter(value, style);
  166. };
  167. }
  168. setColorState(value: any, style: Style) {
  169. if (!style.colorMode) {
  170. return;
  171. }
  172. if (value === null || value === void 0 || _.isArray(value)) {
  173. return;
  174. }
  175. if (_.isNaN(value)) {
  176. return;
  177. }
  178. const numericValue = Number(value);
  179. this.colorState[style.colorMode] = this.getColorForValue(numericValue, style);
  180. }
  181. renderRowVariables(rowIndex) {
  182. const { panelData } = this.props;
  183. const scopedVars = {};
  184. const row = panelData.tableData.rows[rowIndex];
  185. for (let i = 0; i < row.length; i++) {
  186. scopedVars[`__cell_${i}`] = { value: row[i] };
  187. }
  188. return scopedVars;
  189. }
  190. renderCell(columnIndex: number, rowIndex: number, value: any, addWidthHack = false) {
  191. const column = this.columns[columnIndex];
  192. if (column.formatter) {
  193. value = column.formatter(value, column.style);
  194. }
  195. const style: CSSProperties = {};
  196. const cellClasses = [];
  197. let cellClass = '';
  198. if (this.colorState.cell) {
  199. style.backgroundColor = this.colorState.cell;
  200. style.color = 'white';
  201. this.colorState.cell = null;
  202. } else if (this.colorState.value) {
  203. style.color = this.colorState.value;
  204. this.colorState.value = null;
  205. }
  206. if (value === undefined) {
  207. style.display = 'none';
  208. column.hidden = true;
  209. } else {
  210. column.hidden = false;
  211. }
  212. if (column.style && column.style.preserveFormat) {
  213. cellClasses.push('table-panel-cell-pre');
  214. }
  215. let columnHtml: JSX.Element;
  216. if (column.style && column.style.link) {
  217. // Render cell as link
  218. const scopedVars = this.renderRowVariables(rowIndex);
  219. scopedVars['__cell'] = { value: value };
  220. const { replaceVariables } = this.props;
  221. const cellLink = replaceVariables(column.style.linkUrl, scopedVars, encodeURIComponent);
  222. const cellLinkTooltip = 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. _rowGetter = ({ index }) => {
  283. return this.props.panelData.tableData.rows[index];
  284. };
  285. _sort = ({ sortBy, sortDirection }) => {
  286. // const sortedList = this._sortList({sortBy, sortDirection});
  287. // this.setState({sortBy, sortDirection, sortedList});
  288. console.log('TODO, sort!', sortBy, sortDirection);
  289. };
  290. _headerRenderer = (header: TableHeaderProps): ReactNode => {
  291. const tableData = this.props.panelData.tableData!;
  292. const col = tableData.columns[header.dataKey];
  293. if (!col) {
  294. return <div>??{header.dataKey}</div>;
  295. }
  296. return (
  297. <div>
  298. {col.text} {header.sortBy === header.dataKey && <SortIndicator sortDirection={header.sortDirection} />}
  299. </div>
  300. );
  301. };
  302. _cellRenderer = (cell: TableCellProps) => {
  303. const tableData = this.props.panelData.tableData!;
  304. const val = tableData.rows[cell.rowIndex][cell.dataKey];
  305. return <div>{val}</div>;
  306. };
  307. render() {
  308. const { panelData, width, height, options } = this.props;
  309. const { showHeader } = options;
  310. const { sortBy, sortDirection } = this.state;
  311. const { tableData } = panelData;
  312. if (!tableData) {
  313. return <div>No Table Data...</div>;
  314. }
  315. return (
  316. <Table
  317. disableHeader={!showHeader}
  318. headerClassName={'hhhh'}
  319. headerHeight={30}
  320. height={height}
  321. overscanRowCount={10}
  322. rowClassName={'rrrrr'}
  323. rowHeight={30}
  324. rowGetter={this._rowGetter}
  325. rowCount={tableData.rows.length}
  326. sort={this._sort}
  327. sortBy={sortBy}
  328. sortDirection={sortDirection}
  329. width={width}
  330. >
  331. {tableData.columns.map((col, index) => {
  332. return (
  333. <Column
  334. key={index}
  335. dataKey={index}
  336. headerRenderer={this._headerRenderer}
  337. cellRenderer={this._cellRenderer}
  338. flexGrow={1}
  339. width={60}
  340. />
  341. );
  342. })}
  343. </Table>
  344. );
  345. }
  346. }