module.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. ## Better maps
  3. So the cavaet for this panel is that, for better or worse, it does NOT use the terms facet and it
  4. DOES query sequentially. This however means that
  5. ### Parameters
  6. * query :: A single query string, not and array. This panel can only handle one
  7. query at a time.
  8. * size :: How many results to show, more results = slower
  9. * field :: field containing a 2 element array in the format [lon,lat]
  10. * tooltip :: field to extract the tool tip value from
  11. * spyable :: Show the 'eye' icon that reveals the last ES query
  12. ### Group Events
  13. #### Sends
  14. * get_time :: On panel initialization get time range to query
  15. #### Receives
  16. * time :: An object containing the time range to use and the index(es) to query
  17. * query :: An Array of queries, this panel uses only the first one
  18. */
  19. angular.module('kibana.bettermap', [])
  20. .controller('bettermap', function($scope, eventBus, query) {
  21. // Set and populate defaults
  22. var _d = {
  23. status : "Experimental",
  24. query : "*",
  25. size : 1000,
  26. spyable : true,
  27. tooltip : "_id",
  28. field : null,
  29. group : "default"
  30. }
  31. _.defaults($scope.panel,_d)
  32. $scope.init = function() {
  33. $scope.$on('refresh',function(){
  34. $scope.get_data();
  35. })
  36. eventBus.register($scope,'time', function(event,time){set_time(time)});
  37. // Now that we're all setup, request the time from our group
  38. eventBus.broadcast($scope.$id,$scope.panel.group,'get_time')
  39. }
  40. $scope.get_data = function(segment,query_id) {
  41. $scope.panel.error = false;
  42. // Make sure we have everything for the request to complete
  43. if(_.isUndefined($scope.index) || _.isUndefined($scope.time))
  44. return
  45. if(_.isUndefined($scope.panel.field)) {
  46. $scope.panel.error = "Please select a field that contains geo point in [lon,lat] format"
  47. return
  48. }
  49. //$scope.panel.loading = true;
  50. var _segment = _.isUndefined(segment) ? 0 : segment
  51. $scope.segment = _segment;
  52. var boolQuery = ejs.BoolQuery();
  53. _.each(query.list,function(q) {
  54. boolQuery = boolQuery.should(ejs.QueryStringQuery((q.query || '*') + " AND _exists_:"+$scope.panel.field))
  55. })
  56. var request = $scope.ejs.Request().indices($scope.index[_segment])
  57. .query(ejs.FilteredQuery(
  58. boolQuery,
  59. ejs.RangeFilter($scope.time.field)
  60. .from($scope.time.from)
  61. .to($scope.time.to)
  62. )
  63. )
  64. .fields([$scope.panel.field,$scope.panel.tooltip])
  65. .size($scope.panel.size)
  66. .sort($scope.time.field,'desc');
  67. $scope.populate_modal(request)
  68. var results = request.doSearch()
  69. // Populate scope when we have results
  70. results.then(function(results) {
  71. $scope.panel.loading = false;
  72. if(_segment === 0) {
  73. $scope.hits = 0;
  74. $scope.data = [];
  75. query_id = $scope.query_id = new Date().getTime()
  76. }
  77. // Check for error and abort if found
  78. if(!(_.isUndefined(results.error))) {
  79. $scope.panel.error = $scope.parse_error(results.error);
  80. return;
  81. }
  82. // Check that we're still on the same query, if not stop
  83. if($scope.query_id === query_id) {
  84. var scripts = $LAB.script("panels/bettermap/lib/leaflet.js").wait()
  85. scripts.wait(function(){
  86. $scope.data = $scope.data.concat(_.map(results.hits.hits, function(hit) {
  87. return {
  88. coordinates : new L.LatLng(hit.fields[$scope.panel.field][1],hit.fields[$scope.panel.field][0]),
  89. tooltip : hit.fields[$scope.panel.tooltip]
  90. }
  91. }));
  92. });
  93. // Keep only what we need for the set
  94. $scope.data = $scope.data.slice(0,$scope.panel.size)
  95. } else {
  96. return;
  97. }
  98. $scope.$emit('draw')
  99. // Get $size results then stop querying
  100. if($scope.data.length < $scope.panel.size && _segment+1 < $scope.index.length)
  101. $scope.get_data(_segment+1,$scope.query_id)
  102. });
  103. }
  104. // I really don't like this function, too much dom manip. Break out into directive?
  105. $scope.populate_modal = function(request) {
  106. $scope.modal = {
  107. title: "Inspector",
  108. body : "<h5>Last Elasticsearch Query</h5><pre>"+
  109. 'curl -XGET '+config.elasticsearch+'/'+$scope.index+"/_search?pretty -d'\n"+
  110. angular.toJson(JSON.parse(request.toString()),true)+
  111. "'</pre>",
  112. }
  113. }
  114. function set_time(time) {
  115. $scope.time = time;
  116. $scope.index = _.isUndefined(time.index) ? $scope.index : time.index
  117. $scope.get_data();
  118. }
  119. })
  120. .directive('bettermap', function() {
  121. return {
  122. restrict: 'A',
  123. link: function(scope, elem, attrs) {
  124. elem.html('<center><img src="common/img/load_big.gif"></center>')
  125. // Receive render events
  126. scope.$on('draw',function(){
  127. render_panel();
  128. });
  129. scope.$on('render', function(){
  130. if(!_.isUndefined(map)) {
  131. map.invalidateSize();
  132. var panes = map.getPanes()
  133. }
  134. })
  135. var map, markers, layerGroup, mcg;
  136. function render_panel() {
  137. scope.panel.loading = false;
  138. var scripts = $LAB.script("panels/bettermap/lib/leaflet.js").wait()
  139. .script("panels/bettermap/lib/plugins.js")
  140. //add markers dynamically
  141. scripts.wait(function(){
  142. if(_.isUndefined(map)) {
  143. map = L.map(attrs.id, {
  144. scrollWheelZoom: false,
  145. center: [40, -86],
  146. zoom: 10
  147. });
  148. L.tileLayer('http://{s}.tile.cloudmade.com/57cbb6ca8cac418dbb1a402586df4528/22677/256/{z}/{x}/{y}.png', {
  149. maxZoom: 18,
  150. minZoom: 2
  151. }).addTo(map);
  152. layerGroup = new L.MarkerClusterGroup({maxClusterRadius:30});
  153. } else {
  154. layerGroup.clearLayers();
  155. }
  156. _.each(scope.data, function(p) {
  157. if(!_.isUndefined(p.tooltip) && p.tooltip !== '')
  158. layerGroup.addLayer(L.marker(p.coordinates).bindLabel(p.tooltip))
  159. else
  160. layerGroup.addLayer(L.marker(p.coordinates))
  161. })
  162. layerGroup.addTo(map)
  163. map.fitBounds(_.pluck(scope.data,'coordinates'));
  164. })
  165. }
  166. }
  167. };
  168. });