module.js 11 KB

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