module.ts 9.3 KB

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