module.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. angular.module('kibana.parallelcoordinates', [])
  2. .controller('parallelcoordinates', function ($scope, eventBus) {
  3. $scope.activeDocs = [];
  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. $scope.panel.fields = _.clone(fields)
  41. $scope.$emit('render');
  42. });
  43. };
  44. $scope.get_data = function (segment,query_id) {
  45. // Make sure we have everything for the request to complete
  46. if (_.isUndefined($scope.panel.index) || _.isUndefined($scope.time))
  47. return;
  48. var _segment = _.isUndefined(segment) ? 0 : segment
  49. $scope.segment = _segment;
  50. $scope.panel.loading = true;
  51. var request = $scope.ejs.Request().indices($scope.panel.index[_segment])
  52. .query(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. )
  58. )
  59. .size($scope.panel.size*$scope.panel.pages)
  60. .sort($scope.panel.sort[0],$scope.panel.sort[1]);
  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. if(_segment === 0) {
  67. $scope.hits = 0;
  68. $scope.data = [];
  69. query_id = $scope.query_id = new Date().getTime()
  70. }
  71. // Check for error and abort if found
  72. if(!(_.isUndefined(results.error))) {
  73. $scope.panel.error = $scope.parse_error(results.error);
  74. return;
  75. }
  76. // Check that we're still on the same query, if not stop
  77. if($scope.query_id === query_id) {
  78. $scope.data= $scope.data.concat(_.map(results.hits.hits, function(hit) {
  79. return flatten_json(hit['_source']);
  80. }));
  81. $scope.hits += results.hits.total;
  82. // Sort the data
  83. $scope.data = _.sortBy($scope.data, function(v){
  84. return v[$scope.panel.sort[0]]
  85. });
  86. // Reverse if needed
  87. if($scope.panel.sort[1] == 'desc')
  88. $scope.data.reverse();
  89. // Keep only what we need for the set
  90. $scope.data = $scope.data.slice(0,$scope.panel.size * $scope.panel.pages)
  91. } else {
  92. return;
  93. }
  94. $scope.$emit('render')
  95. });
  96. };
  97. // I really don't like this function, too much dom manip. Break out into directive?
  98. $scope.populate_modal = function (request) {
  99. $scope.modal = {
  100. title: "Inspector",
  101. 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>"
  102. }
  103. };
  104. function set_time(time) {
  105. $scope.time = time;
  106. $scope.panel.index = _.isUndefined(time.index) ? $scope.panel.index : time.index
  107. $scope.get_data();
  108. }
  109. $scope.$watch('activeDocs', function(v) {
  110. eventBus.broadcast($scope.$id,$scope.panel.group,"table_documents",
  111. {query:$scope.panel.query,docs:$scope.activeDocs});
  112. });
  113. })
  114. .directive('parallelcoordinates', function () {
  115. return {
  116. restrict: 'A',
  117. link: function (scope, elem, attrs) {
  118. scope.initializing = false;
  119. /**
  120. * Initialize the panels if new, or render existing panels
  121. */
  122. scope.init_or_render = function() {
  123. if (typeof scope.svg === 'undefined') {
  124. //prevent duplicate initialization steps, if render is called again
  125. //before the svg is setup
  126. if (!scope.initializing) {
  127. init_panel();
  128. }
  129. } else {
  130. render_panel();
  131. }
  132. };
  133. /**
  134. * Receive render events
  135. */
  136. scope.$on('render', function () {
  137. scope.init_or_render();
  138. });
  139. /**
  140. * On window resize, re-render the panel
  141. */
  142. angular.element(window).bind('resize', function () {
  143. scope.init_or_render();
  144. });
  145. /**
  146. * Load the various panel-specific scripts then initialize
  147. * the svg and set appropriate D3 settings
  148. */
  149. function init_panel() {
  150. scope.m = [80, 100, 80, 100];
  151. scope.w = $(elem[0]).width() - scope.m[1] - scope.m[3];
  152. scope.h = $(elem[0]).height() - scope.m[0] - scope.m[2];
  153. scope.initializing = true;
  154. // Using LABjs, wait until all scripts are loaded before rendering panel
  155. var scripts = $LAB.script("common/lib/d3.v3.min.js?rand="+Math.floor(Math.random()*10000));
  156. scripts.wait(function () {
  157. scope.x = d3.scale.ordinal().domain(scope.panel.fields).rangePoints([0, scope.w]);
  158. scope.y = {};
  159. scope.line = d3.svg.line().interpolate('cardinal');
  160. scope.axis = d3.svg.axis().orient("left");
  161. scope.svg = d3.select(elem[0]).append("svg")
  162. .attr("width", "100%")
  163. .attr("height", "100%")
  164. .attr("viewbox", "0 0 " + (scope.w + scope.m[1] + scope.m[3]) + " " + (scope.h + scope.m[0] + scope.m[2]))
  165. .append("svg:g")
  166. .attr("transform", "translate(" + scope.m[3] + "," + scope.m[0] + ")");
  167. // Add foreground lines.
  168. scope.foreground = scope.svg.append("svg:g")
  169. .attr("class", "foreground");
  170. scope.initializing = false;
  171. render_panel();
  172. });
  173. }
  174. // Returns the path for a given data point.
  175. function path(d) {
  176. return scope.line(scope.panel.fields.map(function(p) { return [scope.x(p), scope.y[p](d[p])]; }));
  177. }
  178. // Handles a brush event, toggling the display of foreground lines.
  179. function brush() {
  180. var actives = scope.panel.fields.filter(function(p) { return !scope.y[p].brush.empty(); }),
  181. extents = actives.map(function(p) { return scope.y[p].brush.extent(); });
  182. //.fade class hides the "inactive" lines, helps speed up rendering significantly
  183. scope.foregroundLines.classed("fade", function(d) {
  184. return !actives.every(function(p, i) {
  185. var inside = extents[i][0] <= d[p] && d[p] <= extents[i][1];
  186. return inside;
  187. });
  188. });
  189. //activeDocs contains the actual doc records for selected lines.
  190. //will be broadcast out to the table
  191. var activeDocs = _.filter(scope.data, function(v) {
  192. return actives.every(function(p,i) {
  193. var inside = extents[i][0] <= v[p] && v[p] <= extents[i][1];
  194. return inside;
  195. });
  196. })
  197. scope.$apply(function() {
  198. scope.activeDocs = activeDocs;
  199. });
  200. }
  201. //Drag functions are used for dragging the axis aroud
  202. function dragstart(d) {
  203. scope.i = scope.panel.fields.indexOf(d);
  204. }
  205. function drag(d) {
  206. scope.x.range()[scope.i] = d3.event.x;
  207. scope.panel.fields.sort(function(a, b) { return scope.x(a) - scope.x(b); });
  208. scope.foregroundLines.attr("transform", function(d) { return "translate(" + scope.x(d) + ")"; });
  209. scope.traits.attr("transform", function(d) { return "translate(" + scope.x(d) + ")"; });
  210. scope.brushes.attr("transform", function(d) { return "translate(" + scope.x(d) + ")"; });
  211. scope.axisLines.attr("transform", function(d) { return "translate(" + scope.x(d) + ")"; });
  212. scope.foregroundLines.attr("d", path);
  213. }
  214. function dragend(d) {
  215. scope.x.domain(scope.panel.fields).rangePoints([0, scope.w]);
  216. var t = d3.transition().duration(500);
  217. t.selectAll(".trait").attr("transform", function(d) { return "translate(" + scope.x(d) + ")"; });
  218. t.selectAll(".axis").attr("transform", function(d) { return "translate(" + scope.x(d) + ")"; });
  219. t.selectAll(".brush").attr("transform", function(d) { return "translate(" + scope.x(d) + ")"; });
  220. t.selectAll(".foregroundlines").attr("d", path);
  221. }
  222. /**
  223. * Render updates to the SVG. Typically happens when the data changes (time, query)
  224. * or when new options are selected
  225. */
  226. function render_panel() {
  227. //update the svg if the size has changed
  228. scope.w = $(elem[0]).width() - scope.m[1] - scope.m[3];
  229. scope.h = $(elem[0]).height() - scope.m[0] - scope.m[2];
  230. scope.svg.attr("viewbox", "0 0 " + (scope.w + scope.m[1] + scope.m[3]) + " " + (scope.h + scope.m[0] + scope.m[2]));
  231. scope.x = d3.scale.ordinal().domain(scope.panel.fields).rangePoints([0, scope.w]);
  232. scope.y = {};
  233. scope.line = d3.svg.line().interpolate('cardinal');
  234. scope.axis = d3.svg.axis().orient("left");
  235. var colorExtent = d3.extent(scope.data, function(p) { return +p['phpmemory']; });
  236. scope.colors = d3.scale.linear()
  237. .domain([colorExtent[0],colorExtent[1]])
  238. .range(["#4580FF", "#FF9245"]);
  239. scope.panel.fields.forEach(function(d) {
  240. //If it is a string, setup an ordinal scale.
  241. //Otherwise, use a linear scale for numbers
  242. if (_.isString(scope.data[0][d])) {
  243. var value = function(v) { return v[d]; };
  244. var values = _.map(_.uniq(scope.data, value),value);
  245. scope.y[d] = d3.scale.ordinal()
  246. .domain(values)
  247. .rangeBands([scope.h, 0]);
  248. } else if (_.isNumber(scope.data[0][d])) {
  249. scope.y[d] = d3.scale.linear()
  250. .domain(d3.extent(scope.data, function(p) { return +p[d]; }))
  251. .range([scope.h, 0]);
  252. }
  253. scope.y[d].brush = d3.svg.brush()
  254. .y(scope.y[d])
  255. .on("brush", brush);
  256. });
  257. //pull out the actively selected columns for rendering the axis/lines
  258. var activeData = _.map(scope.data, function(d) {
  259. var t = {};
  260. _.each(scope.panel.fields, function(f) {
  261. t[f] = d[f];
  262. });
  263. return t;
  264. });
  265. //Lines
  266. scope.foregroundLines = scope.foreground
  267. .selectAll(".foregroundlines")
  268. .data(activeData, function(d, i){
  269. var id = "";
  270. _.each(d, function(v) {
  271. id += i + "_" + v;
  272. });
  273. return id;
  274. });
  275. scope.foregroundLines
  276. .enter().append("svg:path")
  277. .attr("d", path)
  278. .attr("class", "foregroundlines")
  279. .attr("style", function(d) {
  280. return "stroke:" + scope.colors(d.phpmemory) + ";";
  281. });
  282. scope.foregroundLines.exit().remove();
  283. //Axis group
  284. scope.traits = scope.svg.selectAll(".trait")
  285. .data(scope.panel.fields, String);
  286. scope.traits
  287. .enter().append("svg:g")
  288. .attr("class", "trait")
  289. .attr("transform", function(d) { return "translate(" + scope.x(d) + ")"; });
  290. scope.traits
  291. .exit().remove();
  292. //brushes used to select lines
  293. scope.brushes = scope.svg.selectAll(".brush")
  294. .data(scope.panel.fields, String);
  295. scope.brushes
  296. .enter()
  297. .append("svg:g")
  298. .attr("class", "brush")
  299. .each(function(d) {
  300. d3.select(this)
  301. .call(scope.y[d].brush)
  302. .attr("transform", function(d) { return "translate(" + scope.x(d) + ")"; });
  303. })
  304. .selectAll("rect")
  305. .attr("x", -8)
  306. .attr("width", 16);
  307. //this section is repeated because enter() only works on "new" data, but we always need to
  308. //update the brushes if things change. This just calls the brushing function, so it doesn't
  309. //affect currently active rects
  310. scope.brushes
  311. .each(function(d) {
  312. d3.select(this)
  313. .call(scope.y[d].brush)
  314. .attr("transform", function(d) { return "translate(" + scope.x(d) + ")"; });
  315. });
  316. scope.brushes
  317. .exit().remove();
  318. //vertical axis and labels
  319. scope.axisLines = scope.svg.selectAll(".axis")
  320. .data(scope.panel.fields, String);
  321. scope.axisLines
  322. .enter()
  323. .append("svg:g")
  324. .attr("class", "axis")
  325. .each(function(d) {
  326. d3.select(this)
  327. .call(scope.axis.scale(scope.y[d]))
  328. .attr("transform", function(d) { return "translate(" + scope.x(d) + ")"; });
  329. }).call(d3.behavior.drag()
  330. .origin(function(d) { return {x: scope.x(d)}; })
  331. .on("dragstart", dragstart)
  332. .on("drag", drag)
  333. .on("dragend", dragend))
  334. .append("svg:text")
  335. .attr("text-anchor", "middle")
  336. .attr("y", -9)
  337. .text(String);
  338. scope.axisLines
  339. .exit().remove();
  340. //Simulate a dragend in case there is new data and we need to rearrange
  341. dragend();
  342. }
  343. }
  344. };
  345. });