renderer.ts 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. import _ from 'lodash';
  2. import moment from 'moment';
  3. import { getValueFormat, getColorFromHexRgbOrName, GrafanaThemeType, stringToJsRegex } from '@grafana/ui';
  4. import { ColumnStyle } from '@grafana/ui/src/components/Table/TableCellBuilder';
  5. export class TableRenderer {
  6. formatters: any[];
  7. colorState: any;
  8. constructor(
  9. private panel,
  10. private table,
  11. private isUtc,
  12. private sanitize,
  13. private templateSrv,
  14. private theme?: GrafanaThemeType
  15. ) {
  16. this.initColumns();
  17. }
  18. setTable(table) {
  19. this.table = table;
  20. this.initColumns();
  21. }
  22. initColumns() {
  23. this.formatters = [];
  24. this.colorState = {};
  25. for (let colIndex = 0; colIndex < this.table.columns.length; colIndex++) {
  26. const column = this.table.columns[colIndex];
  27. column.title = column.text;
  28. for (let i = 0; i < this.panel.styles.length; i++) {
  29. const style = this.panel.styles[i];
  30. const regex = stringToJsRegex(style.pattern);
  31. if (column.text.match(regex)) {
  32. column.style = style;
  33. if (style.alias) {
  34. column.title = column.text.replace(regex, style.alias);
  35. }
  36. break;
  37. }
  38. }
  39. this.formatters[colIndex] = this.createColumnFormatter(column);
  40. }
  41. }
  42. getColorForValue(value, style: ColumnStyle) {
  43. if (!style.thresholds) {
  44. return null;
  45. }
  46. for (let i = style.thresholds.length; i > 0; i--) {
  47. if (value >= style.thresholds[i - 1]) {
  48. return getColorFromHexRgbOrName(style.colors[i], this.theme);
  49. }
  50. }
  51. return getColorFromHexRgbOrName(_.first(style.colors), this.theme);
  52. }
  53. defaultCellFormatter(v, style: ColumnStyle) {
  54. if (v === null || v === void 0 || v === undefined) {
  55. return '';
  56. }
  57. if (_.isArray(v)) {
  58. v = v.join(', ');
  59. }
  60. if (style && style.sanitize) {
  61. return this.sanitize(v);
  62. } else {
  63. return _.escape(v);
  64. }
  65. }
  66. createColumnFormatter(column) {
  67. if (!column.style) {
  68. return this.defaultCellFormatter;
  69. }
  70. if (column.style.type === 'hidden') {
  71. return v => {
  72. return undefined;
  73. };
  74. }
  75. if (column.style.type === 'date') {
  76. return v => {
  77. if (v === undefined || v === null) {
  78. return '-';
  79. }
  80. if (_.isArray(v)) {
  81. v = v[0];
  82. }
  83. // if is an epoch (numeric string and len > 12)
  84. if (_.isString(v) && !isNaN(v as any) && v.length > 12) {
  85. v = parseInt(v, 10);
  86. }
  87. let date = moment(v);
  88. if (this.isUtc) {
  89. date = date.utc();
  90. }
  91. return date.format(column.style.dateFormat);
  92. };
  93. }
  94. if (column.style.type === 'string') {
  95. return v => {
  96. if (_.isArray(v)) {
  97. v = v.join(', ');
  98. }
  99. const mappingType = column.style.mappingType || 0;
  100. if (mappingType === 1 && column.style.valueMaps) {
  101. for (let i = 0; i < column.style.valueMaps.length; i++) {
  102. const map = column.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, column.style);
  112. return this.defaultCellFormatter(map.text, column.style);
  113. }
  114. }
  115. }
  116. if (mappingType === 2 && column.style.rangeMaps) {
  117. for (let i = 0; i < column.style.rangeMaps.length; i++) {
  118. const map = column.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, column.style);
  127. return this.defaultCellFormatter(map.text, column.style);
  128. }
  129. }
  130. }
  131. if (v === null || v === void 0) {
  132. return '-';
  133. }
  134. this.setColorState(v, column.style);
  135. return this.defaultCellFormatter(v, column.style);
  136. };
  137. }
  138. if (column.style.type === 'number') {
  139. const valueFormatter = getValueFormat(column.unit || column.style.unit);
  140. return v => {
  141. if (v === null || v === void 0) {
  142. return '-';
  143. }
  144. if (isNaN(v) || _.isArray(v)) {
  145. return this.defaultCellFormatter(v, column.style);
  146. }
  147. this.setColorState(v, column.style);
  148. return valueFormatter(v, column.style.decimals, null);
  149. };
  150. }
  151. return value => {
  152. return this.defaultCellFormatter(value, column.style);
  153. };
  154. }
  155. setColorState(value, style: ColumnStyle) {
  156. if (!style.colorMode) {
  157. return;
  158. }
  159. if (value === null || value === void 0 || _.isArray(value)) {
  160. return;
  161. }
  162. const numericValue = Number(value);
  163. if (isNaN(numericValue)) {
  164. return;
  165. }
  166. this.colorState[style.colorMode] = this.getColorForValue(numericValue, style);
  167. }
  168. renderRowVariables(rowIndex) {
  169. const scopedVars = {};
  170. let cellVariable;
  171. const row = this.table.rows[rowIndex];
  172. for (let i = 0; i < row.length; i++) {
  173. cellVariable = `__cell_${i}`;
  174. scopedVars[cellVariable] = { value: row[i] };
  175. }
  176. return scopedVars;
  177. }
  178. formatColumnValue(colIndex, value) {
  179. return this.formatters[colIndex] ? this.formatters[colIndex](value) : value;
  180. }
  181. renderCell(columnIndex, rowIndex, value, addWidthHack = false) {
  182. value = this.formatColumnValue(columnIndex, value);
  183. const column = this.table.columns[columnIndex];
  184. let cellStyle = '';
  185. let textStyle = '';
  186. const cellClasses = [];
  187. let cellClass = '';
  188. if (this.colorState.cell) {
  189. cellStyle = ' style="background-color:' + this.colorState.cell + '"';
  190. cellClasses.push('table-panel-color-cell');
  191. this.colorState.cell = null;
  192. } else if (this.colorState.value) {
  193. textStyle = ' style="color:' + this.colorState.value + '"';
  194. this.colorState.value = null;
  195. }
  196. // because of the fixed table headers css only solution
  197. // there is an issue if header cell is wider the cell
  198. // this hack adds header content to cell (not visible)
  199. let columnHtml = '';
  200. if (addWidthHack) {
  201. columnHtml = '<div class="table-panel-width-hack">' + this.table.columns[columnIndex].title + '</div>';
  202. }
  203. if (value === undefined) {
  204. cellStyle = ' style="display:none;"';
  205. column.hidden = true;
  206. } else {
  207. column.hidden = false;
  208. }
  209. if (column.hidden === true) {
  210. return '';
  211. }
  212. if (column.style && column.style.preserveFormat) {
  213. cellClasses.push('table-panel-cell-pre');
  214. }
  215. if (column.style && column.style.link) {
  216. // Render cell as link
  217. const scopedVars = this.renderRowVariables(rowIndex);
  218. scopedVars['__cell'] = { value: value };
  219. const cellLink = this.templateSrv.replace(column.style.linkUrl, scopedVars, encodeURIComponent);
  220. const cellLinkTooltip = this.templateSrv.replace(column.style.linkTooltip, scopedVars);
  221. const cellTarget = column.style.linkTargetBlank ? '_blank' : '';
  222. cellClasses.push('table-panel-cell-link');
  223. columnHtml += `
  224. <a href="${cellLink}" target="${cellTarget}" data-link-tooltip data-original-title="${cellLinkTooltip}" data-placement="right"${textStyle}>
  225. ${value}
  226. </a>
  227. `;
  228. } else {
  229. columnHtml += value;
  230. }
  231. if (column.filterable) {
  232. cellClasses.push('table-panel-cell-filterable');
  233. columnHtml += `
  234. <a class="table-panel-filter-link" data-link-tooltip data-original-title="Filter out value" data-placement="bottom"
  235. data-row="${rowIndex}" data-column="${columnIndex}" data-operator="!=">
  236. <i class="fa fa-search-minus"></i>
  237. </a>
  238. <a class="table-panel-filter-link" data-link-tooltip data-original-title="Filter for value" data-placement="bottom"
  239. data-row="${rowIndex}" data-column="${columnIndex}" data-operator="=">
  240. <i class="fa fa-search-plus"></i>
  241. </a>`;
  242. }
  243. if (cellClasses.length) {
  244. cellClass = ' class="' + cellClasses.join(' ') + '"';
  245. }
  246. columnHtml = '<td' + cellClass + cellStyle + textStyle + '>' + columnHtml + '</td>';
  247. return columnHtml;
  248. }
  249. render(page) {
  250. const pageSize = this.panel.pageSize || 100;
  251. const startPos = page * pageSize;
  252. const endPos = Math.min(startPos + pageSize, this.table.rows.length);
  253. let html = '';
  254. for (let y = startPos; y < endPos; y++) {
  255. const row = this.table.rows[y];
  256. let cellHtml = '';
  257. let rowStyle = '';
  258. const rowClasses = [];
  259. let rowClass = '';
  260. for (let i = 0; i < this.table.columns.length; i++) {
  261. cellHtml += this.renderCell(i, y, row[i], y === startPos);
  262. }
  263. if (this.colorState.row) {
  264. rowStyle = ' style="background-color:' + this.colorState.row + '"';
  265. rowClasses.push('table-panel-color-row');
  266. this.colorState.row = null;
  267. }
  268. if (rowClasses.length) {
  269. rowClass = ' class="' + rowClasses.join(' ') + '"';
  270. }
  271. html += '<tr ' + rowClass + rowStyle + '>' + cellHtml + '</tr>';
  272. }
  273. return html;
  274. }
  275. render_values() {
  276. const rows = [];
  277. for (let y = 0; y < this.table.rows.length; y++) {
  278. const row = this.table.rows[y];
  279. const newRow = [];
  280. for (let i = 0; i < this.table.columns.length; i++) {
  281. newRow.push(this.formatColumnValue(i, row[i]));
  282. }
  283. rows.push(newRow);
  284. }
  285. return {
  286. columns: this.table.columns,
  287. rows: rows,
  288. };
  289. }
  290. }