controller.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. ///<reference path="../../headers/common.d.ts" />
  2. import angular = require('angular');
  3. import _ = require('lodash');
  4. import moment = require('moment');
  5. import PanelMeta = require('app/features/panel/panel_meta');
  6. import {TableModel} from './table_model';
  7. export class TablePanelCtrl {
  8. constructor($scope, $rootScope, $q, panelSrv, panelHelper, annotationsSrv) {
  9. $scope.ctrl = this;
  10. $scope.pageIndex = 0;
  11. $scope.panelMeta = new PanelMeta({
  12. panelName: 'Table',
  13. editIcon: "fa fa-table",
  14. fullscreen: true,
  15. metricsEditor: true,
  16. });
  17. $scope.panelMeta.addEditorTab('Options', 'app/panels/table/options.html');
  18. $scope.panelMeta.addEditorTab('Time range', 'app/features/panel/partials/panelTime.html');
  19. var panelDefaults = {
  20. targets: [{}],
  21. transform: 'timeseries_to_rows',
  22. pageSize: 50,
  23. showHeader: true,
  24. columns: [],
  25. fields: [],
  26. scroll: true,
  27. fontSize: '100%',
  28. sort: {col: null, desc: true},
  29. };
  30. $scope.init = function() {
  31. _.defaults($scope.panel, panelDefaults);
  32. panelSrv.init($scope);
  33. };
  34. $scope.refreshData = function(datasource) {
  35. panelHelper.updateTimeRange($scope);
  36. $scope.pageIndex = 0;
  37. if ($scope.panel.transform === 'annotations') {
  38. return annotationsSrv.getAnnotations($scope.dashboard).then(annotations => {
  39. $scope.dataRaw = annotations;
  40. $scope.render();
  41. });
  42. }
  43. return panelHelper.issueMetricQuery($scope, datasource)
  44. .then($scope.dataHandler, function(err) {
  45. $scope.render();
  46. throw err;
  47. });
  48. };
  49. $scope.toggleColumnSort = function(col, colIndex) {
  50. if ($scope.panel.sort.col === colIndex) {
  51. if ($scope.panel.sort.desc) {
  52. $scope.panel.sort.desc = false;
  53. } else {
  54. $scope.panel.sort.col = null;
  55. }
  56. } else {
  57. $scope.panel.sort.col = colIndex;
  58. $scope.panel.sort.desc = true;
  59. }
  60. $scope.render();
  61. };
  62. $scope.dataHandler = function(results) {
  63. $scope.dataRaw = results.data;
  64. $scope.pageIndex = 0;
  65. $scope.render();
  66. };
  67. $scope.render = function() {
  68. $scope.table = TableModel.transform($scope.dataRaw, $scope.panel);
  69. $scope.table.sort($scope.panel.sort);
  70. panelHelper.broadcastRender($scope, $scope.table, $scope.dataRaw);
  71. };
  72. $scope.init();
  73. }
  74. }