module.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. ## Map
  3. LOL. Should this even be documented? Zach's map panel is going to ruin this one.
  4. For serious. This shades a map of the world, the US or Europe with the number of
  5. events that match the query. Uses 2 letter country codes and nothing else. This uses
  6. a terms facet. Its probably safe as long as you point it at the right field. Nach.
  7. There's no way to query sequentially here, so I'm going to hit them all at once!
  8. ### Parameters
  9. * query :: A single query string, not and array. This panel can only handle one
  10. query at a time.
  11. * map :: 'world', 'us' or 'europe'
  12. * colors :: an array of colors to use for the regions of the map. If this is a 2
  13. element array, jquerymap will generate shades between these colors
  14. * size :: How big to make the facet. Higher = more countries
  15. * exclude :: Exlude the array of counties
  16. * spyable :: Show the 'eye' icon that reveals the last ES query
  17. * index_limit :: This does nothing yet. Eventually will limit the query to the first
  18. N indices
  19. */
  20. angular.module('kibana.map', [])
  21. .controller('map', function($scope, $rootScope, query, dashboard, filterSrv) {
  22. // Set and populate defaults
  23. var _d = {
  24. status : "Beta",
  25. query : "*",
  26. map : "world",
  27. colors : ['#A0E2E2', '#265656'],
  28. size : 100,
  29. exclude : [],
  30. spyable : true,
  31. group : "default",
  32. index_limit : 0
  33. }
  34. _.defaults($scope.panel,_d)
  35. $scope.init = function() {
  36. $scope.$on('refresh',function(){$scope.get_data()})
  37. $scope.get_data();
  38. }
  39. $scope.get_data = function() {
  40. // Make sure we have everything for the request to complete
  41. if(dashboard.indices.length == 0) {
  42. return
  43. }
  44. $scope.panel.loading = true;
  45. var request = $scope.ejs.Request().indices(dashboard.indices);
  46. var boolQuery = ejs.BoolQuery();
  47. _.each(query.list,function(q) {
  48. boolQuery = boolQuery.should(ejs.QueryStringQuery(q.query || '*'))
  49. })
  50. // Then the insert into facet and make the request
  51. var request = request
  52. .facet(ejs.TermsFacet('map')
  53. .field($scope.panel.field)
  54. .size($scope.panel['size'])
  55. .exclude($scope.panel.exclude)
  56. .facetFilter(ejs.QueryFilter(
  57. ejs.FilteredQuery(
  58. boolQuery,
  59. filterSrv.getBoolFilter(filterSrv.ids)
  60. )))).size(0);
  61. $scope.populate_modal(request);
  62. var results = request.doSearch();
  63. // Populate scope when we have results
  64. results.then(function(results) {
  65. $scope.panel.loading = false;
  66. $scope.hits = results.hits.total;
  67. $scope.data = {};
  68. _.each(results.facets.map.terms, function(v) {
  69. $scope.data[v.term.toUpperCase()] = v.count;
  70. });
  71. $scope.$emit('render')
  72. });
  73. }
  74. // I really don't like this function, too much dom manip. Break out into directive?
  75. $scope.populate_modal = function(request) {
  76. $scope.modal = {
  77. title: "Inspector",
  78. body : "<h5>Last Elasticsearch Query</h5><pre>"+
  79. 'curl -XGET '+config.elasticsearch+'/'+dashboard.indices+"/_search?pretty -d'\n"+
  80. angular.toJson(JSON.parse(request.toString()),true)+
  81. "'</pre>",
  82. }
  83. }
  84. $scope.build_search = function(field,value) {
  85. filterSrv.set({type:'querystring',mandate:'must',query:field+":"+value})
  86. dashboard.refresh();
  87. }
  88. })
  89. .directive('map', function() {
  90. return {
  91. restrict: 'A',
  92. link: function(scope, elem, attrs) {
  93. elem.html('<center><img src="common/img/load_big.gif"></center>')
  94. // Receive render events
  95. scope.$on('render',function(){
  96. render_panel();
  97. });
  98. // Or if the window is resized
  99. angular.element(window).bind('resize', function(){
  100. render_panel();
  101. });
  102. function render_panel() {
  103. // Using LABjs, wait until all scripts are loaded before rendering panel
  104. var scripts = $LAB.script("panels/map/lib/jquery.jvectormap.min.js").wait()
  105. .script("panels/map/lib/map."+scope.panel.map+".js")
  106. // Populate element. Note that jvectormap appends, does not replace.
  107. scripts.wait(function(){
  108. elem.text('');
  109. $('.jvectormap-zoomin,.jvectormap-zoomout,.jvectormap-label').remove();
  110. var map = elem.vectorMap({
  111. map: scope.panel.map,
  112. regionStyle: {initial: {fill: '#8c8c8c'}},
  113. zoomOnScroll: false,
  114. backgroundColor: null,
  115. series: {
  116. regions: [{
  117. values: scope.data,
  118. scale: scope.panel.colors,
  119. normalizeFunction: 'polynomial'
  120. }]
  121. },
  122. onRegionLabelShow: function(event, label, code){
  123. elem.children('.map-legend').show()
  124. var count = _.isUndefined(scope.data[code]) ? 0 : scope.data[code];
  125. elem.children('.map-legend').text(label.text() + ": " + count);
  126. },
  127. onRegionOut: function(event, code) {
  128. $('.map-legend').hide();
  129. },
  130. onRegionClick: function(event, code) {
  131. var count = _.isUndefined(scope.data[code]) ? 0 : scope.data[code];
  132. if (count != 0)
  133. scope.build_search(scope.panel.field,code)
  134. }
  135. });
  136. elem.prepend('<span class="map-legend"></span>');
  137. $('.map-legend').hide();
  138. })
  139. }
  140. }
  141. };
  142. });