module.js 12 KB

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