module.js 5.4 KB

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