renderer.ts 9.1 KB

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