module.js 5.5 KB

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