renderer.ts 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. // if is an epoch (numeric string and len > 12)
  76. if (_.isString(v) && !isNaN(v) && v.length > 12) {
  77. v = parseInt(v, 10);
  78. }
  79. let date = moment(v);
  80. if (this.isUtc) {
  81. date = date.utc();
  82. }
  83. return date.format(column.style.dateFormat);
  84. };
  85. }
  86. if (column.style.type === 'string') {
  87. return v => {
  88. if (_.isArray(v)) {
  89. v = v.join(', ');
  90. }
  91. const mappingType = column.style.mappingType || 0;
  92. if (mappingType === 1 && column.style.valueMaps) {
  93. for (let i = 0; i < column.style.valueMaps.length; i++) {
  94. const map = column.style.valueMaps[i];
  95. if (v === null) {
  96. if (map.value === 'null') {
  97. return map.text;
  98. }
  99. continue;
  100. }
  101. // Allow both numeric and string values to be mapped
  102. if ((!_.isString(v) && Number(map.value) === Number(v)) || map.value === v) {
  103. this.setColorState(v, column.style);
  104. return this.defaultCellFormatter(map.text, column.style);
  105. }
  106. }
  107. }
  108. if (mappingType === 2 && column.style.rangeMaps) {
  109. for (let i = 0; i < column.style.rangeMaps.length; i++) {
  110. const map = column.style.rangeMaps[i];
  111. if (v === null) {
  112. if (map.from === 'null' && map.to === 'null') {
  113. return map.text;
  114. }
  115. continue;
  116. }
  117. if (Number(map.from) <= Number(v) && Number(map.to) >= Number(v)) {
  118. this.setColorState(v, column.style);
  119. return this.defaultCellFormatter(map.text, column.style);
  120. }
  121. }
  122. }
  123. if (v === null || v === void 0) {
  124. return '-';
  125. }
  126. this.setColorState(v, column.style);
  127. return this.defaultCellFormatter(v, column.style);
  128. };
  129. }
  130. if (column.style.type === 'number') {
  131. const valueFormatter = kbn.valueFormats[column.unit || column.style.unit];
  132. return v => {
  133. if (v === null || v === void 0) {
  134. return '-';
  135. }
  136. if (_.isString(v) || _.isArray(v)) {
  137. return this.defaultCellFormatter(v, column.style);
  138. }
  139. this.setColorState(v, column.style);
  140. return valueFormatter(v, column.style.decimals, null);
  141. };
  142. }
  143. return value => {
  144. return this.defaultCellFormatter(value, column.style);
  145. };
  146. }
  147. setColorState(value, style) {
  148. if (!style.colorMode) {
  149. return;
  150. }
  151. if (value === null || value === void 0 || _.isArray(value)) {
  152. return;
  153. }
  154. const numericValue = Number(value);
  155. if (isNaN(numericValue)) {
  156. return;
  157. }
  158. this.colorState[style.colorMode] = this.getColorForValue(numericValue, style);
  159. }
  160. renderRowVariables(rowIndex) {
  161. const scopedVars = {};
  162. let cellVariable;
  163. const row = this.table.rows[rowIndex];
  164. for (let i = 0; i < row.length; i++) {
  165. cellVariable = `__cell_${i}`;
  166. scopedVars[cellVariable] = { value: row[i] };
  167. }
  168. return scopedVars;
  169. }
  170. formatColumnValue(colIndex, value) {
  171. return this.formatters[colIndex] ? this.formatters[colIndex](value) : value;
  172. }
  173. renderCell(columnIndex, rowIndex, value, addWidthHack = false) {
  174. value = this.formatColumnValue(columnIndex, value);
  175. const column = this.table.columns[columnIndex];
  176. let cellStyle = '';
  177. let textStyle = '';
  178. const cellClasses = [];
  179. let cellClass = '';
  180. if (this.colorState.cell) {
  181. cellStyle = ' style="background-color:' + this.colorState.cell + '"';
  182. cellClasses.push('table-panel-color-cell');
  183. this.colorState.cell = null;
  184. } else if (this.colorState.value) {
  185. textStyle = ' style="color:' + this.colorState.value + '"';
  186. this.colorState.value = null;
  187. }
  188. // because of the fixed table headers css only solution
  189. // there is an issue if header cell is wider the cell
  190. // this hack adds header content to cell (not visible)
  191. let columnHtml = '';
  192. if (addWidthHack) {
  193. columnHtml = '<div class="table-panel-width-hack">' + this.table.columns[columnIndex].title + '</div>';
  194. }
  195. if (value === undefined) {
  196. cellStyle = ' style="display:none;"';
  197. column.hidden = true;
  198. } else {
  199. column.hidden = false;
  200. }
  201. if (column.hidden === true) {
  202. return '';
  203. }
  204. if (column.style && column.style.preserveFormat) {
  205. cellClasses.push('table-panel-cell-pre');
  206. }
  207. if (column.style && column.style.link) {
  208. // Render cell as link
  209. const scopedVars = this.renderRowVariables(rowIndex);
  210. scopedVars['__cell'] = { value: value };
  211. const cellLink = this.templateSrv.replace(column.style.linkUrl, scopedVars, encodeURIComponent);
  212. const cellLinkTooltip = this.templateSrv.replace(column.style.linkTooltip, scopedVars);
  213. const cellTarget = column.style.linkTargetBlank ? '_blank' : '';
  214. cellClasses.push('table-panel-cell-link');
  215. columnHtml += `
  216. <a href="${cellLink}" target="${cellTarget}" data-link-tooltip data-original-title="${cellLinkTooltip}" data-placement="right"${textStyle}>
  217. ${value}
  218. </a>
  219. `;
  220. } else {
  221. columnHtml += value;
  222. }
  223. if (column.filterable) {
  224. cellClasses.push('table-panel-cell-filterable');
  225. columnHtml += `
  226. <a class="table-panel-filter-link" data-link-tooltip data-original-title="Filter out value" data-placement="bottom"
  227. data-row="${rowIndex}" data-column="${columnIndex}" data-operator="!=">
  228. <i class="fa fa-search-minus"></i>
  229. </a>
  230. <a class="table-panel-filter-link" data-link-tooltip data-original-title="Filter for value" data-placement="bottom"
  231. data-row="${rowIndex}" data-column="${columnIndex}" data-operator="=">
  232. <i class="fa fa-search-plus"></i>
  233. </a>`;
  234. }
  235. if (cellClasses.length) {
  236. cellClass = ' class="' + cellClasses.join(' ') + '"';
  237. }
  238. columnHtml = '<td' + cellClass + cellStyle + textStyle + '>' + columnHtml + '</td>';
  239. return columnHtml;
  240. }
  241. render(page) {
  242. const pageSize = this.panel.pageSize || 100;
  243. const startPos = page * pageSize;
  244. const endPos = Math.min(startPos + pageSize, this.table.rows.length);
  245. let html = '';
  246. const rowClasses = [];
  247. let rowClass = '';
  248. for (let y = startPos; y < endPos; y++) {
  249. const row = this.table.rows[y];
  250. let cellHtml = '';
  251. let rowStyle = '';
  252. for (let i = 0; i < this.table.columns.length; i++) {
  253. cellHtml += this.renderCell(i, y, row[i], y === startPos);
  254. }
  255. if (this.colorState.row) {
  256. rowStyle = ' style="background-color:' + this.colorState.row + '"';
  257. rowClasses.push('table-panel-color-row');
  258. this.colorState.row = null;
  259. }
  260. if (rowClasses.length) {
  261. rowClass = ' class="' + rowClasses.join(' ') + '"';
  262. }
  263. html += '<tr ' + rowClass + rowStyle + '>' + cellHtml + '</tr>';
  264. }
  265. return html;
  266. }
  267. render_values() {
  268. const rows = [];
  269. for (let y = 0; y < this.table.rows.length; y++) {
  270. const row = this.table.rows[y];
  271. const newRow = [];
  272. for (let i = 0; i < this.table.columns.length; i++) {
  273. newRow.push(this.formatColumnValue(i, row[i]));
  274. }
  275. rows.push(newRow);
  276. }
  277. return {
  278. columns: this.table.columns,
  279. rows: rows,
  280. };
  281. }
  282. }