controller.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. ///<reference path="../../../headers/common.d.ts" />
  2. import angular from 'angular';
  3. import _ from 'lodash';
  4. import moment from 'moment';
  5. import * as FileExport from 'app/core/utils/file_export';
  6. import {MetricsPanelCtrl} from '../../../features/panel/panel';
  7. import {transformDataToTable} from './transformers';
  8. import {tablePanelEditor} from './editor';
  9. var panelDefaults = {
  10. targets: [{}],
  11. transform: 'timeseries_to_columns',
  12. pageSize: null,
  13. showHeader: true,
  14. styles: [
  15. {
  16. type: 'date',
  17. pattern: 'Time',
  18. dateFormat: 'YYYY-MM-DD HH:mm:ss',
  19. },
  20. {
  21. unit: 'short',
  22. type: 'number',
  23. decimals: 2,
  24. colors: ["rgba(245, 54, 54, 0.9)", "rgba(237, 129, 40, 0.89)", "rgba(50, 172, 45, 0.97)"],
  25. colorMode: null,
  26. pattern: '/.*/',
  27. thresholds: [],
  28. }
  29. ],
  30. columns: [],
  31. scroll: true,
  32. fontSize: '100%',
  33. sort: {col: 0, desc: true},
  34. };
  35. export class TablePanelCtrl extends MetricsPanelCtrl {
  36. pageIndex: number;
  37. dataRaw: any;
  38. table: any;
  39. /** @ngInject */
  40. constructor($scope, $injector, private annotationsSrv) {
  41. super($scope, $injector);
  42. this.pageIndex = 0;
  43. if (this.panel.styles === void 0) {
  44. this.panel.styles = this.panel.columns;
  45. this.panel.columns = this.panel.fields;
  46. delete this.panel.columns;
  47. delete this.panel.fields;
  48. }
  49. _.defaults(this.panel, panelDefaults);
  50. }
  51. initEditMode() {
  52. super.initEditMode();
  53. this.addEditorTab('Options', tablePanelEditor, 1);
  54. }
  55. getExtendedMenu() {
  56. var menu = super.getExtendedMenu();
  57. menu.push({text: 'Export CSV', click: 'ctrl.exportCsv()'});
  58. return menu;
  59. }
  60. refreshData(datasource) {
  61. this.pageIndex = 0;
  62. if (this.panel.transform === 'annotations') {
  63. return this.annotationsSrv.getAnnotations(this.dashboard).then(annotations => {
  64. this.dataRaw = annotations;
  65. this.render();
  66. });
  67. }
  68. return this.issueQueries(datasource)
  69. .then(this.dataHandler.bind(this))
  70. .catch(err => {
  71. this.render();
  72. throw err;
  73. });
  74. }
  75. toggleColumnSort(col, colIndex) {
  76. if (this.panel.sort.col === colIndex) {
  77. if (this.panel.sort.desc) {
  78. this.panel.sort.desc = false;
  79. } else {
  80. this.panel.sort.col = null;
  81. }
  82. } else {
  83. this.panel.sort.col = colIndex;
  84. this.panel.sort.desc = true;
  85. }
  86. this.render();
  87. }
  88. dataHandler(results) {
  89. this.dataRaw = results.data;
  90. this.pageIndex = 0;
  91. this.render();
  92. }
  93. render() {
  94. // automatically correct transform mode
  95. // based on data
  96. if (this.dataRaw && this.dataRaw.length) {
  97. if (this.dataRaw[0].type === 'table') {
  98. this.panel.transform = 'table';
  99. } else {
  100. if (this.dataRaw[0].type === 'docs') {
  101. this.panel.transform = 'json';
  102. } else {
  103. if (this.panel.transform === 'table' || this.panel.transform === 'json') {
  104. this.panel.transform = 'timeseries_to_rows';
  105. }
  106. }
  107. }
  108. }
  109. this.table = transformDataToTable(this.dataRaw, this.panel);
  110. this.table.sort(this.panel.sort);
  111. this.broadcastRender(this.table);
  112. }
  113. exportCsv() {
  114. FileExport.exportTableDataToCsv(this.table);
  115. }
  116. }