module.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. angular.module('kibana.histogram', [])
  2. .controller('histogram', function($scope, eventBus) {
  3. // Set and populate defaults
  4. var _d = {
  5. query : [ {query: "*", label:"Query"} ],
  6. interval : secondsToHms(calculate_interval($scope.from,$scope.to,40,0)/1000),
  7. show : ['bars','y-axis','x-axis','legend'],
  8. fill : 3,
  9. linewidth : 3,
  10. timezone : 'browser', // browser, utc or a standard timezone
  11. spyable : true,
  12. zoomlinks : true,
  13. group : "default",
  14. }
  15. _.defaults($scope.panel,_d)
  16. $scope.init = function() {
  17. eventBus.register($scope,'time', function(event,time){$scope.set_time(time)});
  18. eventBus.register($scope,'query', function(event, query) {
  19. if(_.isArray(query)) {
  20. $scope.panel.query = _.map(query,function(q) {
  21. return {query: q, label: q};
  22. })
  23. } else {
  24. $scope.panel.query[0] = {query: query, label: query}
  25. }
  26. $scope.get_data();
  27. });
  28. // Now that we're all setup, request the time from our group if we don't
  29. // have it yet
  30. if(_.isUndefined($scope.time))
  31. eventBus.broadcast($scope.$id,$scope.panel.group,'get_time')
  32. }
  33. $scope.remove_query = function(q) {
  34. $scope.panel.query = _.without($scope.panel.query,q);
  35. $scope.get_data();
  36. }
  37. $scope.add_query = function(label,query) {
  38. if(!(_.isArray($scope.panel.query)))
  39. $scope.panel.query = new Array();
  40. $scope.panel.query.unshift({
  41. query: query,
  42. label: label,
  43. });
  44. $scope.get_data();
  45. }
  46. $scope.get_data = function(segment,query_id) {
  47. delete $scope.panel.error
  48. // Make sure we have everything for the request to complete
  49. if(_.isUndefined($scope.panel.index) || _.isUndefined($scope.time))
  50. return
  51. var _segment = _.isUndefined(segment) ? 0 : segment
  52. $scope.panel.loading = true;
  53. var request = $scope.ejs.Request().indices($scope.panel.index[_segment]);
  54. // Build the question part of the query
  55. var queries = [];
  56. _.each($scope.panel.query, function(v) {
  57. queries.push($scope.ejs.FilteredQuery(
  58. ejs.QueryStringQuery(v.query || '*'),
  59. ejs.RangeFilter($scope.time.field)
  60. .from($scope.time.from)
  61. .to($scope.time.to))
  62. )
  63. });
  64. // Build the facet part
  65. _.each(queries, function(v) {
  66. request = request
  67. .facet($scope.ejs.DateHistogramFacet("chart"+_.indexOf(queries,v))
  68. .field($scope.time.field)
  69. .interval($scope.panel.interval)
  70. .facetFilter($scope.ejs.QueryFilter(v))
  71. ).size(0)
  72. })
  73. $scope.populate_modal(request);
  74. // Then run it
  75. var results = request.doSearch();
  76. // Populate scope when we have results
  77. results.then(function(results) {
  78. $scope.panel.loading = false;
  79. if(_segment == 0) {
  80. $scope.hits = 0;
  81. $scope.data = [];
  82. query_id = $scope.query_id = new Date().getTime();
  83. }
  84. // Check for error and abort if found
  85. if(!(_.isUndefined(results.error))) {
  86. $scope.panel.error = $scope.parse_error(results.error);
  87. return;
  88. }
  89. if($scope.query_id === query_id) {
  90. var i = 0;
  91. _.each(results.facets, function(v, k) {
  92. // If this isn't a date histogram it must be a QueryFacet, get the
  93. // count and return
  94. if(v._type !== 'date_histogram') {
  95. //$scope.hits += v.count;
  96. return
  97. }
  98. // Null values at each end of the time range ensure we see entire range
  99. if(_.isUndefined($scope.data[i]) || _segment == 0) {
  100. var data = [[$scope.time.from.getTime(), null],[$scope.time.to.getTime(), null]];
  101. var hits = 0;
  102. } else {
  103. var data = $scope.data[i].data
  104. var hits = $scope.data[i].hits
  105. }
  106. // Assemble segments
  107. var segment_data = [];
  108. _.each(v.entries, function(v, k) {
  109. segment_data.push([v['time'],v['count']])
  110. hits += v['count'];
  111. $scope.hits += v['count'];
  112. });
  113. data.splice.apply(data,[1,0].concat(segment_data))
  114. // Create the flot series
  115. var series = {
  116. data: {
  117. label: $scope.panel.query[i].label || "query"+(parseInt(i)+1),
  118. data: data,
  119. hits: hits
  120. },
  121. };
  122. if (!(_.isUndefined($scope.panel.query[i].color)))
  123. series.data.color = $scope.panel.query[i].color;
  124. $scope.data[i] = series.data
  125. i++;
  126. });
  127. $scope.$emit('render')
  128. if(_segment < $scope.panel.index.length-1) {
  129. $scope.get_data(_segment+1,query_id)
  130. }
  131. }
  132. });
  133. }
  134. // function $scope.zoom
  135. // factor :: Zoom factor, so 0.5 = cuts timespan in half, 2 doubles timespan
  136. $scope.zoom = function(factor) {
  137. eventBus.broadcast($scope.$id,$scope.panel.group,'zoom',factor)
  138. }
  139. // I really don't like this function, too much dom manip. Break out into directive?
  140. $scope.populate_modal = function(request) {
  141. $scope.modal = {
  142. title: "Inspector",
  143. body : "<h5>Last Elasticsearch Query</h5><pre>"+
  144. 'curl -XGET '+config.elasticsearch+'/'+$scope.panel.index+"/_search?pretty -d'\n"+
  145. angular.toJson(JSON.parse(request.toString()),true)+
  146. "'</pre>",
  147. }
  148. }
  149. $scope.set_time = function(time) {
  150. $scope.time = time;
  151. $scope.panel.index = _.isUndefined(time.index) ? $scope.panel.index : time.index
  152. $scope.panel.interval = secondsToHms(
  153. calculate_interval(time.from,time.to,50,0)/1000);
  154. $scope.get_data();
  155. }
  156. })
  157. .directive('histogramChart', function(eventBus) {
  158. return {
  159. restrict: 'A',
  160. link: function(scope, elem, attrs, ctrl) {
  161. var height = scope.panel.height || scope.row.height;
  162. // Receive render events
  163. scope.$on('render',function(){
  164. render_panel();
  165. });
  166. // Re-render if the window is resized
  167. angular.element(window).bind('resize', function(){
  168. render_panel();
  169. });
  170. // Function for rendering panel
  171. function render_panel() {
  172. // Determine format
  173. var show = _.isUndefined(scope.panel.show) ? {
  174. bars: true, lines: false, points: false
  175. } : {
  176. lines: _.indexOf(scope.panel.show,'lines') < 0 ? false : true,
  177. bars: _.indexOf(scope.panel.show,'bars') < 0 ? false : true,
  178. points: _.indexOf(scope.panel.show,'points') < 0 ? false : true,
  179. stack: _.indexOf(scope.panel.show,'stack') < 0 ? null : true,
  180. legend: _.indexOf(scope.panel.show,'legend') < 0 ? false : true,
  181. 'x-axis': _.indexOf(scope.panel.show,'x-axis') < 0 ? false : true,
  182. 'y-axis': _.indexOf(scope.panel.show,'y-axis') < 0 ? false : true,
  183. }
  184. // Set barwidth based on specified interval
  185. var barwidth = interval_to_seconds(scope.panel.interval)*1000
  186. var scripts = $LAB.script("common/lib/panels/jquery.flot.js")
  187. .script("common/lib/panels/jquery.flot.time.js")
  188. .script("common/lib/panels/jquery.flot.stack.js")
  189. .script("common/lib/panels/jquery.flot.selection.js")
  190. .script("common/lib/panels/timezone.js")
  191. // Populate element. Note that jvectormap appends, does not replace.
  192. scripts.wait(function(){
  193. // Populate element
  194. try {
  195. var plot = $.plot(elem, scope.data, {
  196. legend: {
  197. show: false,
  198. },
  199. series: {
  200. stack: show.stack,
  201. lines: {
  202. show: show.lines,
  203. fill: scope.panel.fill/10,
  204. lineWidth: scope.panel.linewidth,
  205. steps: true
  206. },
  207. bars: { show: show.bars, fill: 1, barWidth: barwidth/1.8 },
  208. points: { show: show.points, fill: 1, fillColor: false},
  209. shadowSize: 1
  210. },
  211. yaxis: { show: show['y-axis'], min: 0, color: "#000" },
  212. xaxis: {
  213. timezone: scope.panel.timezone,
  214. show: show['x-axis'],
  215. mode: "time",
  216. timeformat: time_format(scope.panel.interval),
  217. label: "Datetime",
  218. color: "#000",
  219. },
  220. selection: {
  221. mode: "x",
  222. color: '#666'
  223. },
  224. grid: {
  225. backgroundColor: '#fff',
  226. borderWidth: 0,
  227. borderColor: '#eee',
  228. color: "#eee",
  229. hoverable: true,
  230. },
  231. colors: ['#EB6841','#00A0B0','#6A4A3C','#EDC951','#CC333F']
  232. })
  233. scope.legend = [];
  234. _.each(plot.getData(),function(series) {
  235. scope.legend.push(_.pick(series,'label','color','hits'))
  236. })
  237. // Work around for missing legend at initialization
  238. if(!scope.$$phase)
  239. scope.$apply()
  240. } catch(e) {
  241. elem.text(e)
  242. }
  243. })
  244. }
  245. function time_format(interval) {
  246. var _int = interval_to_seconds(interval)
  247. if(_int >= 2628000)
  248. return "%m/%y"
  249. if(_int >= 86400)
  250. return "%m/%d/%y"
  251. if(_int >= 60)
  252. return "%H:%M<br>%m/%d"
  253. else
  254. return "%H:%M:%S"
  255. }
  256. function tt(x, y, contents) {
  257. var tooltip = $('#pie-tooltip').length ?
  258. $('#pie-tooltip') : $('<div id="pie-tooltip"></div>');
  259. //var tooltip = $('#pie-tooltip')
  260. tooltip.html(contents).css({
  261. position: 'absolute',
  262. top : y + 5,
  263. left : x + 5,
  264. color : "#000",
  265. border : '3px solid #000',
  266. padding : '10px',
  267. 'font-size': '11pt',
  268. 'font-weight' : 'bold',
  269. 'background-color': '#FFF',
  270. 'border-radius': '10px',
  271. }).appendTo("body");
  272. }
  273. elem.bind("plothover", function (event, pos, item) {
  274. if (item) {
  275. tt(pos.pageX, pos.pageY,
  276. "<div style='vertical-align:middle;display:inline-block;background:"+item.series.color+";height:20px;width:20px'></div> "+
  277. item.datapoint[1].toFixed(0) + " @ " +
  278. new Date(item.datapoint[0]).format('mm/dd HH:MM:ss'));
  279. } else {
  280. $("#pie-tooltip").remove();
  281. }
  282. });
  283. elem.bind("plotselected", function (event, ranges) {
  284. scope.time.from = new Date(ranges.xaxis.from);
  285. scope.time.to = new Date(ranges.xaxis.to)
  286. eventBus.broadcast(scope.$id,scope.panel.group,'set_time',scope.time)
  287. });
  288. }
  289. };
  290. })