module.js 11 KB

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