module.js 6.1 KB

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