module.js 5.5 KB

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