module.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. import _ from 'lodash';
  2. import $ from 'jquery';
  3. import { MetricsPanelCtrl } from 'app/plugins/sdk';
  4. import config from 'app/core/config';
  5. import { transformDataToTable } from './transformers';
  6. import { tablePanelEditor } from './editor';
  7. import { columnOptionsTab } from './column_options';
  8. import { TableRenderer } from './renderer';
  9. import { isTableData } from '@grafana/data';
  10. import { TemplateSrv } from 'app/features/templating/template_srv';
  11. class TablePanelCtrl extends MetricsPanelCtrl {
  12. static templateUrl = 'module.html';
  13. pageIndex: number;
  14. dataRaw: any;
  15. table: any;
  16. renderer: any;
  17. panelDefaults: any = {
  18. targets: [{}],
  19. transform: 'timeseries_to_columns',
  20. pageSize: null,
  21. showHeader: true,
  22. styles: [
  23. {
  24. type: 'date',
  25. pattern: 'Time',
  26. alias: 'Time',
  27. dateFormat: 'YYYY-MM-DD HH:mm:ss',
  28. },
  29. {
  30. unit: 'short',
  31. type: 'number',
  32. alias: '',
  33. decimals: 2,
  34. colors: ['rgba(245, 54, 54, 0.9)', 'rgba(237, 129, 40, 0.89)', 'rgba(50, 172, 45, 0.97)'],
  35. colorMode: null,
  36. pattern: '/.*/',
  37. thresholds: [],
  38. },
  39. ],
  40. columns: [],
  41. scroll: true,
  42. fontSize: '100%',
  43. sort: { col: 0, desc: true },
  44. };
  45. /** @ngInject */
  46. constructor(
  47. $scope: any,
  48. $injector: any,
  49. templateSrv: TemplateSrv,
  50. private annotationsSrv: any,
  51. private $sanitize: any,
  52. private variableSrv: any
  53. ) {
  54. super($scope, $injector);
  55. this.pageIndex = 0;
  56. if (this.panel.styles === void 0) {
  57. this.panel.styles = this.panel.columns;
  58. this.panel.columns = this.panel.fields;
  59. delete this.panel.columns;
  60. delete this.panel.fields;
  61. }
  62. _.defaults(this.panel, this.panelDefaults);
  63. this.events.on('data-received', this.onDataReceived.bind(this));
  64. this.events.on('data-error', this.onDataError.bind(this));
  65. this.events.on('data-snapshot-load', this.onDataReceived.bind(this));
  66. this.events.on('init-edit-mode', this.onInitEditMode.bind(this));
  67. this.events.on('init-panel-actions', this.onInitPanelActions.bind(this));
  68. }
  69. onInitEditMode() {
  70. this.addEditorTab('Options', tablePanelEditor, 2);
  71. this.addEditorTab('Column Styles', columnOptionsTab, 3);
  72. }
  73. onInitPanelActions(actions: any[]) {
  74. actions.push({ text: 'Export CSV', click: 'ctrl.exportCsv()' });
  75. }
  76. issueQueries(datasource: any) {
  77. this.pageIndex = 0;
  78. if (this.panel.transform === 'annotations') {
  79. return this.annotationsSrv
  80. .getAnnotations({
  81. dashboard: this.dashboard,
  82. panel: this.panel,
  83. range: this.range,
  84. })
  85. .then((anno: any) => {
  86. this.loading = false;
  87. this.dataRaw = anno;
  88. this.pageIndex = 0;
  89. this.render();
  90. return { data: this.dataRaw }; // Not used
  91. });
  92. }
  93. return super.issueQueries(datasource);
  94. }
  95. onDataError(err: any) {
  96. this.dataRaw = [];
  97. this.render();
  98. }
  99. onDataReceived(dataList: any) {
  100. this.dataRaw = dataList;
  101. this.pageIndex = 0;
  102. // automatically correct transform mode based on data
  103. if (this.dataRaw && this.dataRaw.length) {
  104. if (isTableData(this.dataRaw[0])) {
  105. this.panel.transform = 'table';
  106. } else {
  107. if (this.dataRaw[0].type === 'docs') {
  108. this.panel.transform = 'json';
  109. } else {
  110. if (this.panel.transform === 'table' || this.panel.transform === 'json') {
  111. this.panel.transform = 'timeseries_to_rows';
  112. }
  113. }
  114. }
  115. }
  116. this.render();
  117. }
  118. render() {
  119. this.table = transformDataToTable(this.dataRaw, this.panel);
  120. this.table.sort(this.panel.sort);
  121. this.renderer = new TableRenderer(
  122. this.panel,
  123. this.table,
  124. this.dashboard.isTimezoneUtc(),
  125. this.$sanitize,
  126. this.templateSrv,
  127. config.theme.type
  128. );
  129. return super.render(this.table);
  130. }
  131. toggleColumnSort(col: any, colIndex: any) {
  132. // remove sort flag from current column
  133. if (this.table.columns[this.panel.sort.col]) {
  134. this.table.columns[this.panel.sort.col].sort = false;
  135. }
  136. if (this.panel.sort.col === colIndex) {
  137. if (this.panel.sort.desc) {
  138. this.panel.sort.desc = false;
  139. } else {
  140. this.panel.sort.col = null;
  141. }
  142. } else {
  143. this.panel.sort.col = colIndex;
  144. this.panel.sort.desc = true;
  145. }
  146. this.render();
  147. }
  148. exportCsv() {
  149. const scope = this.$scope.$new(true);
  150. scope.tableData = this.renderer.render_values();
  151. scope.panel = 'table';
  152. this.publishAppEvent('show-modal', {
  153. templateHtml: '<export-data-modal panel="panel" data="tableData"></export-data-modal>',
  154. scope,
  155. modalClass: 'modal--narrow',
  156. });
  157. }
  158. link(scope: any, elem: JQuery, attrs: any, ctrl: TablePanelCtrl) {
  159. let data: any;
  160. const panel = ctrl.panel;
  161. let pageCount = 0;
  162. function getTableHeight() {
  163. let panelHeight = ctrl.height;
  164. if (pageCount > 1) {
  165. panelHeight -= 26;
  166. }
  167. return panelHeight - 31 + 'px';
  168. }
  169. function appendTableRows(tbodyElem: JQuery) {
  170. ctrl.renderer.setTable(data);
  171. tbodyElem.empty();
  172. tbodyElem.html(ctrl.renderer.render(ctrl.pageIndex));
  173. }
  174. function switchPage(e: any) {
  175. const el = $(e.currentTarget);
  176. ctrl.pageIndex = parseInt(el.text(), 10) - 1;
  177. renderPanel();
  178. }
  179. function appendPaginationControls(footerElem: JQuery) {
  180. footerElem.empty();
  181. const pageSize = panel.pageSize || 100;
  182. pageCount = Math.ceil(data.rows.length / pageSize);
  183. if (pageCount === 1) {
  184. return;
  185. }
  186. const startPage = Math.max(ctrl.pageIndex - 3, 0);
  187. const endPage = Math.min(pageCount, startPage + 9);
  188. const paginationList = $('<ul></ul>');
  189. for (let i = startPage; i < endPage; i++) {
  190. const activeClass = i === ctrl.pageIndex ? 'active' : '';
  191. const pageLinkElem = $(
  192. '<li><a class="table-panel-page-link pointer ' + activeClass + '">' + (i + 1) + '</a></li>'
  193. );
  194. paginationList.append(pageLinkElem);
  195. }
  196. footerElem.append(paginationList);
  197. }
  198. function renderPanel() {
  199. const panelElem = elem.parents('.panel-content');
  200. const rootElem = elem.find('.table-panel-scroll');
  201. const tbodyElem = elem.find('tbody');
  202. const footerElem = elem.find('.table-panel-footer');
  203. elem.css({ 'font-size': panel.fontSize });
  204. panelElem.addClass('table-panel-content');
  205. appendTableRows(tbodyElem);
  206. appendPaginationControls(footerElem);
  207. rootElem.css({ 'max-height': panel.scroll ? getTableHeight() : '' });
  208. }
  209. // hook up link tooltips
  210. elem.tooltip({
  211. selector: '[data-link-tooltip]',
  212. });
  213. function addFilterClicked(e: any) {
  214. const filterData = $(e.currentTarget).data();
  215. const options = {
  216. datasource: panel.datasource,
  217. key: data.columns[filterData.column].text,
  218. value: data.rows[filterData.row][filterData.column],
  219. operator: filterData.operator,
  220. };
  221. ctrl.variableSrv.setAdhocFilter(options);
  222. }
  223. elem.on('click', '.table-panel-page-link', switchPage);
  224. elem.on('click', '.table-panel-filter-link', addFilterClicked);
  225. const unbindDestroy = scope.$on('$destroy', () => {
  226. elem.off('click', '.table-panel-page-link');
  227. elem.off('click', '.table-panel-filter-link');
  228. unbindDestroy();
  229. });
  230. ctrl.events.on('render', (renderData: any) => {
  231. data = renderData || data;
  232. if (data) {
  233. renderPanel();
  234. }
  235. ctrl.renderingCompleted();
  236. });
  237. }
  238. }
  239. export { TablePanelCtrl, TablePanelCtrl as PanelCtrl };