module.ts 7.4 KB

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