module.ts 7.6 KB

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