module.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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.addEditorTab('Thresholds & Time Regions', 'public/app/plugins/panel/graph/tab_thresholds_time_regions.html');
  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 };