module.js 13 KB

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