module.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. import './graph';
  2. import './series_overrides_ctrl';
  3. import './thresholds_form';
  4. import template from './template';
  5. import _ from 'lodash';
  6. import { MetricsPanelCtrl } from 'app/plugins/sdk';
  7. import { DataProcessor } from './data_processor';
  8. import { axesEditorComponent } from './axes_editor';
  9. class GraphCtrl extends MetricsPanelCtrl {
  10. static template = template;
  11. renderError: boolean;
  12. hiddenSeries: any = {};
  13. seriesList: any = [];
  14. dataList: any = [];
  15. annotations: any = [];
  16. alertState: any;
  17. annotationsPromise: any;
  18. dataWarning: any;
  19. colors: any = [];
  20. subTabIndex: number;
  21. processor: DataProcessor;
  22. panelDefaults = {
  23. // datasource name, null = default datasource
  24. datasource: null,
  25. // sets client side (flot) or native graphite png renderer (png)
  26. renderer: 'flot',
  27. yaxes: [
  28. {
  29. label: null,
  30. show: true,
  31. logBase: 1,
  32. min: null,
  33. max: null,
  34. format: 'short',
  35. },
  36. {
  37. label: null,
  38. show: true,
  39. logBase: 1,
  40. min: null,
  41. max: null,
  42. format: 'short',
  43. },
  44. ],
  45. xaxis: {
  46. show: true,
  47. mode: 'time',
  48. name: null,
  49. values: [],
  50. buckets: null,
  51. },
  52. yaxis: {
  53. align: false,
  54. alignLevel: null,
  55. },
  56. // show/hide lines
  57. lines: true,
  58. // fill factor
  59. fill: 1,
  60. // line width in pixels
  61. linewidth: 1,
  62. // show/hide dashed line
  63. dashes: false,
  64. // length of a dash
  65. dashLength: 10,
  66. // length of space between two dashes
  67. paceLength: 10,
  68. // show hide points
  69. points: false,
  70. // point radius in pixels
  71. pointradius: 2,
  72. // show hide bars
  73. bars: false,
  74. // enable/disable stacking
  75. stack: false,
  76. // stack percentage mode
  77. percentage: false,
  78. // legend options
  79. legend: {
  80. show: true, // disable/enable legend
  81. values: false, // disable/enable legend values
  82. min: false,
  83. max: false,
  84. current: false,
  85. total: false,
  86. avg: false,
  87. },
  88. // how null points should be handled
  89. nullPointMode: 'null',
  90. // staircase line mode
  91. steppedLine: false,
  92. // tooltip options
  93. tooltip: {
  94. value_type: 'individual',
  95. shared: true,
  96. sort: 0,
  97. },
  98. // time overrides
  99. timeFrom: null,
  100. timeShift: null,
  101. // metric queries
  102. targets: [{}],
  103. // series color overrides
  104. aliasColors: {},
  105. // other style overrides
  106. seriesOverrides: [],
  107. thresholds: [],
  108. };
  109. /** @ngInject */
  110. constructor($scope, $injector, private annotationsSrv) {
  111. super($scope, $injector);
  112. _.defaults(this.panel, this.panelDefaults);
  113. _.defaults(this.panel.tooltip, this.panelDefaults.tooltip);
  114. _.defaults(this.panel.legend, this.panelDefaults.legend);
  115. _.defaults(this.panel.xaxis, this.panelDefaults.xaxis);
  116. this.processor = new DataProcessor(this.panel);
  117. this.events.on('render', this.onRender.bind(this));
  118. this.events.on('data-received', this.onDataReceived.bind(this));
  119. this.events.on('data-error', this.onDataError.bind(this));
  120. this.events.on('data-snapshot-load', this.onDataSnapshotLoad.bind(this));
  121. this.events.on('init-edit-mode', this.onInitEditMode.bind(this));
  122. this.events.on('init-panel-actions', this.onInitPanelActions.bind(this));
  123. }
  124. onInitEditMode() {
  125. this.addEditorTab('Display', 'public/app/plugins/panel/graph/tab_display.html', 4);
  126. this.addEditorTab('Axes', axesEditorComponent, 2);
  127. this.addEditorTab('Legend', 'public/app/plugins/panel/graph/tab_legend.html', 3);
  128. // if (config.alertingEnabled) {
  129. // this.addEditorTab('Alert', alertTab, 5);
  130. // }
  131. this.subTabIndex = 0;
  132. }
  133. onInitPanelActions(actions) {
  134. actions.push({ text: 'Export CSV', click: 'ctrl.exportCsv()' });
  135. actions.push({ text: 'Toggle legend', click: 'ctrl.toggleLegend()', shortcut: 'p l' });
  136. }
  137. issueQueries(datasource) {
  138. this.annotationsPromise = this.annotationsSrv.getAnnotations({
  139. dashboard: this.dashboard,
  140. panel: this.panel,
  141. range: this.range,
  142. });
  143. /* Wait for annotationSrv requests to get datasources to
  144. * resolve before issuing queries. This allows the annotations
  145. * service to fire annotations queries before graph queries
  146. * (but not wait for completion). This resolves
  147. * issue 11806.
  148. */
  149. return this.annotationsSrv.datasourcePromises.then(r => {
  150. return super.issueQueries(datasource);
  151. });
  152. }
  153. zoomOut(evt) {
  154. this.publishAppEvent('zoom-out', 2);
  155. }
  156. onDataSnapshotLoad(snapshotData) {
  157. this.annotationsPromise = this.annotationsSrv.getAnnotations({
  158. dashboard: this.dashboard,
  159. panel: this.panel,
  160. range: this.range,
  161. });
  162. this.onDataReceived(snapshotData);
  163. }
  164. onDataError(err) {
  165. this.seriesList = [];
  166. this.annotations = [];
  167. this.render([]);
  168. }
  169. onDataReceived(dataList) {
  170. this.dataList = dataList;
  171. this.seriesList = this.processor.getSeriesList({
  172. dataList: dataList,
  173. range: this.range,
  174. });
  175. this.dataWarning = null;
  176. const datapointsCount = this.seriesList.reduce((prev, series) => {
  177. return prev + series.datapoints.length;
  178. }, 0);
  179. if (datapointsCount === 0) {
  180. this.dataWarning = {
  181. title: 'No data points',
  182. tip: 'No datapoints returned from data query',
  183. };
  184. } else {
  185. for (const series of this.seriesList) {
  186. if (series.isOutsideRange) {
  187. this.dataWarning = {
  188. title: 'Data points outside time range',
  189. tip: 'Can be caused by timezone mismatch or missing time filter in query',
  190. };
  191. break;
  192. }
  193. }
  194. }
  195. this.annotationsPromise.then(
  196. result => {
  197. this.loading = false;
  198. this.alertState = result.alertState;
  199. this.annotations = result.annotations;
  200. this.render(this.seriesList);
  201. },
  202. () => {
  203. this.loading = false;
  204. this.render(this.seriesList);
  205. }
  206. );
  207. }
  208. onRender() {
  209. if (!this.seriesList) {
  210. return;
  211. }
  212. for (const series of this.seriesList) {
  213. series.applySeriesOverrides(this.panel.seriesOverrides);
  214. if (series.unit) {
  215. this.panel.yaxes[series.yaxis - 1].format = series.unit;
  216. }
  217. }
  218. }
  219. onColorChange = (series, color) => {
  220. series.setColor(color);
  221. this.panel.aliasColors[series.alias] = series.color;
  222. this.render();
  223. };
  224. onToggleSeries = hiddenSeries => {
  225. this.hiddenSeries = hiddenSeries;
  226. this.render();
  227. };
  228. onToggleSort = (sortBy, sortDesc) => {
  229. this.panel.legend.sort = sortBy;
  230. this.panel.legend.sortDesc = sortDesc;
  231. this.render();
  232. };
  233. onToggleAxis = info => {
  234. let override = _.find(this.panel.seriesOverrides, { alias: info.alias });
  235. if (!override) {
  236. override = { alias: info.alias };
  237. this.panel.seriesOverrides.push(override);
  238. }
  239. override.yaxis = info.yaxis;
  240. this.render();
  241. };
  242. addSeriesOverride(override) {
  243. this.panel.seriesOverrides.push(override || {});
  244. }
  245. removeSeriesOverride(override) {
  246. this.panel.seriesOverrides = _.without(this.panel.seriesOverrides, override);
  247. this.render();
  248. }
  249. toggleLegend() {
  250. this.panel.legend.show = !this.panel.legend.show;
  251. this.refresh();
  252. }
  253. legendValuesOptionChanged() {
  254. const legend = this.panel.legend;
  255. legend.values = legend.min || legend.max || legend.avg || legend.current || legend.total;
  256. this.render();
  257. }
  258. exportCsv() {
  259. const scope = this.$scope.$new(true);
  260. scope.seriesList = this.seriesList;
  261. this.publishAppEvent('show-modal', {
  262. templateHtml: '<export-data-modal data="seriesList"></export-data-modal>',
  263. scope,
  264. modalClass: 'modal--narrow',
  265. });
  266. }
  267. }
  268. export { GraphCtrl, GraphCtrl as PanelCtrl };