renderer.ts 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 cellStyle = '';
  173. let textStyle = '';
  174. const cellClasses = [];
  175. let cellClass = '';
  176. if (this.colorState.cell) {
  177. cellStyle = ' style="background-color:' + this.colorState.cell + '"';
  178. cellClasses.push('table-panel-color-cell');
  179. this.colorState.cell = null;
  180. } else if (this.colorState.value) {
  181. textStyle = ' style="color:' + this.colorState.value + '"';
  182. this.colorState.value = null;
  183. }
  184. // because of the fixed table headers css only solution
  185. // there is an issue if header cell is wider the cell
  186. // this hack adds header content to cell (not visible)
  187. let columnHtml = '';
  188. if (addWidthHack) {
  189. columnHtml = '<div class="table-panel-width-hack">' + this.table.columns[columnIndex].title + '</div>';
  190. }
  191. if (value === undefined) {
  192. cellStyle = ' style="display:none;"';
  193. column.hidden = true;
  194. } else {
  195. column.hidden = false;
  196. }
  197. if (column.hidden === true) {
  198. return '';
  199. }
  200. if (column.style && column.style.preserveFormat) {
  201. cellClasses.push('table-panel-cell-pre');
  202. }
  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 cellLink = this.templateSrv.replace(column.style.linkUrl, scopedVars, encodeURIComponent);
  208. const cellLinkTooltip = this.templateSrv.replace(column.style.linkTooltip, scopedVars);
  209. const cellTarget = column.style.linkTargetBlank ? '_blank' : '';
  210. cellClasses.push('table-panel-cell-link');
  211. columnHtml += `
  212. <a href="${cellLink}" target="${cellTarget}" data-link-tooltip data-original-title="${cellLinkTooltip}" data-placement="right"${textStyle}>
  213. ${value}
  214. </a>
  215. `;
  216. } else {
  217. columnHtml += value;
  218. }
  219. if (column.filterable) {
  220. cellClasses.push('table-panel-cell-filterable');
  221. columnHtml += `
  222. <a class="table-panel-filter-link" data-link-tooltip data-original-title="Filter out value" data-placement="bottom"
  223. data-row="${rowIndex}" data-column="${columnIndex}" data-operator="!=">
  224. <i class="fa fa-search-minus"></i>
  225. </a>
  226. <a class="table-panel-filter-link" data-link-tooltip data-original-title="Filter for value" data-placement="bottom"
  227. data-row="${rowIndex}" data-column="${columnIndex}" data-operator="=">
  228. <i class="fa fa-search-plus"></i>
  229. </a>`;
  230. }
  231. if (cellClasses.length) {
  232. cellClass = ' class="' + cellClasses.join(' ') + '"';
  233. }
  234. columnHtml = '<td' + cellClass + cellStyle + textStyle + '>' + columnHtml + '</td>';
  235. return columnHtml;
  236. }
  237. render(page) {
  238. const pageSize = this.panel.pageSize || 100;
  239. const startPos = page * pageSize;
  240. const endPos = Math.min(startPos + pageSize, this.table.rows.length);
  241. let html = '';
  242. const rowClasses = [];
  243. let rowClass = '';
  244. for (let y = startPos; y < endPos; y++) {
  245. const row = this.table.rows[y];
  246. let cellHtml = '';
  247. let rowStyle = '';
  248. for (let i = 0; i < this.table.columns.length; i++) {
  249. cellHtml += this.renderCell(i, y, row[i], y === startPos);
  250. }
  251. if (this.colorState.row) {
  252. rowStyle = ' style="background-color:' + this.colorState.row + '"';
  253. rowClasses.push('table-panel-color-row');
  254. this.colorState.row = null;
  255. }
  256. if (rowClasses.length) {
  257. rowClass = ' class="' + rowClasses.join(' ') + '"';
  258. }
  259. html += '<tr ' + rowClass + rowStyle + '>' + cellHtml + '</tr>';
  260. }
  261. return html;
  262. }
  263. render_values() {
  264. const rows = [];
  265. for (let y = 0; y < this.table.rows.length; y++) {
  266. const row = this.table.rows[y];
  267. const newRow = [];
  268. for (let i = 0; i < this.table.columns.length; i++) {
  269. newRow.push(this.formatColumnValue(i, row[i]));
  270. }
  271. rows.push(newRow);
  272. }
  273. return {
  274. columns: this.table.columns,
  275. rows: rows,
  276. };
  277. }
  278. }