module.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. /** @scratch /panels/5
  2. * include::panels/histogram.asciidoc[]
  3. */
  4. /** @scratch /panels/histogram/0
  5. * == Histogram
  6. * Status: *Stable*
  7. *
  8. * The histogram panel allow for the display of time charts. It includes several modes and tranformations
  9. * to display event counts, mean, min, max and total of numeric fields, and derivatives of counter
  10. * fields.
  11. *
  12. */
  13. define([
  14. 'angular',
  15. 'app',
  16. 'jquery',
  17. 'underscore',
  18. 'kbn',
  19. 'moment',
  20. './timeSeries',
  21. './graphiteSrv',
  22. 'jquery.flot',
  23. 'jquery.flot.events',
  24. 'jquery.flot.selection',
  25. 'jquery.flot.time',
  26. 'jquery.flot.byte',
  27. 'jquery.flot.stack',
  28. 'jquery.flot.stackpercent'
  29. ],
  30. function (angular, app, $, _, kbn, moment, timeSeries, graphiteSrv) {
  31. 'use strict';
  32. var module = angular.module('kibana.panels.graphite', []);
  33. app.useModule(module);
  34. module.controller('graphite', function($scope, $rootScope, querySrv, dashboard, filterSrv) {
  35. $scope.panelMeta = {
  36. modals : [
  37. {
  38. description: "Inspect",
  39. icon: "icon-info-sign",
  40. partial: "app/partials/inspector.html",
  41. show: $scope.panel.spyable
  42. }
  43. ],
  44. editorTabs : [
  45. {
  46. title:'Style',
  47. src:'app/panels/graphite/styleEditor.html'
  48. }
  49. ],
  50. status : "Stable",
  51. description : "A bucketed time series chart of the current query or queries. Uses the "+
  52. "Elasticsearch date_histogram facet. If using time stamped indices this panel will query"+
  53. " them sequentially to attempt to apply the lighest possible load to your Elasticsearch cluster"
  54. };
  55. // Set and populate defaults
  56. var _d = {
  57. /** @scratch /panels/histogram/3
  58. * x-axis:: Show the x-axis
  59. */
  60. 'x-axis' : true,
  61. /** @scratch /panels/histogram/3
  62. * y-axis:: Show the y-axis
  63. */
  64. 'y-axis' : true,
  65. /** @scratch /panels/histogram/3
  66. * scale:: Scale the y-axis by this factor
  67. */
  68. scale : 1,
  69. /** @scratch /panels/histogram/3
  70. * y_format:: 'none','bytes','short '
  71. */
  72. y_format : 'none',
  73. /** @scratch /panels/histogram/5
  74. * grid object:: Min and max y-axis values
  75. * grid.min::: Minimum y-axis value
  76. * grid.max::: Maximum y-axis value
  77. */
  78. grid : {
  79. max: null,
  80. min: 0
  81. },
  82. /** @scratch /panels/histogram/3
  83. * ==== Annotations
  84. * annotate object:: A query can be specified, the results of which will be displayed as markers on
  85. * the chart. For example, for noting code deploys.
  86. * annotate.enable::: Should annotations, aka markers, be shown?
  87. * annotate.query::: Lucene query_string syntax query to use for markers.
  88. * annotate.size::: Max number of markers to show
  89. * annotate.field::: Field from documents to show
  90. * annotate.sort::: Sort array in format [field,order], For example [`@timestamp',`desc']
  91. */
  92. annotate : {
  93. enable : false,
  94. query : "*",
  95. size : 20,
  96. field : '_type',
  97. sort : ['_score','desc']
  98. },
  99. /** @scratch /panels/histogram/3
  100. * ==== Interval options
  101. * auto_int:: Automatically scale intervals?
  102. */
  103. auto_int : true,
  104. /** @scratch /panels/histogram/3
  105. * resolution:: If auto_int is true, shoot for this many bars.
  106. */
  107. resolution : 100,
  108. /** @scratch /panels/histogram/3
  109. * interval:: If auto_int is set to false, use this as the interval.
  110. */
  111. interval : '5m',
  112. /** @scratch /panels/histogram/3
  113. * interval:: Array of possible intervals in the *View* selector. Example [`auto',`1s',`5m',`3h']
  114. */
  115. intervals : ['auto','1s','1m','5m','10m','30m','1h','3h','12h','1d','1w','1y'],
  116. /** @scratch /panels/histogram/3
  117. * ==== Drawing options
  118. * lines:: Show line chart
  119. */
  120. lines : true,
  121. /** @scratch /panels/histogram/3
  122. * fill:: Area fill factor for line charts, 1-10
  123. */
  124. fill : 0,
  125. /** @scratch /panels/histogram/3
  126. * linewidth:: Weight of lines in pixels
  127. */
  128. linewidth : 1,
  129. /** @scratch /panels/histogram/3
  130. * points:: Show points on chart
  131. */
  132. points : false,
  133. /** @scratch /panels/histogram/3
  134. * pointradius:: Size of points in pixels
  135. */
  136. pointradius : 5,
  137. /** @scratch /panels/histogram/3
  138. * bars:: Show bars on chart
  139. */
  140. bars : false,
  141. /** @scratch /panels/histogram/3
  142. * stack:: Stack multiple series
  143. */
  144. stack : true,
  145. /** @scratch /panels/histogram/3
  146. * spyable:: Show inspect icon
  147. */
  148. spyable : true,
  149. /** @scratch /panels/histogram/3
  150. * zoomlinks:: Show `Zoom Out' link
  151. */
  152. zoomlinks : false,
  153. /** @scratch /panels/histogram/3
  154. * options:: Show quick view options section
  155. */
  156. options : false,
  157. /** @scratch /panels/histogram/3
  158. * legend:: Display the legond
  159. */
  160. legend : true,
  161. /** @scratch /panels/histogram/3
  162. * interactive:: Enable click-and-drag to zoom functionality
  163. */
  164. interactive : true,
  165. /** @scratch /panels/histogram/3
  166. * legend_counts:: Show counts in legend
  167. */
  168. legend_counts : true,
  169. /** @scratch /panels/histogram/3
  170. * ==== Transformations
  171. * timezone:: Correct for browser timezone?. Valid values: browser, utc
  172. */
  173. timezone : 'browser', // browser or utc
  174. /** @scratch /panels/histogram/3
  175. * percentage:: Show the y-axis as a percentage of the axis total. Only makes sense for multiple
  176. * queries
  177. */
  178. percentage : false,
  179. /** @scratch /panels/histogram/3
  180. * zerofill:: Improves the accuracy of line charts at a small performance cost.
  181. */
  182. zerofill : true,
  183. /** @scratch /panels/histogram/3
  184. * derivative:: Show each point on the x-axis as the change from the previous point
  185. */
  186. tooltip : {
  187. value_type: 'cumulative',
  188. query_as_alias: true
  189. },
  190. targets: []
  191. };
  192. _.defaults($scope.panel,_d);
  193. _.defaults($scope.panel.tooltip,_d.tooltip);
  194. _.defaults($scope.panel.annotate,_d.annotate);
  195. _.defaults($scope.panel.grid,_d.grid);
  196. $scope.init = function() {
  197. // Hide view options by default
  198. $scope.options = false;
  199. $scope.$on('refresh',function(){
  200. $scope.get_data();
  201. });
  202. // Always show the query if an alias isn't set. Users can set an alias if the query is too
  203. // long
  204. $scope.panel.tooltip.query_as_alias = true;
  205. $scope.get_data();
  206. };
  207. $scope.set_interval = function(interval) {
  208. if(interval !== 'auto') {
  209. $scope.panel.auto_int = false;
  210. $scope.panel.interval = interval;
  211. } else {
  212. $scope.panel.auto_int = true;
  213. }
  214. };
  215. $scope.interval_label = function(interval) {
  216. return $scope.panel.auto_int && interval === $scope.panel.interval ? interval+" (auto)" : interval;
  217. };
  218. /**
  219. * The time range effecting the panel
  220. * @return {[type]} [description]
  221. */
  222. $scope.get_time_range = function () {
  223. var range = $scope.range = filterSrv.timeRange('last');
  224. return range;
  225. };
  226. $scope.get_interval = function () {
  227. var interval = $scope.panel.interval;
  228. var range;
  229. if ($scope.panel.auto_int) {
  230. range = $scope.get_time_range();
  231. if (range) {
  232. interval = kbn.secondsToHms(
  233. kbn.calculate_interval(range.from, range.to, $scope.panel.resolution, 0) / 1000
  234. );
  235. }
  236. }
  237. $scope.panel.interval = interval || '10m';
  238. return $scope.panel.interval;
  239. };
  240. $scope.colors = [
  241. "#7EB26D","#EAB839","#6ED0E0","#EF843C","#E24D42","#1F78C1","#BA43A9","#705DA0", //1
  242. "#508642","#CCA300","#447EBC","#C15C17","#890F02","#0A437C","#6D1F62","#584477", //2
  243. "#B7DBAB","#F4D598","#70DBED","#F9BA8F","#F29191","#82B5D8","#E5A8E2","#AEA2E0", //3
  244. "#629E51","#E5AC0E","#64B0C8","#E0752D","#BF1B00","#0A50A1","#962D82","#614D93", //4
  245. "#9AC48A","#F2C96D","#65C5DB","#F9934E","#EA6460","#5195CE","#D683CE","#806EB7", //5
  246. "#3F6833","#967302","#2F575E","#99440A","#58140C","#052B51","#511749","#3F2B5B", //6
  247. "#E0F9D7","#FCEACA","#CFFAFF","#F9E2D2","#FCE2DE","#BADFF4","#F9D9F9","#DEDAF7" //7
  248. ];
  249. /**
  250. * Fetch the data for a chunk of a queries results. Multiple segments occur when several indicies
  251. * need to be consulted (like timestamped logstash indicies)
  252. *
  253. * The results of this function are stored on the scope's data property. This property will be an
  254. * array of objects with the properties info, time_series, and hits. These objects are used in the
  255. * render_panel function to create the historgram.
  256. *
  257. */
  258. $scope.get_data = function() {
  259. delete $scope.panel.error;
  260. $scope.panelMeta.loading = true;
  261. var range = $scope.get_time_range();
  262. var interval = $scope.get_interval(range);
  263. console.log('Interval: ' + interval);
  264. var graphiteLoadOptions = {
  265. range: range,
  266. targets: $scope.panel.targets
  267. };
  268. var result = RQ.sequence([
  269. graphiteSrv.loadGraphiteData(graphiteLoadOptions),
  270. $scope.receiveGraphiteData(range, interval)
  271. ]);
  272. result(function (success, failure) {
  273. if (failure) {
  274. $scope.panel.error = 'Failed to do fetch graphite data: ' + failure;
  275. return;
  276. }
  277. $scope.panelMeta.loading = false;
  278. $scope.$apply();
  279. // Tell the histogram directive to render.
  280. $scope.$emit('render');
  281. });
  282. };
  283. $scope.receiveGraphiteData = function(range, interval) {
  284. return function receive_graphite_data_requestor(requestion, results) {
  285. $scope.data = [];
  286. if(results.length == 0 ) {
  287. requestion('no data in response from graphite');
  288. }
  289. console.log('Data from graphite:', results);
  290. console.log('nr datapoints from graphite: %d', results[0].datapoints.length);
  291. var tsOpts = {
  292. interval: interval,
  293. start_date: range && range.from,
  294. end_date: range && range.to,
  295. fill_style: 'connect'
  296. };
  297. _.each(results, function(targetData) {
  298. var time_series = new timeSeries.ZeroFilled(tsOpts);
  299. _.each(targetData.datapoints, function(valueArray) {
  300. if (valueArray[0]) {
  301. time_series.addValue(valueArray[1] * 1000, valueArray[0]);
  302. }
  303. });
  304. $scope.data.push({
  305. info: {
  306. alias: targetData.target,
  307. color: $scope.colors[$scope.data.length],
  308. enable: true
  309. },
  310. time_series: time_series,
  311. hits: 0
  312. });
  313. });
  314. requestion('ok');
  315. };
  316. };
  317. $scope.add_target = function() {
  318. $scope.panel.targets.push({target: ''});
  319. };
  320. // function $scope.zoom
  321. // factor :: Zoom factor, so 0.5 = cuts timespan in half, 2 doubles timespan
  322. $scope.zoom = function(factor) {
  323. var _range = filterSrv.timeRange('last');
  324. var _timespan = (_range.to.valueOf() - _range.from.valueOf());
  325. var _center = _range.to.valueOf() - _timespan/2;
  326. var _to = (_center + (_timespan*factor)/2);
  327. var _from = (_center - (_timespan*factor)/2);
  328. // If we're not already looking into the future, don't.
  329. if(_to > Date.now() && _range.to < Date.now()) {
  330. var _offset = _to - Date.now();
  331. _from = _from - _offset;
  332. _to = Date.now();
  333. }
  334. if(factor > 1) {
  335. filterSrv.removeByType('time');
  336. }
  337. filterSrv.set({
  338. type:'time',
  339. from:moment.utc(_from).toDate(),
  340. to:moment.utc(_to).toDate(),
  341. field:$scope.panel.time_field
  342. });
  343. };
  344. $scope.openConfigureModal = function($event) {
  345. $event.preventDefault();
  346. $event.stopPropagation();
  347. var closeEditMode = $rootScope.$on('fullEditMode', function(evt, enabled) {
  348. $scope.inEditMode = enabled;
  349. if (!enabled) {
  350. closeEditMode();
  351. }
  352. setTimeout(function() {
  353. $scope.$emit('render');
  354. }, 200);
  355. });
  356. $rootScope.$emit('fullEditMode', true);
  357. };
  358. // I really don't like this function, too much dom manip. Break out into directive?
  359. $scope.populate_modal = function(request) {
  360. $scope.inspector = angular.toJson(request,true);
  361. };
  362. $scope.set_refresh = function (state) {
  363. $scope.refresh = state;
  364. };
  365. $scope.close_edit = function() {
  366. if($scope.refresh) {
  367. $scope.get_data();
  368. }
  369. $scope.refresh = false;
  370. $scope.$emit('render');
  371. };
  372. $scope.render = function() {
  373. $scope.$emit('render');
  374. };
  375. });
  376. module.directive('histogramChart', function(dashboard, filterSrv) {
  377. return {
  378. restrict: 'A',
  379. template: '<div></div>',
  380. link: function(scope, elem) {
  381. // Receive render events
  382. scope.$on('render',function(){
  383. render_panel();
  384. });
  385. // Re-render if the window is resized
  386. angular.element(window).bind('resize', function(){
  387. render_panel();
  388. });
  389. var scale = function(series,factor) {
  390. return _.map(series,function(p) {
  391. return [p[0],p[1]*factor];
  392. });
  393. };
  394. var scaleSeconds = function(series,interval) {
  395. return _.map(series,function(p) {
  396. return [p[0],p[1]/kbn.interval_to_seconds(interval)];
  397. });
  398. };
  399. var derivative = function(series) {
  400. return _.map(series, function(p,i) {
  401. var _v;
  402. if(i === 0 || p[1] === null) {
  403. _v = [p[0],null];
  404. } else {
  405. _v = series[i-1][1] === null ? [p[0],null] : [p[0],p[1]-(series[i-1][1])];
  406. }
  407. return _v;
  408. });
  409. };
  410. // Function for rendering panel
  411. function render_panel() {
  412. // IE doesn't work without this
  413. elem.css({height:scope.panel.height || scope.row.height});
  414. // Populate from the query service
  415. try {
  416. _.each(scope.data, function(series) {
  417. series.label = series.info.alias;
  418. series.color = series.info.color;
  419. });
  420. } catch(e) {return;}
  421. // Set barwidth based on specified interval
  422. var barwidth = kbn.interval_to_ms(scope.panel.interval);
  423. var stack = scope.panel.stack ? true : null;
  424. // Populate element
  425. try {
  426. var options = {
  427. legend: { show: false },
  428. series: {
  429. stackpercent: scope.panel.stack ? scope.panel.percentage : false,
  430. stack: scope.panel.percentage ? null : stack,
  431. lines: {
  432. show: scope.panel.lines,
  433. // Silly, but fixes bug in stacked percentages
  434. fill: scope.panel.fill === 0 ? 0.001 : scope.panel.fill/10,
  435. lineWidth: scope.panel.linewidth,
  436. steps: false
  437. },
  438. bars: {
  439. show: scope.panel.bars,
  440. fill: 1,
  441. barWidth: barwidth/1.5,
  442. zero: false,
  443. lineWidth: 0
  444. },
  445. points: {
  446. show: scope.panel.points,
  447. fill: 1,
  448. fillColor: false,
  449. radius: scope.panel.pointradius
  450. },
  451. shadowSize: 1
  452. },
  453. yaxis: {
  454. show: scope.panel['y-axis'],
  455. min: scope.panel.grid.min,
  456. max: scope.panel.percentage && scope.panel.stack ? 100 : scope.panel.grid.max
  457. },
  458. xaxis: {
  459. timezone: scope.panel.timezone,
  460. show: scope.panel['x-axis'],
  461. mode: "time",
  462. min: _.isUndefined(scope.range.from) ? null : scope.range.from.getTime(),
  463. max: _.isUndefined(scope.range.to) ? null : scope.range.to.getTime(),
  464. timeformat: time_format(scope.panel.interval),
  465. label: "Datetime",
  466. ticks: elem.width()/100
  467. },
  468. grid: {
  469. backgroundColor: null,
  470. borderWidth: 0,
  471. hoverable: true,
  472. color: '#c8c8c8'
  473. }
  474. };
  475. if(scope.panel.y_format === 'bytes') {
  476. options.yaxis.mode = "byte";
  477. }
  478. if(scope.panel.y_format === 'short') {
  479. options.yaxis.tickFormatter = function(val) {
  480. return kbn.shortFormat(val,0);
  481. };
  482. }
  483. if(scope.panel.annotate.enable) {
  484. options.events = {
  485. levels: 1,
  486. data: scope.annotations,
  487. types: {
  488. 'annotation': {
  489. level: 1,
  490. icon: {
  491. icon: "icon-tag icon-flip-vertical",
  492. size: 20,
  493. color: "#222",
  494. outline: "#bbb"
  495. }
  496. }
  497. }
  498. //xaxis: int // the x axis to attach events to
  499. };
  500. }
  501. if(scope.panel.interactive) {
  502. options.selection = { mode: "x", color: '#666' };
  503. }
  504. // when rendering stacked bars, we need to ensure each point that has data is zero-filled
  505. // so that the stacking happens in the proper order
  506. var required_times = [];
  507. if (scope.data.length > 1) {
  508. required_times = Array.prototype.concat.apply([], _.map(scope.data, function (query) {
  509. return query.time_series.getOrderedTimes();
  510. }));
  511. required_times = _.uniq(required_times.sort(function (a, b) {
  512. // decending numeric sort
  513. return a-b;
  514. }), true);
  515. }
  516. for (var i = 0; i < scope.data.length; i++) {
  517. var _d = scope.data[i].time_series.getFlotPairs(required_times);
  518. scope.data[i].data = _d;
  519. }
  520. var totalDataPoints = _.reduce(scope.data, function(num, series) { return series.data.length + num; }, 0);
  521. console.log('Datapoints[0] count:', scope.data[0].data.length);
  522. console.log('Datapoints.Total count:', totalDataPoints);
  523. scope.plot = $.plot(elem, scope.data, options);
  524. } catch(e) {
  525. console.log(e);
  526. // Nothing to do here
  527. }
  528. }
  529. function time_format(interval) {
  530. var _int = kbn.interval_to_seconds(interval);
  531. if(_int >= 2628000) {
  532. return "%Y-%m";
  533. }
  534. if(_int >= 86400) {
  535. return "%Y-%m-%d";
  536. }
  537. if(_int >= 60) {
  538. return "%H:%M<br>%m-%d";
  539. }
  540. return "%H:%M:%S";
  541. }
  542. var $tooltip = $('<div>');
  543. elem.bind("plothover", function (event, pos, item) {
  544. var group, value, timestamp;
  545. if (item) {
  546. if (item.series.info.alias || scope.panel.tooltip.query_as_alias) {
  547. group = '<small style="font-size:0.9em;">' +
  548. '<i class="icon-circle" style="color:'+item.series.color+';"></i>' + ' ' +
  549. (item.series.info.alias || item.series.info.query)+
  550. '</small><br>';
  551. } else {
  552. group = kbn.query_color_dot(item.series.color, 15) + ' ';
  553. }
  554. value = (scope.panel.stack && scope.panel.tooltip.value_type === 'individual') ?
  555. item.datapoint[1] - item.datapoint[2] :
  556. item.datapoint[1];
  557. if(scope.panel.y_format === 'bytes') {
  558. value = kbn.byteFormat(value,2);
  559. }
  560. if(scope.panel.y_format === 'short') {
  561. value = kbn.shortFormat(value,2);
  562. }
  563. timestamp = scope.panel.timezone === 'browser' ?
  564. moment(item.datapoint[0]).format('YYYY-MM-DD HH:mm:ss') :
  565. moment.utc(item.datapoint[0]).format('YYYY-MM-DD HH:mm:ss');
  566. $tooltip
  567. .html(
  568. group + value + " @ " + timestamp
  569. )
  570. .place_tt(pos.pageX, pos.pageY);
  571. } else {
  572. $tooltip.detach();
  573. }
  574. });
  575. elem.bind("plotselected", function (event, ranges) {
  576. filterSrv.set({
  577. type : 'time',
  578. from : moment.utc(ranges.xaxis.from).toDate(),
  579. to : moment.utc(ranges.xaxis.to).toDate(),
  580. field : scope.panel.time_field
  581. });
  582. });
  583. }
  584. };
  585. });
  586. });