module.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*jshint globalstrict:true */
  2. /*global angular:true */
  3. /*global L:false*/
  4. /*
  5. ## Better maps
  6. ### Parameters
  7. * size :: How many results to show, more results = slower
  8. * field :: field containing a 2 element array in the format [lon,lat]
  9. * tooltip :: field to extract the tool tip value from
  10. * spyable :: Show the 'eye' icon that reveals the last ES query
  11. */
  12. 'use strict';
  13. angular.module('kibana.bettermap', [])
  14. .controller('bettermap', function($scope, querySrv, dashboard, filterSrv) {
  15. $scope.panelMeta = {
  16. status : "Experimental",
  17. description : "Displays geo points in clustered groups on a map. The cavaet for this panel is"+
  18. " that, for better or worse, it does NOT use the terms facet and it <b>does</b> query "+
  19. "sequentially. This however means that it transfers more data and is generally heavier to"+
  20. " compute, while showing less actual data. If you have a time filter, it will attempt to"+
  21. " show to most recent points in your search, up to your defined limit"
  22. };
  23. // Set and populate defaults
  24. var _d = {
  25. queries : {
  26. mode : 'all',
  27. ids : []
  28. },
  29. size : 1000,
  30. spyable : true,
  31. tooltip : "_id",
  32. field : null
  33. };
  34. _.defaults($scope.panel,_d);
  35. $scope.init = function() {
  36. $scope.$on('refresh',function(){
  37. $scope.get_data();
  38. });
  39. $scope.get_data();
  40. };
  41. $scope.get_data = function(segment,query_id) {
  42. $scope.panel.error = false;
  43. // Make sure we have everything for the request to complete
  44. if(dashboard.indices.length === 0) {
  45. return;
  46. }
  47. if(_.isUndefined($scope.panel.field)) {
  48. $scope.panel.error = "Please select a field that contains geo point in [lon,lat] format";
  49. return;
  50. }
  51. // Determine the field to sort on
  52. var timeField = _.uniq(_.pluck(filterSrv.getByType('time'),'field'));
  53. if(timeField.length > 1) {
  54. $scope.panel.error = "Time field must be consistent amongst time filters";
  55. } else if(timeField.length === 0) {
  56. timeField = null;
  57. } else {
  58. timeField = timeField[0];
  59. }
  60. var _segment = _.isUndefined(segment) ? 0 : segment;
  61. $scope.panel.queries.ids = querySrv.idsByMode($scope.panel.queries);
  62. // This could probably be changed to a BoolFilter
  63. var boolQuery = $scope.ejs.BoolQuery();
  64. _.each($scope.panel.queries.ids,function(id) {
  65. boolQuery = boolQuery.should(querySrv.getEjsObj(id));
  66. });
  67. var request = $scope.ejs.Request().indices(dashboard.indices[_segment])
  68. .query($scope.ejs.FilteredQuery(
  69. boolQuery,
  70. filterSrv.getBoolFilter(filterSrv.ids).must($scope.ejs.ExistsFilter($scope.panel.field))
  71. ))
  72. .fields([$scope.panel.field,$scope.panel.tooltip])
  73. .size($scope.panel.size);
  74. if(!_.isNull(timeField)) {
  75. request = request.sort(timeField,'desc');
  76. }
  77. $scope.populate_modal(request);
  78. var results = request.doSearch();
  79. // Populate scope when we have results
  80. results.then(function(results) {
  81. $scope.panelMeta.loading = false;
  82. if(_segment === 0) {
  83. $scope.hits = 0;
  84. $scope.data = [];
  85. query_id = $scope.query_id = new Date().getTime();
  86. }
  87. // Check for error and abort if found
  88. if(!(_.isUndefined(results.error))) {
  89. $scope.panel.error = $scope.parse_error(results.error);
  90. return;
  91. }
  92. // Check that we're still on the same query, if not stop
  93. if($scope.query_id === query_id) {
  94. var scripts = $LAB.script("panels/bettermap/lib/leaflet.js").wait();
  95. scripts.wait(function(){
  96. $scope.data = $scope.data.concat(_.map(results.hits.hits, function(hit) {
  97. return {
  98. coordinates : new L.LatLng(hit.fields[$scope.panel.field][1],hit.fields[$scope.panel.field][0]),
  99. tooltip : hit.fields[$scope.panel.tooltip]
  100. };
  101. }));
  102. });
  103. // Keep only what we need for the set
  104. $scope.data = $scope.data.slice(0,$scope.panel.size);
  105. } else {
  106. return;
  107. }
  108. $scope.$emit('draw');
  109. // Get $size results then stop querying
  110. if($scope.data.length < $scope.panel.size && _segment+1 < dashboard.indices.length) {
  111. $scope.get_data(_segment+1,$scope.query_id);
  112. }
  113. });
  114. };
  115. $scope.populate_modal = function(request) {
  116. $scope.inspector = angular.toJson(JSON.parse(request.toString()),true);
  117. };
  118. })
  119. .directive('bettermap', function() {
  120. return {
  121. restrict: 'A',
  122. link: function(scope, elem, attrs) {
  123. elem.html('<center><img src="common/img/load_big.gif"></center>');
  124. // Receive render events
  125. scope.$on('draw',function(){
  126. render_panel();
  127. });
  128. scope.$on('render', function(){
  129. if(!_.isUndefined(map)) {
  130. map.invalidateSize();
  131. var panes = map.getPanes();
  132. }
  133. });
  134. var map, markers, layerGroup, mcg;
  135. function render_panel() {
  136. scope.panelMeta.loading = false;
  137. var scripts = $LAB.script("panels/bettermap/lib/leaflet.js").wait()
  138. .script("panels/bettermap/lib/plugins.js");
  139. //add markers dynamically
  140. scripts.wait(function(){
  141. if(_.isUndefined(map)) {
  142. map = L.map(attrs.id, {
  143. scrollWheelZoom: false,
  144. center: [40, -86],
  145. zoom: 10
  146. });
  147. L.tileLayer('http://{s}.tile.cloudmade.com/57cbb6ca8cac418dbb1a402586df4528/22677/256/{z}/{x}/{y}.png', {
  148. maxZoom: 18,
  149. minZoom: 2
  150. }).addTo(map);
  151. layerGroup = new L.MarkerClusterGroup({maxClusterRadius:30});
  152. } else {
  153. layerGroup.clearLayers();
  154. }
  155. _.each(scope.data, function(p) {
  156. if(!_.isUndefined(p.tooltip) && p.tooltip !== '') {
  157. layerGroup.addLayer(L.marker(p.coordinates).bindLabel(p.tooltip));
  158. } else {
  159. layerGroup.addLayer(L.marker(p.coordinates));
  160. }
  161. });
  162. layerGroup.addTo(map);
  163. map.fitBounds(_.pluck(scope.data,'coordinates'));
  164. });
  165. }
  166. }
  167. };
  168. });