module.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. angular.module('kibana.parallelcoordinates', [])
  2. .controller('parallelcoordinates', function ($scope, eventBus) {
  3. console.log("controller");
  4. $scope.activeDocs = [];
  5. // Set and populate defaults
  6. var _d = {
  7. query : "*",
  8. size : 100, // Per page
  9. pages : 5, // Pages available
  10. offset : 0,
  11. sort : ['@timestamp','desc'],
  12. group : "default",
  13. style : {'font-size': '9pt'},
  14. fields : [],
  15. sortable: true,
  16. spyable: true
  17. }
  18. _.defaults($scope.panel, _d)
  19. $scope.init = function () {
  20. $scope.set_listeners($scope.panel.group);
  21. // Now that we're all setup, request the time from our group
  22. eventBus.broadcast($scope.$id,$scope.panel.group,"get_time")
  23. //and get the currently selected fields
  24. eventBus.broadcast($scope.$id,$scope.panel.group,"get_fields")
  25. };
  26. $scope.set_listeners = function(group) {
  27. eventBus.register($scope,'time',function(event,time) {
  28. $scope.panel.offset = 0;
  29. set_time(time)
  30. });
  31. eventBus.register($scope,'query',function(event,query) {
  32. $scope.panel.offset = 0;
  33. $scope.panel.query = _.isArray(query) ? query[0] : query;
  34. $scope.get_data();
  35. });
  36. eventBus.register($scope,'sort', function(event,sort){
  37. $scope.panel.sort = _.clone(sort);
  38. $scope.get_data();
  39. });
  40. eventBus.register($scope,'selected_fields', function(event, fields) {
  41. $scope.panel.fields = _.clone(fields)
  42. $scope.$emit('render');
  43. });
  44. };
  45. $scope.get_data = function (segment,query_id) {
  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. $scope.$emit('render')
  96. });
  97. };
  98. // I really don't like this function, too much dom manip. Break out into directive?
  99. $scope.populate_modal = function (request) {
  100. $scope.modal = {
  101. title: "Inspector",
  102. 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>"
  103. }
  104. };
  105. function set_time(time) {
  106. $scope.time = time;
  107. $scope.panel.index = _.isUndefined(time.index) ? $scope.panel.index : time.index
  108. $scope.get_data();
  109. }
  110. $scope.$watch('activeDocs', function(v) {
  111. eventBus.broadcast($scope.$id,$scope.panel.group,"table_documents",
  112. {query:$scope.panel.query,docs:$scope.activeDocs});
  113. });
  114. })
  115. .directive('parallelcoordinates', function () {
  116. return {
  117. restrict: 'A',
  118. link: function (scope, elem, attrs) {
  119. console.log("directive");
  120. scope.initializing = false;
  121. /**
  122. * Initialize the panels if new, or render existing panels
  123. */
  124. scope.init_or_render = function() {
  125. if (typeof scope.svg === 'undefined') {
  126. console.log("init");
  127. //prevent duplicate initialization steps, if render is called again
  128. //before the svg is setup
  129. if (!scope.initializing) {
  130. init_panel();
  131. }
  132. } else {
  133. console.log("render");
  134. render_panel();
  135. }
  136. };
  137. /**
  138. * Receive render events
  139. */
  140. scope.$on('render', function () {
  141. console.log("on render");
  142. scope.init_or_render();
  143. });
  144. /**
  145. * On window resize, re-render the panel
  146. */
  147. angular.element(window).bind('resize', function () {
  148. console.log("on resize");
  149. scope.init_or_render();
  150. });
  151. /**
  152. * Load the various panel-specific scripts then initialize
  153. * the svg and set appropriate D3 settings
  154. */
  155. function init_panel() {
  156. scope.m = [80, 160, 200, 160];
  157. scope.w = $(elem[0]).width() - scope.m[1] - scope.m[3],
  158. scope.h = $(elem[0]).height() - scope.m[0] - scope.m[2];
  159. console.log("init");
  160. console.log("fields", scope.panel.fields);
  161. scope.initializing = true;
  162. // Using LABjs, wait until all scripts are loaded before rendering panel
  163. var scripts = $LAB.script("common/lib/d3.v3.min.js?rand="+Math.floor(Math.random()*10000));
  164. scripts.wait(function () {
  165. scope.x = d3.scale.ordinal().domain(scope.panel.fields).rangePoints([0, scope.w]);
  166. scope.y = {};
  167. scope.line = d3.svg.line().interpolate('cardinal');
  168. scope.axis = d3.svg.axis().orient("left");
  169. scope.svg = d3.select(elem[0]).append("svg")
  170. .attr("width", "100%")
  171. .attr("height", "100%")
  172. .attr("viewbox", "0 0 " + (scope.w + scope.m[1] + scope.m[3]) + " " + (scope.h + scope.m[0] + scope.m[2]))
  173. .append("svg:g")
  174. .attr("transform", "translate(" + scope.m[3] + "," + scope.m[0] + ")");
  175. // Add foreground lines.
  176. scope.foreground = scope.svg.append("svg:g")
  177. .attr("class", "foreground");
  178. scope.initializing = false;
  179. console.log("init done");
  180. render_panel();
  181. });
  182. }
  183. // Returns the path for a given data point.
  184. function path(d) {
  185. return scope.line(scope.panel.fields.map(function(p) { return [scope.x(p), scope.y[p](d[p])]; }));
  186. }
  187. // Handles a brush event, toggling the display of foreground lines.
  188. function brush() {
  189. var actives = scope.panel.fields.filter(function(p) { return !scope.y[p].brush.empty(); }),
  190. extents = actives.map(function(p) { return scope.y[p].brush.extent(); });
  191. scope.foregroundLines.classed("fade", function(d) {
  192. return !actives.every(function(p, i) {
  193. var inside = extents[i][0] <= d[p] && d[p] <= extents[i][1];
  194. return inside;
  195. });
  196. });
  197. var activeDocs = _.filter(scope.data, function(v) {
  198. return actives.every(function(p,i) {
  199. var inside = extents[i][0] <= v[p] && v[p] <= extents[i][1];
  200. return inside;
  201. });
  202. })
  203. scope.$apply(function() {
  204. scope.activeDocs = activeDocs;
  205. });
  206. }
  207. function dragstart(d) {
  208. scope.i = scope.panel.fields.indexOf(d);
  209. console.log("dragstart", d, scope.i)
  210. }
  211. function drag(d) {
  212. console.log("drag", d, scope.i)
  213. scope.x.range()[scope.i] = d3.event.x;
  214. scope.panel.fields.sort(function(a, b) { return scope.x(a) - scope.x(b); });
  215. scope.foregroundLines.attr("transform", function(d) { return "translate(" + scope.x(d) + ")"; });
  216. scope.traits.attr("transform", function(d) { return "translate(" + scope.x(d) + ")"; });
  217. scope.brushes.attr("transform", function(d) { return "translate(" + scope.x(d) + ")"; });
  218. scope.axisLines.attr("transform", function(d) { return "translate(" + scope.x(d) + ")"; });
  219. scope.foregroundLines.attr("d", path);
  220. }
  221. function dragend(d) {
  222. console.log("dragend", d)
  223. scope.x.domain(scope.panel.fields).rangePoints([0, scope.w]);
  224. var t = d3.transition().duration(500);
  225. t.selectAll(".trait").attr("transform", function(d) { return "translate(" + scope.x(d) + ")"; });
  226. t.selectAll(".axis").attr("transform", function(d) { return "translate(" + scope.x(d) + ")"; });
  227. t.selectAll(".brush").attr("transform", function(d) { return "translate(" + scope.x(d) + ")"; });
  228. t.selectAll(".foregroundlines").attr("d", path);
  229. }
  230. /**
  231. * Render updates to the SVG. Typically happens when the data changes (time, query)
  232. * or when new options are selected
  233. */
  234. function render_panel() {
  235. console.log("render_panel");
  236. scope.x = d3.scale.ordinal().domain(scope.panel.fields).rangePoints([0, scope.w]);
  237. scope.y = {};
  238. scope.line = d3.svg.line().interpolate('cardinal');
  239. scope.axis = d3.svg.axis().orient("left");
  240. var colorExtent = d3.extent(scope.data, function(p) { return +p['phpmemory']; });
  241. scope.colors = d3.scale.linear()
  242. .domain([colorExtent[0],colorExtent[1]])
  243. .range(["#4580FF", "#FF9245"]);
  244. scope.panel.fields.forEach(function(d) {
  245. if (_.isString(scope.data[0][d])) {
  246. var value = function(v) {
  247. return v[d];
  248. };
  249. var values = _.map(_.uniq(scope.data, value),value);
  250. scope.y[d] = d3.scale.ordinal()
  251. .domain(values)
  252. .rangeBands([scope.h, 0]);
  253. } else if (_.isNumber(scope.data[0][d])) {
  254. scope.y[d] = d3.scale.linear()
  255. .domain(d3.extent(scope.data, function(p) { return +p[d]; }))
  256. .range([scope.h, 0]);
  257. }
  258. scope.y[d].brush = d3.svg.brush()
  259. .y(scope.y[d])
  260. .on("brush", brush);
  261. });
  262. var activeData = _.map(scope.data, function(d) {
  263. var t = {};
  264. _.each(scope.panel.fields, function(f) {
  265. t[f] = d[f];
  266. });
  267. return t;
  268. });
  269. scope.foregroundLines = scope.foreground
  270. .selectAll(".foregroundlines")
  271. .data(activeData, function(d, i){
  272. var id = "";
  273. _.each(d, function(v) {
  274. id += i + "_" + v;
  275. });
  276. return id;
  277. });
  278. scope.foregroundLines
  279. .enter().append("svg:path")
  280. .attr("d", path)
  281. .attr("class", "foregroundlines")
  282. .attr("style", function(d) {
  283. return "stroke:" + scope.colors(d.phpmemory) + ";";
  284. });
  285. scope.foregroundLines.exit().remove();
  286. scope.traits = scope.svg.selectAll(".trait")
  287. .data(scope.panel.fields, String);
  288. scope.traits
  289. .enter().append("svg:g")
  290. .attr("class", "trait")
  291. .attr("transform", function(d) { return "translate(" + scope.x(d) + ")"; });
  292. scope.brushes = scope.svg.selectAll(".brush")
  293. .data(scope.panel.fields, String);
  294. scope.brushes
  295. .enter()
  296. .append("svg:g")
  297. .attr("class", "brush")
  298. .each(function(d) {
  299. d3.select(this)
  300. .call(scope.y[d].brush)
  301. .attr("transform", function(d) { return "translate(" + scope.x(d) + ")"; });
  302. })
  303. .selectAll("rect")
  304. .attr("x", -8)
  305. .attr("width", 16);
  306. scope.brushes
  307. .each(function(d) {
  308. d3.select(this)
  309. .call(scope.y[d].brush)
  310. .attr("transform", function(d) { return "translate(" + scope.x(d) + ")"; });
  311. });
  312. scope.axisLines = scope.svg.selectAll(".axis")
  313. .data(scope.panel.fields, String);
  314. scope.axisLines
  315. .enter()
  316. .append("svg:g")
  317. .attr("class", "axis")
  318. .each(function(d) {
  319. d3.select(this)
  320. .call(scope.axis.scale(scope.y[d]))
  321. .attr("transform", function(d) { return "translate(" + scope.x(d) + ")"; });
  322. }).call(d3.behavior.drag()
  323. .origin(function(d) { return {x: scope.x(d)}; })
  324. .on("dragstart", dragstart)
  325. .on("drag", drag)
  326. .on("dragend", dragend))
  327. .append("svg:text")
  328. .attr("text-anchor", "middle")
  329. .attr("y", -9)
  330. .text(String);
  331. scope.brushes
  332. .exit().remove();
  333. scope.axisLines
  334. .exit().remove();
  335. scope.traits
  336. .exit().remove();
  337. dragend();
  338. }
  339. }
  340. };
  341. });