module.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. angular.module('kibana.parallelcoordinates', [])
  2. .controller('parallelcoordinates', function ($scope, eventBus) {
  3. console.log("controller");
  4. // Set and populate defaults
  5. var _d = {
  6. query : "*",
  7. size : 100, // Per page
  8. pages : 5, // Pages available
  9. offset : 0,
  10. sort : ['@timestamp','desc'],
  11. group : "default",
  12. style : {'font-size': '9pt'},
  13. fields : [],
  14. sortable: true,
  15. spyable: true,
  16. }
  17. _.defaults($scope.panel, _d)
  18. $scope.init = function () {
  19. $scope.set_listeners($scope.panel.group);
  20. // Now that we're all setup, request the time from our group
  21. eventBus.broadcast($scope.$id,$scope.panel.group,"get_time")
  22. //and get the currently selected fields
  23. eventBus.broadcast($scope.$id,$scope.panel.group,"get_fields")
  24. };
  25. $scope.set_listeners = function(group) {
  26. eventBus.register($scope,'time',function(event,time) {
  27. $scope.panel.offset = 0;
  28. set_time(time)
  29. });
  30. eventBus.register($scope,'query',function(event,query) {
  31. $scope.panel.offset = 0;
  32. $scope.panel.query = _.isArray(query) ? query[0] : query;
  33. $scope.get_data();
  34. });
  35. eventBus.register($scope,'sort', function(event,sort){
  36. $scope.panel.sort = _.clone(sort);
  37. $scope.get_data();
  38. });
  39. eventBus.register($scope,'selected_fields', function(event, fields) {
  40. console.log("selected_fields", fields);
  41. $scope.panel.fields = _.clone(fields)
  42. });
  43. };
  44. $scope.get_data = function (segment,query_id) {
  45. console.log("get_data");
  46. // Make sure we have everything for the request to complete
  47. if (_.isUndefined($scope.panel.index) || _.isUndefined($scope.time))
  48. return;
  49. var _segment = _.isUndefined(segment) ? 0 : segment
  50. $scope.segment = _segment;
  51. $scope.panel.loading = true;
  52. var request = $scope.ejs.Request().indices($scope.panel.index[_segment])
  53. .query(ejs.FilteredQuery(
  54. ejs.QueryStringQuery($scope.panel.query || '*'),
  55. ejs.RangeFilter($scope.time.field)
  56. .from($scope.time.from)
  57. .to($scope.time.to)
  58. )
  59. )
  60. .size($scope.panel.size*$scope.panel.pages)
  61. .sort($scope.panel.sort[0],$scope.panel.sort[1]);
  62. $scope.populate_modal(request);
  63. var results = request.doSearch();
  64. // Populate scope when we have results
  65. results.then(function (results) {
  66. $scope.panel.loading = false;
  67. if(_segment === 0) {
  68. $scope.hits = 0;
  69. $scope.data = [];
  70. query_id = $scope.query_id = new Date().getTime()
  71. }
  72. // Check for error and abort if found
  73. if(!(_.isUndefined(results.error))) {
  74. $scope.panel.error = $scope.parse_error(results.error);
  75. return;
  76. }
  77. // Check that we're still on the same query, if not stop
  78. if($scope.query_id === query_id) {
  79. $scope.data= $scope.data.concat(_.map(results.hits.hits, function(hit) {
  80. return flatten_json(hit['_source']);
  81. }));
  82. $scope.hits += results.hits.total;
  83. // Sort the data
  84. $scope.data = _.sortBy($scope.data, function(v){
  85. return v[$scope.panel.sort[0]]
  86. });
  87. // Reverse if needed
  88. if($scope.panel.sort[1] == 'desc')
  89. $scope.data.reverse();
  90. // Keep only what we need for the set
  91. $scope.data = $scope.data.slice(0,$scope.panel.size * $scope.panel.pages)
  92. } else {
  93. return;
  94. }
  95. console.log("emit render");
  96. console.log("data",$scope.data);
  97. $scope.$emit('render')
  98. });
  99. };
  100. // I really don't like this function, too much dom manip. Break out into directive?
  101. $scope.populate_modal = function (request) {
  102. $scope.modal = {
  103. title: "Inspector",
  104. 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>"
  105. }
  106. };
  107. function set_time(time) {
  108. $scope.time = time;
  109. $scope.panel.index = _.isUndefined(time.index) ? $scope.panel.index : time.index
  110. $scope.get_data();
  111. }
  112. })
  113. .directive('parallelcoordinates', function () {
  114. return {
  115. restrict: 'A',
  116. link: function (scope, elem, attrs) {
  117. console.log("directive");
  118. //elem.html('')
  119. scope.initializing = false;
  120. /**
  121. * Initialize the panels if new, or render existing panels
  122. */
  123. scope.init_or_render = function() {
  124. if (typeof scope.svg === 'undefined') {
  125. console.log("init");
  126. //prevent duplicate initialization steps, if render is called again
  127. //before the svg is setup
  128. if (!scope.initializing) {
  129. init_panel();
  130. }
  131. } else {
  132. console.log("render");
  133. render_panel();
  134. }
  135. };
  136. /**
  137. * Receive render events
  138. */
  139. scope.$on('render', function () {
  140. console.log("on render");
  141. scope.init_or_render();
  142. });
  143. /**
  144. * On window resize, re-render the panel
  145. */
  146. angular.element(window).bind('resize', function () {
  147. console.log("on resize");
  148. scope.init_or_render();
  149. });
  150. var species = ["setosa", "versicolor", "virginica"],
  151. traits = ["sepal length", "petal length", "sepal width", "petal width"];
  152. var m = [80, 160, 200, 160],
  153. w = 1280 - m[1] - m[3],
  154. h = 800 - m[0] - m[2];
  155. var x, y,line,axis,foreground,svg;
  156. /**
  157. * Load the various panel-specific scripts then initialize
  158. * the svg and set appropriate D3 settings
  159. */
  160. function init_panel() {
  161. console.log("init");
  162. console.log("fields", scope.panel.fields);
  163. scope.initializing = true;
  164. // Using LABjs, wait until all scripts are loaded before rendering panel
  165. var scripts = $LAB.script("common/lib/d3.v3.min.js?rand="+Math.floor(Math.random()*10000))
  166. .script("panels/parallelcoordinates/lib/d3.csv.js?rand="+Math.floor(Math.random()*10000));
  167. scripts.wait(function () {
  168. console.log("scripts loaded");
  169. x = d3.scale.ordinal().domain(traits).rangePoints([0, w]);
  170. y = {};
  171. line = d3.svg.line();
  172. axis = d3.svg.axis().orient("left");
  173. svg = d3.select(elem[0]).append("svg")
  174. .attr("width", "100%")
  175. .attr("height", "100%")
  176. .attr("viewbox", "0 0 " + (w + m[1] + m[3]) + " " + (h + m[0] + m[2]))
  177. .append("svg:g")
  178. .attr("transform", "translate(" + m[3] + "," + m[0] + ")");
  179. console.log("loaded");
  180. //console.log(flowers);
  181. // Create a scale and brush for each trait.
  182. scope.panel.fields.forEach(function(d) {
  183. console.log("extent", d3.extent(scope.data, function(p) { return +p[d]; }));
  184. y[d] = d3.scale.linear()
  185. .domain(d3.extent(scope.data, function(p) { return +p[d]; }))
  186. .range([h, 0]);
  187. y[d].brush = d3.svg.brush()
  188. .y(y[d])
  189. .on("brush", brush);
  190. });
  191. console.log("y", y);
  192. // Add foreground lines.
  193. foreground = svg.append("svg:g")
  194. .attr("class", "foreground")
  195. .selectAll("path")
  196. .data(scope.data)
  197. .enter().append("svg:path")
  198. .attr("d", path)
  199. .attr("class", 'setosa');
  200. // Add a group element for each trait.
  201. scope.g = svg.selectAll(".trait")
  202. .data(scope.panel.fields)
  203. .enter().append("svg:g")
  204. .attr("class", "trait")
  205. .attr("transform", function(d) { return "translate(" + x(d) + ")"; })
  206. .call(d3.behavior.drag()
  207. .origin(function(d) { return {x: x(d)}; })
  208. .on("dragstart", dragstart)
  209. .on("drag", drag)
  210. .on("dragend", dragend));
  211. // Add a brush for each axis.
  212. scope.g.append("svg:g")
  213. .attr("class", "brush")
  214. .each(function(d) { d3.select(this).call(y[d].brush); })
  215. .selectAll("rect")
  216. .attr("x", -8)
  217. .attr("width", 16);
  218. // Add an axis and title.
  219. scope.g.append("svg:g")
  220. .attr("class", "axis")
  221. .each(function(d) { d3.select(this).call(axis.scale(y[d])); })
  222. .append("svg:text")
  223. .attr("text-anchor", "middle")
  224. .attr("y", -9)
  225. .text(String);
  226. scope.initializing = false;
  227. render_panel();
  228. });
  229. }
  230. // Returns the path for a given data point.
  231. function path(d) {
  232. return line(scope.panel.fields.map(function(p) { return [x(p), y[p](d[p])]; }));
  233. }
  234. // Handles a brush event, toggling the display of foreground lines.
  235. function brush() {
  236. var actives = scope.panel.fields.filter(function(p) { return !y[p].brush.empty(); }),
  237. extents = actives.map(function(p) { return y[p].brush.extent(); });
  238. foreground.classed("fade", function(d) {
  239. return !actives.every(function(p, i) {
  240. return extents[i][0] <= d[p] && d[p] <= extents[i][1];
  241. });
  242. });
  243. }
  244. function dragstart(d) {
  245. i = scope.panel.fields.indexOf(d);
  246. }
  247. function drag(d) {
  248. x.range()[i] = d3.event.x;
  249. scope.panel.fields.sort(function(a, b) { return x(a) - x(b); });
  250. scope.g.attr("transform", function(d) { return "translate(" + x(d) + ")"; });
  251. foreground.attr("d", path);
  252. }
  253. function dragend(d) {
  254. x.domain(scope.panel.fields).rangePoints([0, w]);
  255. var t = d3.transition().duration(500);
  256. t.selectAll(".trait").attr("transform", function(d) { return "translate(" + x(d) + ")"; });
  257. t.selectAll(".foreground path").attr("d", path);
  258. }
  259. /**
  260. * Render updates to the SVG. Typically happens when the data changes (time, query)
  261. * or when new options are selected
  262. */
  263. function render_panel() {
  264. var width = $(elem[0]).width(),
  265. height = $(elem[0]).height();
  266. }
  267. }
  268. };
  269. });