module.ts 8.5 KB

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