module.ts 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. return super.issueQueries(datasource);
  146. }
  147. zoomOut(evt) {
  148. this.publishAppEvent('zoom-out', 2);
  149. }
  150. onDataSnapshotLoad(snapshotData) {
  151. this.annotationsPromise = this.annotationsSrv.getAnnotations({
  152. dashboard: this.dashboard,
  153. panel: this.panel,
  154. range: this.range,
  155. });
  156. this.onDataReceived(snapshotData);
  157. }
  158. onDataError(err) {
  159. this.seriesList = [];
  160. this.annotations = [];
  161. this.render([]);
  162. }
  163. onDataReceived(dataList) {
  164. console.log(dataList);
  165. this.dataList = dataList;
  166. this.seriesList = this.processor.getSeriesList({
  167. dataList: dataList,
  168. range: this.range,
  169. });
  170. this.dataWarning = null;
  171. const datapointsCount = this.seriesList.reduce((prev, series) => {
  172. return prev + series.datapoints.length;
  173. }, 0);
  174. if (datapointsCount === 0) {
  175. this.dataWarning = {
  176. title: 'No data points',
  177. tip: 'No datapoints returned from data query',
  178. };
  179. } else {
  180. for (const series of this.seriesList) {
  181. if (series.isOutsideRange) {
  182. this.dataWarning = {
  183. title: 'Data points outside time range',
  184. tip: 'Can be caused by timezone mismatch or missing time filter in query',
  185. };
  186. break;
  187. }
  188. }
  189. }
  190. this.annotationsPromise.then(
  191. result => {
  192. this.loading = false;
  193. this.alertState = result.alertState;
  194. this.annotations = result.annotations;
  195. this.render(this.seriesList);
  196. },
  197. () => {
  198. this.loading = false;
  199. this.render(this.seriesList);
  200. }
  201. );
  202. }
  203. onRender() {
  204. if (!this.seriesList) {
  205. return;
  206. }
  207. for (const series of this.seriesList) {
  208. series.applySeriesOverrides(this.panel.seriesOverrides);
  209. if (series.unit) {
  210. this.panel.yaxes[series.yaxis - 1].format = series.unit;
  211. }
  212. }
  213. }
  214. changeSeriesColor(series, color) {
  215. series.setColor(color);
  216. this.panel.aliasColors[series.alias] = series.color;
  217. this.render();
  218. }
  219. toggleSeries(serie, event) {
  220. if (event.ctrlKey || event.metaKey || event.shiftKey) {
  221. if (this.hiddenSeries[serie.alias]) {
  222. delete this.hiddenSeries[serie.alias];
  223. } else {
  224. this.hiddenSeries[serie.alias] = true;
  225. }
  226. } else {
  227. this.toggleSeriesExclusiveMode(serie);
  228. }
  229. this.render();
  230. }
  231. toggleSeriesExclusiveMode(serie) {
  232. const hidden = this.hiddenSeries;
  233. if (hidden[serie.alias]) {
  234. delete hidden[serie.alias];
  235. }
  236. // check if every other series is hidden
  237. const alreadyExclusive = _.every(this.seriesList, value => {
  238. if (value.alias === serie.alias) {
  239. return true;
  240. }
  241. return hidden[value.alias];
  242. });
  243. if (alreadyExclusive) {
  244. // remove all hidden series
  245. _.each(this.seriesList, value => {
  246. delete this.hiddenSeries[value.alias];
  247. });
  248. } else {
  249. // hide all but this serie
  250. _.each(this.seriesList, value => {
  251. if (value.alias === serie.alias) {
  252. return;
  253. }
  254. this.hiddenSeries[value.alias] = true;
  255. });
  256. }
  257. }
  258. toggleAxis(info) {
  259. let override = _.find(this.panel.seriesOverrides, { alias: info.alias });
  260. if (!override) {
  261. override = { alias: info.alias };
  262. this.panel.seriesOverrides.push(override);
  263. }
  264. info.yaxis = override.yaxis = info.yaxis === 2 ? 1 : 2;
  265. this.render();
  266. }
  267. addSeriesOverride(override) {
  268. this.panel.seriesOverrides.push(override || {});
  269. }
  270. removeSeriesOverride(override) {
  271. this.panel.seriesOverrides = _.without(this.panel.seriesOverrides, override);
  272. this.render();
  273. }
  274. toggleLegend() {
  275. this.panel.legend.show = !this.panel.legend.show;
  276. this.refresh();
  277. }
  278. legendValuesOptionChanged() {
  279. const legend = this.panel.legend;
  280. legend.values = legend.min || legend.max || legend.avg || legend.current || legend.total;
  281. this.render();
  282. }
  283. exportCsv() {
  284. const scope = this.$scope.$new(true);
  285. scope.seriesList = this.seriesList;
  286. this.publishAppEvent('show-modal', {
  287. templateHtml: '<export-data-modal data="seriesList"></export-data-modal>',
  288. scope,
  289. modalClass: 'modal--narrow',
  290. });
  291. }
  292. }
  293. export { GraphCtrl, GraphCtrl as PanelCtrl };