renderer.ts 9.5 KB

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