module.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*jshint globalstrict:true */
  2. /*global angular:true */
  3. /*global L:false*/
  4. /*
  5. ## Better maps
  6. So the cavaet for this panel is that, for better or worse, it does NOT use the terms facet and it
  7. DOES query sequentially. This however means that it transfer more data and is generally heavier
  8. to computer, while showing less actual data
  9. ### Parameters
  10. * size :: How many results to show, more results = slower
  11. * field :: field containing a 2 element array in the format [lon,lat]
  12. * tooltip :: field to extract the tool tip value from
  13. * spyable :: Show the 'eye' icon that reveals the last ES query
  14. */
  15. 'use strict';
  16. angular.module('kibana.bettermap', [])
  17. .controller('bettermap', function($scope, querySrv, dashboard, filterSrv) {
  18. // Set and populate defaults
  19. var _d = {
  20. status : "Experimental",
  21. queries : {
  22. mode : 'all',
  23. ids : []
  24. },
  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. $scope.get_data();
  37. };
  38. $scope.get_data = function(segment,query_id) {
  39. $scope.panel.error = false;
  40. // Make sure we have everything for the request to complete
  41. if(dashboard.indices.length === 0) {
  42. return;
  43. }
  44. if(_.isUndefined($scope.panel.field)) {
  45. $scope.panel.error = "Please select a field that contains geo point in [lon,lat] format";
  46. return;
  47. }
  48. // Determine the field to sort on
  49. var timeField = _.uniq(_.pluck(filterSrv.getByType('time'),'field'));
  50. if(timeField.length > 1) {
  51. $scope.panel.error = "Time field must be consistent amongst time filters";
  52. } else if(timeField.length === 0) {
  53. timeField = null;
  54. } else {
  55. timeField = timeField[0];
  56. }
  57. var _segment = _.isUndefined(segment) ? 0 : segment;
  58. $scope.panel.queries.ids = querySrv.idsByMode($scope.panel.queries);
  59. // This could probably be changed to a BoolFilter
  60. var boolQuery = $scope.ejs.BoolQuery();
  61. _.each($scope.panel.queries.ids,function(id) {
  62. boolQuery = boolQuery.should(querySrv.getEjsObj(id));
  63. });
  64. var request = $scope.ejs.Request().indices(dashboard.indices[_segment])
  65. .query($scope.ejs.FilteredQuery(
  66. boolQuery,
  67. filterSrv.getBoolFilter(filterSrv.ids).must($scope.ejs.ExistsFilter($scope.panel.field))
  68. ))
  69. .fields([$scope.panel.field,$scope.panel.tooltip])
  70. .size($scope.panel.size);
  71. if(!_.isNull(timeField)) {
  72. request = request.sort(timeField,'desc');
  73. }
  74. $scope.populate_modal(request);
  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. // Check that we're still on the same query, if not stop
  90. if($scope.query_id === query_id) {
  91. var scripts = $LAB.script("panels/bettermap/lib/leaflet.js").wait();
  92. scripts.wait(function(){
  93. $scope.data = $scope.data.concat(_.map(results.hits.hits, function(hit) {
  94. return {
  95. coordinates : new L.LatLng(hit.fields[$scope.panel.field][1],hit.fields[$scope.panel.field][0]),
  96. tooltip : hit.fields[$scope.panel.tooltip]
  97. };
  98. }));
  99. });
  100. // Keep only what we need for the set
  101. $scope.data = $scope.data.slice(0,$scope.panel.size);
  102. } else {
  103. return;
  104. }
  105. $scope.$emit('draw');
  106. // Get $size results then stop querying
  107. if($scope.data.length < $scope.panel.size && _segment+1 < dashboard.indices.length) {
  108. $scope.get_data(_segment+1,$scope.query_id);
  109. }
  110. });
  111. };
  112. // I really don't like this function, too much dom manip. Break out into directive?
  113. $scope.populate_modal = function(request) {
  114. $scope.modal = {
  115. title: "Inspector",
  116. body : "<h5>Last Elasticsearch Query</h5><pre>"+
  117. 'curl -XGET '+config.elasticsearch+'/'+dashboard.indices+"/_search?pretty -d'\n"+
  118. angular.toJson(JSON.parse(request.toString()),true)+
  119. "'</pre>",
  120. };
  121. };
  122. })
  123. .directive('bettermap', function() {
  124. return {
  125. restrict: 'A',
  126. link: function(scope, elem, attrs) {
  127. elem.html('<center><img src="common/img/load_big.gif"></center>');
  128. // Receive render events
  129. scope.$on('draw',function(){
  130. render_panel();
  131. });
  132. scope.$on('render', function(){
  133. if(!_.isUndefined(map)) {
  134. map.invalidateSize();
  135. var panes = map.getPanes();
  136. }
  137. });
  138. var map, markers, layerGroup, mcg;
  139. function render_panel() {
  140. scope.panel.loading = false;
  141. var scripts = $LAB.script("panels/bettermap/lib/leaflet.js").wait()
  142. .script("panels/bettermap/lib/plugins.js");
  143. //add markers dynamically
  144. scripts.wait(function(){
  145. if(_.isUndefined(map)) {
  146. map = L.map(attrs.id, {
  147. scrollWheelZoom: false,
  148. center: [40, -86],
  149. zoom: 10
  150. });
  151. L.tileLayer('http://{s}.tile.cloudmade.com/57cbb6ca8cac418dbb1a402586df4528/22677/256/{z}/{x}/{y}.png', {
  152. maxZoom: 18,
  153. minZoom: 2
  154. }).addTo(map);
  155. layerGroup = new L.MarkerClusterGroup({maxClusterRadius:30});
  156. } else {
  157. layerGroup.clearLayers();
  158. }
  159. _.each(scope.data, function(p) {
  160. if(!_.isUndefined(p.tooltip) && p.tooltip !== '') {
  161. layerGroup.addLayer(L.marker(p.coordinates).bindLabel(p.tooltip));
  162. } else {
  163. layerGroup.addLayer(L.marker(p.coordinates));
  164. }
  165. });
  166. layerGroup.addTo(map);
  167. map.fitBounds(_.pluck(scope.data,'coordinates'));
  168. });
  169. }
  170. }
  171. };
  172. });