module.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. */
  13. angular.module('kibana.bettermap', [])
  14. .controller('bettermap', function($scope, query, dashboard, filterSrv) {
  15. // Set and populate defaults
  16. var _d = {
  17. status : "Experimental",
  18. query : "*",
  19. size : 1000,
  20. spyable : true,
  21. tooltip : "_id",
  22. field : null,
  23. group : "default"
  24. }
  25. _.defaults($scope.panel,_d)
  26. $scope.init = function() {
  27. $scope.$on('refresh',function(){
  28. $scope.get_data();
  29. })
  30. $scope.get_data();
  31. }
  32. $scope.get_data = function(segment,query_id) {
  33. $scope.panel.error = false;
  34. // Make sure we have everything for the request to complete
  35. if(dashboard.indices.length == 0) {
  36. return;
  37. }
  38. if(_.isUndefined($scope.panel.field)) {
  39. $scope.panel.error = "Please select a field that contains geo point in [lon,lat] format"
  40. return
  41. }
  42. // Determine the field to sort on
  43. var timeField = _.uniq(_.pluck(filterSrv.getByType('time'),'field'))
  44. if(timeField.length > 1) {
  45. $scope.panel.error = "Time field must be consistent amongst time filters"
  46. } else if(timeField.length == 0) {
  47. timeField = null;
  48. } else {
  49. timeField = timeField[0]
  50. }
  51. var _segment = _.isUndefined(segment) ? 0 : segment
  52. var boolQuery = ejs.BoolQuery();
  53. _.each(query.list,function(q) {
  54. boolQuery = boolQuery.should(ejs.QueryStringQuery((q.query || '*')))
  55. })
  56. var request = $scope.ejs.Request().indices(dashboard.indices[_segment])
  57. .query(ejs.FilteredQuery(
  58. boolQuery,
  59. filterSrv.getBoolFilter(filterSrv.ids).must(ejs.ExistsFilter($scope.panel.field))
  60. ))
  61. .fields([$scope.panel.field,$scope.panel.tooltip])
  62. .size($scope.panel.size)
  63. if(!_.isNull(timeField)) {
  64. request = request.sort(timeField,'desc');
  65. }
  66. $scope.populate_modal(request)
  67. var results = request.doSearch()
  68. // Populate scope when we have results
  69. results.then(function(results) {
  70. $scope.panel.loading = false;
  71. if(_segment === 0) {
  72. $scope.hits = 0;
  73. $scope.data = [];
  74. query_id = $scope.query_id = new Date().getTime()
  75. }
  76. // Check for error and abort if found
  77. if(!(_.isUndefined(results.error))) {
  78. $scope.panel.error = $scope.parse_error(results.error);
  79. return;
  80. }
  81. // Check that we're still on the same query, if not stop
  82. if($scope.query_id === query_id) {
  83. var scripts = $LAB.script("panels/bettermap/lib/leaflet.js").wait()
  84. scripts.wait(function(){
  85. $scope.data = $scope.data.concat(_.map(results.hits.hits, function(hit) {
  86. return {
  87. coordinates : new L.LatLng(hit.fields[$scope.panel.field][1],hit.fields[$scope.panel.field][0]),
  88. tooltip : hit.fields[$scope.panel.tooltip]
  89. }
  90. }));
  91. });
  92. // Keep only what we need for the set
  93. $scope.data = $scope.data.slice(0,$scope.panel.size)
  94. } else {
  95. return;
  96. }
  97. $scope.$emit('draw')
  98. // Get $size results then stop querying
  99. if($scope.data.length < $scope.panel.size && _segment+1 < dashboard.indices.length)
  100. $scope.get_data(_segment+1,$scope.query_id)
  101. });
  102. }
  103. // I really don't like this function, too much dom manip. Break out into directive?
  104. $scope.populate_modal = function(request) {
  105. $scope.modal = {
  106. title: "Inspector",
  107. body : "<h5>Last Elasticsearch Query</h5><pre>"+
  108. 'curl -XGET '+config.elasticsearch+'/'+dashboard.indices+"/_search?pretty -d'\n"+
  109. angular.toJson(JSON.parse(request.toString()),true)+
  110. "'</pre>",
  111. }
  112. }
  113. })
  114. .directive('bettermap', function() {
  115. return {
  116. restrict: 'A',
  117. link: function(scope, elem, attrs) {
  118. elem.html('<center><img src="common/img/load_big.gif"></center>')
  119. // Receive render events
  120. scope.$on('draw',function(){
  121. render_panel();
  122. });
  123. scope.$on('render', function(){
  124. if(!_.isUndefined(map)) {
  125. map.invalidateSize();
  126. var panes = map.getPanes()
  127. }
  128. })
  129. var map, markers, layerGroup, mcg;
  130. function render_panel() {
  131. scope.panel.loading = false;
  132. var scripts = $LAB.script("panels/bettermap/lib/leaflet.js").wait()
  133. .script("panels/bettermap/lib/plugins.js")
  134. //add markers dynamically
  135. scripts.wait(function(){
  136. if(_.isUndefined(map)) {
  137. map = L.map(attrs.id, {
  138. scrollWheelZoom: false,
  139. center: [40, -86],
  140. zoom: 10
  141. });
  142. L.tileLayer('http://{s}.tile.cloudmade.com/57cbb6ca8cac418dbb1a402586df4528/22677/256/{z}/{x}/{y}.png', {
  143. maxZoom: 18,
  144. minZoom: 2
  145. }).addTo(map);
  146. layerGroup = new L.MarkerClusterGroup({maxClusterRadius:30});
  147. } else {
  148. layerGroup.clearLayers();
  149. }
  150. _.each(scope.data, function(p) {
  151. if(!_.isUndefined(p.tooltip) && p.tooltip !== '')
  152. layerGroup.addLayer(L.marker(p.coordinates).bindLabel(p.tooltip))
  153. else
  154. layerGroup.addLayer(L.marker(p.coordinates))
  155. })
  156. layerGroup.addTo(map)
  157. map.fitBounds(_.pluck(scope.data,'coordinates'));
  158. })
  159. }
  160. }
  161. };
  162. });