controller.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. /** @ngInject */
  9. constructor($scope, $rootScope, $q, panelSrv, panelHelper, annotationsSrv) {
  10. $scope.ctrl = this;
  11. $scope.pageIndex = 0;
  12. $scope.panelMeta = new PanelMeta({
  13. panelName: 'Table',
  14. editIcon: "fa fa-table",
  15. fullscreen: true,
  16. metricsEditor: true,
  17. });
  18. $scope.panelMeta.addEditorTab('Options', 'app/plugins/panels/table/options.html');
  19. $scope.panelMeta.addEditorTab('Time range', 'app/features/panel/partials/panelTime.html');
  20. var panelDefaults = {
  21. targets: [{}],
  22. transform: 'timeseries_to_rows',
  23. pageSize: null,
  24. showHeader: true,
  25. styles: [
  26. {
  27. type: 'date',
  28. pattern: 'Time',
  29. dateFormat: 'YYYY-MM-DD HH:mm:ss',
  30. },
  31. {
  32. unit: 'short',
  33. type: 'number',
  34. decimals: 2,
  35. colors: ["rgba(245, 54, 54, 0.9)", "rgba(237, 129, 40, 0.89)", "rgba(50, 172, 45, 0.97)"],
  36. colorMode: null,
  37. pattern: '/.*/',
  38. thresholds: [],
  39. }
  40. ],
  41. columns: [],
  42. scroll: true,
  43. fontSize: '100%',
  44. sort: {col: 0, desc: true},
  45. };
  46. $scope.init = function() {
  47. if ($scope.panel.styles === void 0) {
  48. $scope.panel.styles = $scope.panel.columns;
  49. $scope.panel.columns = $scope.panel.fields;
  50. delete $scope.panel.columns;
  51. delete $scope.panel.fields;
  52. }
  53. _.defaults($scope.panel, panelDefaults);
  54. panelSrv.init($scope);
  55. };
  56. $scope.refreshData = function(datasource) {
  57. panelHelper.updateTimeRange($scope);
  58. $scope.pageIndex = 0;
  59. if ($scope.panel.transform === 'annotations') {
  60. return annotationsSrv.getAnnotations($scope.dashboard).then(annotations => {
  61. $scope.dataRaw = annotations;
  62. $scope.render();
  63. });
  64. }
  65. return panelHelper.issueMetricQuery($scope, datasource)
  66. .then($scope.dataHandler, function(err) {
  67. $scope.render();
  68. throw err;
  69. });
  70. };
  71. $scope.toggleColumnSort = function(col, colIndex) {
  72. if ($scope.panel.sort.col === colIndex) {
  73. if ($scope.panel.sort.desc) {
  74. $scope.panel.sort.desc = false;
  75. } else {
  76. $scope.panel.sort.col = null;
  77. }
  78. } else {
  79. $scope.panel.sort.col = colIndex;
  80. $scope.panel.sort.desc = true;
  81. }
  82. $scope.render();
  83. };
  84. $scope.dataHandler = function(results) {
  85. $scope.dataRaw = results.data;
  86. $scope.pageIndex = 0;
  87. $scope.render();
  88. };
  89. $scope.render = function() {
  90. $scope.table = TableModel.transform($scope.dataRaw, $scope.panel);
  91. $scope.table.sort($scope.panel.sort);
  92. panelHelper.broadcastRender($scope, $scope.table, $scope.dataRaw);
  93. };
  94. $scope.init();
  95. }
  96. }