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