module.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /** @scratch /panels/5
  2. * include::panels/bettermap.asciidoc[]
  3. */
  4. /** @scratch /panels/bettermap/0
  5. * == Bettermap
  6. * Status: *Experimental*
  7. *
  8. * Bettermap is called bettermap for lack of a better name. Bettermap uses geographic coordinates to
  9. * create clusters of markers on map and shade them orange, yellow and green depending on the
  10. * density of the cluster.
  11. *
  12. * To drill down, click on a cluster. The map will be zoomed and the cluster broken into smaller cluster.
  13. * When it no longer makes visual sense to cluster, individual markers will be displayed. Hover over
  14. * a marker to see the tooltip value/
  15. *
  16. * IMPORTANT: bettermap requires an internet connection to download its map panels.
  17. */
  18. define([
  19. 'angular',
  20. 'app',
  21. 'underscore',
  22. './leaflet/leaflet-src',
  23. 'require',
  24. 'css!./module.css',
  25. 'css!./leaflet/leaflet.css',
  26. 'css!./leaflet/plugins.css'
  27. ],
  28. function (angular, app, _, L, localRequire) {
  29. 'use strict';
  30. var module = angular.module('kibana.panels.bettermap', []);
  31. app.useModule(module);
  32. module.controller('bettermap', function($scope, querySrv, dashboard, filterSrv) {
  33. $scope.panelMeta = {
  34. editorTabs : [
  35. {
  36. title: 'Queries',
  37. src: 'app/partials/querySelect.html'
  38. }
  39. ],
  40. modals : [
  41. {
  42. description: "Inspect",
  43. icon: "icon-info-sign",
  44. partial: "app/partials/inspector.html",
  45. show: $scope.panel.spyable
  46. }
  47. ],
  48. status : "Experimental",
  49. description : "Displays geo points in clustered groups on a map. The cavaet for this panel is"+
  50. " that, for better or worse, it does NOT use the terms facet and it <b>does</b> query "+
  51. "sequentially. This however means that it transfers more data and is generally heavier to"+
  52. " compute, while showing less actual data. If you have a time filter, it will attempt to"+
  53. " show to most recent points in your search, up to your defined limit"
  54. };
  55. // Set and populate defaults
  56. var _d = {
  57. /** @scratch /panels/bettermap/3
  58. * === Parameters
  59. *
  60. * field:: The field that contains the coordinates, in geojson format. GeoJSON is
  61. * +[longitude,latitude]+ in an array. This is different from most implementations, which use
  62. * latitude, longitude.
  63. */
  64. field : null,
  65. /** @scratch /panels/bettermap/5
  66. * size:: The number of documents to use when drawing the map
  67. */
  68. size : 1000,
  69. /** @scratch /panels/bettermap/5
  70. * spyable:: Should the `inspect` icon be shown?
  71. */
  72. spyable : true,
  73. /** @scratch /panels/bettermap/5
  74. * tooltip:: Which field to use for the tooltip when hovering over a marker
  75. */
  76. tooltip : "_id",
  77. /** @scratch /panels/bettermap/5
  78. * ==== Queries
  79. * queries object:: This object describes the queries to use on this panel.
  80. * queries.mode::: Of the queries available, which to use. Options: +all, pinned, unpinned, selected+
  81. * queries.ids::: In +selected+ mode, which query ids are selected.
  82. */
  83. queries : {
  84. mode : 'all',
  85. ids : []
  86. },
  87. };
  88. _.defaults($scope.panel,_d);
  89. // inorder to use relative paths in require calls, require needs a context to run. Without
  90. // setting this property the paths would be relative to the app not this context/file.
  91. $scope.requireContext = localRequire;
  92. $scope.init = function() {
  93. $scope.$on('refresh',function(){
  94. $scope.get_data();
  95. });
  96. $scope.get_data();
  97. };
  98. $scope.get_data = function(segment,query_id) {
  99. $scope.require(['./leaflet/plugins'], function () {
  100. $scope.panel.error = false;
  101. // Make sure we have everything for the request to complete
  102. if(dashboard.indices.length === 0) {
  103. return;
  104. }
  105. if(_.isUndefined($scope.panel.field)) {
  106. $scope.panel.error = "Please select a field that contains geo point in [lon,lat] format";
  107. return;
  108. }
  109. // Determine the field to sort on
  110. var timeField = _.uniq(_.pluck(filterSrv.getByType('time'),'field'));
  111. if(timeField.length > 1) {
  112. $scope.panel.error = "Time field must be consistent amongst time filters";
  113. } else if(timeField.length === 0) {
  114. timeField = null;
  115. } else {
  116. timeField = timeField[0];
  117. }
  118. var _segment = _.isUndefined(segment) ? 0 : segment;
  119. $scope.panel.queries.ids = querySrv.idsByMode($scope.panel.queries);
  120. var queries = querySrv.getQueryObjs($scope.panel.queries.ids);
  121. var boolQuery = $scope.ejs.BoolQuery();
  122. _.each(queries,function(q) {
  123. boolQuery = boolQuery.should(querySrv.toEjsObj(q));
  124. });
  125. var request = $scope.ejs.Request().indices(dashboard.indices[_segment])
  126. .query($scope.ejs.FilteredQuery(
  127. boolQuery,
  128. filterSrv.getBoolFilter(filterSrv.ids).must($scope.ejs.ExistsFilter($scope.panel.field))
  129. ))
  130. .fields([$scope.panel.field,$scope.panel.tooltip])
  131. .size($scope.panel.size);
  132. if(!_.isNull(timeField)) {
  133. request = request.sort(timeField,'desc');
  134. }
  135. $scope.populate_modal(request);
  136. var results = request.doSearch();
  137. // Populate scope when we have results
  138. results.then(function(results) {
  139. $scope.panelMeta.loading = false;
  140. if(_segment === 0) {
  141. $scope.hits = 0;
  142. $scope.data = [];
  143. query_id = $scope.query_id = new Date().getTime();
  144. }
  145. // Check for error and abort if found
  146. if(!(_.isUndefined(results.error))) {
  147. $scope.panel.error = $scope.parse_error(results.error);
  148. return;
  149. }
  150. // Check that we're still on the same query, if not stop
  151. if($scope.query_id === query_id) {
  152. // Keep only what we need for the set
  153. $scope.data = $scope.data.slice(0,$scope.panel.size).concat(_.map(results.hits.hits, function(hit) {
  154. return {
  155. coordinates : new L.LatLng(hit.fields[$scope.panel.field][1],hit.fields[$scope.panel.field][0]),
  156. tooltip : hit.fields[$scope.panel.tooltip]
  157. };
  158. }));
  159. } else {
  160. return;
  161. }
  162. $scope.$emit('draw');
  163. // Get $size results then stop querying
  164. if($scope.data.length < $scope.panel.size && _segment+1 < dashboard.indices.length) {
  165. $scope.get_data(_segment+1,$scope.query_id);
  166. }
  167. });
  168. });
  169. };
  170. $scope.populate_modal = function(request) {
  171. $scope.inspector = angular.toJson(JSON.parse(request.toString()),true);
  172. };
  173. });
  174. module.directive('bettermap', function() {
  175. return {
  176. restrict: 'A',
  177. link: function(scope, elem, attrs) {
  178. elem.html('<center><img src="img/load_big.gif"></center>');
  179. // Receive render events
  180. scope.$on('draw',function(){
  181. render_panel();
  182. });
  183. scope.$on('render', function(){
  184. if(!_.isUndefined(map)) {
  185. map.invalidateSize();
  186. map.getPanes();
  187. }
  188. });
  189. var map, layerGroup;
  190. function render_panel() {
  191. scope.require(['./leaflet/plugins'], function () {
  192. scope.panelMeta.loading = false;
  193. L.Icon.Default.imagePath = 'app/panels/bettermap/leaflet/images';
  194. if(_.isUndefined(map)) {
  195. map = L.map(attrs.id, {
  196. scrollWheelZoom: false,
  197. center: [40, -86],
  198. zoom: 10
  199. });
  200. // This could be made configurable?
  201. L.tileLayer('http://{s}.tile.cloudmade.com/57cbb6ca8cac418dbb1a402586df4528/22677/256/{z}/{x}/{y}.png', {
  202. maxZoom: 18,
  203. minZoom: 2
  204. }).addTo(map);
  205. layerGroup = new L.MarkerClusterGroup({maxClusterRadius:30});
  206. } else {
  207. layerGroup.clearLayers();
  208. }
  209. _.each(scope.data, function(p) {
  210. if(!_.isUndefined(p.tooltip) && p.tooltip !== '') {
  211. layerGroup.addLayer(L.marker(p.coordinates).bindLabel(p.tooltip));
  212. } else {
  213. layerGroup.addLayer(L.marker(p.coordinates));
  214. }
  215. });
  216. layerGroup.addTo(map);
  217. map.fitBounds(_.pluck(scope.data,'coordinates'));
  218. });
  219. }
  220. }
  221. };
  222. });
  223. });