module.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. angular.module('kibana.map2', [])
  2. .controller('map2', function($scope, eventBus) {
  3. // Set and populate defaults
  4. var _d = {
  5. query : "*",
  6. map : "world",
  7. colors : ['#C8EEFF', '#0071A4'],
  8. size : 1000,
  9. exclude : [],
  10. spyable : true,
  11. group : "default",
  12. index_limit : 0
  13. }
  14. _.defaults($scope.panel,_d)
  15. console.log("$scope.panel", $scope.panel);
  16. console.log("_d", _d);
  17. $scope.init = function() {
  18. console.log("init");
  19. eventBus.register($scope,'time', function(event,time){set_time(time)});
  20. eventBus.register($scope,'query', function(event, query) {
  21. $scope.panel.query = _.isArray(query) ? query[0] : query;
  22. $scope.get_data();
  23. });
  24. // Now that we're all setup, request the time from our group
  25. eventBus.broadcast($scope.$id,$scope.panel.group,'get_time')
  26. }
  27. $scope.isNumber = function(n) {
  28. return !isNaN(parseFloat(n)) && isFinite(n);
  29. }
  30. $scope.get_data = function() {
  31. console.log("get_data");
  32. // Make sure we have everything for the request to complete
  33. if(_.isUndefined($scope.panel.index) || _.isUndefined($scope.time))
  34. return
  35. $scope.panel.loading = true;
  36. var request = $scope.ejs.Request().indices($scope.panel.index);
  37. var facet = $scope.ejs.TermsFacet('map')
  38. .field($scope.panel.field)
  39. .size(1000)
  40. .exclude($scope.panel.exclude)
  41. .facetFilter(ejs.QueryFilter(
  42. ejs.FilteredQuery(
  43. ejs.QueryStringQuery($scope.panel.query || '*'),
  44. ejs.RangeFilter($scope.time.field)
  45. .from($scope.time.from)
  46. .to($scope.time.to)
  47. )));
  48. // Then the insert into facet and make the request
  49. var request = request.facet(facet).size(0);
  50. $scope.populate_modal(request);
  51. var results = request.doSearch();
  52. // Populate scope when we have results
  53. results.then(function(results) {
  54. $scope.panel.loading = false;
  55. $scope.hits = results.hits.total;
  56. $scope.data = {};
  57. console.log("results",results);
  58. _.each(results.facets.map.terms, function(v) {
  59. //FIX THIS
  60. if (!$scope.isNumber(v.term)) {
  61. $scope.data[v.term.toUpperCase()] = v.count;
  62. } else {
  63. $scope.data[v.term] = v.count;
  64. }
  65. });
  66. console.log("emit render");
  67. $scope.$emit('render')
  68. });
  69. }
  70. // I really don't like this function, too much dom manip. Break out into directive?
  71. $scope.populate_modal = function(request) {
  72. $scope.modal = {
  73. title: "Inspector",
  74. body : "<h5>Last Elasticsearch Query</h5><pre>"+
  75. 'curl -XGET '+config.elasticsearch+'/'+$scope.panel.index+"/_search?pretty -d'\n"+
  76. angular.toJson(JSON.parse(request.toString()),true)+
  77. "'</pre>",
  78. }
  79. }
  80. function set_time(time) {
  81. $scope.time = time;
  82. $scope.panel.index = _.isUndefined(time.index) ? $scope.panel.index : time.index
  83. $scope.get_data();
  84. }
  85. $scope.build_search = function(field,value) {
  86. $scope.panel.query = add_to_query($scope.panel.query,field,value,false)
  87. $scope.get_data();
  88. eventBus.broadcast($scope.$id,$scope.panel.group,'query',$scope.panel.query);
  89. }
  90. })
  91. .directive('map2', function() {
  92. return {
  93. restrict: 'A',
  94. link: function(scope, elem, attrs) {
  95. elem.html('<center><img src="common/img/load_big.gif"></center>')
  96. // Receive render events
  97. scope.$on('render',function(){
  98. console.log("render");
  99. render_panel();
  100. });
  101. // Or if the window is resized
  102. angular.element(window).bind('resize', function(){
  103. console.log("resize");
  104. render_panel();
  105. });
  106. function render_panel() {
  107. console.log("render_panel");
  108. console.log(scope.panel);
  109. console.log(elem);
  110. // Using LABjs, wait until all scripts are loaded before rendering panel
  111. var scripts = $LAB.script("panels/map2/lib/d3.v3.min.js")
  112. .script("panels/map2/lib/topojson.v0.min.js")
  113. .script("panels/map2/lib/node-geohash.js")
  114. .script("panels/map2/lib/d3.hexbin.v0.min.js");
  115. // Populate element. Note that jvectormap appends, does not replace.
  116. scripts.wait(function(){
  117. elem.text('');
  118. //Better way to get these values? Seems kludgy to use jQuery on the div...
  119. var width = $(elem[0]).width(),
  120. height = $(elem[0]).height();
  121. console.log("draw map", width, height);
  122. //Scale the map by whichever dimension is the smallest, helps to make sure the whole map is shown
  123. var scale = (width > height) ? (height / 2 / Math.PI) : (width / 2 / Math.PI);
  124. var projection = d3.geo.mercator()
  125. .translate([0, 0])
  126. .scale(scale);
  127. var zoom = d3.behavior.zoom()
  128. .scaleExtent([1, 8])
  129. .on("zoom", move);
  130. var path = d3.geo.path()
  131. .projection(projection);
  132. var svg = d3.select(elem[0]).append("svg")
  133. .attr("width", width)
  134. .attr("height", height)
  135. .append("g")
  136. .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")")
  137. .call(zoom);
  138. var g = svg.append("g");
  139. svg.append("rect")
  140. .attr("class", "overlay")
  141. .attr("x", -width / 2)
  142. .attr("y", -height / 2)
  143. .attr("width", width)
  144. .attr("height", height);
  145. d3.json("panels/map2/lib/world-50m.json", function(error, world) {
  146. g.append("path")
  147. .datum(topojson.object(world, world.objects.countries))
  148. .attr("class", "land")
  149. .attr("d", path);
  150. g.append("path")
  151. .datum(topojson.mesh(world, world.objects.countries, function(a, b) { return a !== b; }))
  152. .attr("class", "boundary")
  153. .attr("d", path);
  154. var points = _.map(scope.data, function (k,v) {
  155. var decoded = geohash.decode(v);
  156. return projection([decoded.longitude, decoded.latitude]);
  157. })
  158. var color = d3.scale.linear()
  159. .domain([0, 20])
  160. .range(["white", "steelblue"])
  161. .interpolate(d3.interpolateLab);
  162. var hexbin = d3.hexbin()
  163. .size([width, height])
  164. .radius(10);
  165. g.selectAll(".hexagon")
  166. .data(hexbin(points))
  167. .enter().append("path")
  168. .attr("d", function(d) { return hexbin.hexagon(); })
  169. .attr("class", "hexagon")
  170. .attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; })
  171. .style("fill", function(d) { console.log(d); return color(d.length); })
  172. .attr("opacity", 1);
  173. /*
  174. raw, ugly points
  175. */
  176. var points = _.map(scope.data, function (k,v) {
  177. var decoded = geohash.decode(v);
  178. return {lat: decoded.latitude, lon: decoded.longitude};
  179. })
  180. g.selectAll("circles.points")
  181. .data(points)
  182. .enter()
  183. .append("circle")
  184. .attr("r",1)
  185. .attr("transform", function(d) {return "translate(" + projection([d.lon,d.lat]) + ")";});
  186. });
  187. function move() {
  188. var t = d3.event.translate,
  189. s = d3.event.scale;
  190. t[0] = Math.min(width / 2 * (s - 1), Math.max(width / 2 * (1 - s), t[0]));
  191. t[1] = Math.min(height / 2 * (s - 1) + 230 * s, Math.max(height / 2 * (1 - s) - 230 * s, t[1]));
  192. zoom.translate(t);
  193. g.style("stroke-width", 1 / s).attr("transform", "translate(" + t + ")scale(" + s + ")");
  194. }
  195. })
  196. }
  197. }
  198. };
  199. });