controller.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. {
  26. type: 'date',
  27. pattern: 'Time',
  28. dateFormat: 'YYYY-MM-DD HH:mm:ss',
  29. },
  30. {
  31. unit: 'short',
  32. type: 'number',
  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. fields: [],
  41. scroll: true,
  42. fontSize: '100%',
  43. sort: {col: 0, desc: true},
  44. };
  45. $scope.init = function() {
  46. _.defaults($scope.panel, panelDefaults);
  47. panelSrv.init($scope);
  48. };
  49. $scope.refreshData = function(datasource) {
  50. panelHelper.updateTimeRange($scope);
  51. $scope.pageIndex = 0;
  52. if ($scope.panel.transform === 'annotations') {
  53. return annotationsSrv.getAnnotations($scope.dashboard).then(annotations => {
  54. $scope.dataRaw = annotations;
  55. $scope.render();
  56. });
  57. }
  58. return panelHelper.issueMetricQuery($scope, datasource)
  59. .then($scope.dataHandler, function(err) {
  60. $scope.render();
  61. throw err;
  62. });
  63. };
  64. $scope.toggleColumnSort = function(col, colIndex) {
  65. if ($scope.panel.sort.col === colIndex) {
  66. if ($scope.panel.sort.desc) {
  67. $scope.panel.sort.desc = false;
  68. } else {
  69. $scope.panel.sort.col = null;
  70. }
  71. } else {
  72. $scope.panel.sort.col = colIndex;
  73. $scope.panel.sort.desc = true;
  74. }
  75. $scope.render();
  76. };
  77. $scope.dataHandler = function(results) {
  78. $scope.dataRaw = results.data;
  79. $scope.pageIndex = 0;
  80. $scope.render();
  81. };
  82. $scope.render = function() {
  83. $scope.table = TableModel.transform($scope.dataRaw, $scope.panel);
  84. $scope.table.sort($scope.panel.sort);
  85. panelHelper.broadcastRender($scope, $scope.table, $scope.dataRaw);
  86. };
  87. $scope.init();
  88. }
  89. }