module.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. import './graph';
  2. import './series_overrides_ctrl';
  3. import './thresholds_form';
  4. import './time_regions_form';
  5. import './annotation_tooltip';
  6. import './event_editor';
  7. import { auto } from 'angular';
  8. import { defaults, find, without } from 'lodash';
  9. import { DataFrame, FieldConfigProperty, PanelEvents, PanelPlugin } from '@grafana/data';
  10. import { locationService } from '@grafana/runtime';
  11. import { MetricsPanelCtrl } from 'app/angular/panel/metrics_panel_ctrl';
  12. import config from 'app/core/config';
  13. import TimeSeries from 'app/core/time_series2';
  14. import { ThresholdMapper } from 'app/features/alerting/state/ThresholdMapper';
  15. import { changePanelPlugin } from 'app/features/panel/state/actions';
  16. import { dispatch } from 'app/store/store';
  17. import { appEvents } from '../../../core/core';
  18. import { loadSnapshotData } from '../../../features/dashboard/utils/loadSnapshotData';
  19. import { annotationsFromDataFrames } from '../../../features/query/state/DashboardQueryRunner/utils';
  20. import { ZoomOutEvent } from '../../../types/events';
  21. import { GraphContextMenuCtrl } from './GraphContextMenuCtrl';
  22. import { graphPanelMigrationHandler } from './GraphMigrations';
  23. import { axesEditorComponent } from './axes_editor';
  24. import { DataProcessor } from './data_processor';
  25. import template from './template';
  26. import { DataWarning, GraphFieldConfig, GraphPanelOptions } from './types';
  27. import { getDataTimeRange } from './utils';
  28. export class GraphCtrl extends MetricsPanelCtrl {
  29. static template = template;
  30. renderError = false;
  31. hiddenSeries: any = {};
  32. hiddenSeriesTainted = false;
  33. seriesList: TimeSeries[] = [];
  34. dataList: DataFrame[] = [];
  35. annotations: any = [];
  36. alertState: any;
  37. dataWarning?: DataWarning;
  38. colors: any = [];
  39. subTabIndex = 0;
  40. processor: DataProcessor;
  41. contextMenuCtrl: GraphContextMenuCtrl;
  42. panelDefaults: any = {
  43. // datasource name, null = default datasource
  44. datasource: null,
  45. // sets client side (flot) or native graphite png renderer (png)
  46. renderer: 'flot',
  47. yaxes: [
  48. {
  49. label: null,
  50. show: true,
  51. logBase: 1,
  52. min: null,
  53. max: null,
  54. format: 'short',
  55. },
  56. {
  57. label: null,
  58. show: true,
  59. logBase: 1,
  60. min: null,
  61. max: null,
  62. format: 'short',
  63. },
  64. ],
  65. xaxis: {
  66. show: true,
  67. mode: 'time',
  68. name: null,
  69. values: [],
  70. buckets: null,
  71. },
  72. yaxis: {
  73. align: false,
  74. alignLevel: null,
  75. },
  76. // show/hide lines
  77. lines: true,
  78. // fill factor
  79. fill: 1,
  80. // fill gradient
  81. fillGradient: 0,
  82. // line width in pixels
  83. linewidth: 1,
  84. // show/hide dashed line
  85. dashes: false,
  86. // show/hide line
  87. hiddenSeries: false,
  88. // length of a dash
  89. dashLength: 10,
  90. // length of space between two dashes
  91. spaceLength: 10,
  92. // show hide points
  93. points: false,
  94. // point radius in pixels
  95. pointradius: 2,
  96. // show hide bars
  97. bars: false,
  98. // enable/disable stacking
  99. stack: false,
  100. // stack percentage mode
  101. percentage: false,
  102. // legend options
  103. legend: {
  104. show: true, // disable/enable legend
  105. values: false, // disable/enable legend values
  106. min: false,
  107. max: false,
  108. current: false,
  109. total: false,
  110. avg: false,
  111. },
  112. // how null points should be handled
  113. nullPointMode: 'null',
  114. // staircase line mode
  115. steppedLine: false,
  116. // tooltip options
  117. tooltip: {
  118. value_type: 'individual',
  119. shared: true,
  120. sort: 0,
  121. },
  122. // time overrides
  123. timeFrom: null,
  124. timeShift: null,
  125. // metric queries
  126. targets: [{}],
  127. // series color overrides
  128. aliasColors: {},
  129. // other style overrides
  130. seriesOverrides: [],
  131. thresholds: [],
  132. timeRegions: [],
  133. options: {
  134. // show/hide alert threshold lines and fill
  135. alertThreshold: true,
  136. },
  137. };
  138. /** @ngInject */
  139. constructor($scope: any, $injector: auto.IInjectorService) {
  140. super($scope, $injector);
  141. defaults(this.panel, this.panelDefaults);
  142. defaults(this.panel.tooltip, this.panelDefaults.tooltip);
  143. defaults(this.panel.legend, this.panelDefaults.legend);
  144. defaults(this.panel.xaxis, this.panelDefaults.xaxis);
  145. defaults(this.panel.options, this.panelDefaults.options);
  146. this.useDataFrames = true;
  147. this.processor = new DataProcessor(this.panel);
  148. this.contextMenuCtrl = new GraphContextMenuCtrl($scope);
  149. this.events.on(PanelEvents.render, this.onRender.bind(this));
  150. this.events.on(PanelEvents.dataFramesReceived, this.onDataFramesReceived.bind(this));
  151. this.events.on(PanelEvents.dataSnapshotLoad, this.onDataSnapshotLoad.bind(this));
  152. this.events.on(PanelEvents.editModeInitialized, this.onInitEditMode.bind(this));
  153. this.events.on(PanelEvents.initPanelActions, this.onInitPanelActions.bind(this));
  154. // set axes format from field config
  155. const fieldConfigUnit = this.panel.fieldConfig.defaults.unit;
  156. if (fieldConfigUnit) {
  157. this.panel.yaxes[0].format = fieldConfigUnit;
  158. }
  159. }
  160. onInitEditMode() {
  161. this.addEditorTab('Display', 'public/app/plugins/panel/graph/tab_display.html');
  162. this.addEditorTab('Series overrides', 'public/app/plugins/panel/graph/tab_series_overrides.html');
  163. this.addEditorTab('Axes', axesEditorComponent);
  164. this.addEditorTab('Legend', 'public/app/plugins/panel/graph/tab_legend.html');
  165. this.addEditorTab('Thresholds', 'public/app/plugins/panel/graph/tab_thresholds.html');
  166. this.addEditorTab('Time regions', 'public/app/plugins/panel/graph/tab_time_regions.html');
  167. this.subTabIndex = 0;
  168. this.hiddenSeriesTainted = false;
  169. }
  170. onInitPanelActions(actions: any[]) {
  171. actions.push({ text: 'Toggle legend', click: 'ctrl.toggleLegend()', shortcut: 'p l' });
  172. }
  173. zoomOut(evt: any) {
  174. appEvents.publish(new ZoomOutEvent({ scale: 2 }));
  175. }
  176. onDataSnapshotLoad(snapshotData: any) {
  177. const { series, annotations } = loadSnapshotData(this.panel, this.dashboard);
  178. this.panelData!.annotations = annotations;
  179. this.onDataFramesReceived(series);
  180. }
  181. onDataFramesReceived(data: DataFrame[]) {
  182. this.dataList = data;
  183. this.seriesList = this.processor.getSeriesList({
  184. dataList: this.dataList,
  185. range: this.range,
  186. });
  187. this.dataWarning = this.getDataWarning();
  188. this.alertState = undefined;
  189. (this.seriesList as any).alertState = undefined;
  190. if (this.panelData!.alertState) {
  191. this.alertState = this.panelData!.alertState;
  192. (this.seriesList as any).alertState = this.alertState.state;
  193. }
  194. this.annotations = [];
  195. if (this.panelData!.annotations?.length) {
  196. this.annotations = annotationsFromDataFrames(this.panelData!.annotations);
  197. }
  198. this.loading = false;
  199. this.render(this.seriesList);
  200. }
  201. getDataWarning(): DataWarning | undefined {
  202. const datapointsCount = this.seriesList.reduce((prev, series) => {
  203. return prev + series.datapoints.length;
  204. }, 0);
  205. if (datapointsCount === 0) {
  206. if (this.dataList) {
  207. for (const frame of this.dataList) {
  208. if (frame.length && frame.fields?.length) {
  209. return {
  210. title: 'Unable to graph data',
  211. tip: 'Data exists, but is not timeseries',
  212. actionText: 'Switch to table view',
  213. action: () => {
  214. dispatch(changePanelPlugin({ panel: this.panel, pluginId: 'table' }));
  215. },
  216. };
  217. }
  218. }
  219. }
  220. return {
  221. title: 'No data',
  222. tip: 'No data returned from query',
  223. };
  224. }
  225. // If any data is in range, do not return an error
  226. for (const series of this.seriesList) {
  227. if (!series.isOutsideRange) {
  228. return undefined;
  229. }
  230. }
  231. // All data is outside the time range
  232. const dataWarning: DataWarning = {
  233. title: 'Data outside time range',
  234. tip: 'Can be caused by timezone mismatch or missing time filter in query',
  235. };
  236. const range = getDataTimeRange(this.dataList);
  237. if (range) {
  238. dataWarning.actionText = 'Zoom to data';
  239. dataWarning.action = () => {
  240. locationService.partial({
  241. from: range.from,
  242. to: range.to,
  243. });
  244. };
  245. }
  246. return dataWarning;
  247. }
  248. onRender() {
  249. if (!this.seriesList) {
  250. return;
  251. }
  252. ThresholdMapper.alertToGraphThresholds(this.panel);
  253. for (const series of this.seriesList) {
  254. series.applySeriesOverrides(this.panel.seriesOverrides);
  255. // Always use the configured field unit
  256. if (series.unit) {
  257. this.panel.yaxes[series.yaxis - 1].format = series.unit;
  258. }
  259. if (this.hiddenSeriesTainted === false && series.hiddenSeries === true) {
  260. this.hiddenSeries[series.alias] = true;
  261. }
  262. }
  263. }
  264. onColorChange = (series: any, color: string) => {
  265. series.setColor(config.theme.visualization.getColorByName(color));
  266. this.panel.aliasColors[series.alias] = color;
  267. this.render();
  268. };
  269. onToggleSeries = (hiddenSeries: any) => {
  270. this.hiddenSeriesTainted = true;
  271. this.hiddenSeries = hiddenSeries;
  272. this.render();
  273. };
  274. onToggleSort = (sortBy: any, sortDesc: any) => {
  275. this.panel.legend.sort = sortBy;
  276. this.panel.legend.sortDesc = sortDesc;
  277. this.render();
  278. };
  279. onToggleAxis = (info: { alias: any; yaxis: any }) => {
  280. let override: any = find(this.panel.seriesOverrides, { alias: info.alias });
  281. if (!override) {
  282. override = { alias: info.alias };
  283. this.panel.seriesOverrides.push(override);
  284. }
  285. override.yaxis = info.yaxis;
  286. this.render();
  287. };
  288. addSeriesOverride(override: any) {
  289. this.panel.seriesOverrides.push(override || {});
  290. }
  291. removeSeriesOverride(override: any) {
  292. this.panel.seriesOverrides = without(this.panel.seriesOverrides, override);
  293. this.render();
  294. }
  295. toggleLegend() {
  296. this.panel.legend.show = !this.panel.legend.show;
  297. this.render();
  298. }
  299. legendValuesOptionChanged() {
  300. const legend = this.panel.legend;
  301. legend.values = legend.min || legend.max || legend.avg || legend.current || legend.total;
  302. this.render();
  303. }
  304. onContextMenuClose = () => {
  305. this.contextMenuCtrl.toggleMenu();
  306. };
  307. getTimeZone = () => this.dashboard.getTimezone();
  308. getDataFrameByRefId = (refId: string) => {
  309. return this.dataList.filter((dataFrame) => dataFrame.refId === refId)[0];
  310. };
  311. migrateToReact() {
  312. this.onPluginTypeChange(config.panels['timeseries']);
  313. }
  314. }
  315. // Use new react style configuration
  316. export const plugin = new PanelPlugin<GraphPanelOptions, GraphFieldConfig>(null)
  317. .useFieldConfig({
  318. disableStandardOptions: [
  319. FieldConfigProperty.NoValue,
  320. FieldConfigProperty.Thresholds,
  321. FieldConfigProperty.Max,
  322. FieldConfigProperty.Min,
  323. FieldConfigProperty.Decimals,
  324. FieldConfigProperty.Color,
  325. FieldConfigProperty.Mappings,
  326. ],
  327. })
  328. .setDataSupport({ annotations: true, alertStates: true })
  329. .setMigrationHandler(graphPanelMigrationHandler);
  330. // Use the angular ctrt rather than a react one
  331. plugin.angularPanelCtrl = GraphCtrl;