module.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. $scope.$emit('render');
  43. });
  44. };
  45. $scope.get_data = function (segment,query_id) {
  46. console.log("get_data");
  47. // Make sure we have everything for the request to complete
  48. if (_.isUndefined($scope.panel.index) || _.isUndefined($scope.time))
  49. return;
  50. var _segment = _.isUndefined(segment) ? 0 : segment
  51. $scope.segment = _segment;
  52. $scope.panel.loading = true;
  53. var request = $scope.ejs.Request().indices($scope.panel.index[_segment])
  54. .query(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. )
  60. )
  61. .size($scope.panel.size*$scope.panel.pages)
  62. .sort($scope.panel.sort[0],$scope.panel.sort[1]);
  63. $scope.populate_modal(request);
  64. var results = request.doSearch();
  65. // Populate scope when we have results
  66. results.then(function (results) {
  67. $scope.panel.loading = false;
  68. if(_segment === 0) {
  69. $scope.hits = 0;
  70. $scope.data = [];
  71. query_id = $scope.query_id = new Date().getTime()
  72. }
  73. // Check for error and abort if found
  74. if(!(_.isUndefined(results.error))) {
  75. $scope.panel.error = $scope.parse_error(results.error);
  76. return;
  77. }
  78. // Check that we're still on the same query, if not stop
  79. if($scope.query_id === query_id) {
  80. $scope.data= $scope.data.concat(_.map(results.hits.hits, function(hit) {
  81. return flatten_json(hit['_source']);
  82. }));
  83. $scope.hits += results.hits.total;
  84. // Sort the data
  85. $scope.data = _.sortBy($scope.data, function(v){
  86. return v[$scope.panel.sort[0]]
  87. });
  88. // Reverse if needed
  89. if($scope.panel.sort[1] == 'desc')
  90. $scope.data.reverse();
  91. // Keep only what we need for the set
  92. $scope.data = $scope.data.slice(0,$scope.panel.size * $scope.panel.pages)
  93. } else {
  94. return;
  95. }
  96. console.log("emit render");
  97. console.log("data",$scope.data);
  98. $scope.$emit('render')
  99. });
  100. };
  101. // I really don't like this function, too much dom manip. Break out into directive?
  102. $scope.populate_modal = function (request) {
  103. $scope.modal = {
  104. title: "Inspector",
  105. 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>"
  106. }
  107. };
  108. function set_time(time) {
  109. $scope.time = time;
  110. $scope.panel.index = _.isUndefined(time.index) ? $scope.panel.index : time.index
  111. $scope.get_data();
  112. }
  113. })
  114. .directive('parallelcoordinates', function () {
  115. return {
  116. restrict: 'A',
  117. link: function (scope, elem, attrs) {
  118. console.log("directive");
  119. //elem.html('')
  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. return extents[i][0] <= d[p] && d[p] <= extents[i][1];
  194. });
  195. });
  196. }
  197. function dragstart(d) {
  198. scope.i = scope.panel.fields.indexOf(d);
  199. console.log("dragstart", d, scope.i)
  200. }
  201. function drag(d) {
  202. console.log("drag", d, scope.i)
  203. scope.x.range()[scope.i] = d3.event.x;
  204. scope.panel.fields.sort(function(a, b) { return scope.x(a) - scope.x(b); });
  205. scope.foregroundLines.attr("transform", function(d) { return "translate(" + scope.x(d) + ")"; });
  206. scope.traits.attr("transform", function(d) { return "translate(" + scope.x(d) + ")"; });
  207. scope.brushes.attr("transform", function(d) { return "translate(" + scope.x(d) + ")"; });
  208. scope.axisLines.attr("transform", function(d) { return "translate(" + scope.x(d) + ")"; });
  209. scope.foregroundLines.attr("d", path);
  210. }
  211. function dragend(d) {
  212. console.log("dragend", d)
  213. scope.x.domain(scope.panel.fields).rangePoints([0, scope.w]);
  214. var t = d3.transition().duration(500);
  215. t.selectAll(".trait").attr("transform", function(d) { return "translate(" + scope.x(d) + ")"; });
  216. t.selectAll(".axis").attr("transform", function(d) { return "translate(" + scope.x(d) + ")"; });
  217. t.selectAll(".brush").attr("transform", function(d) { return "translate(" + scope.x(d) + ")"; });
  218. t.selectAll(".foregroundlines").attr("d", path);
  219. }
  220. /**
  221. * Render updates to the SVG. Typically happens when the data changes (time, query)
  222. * or when new options are selected
  223. */
  224. function render_panel() {
  225. console.log("render_panel");
  226. scope.x = d3.scale.ordinal().domain(scope.panel.fields).rangePoints([0, scope.w]);
  227. scope.y = {};
  228. scope.line = d3.svg.line().interpolate('cardinal');
  229. scope.axis = d3.svg.axis().orient("left");
  230. var colorExtent = d3.extent(scope.data, function(p) { return +p['phpmemory']; });
  231. scope.colors = d3.scale.linear()
  232. .domain([colorExtent[0],colorExtent[1]])
  233. .range(["#4580FF", "#FF9245"]);
  234. scope.panel.fields.forEach(function(d) {
  235. scope.y[d] = d3.scale.linear()
  236. .domain(d3.extent(scope.data, function(p) { return +p[d]; }))
  237. .range([scope.h, 0]);
  238. scope.y[d].brush = d3.svg.brush()
  239. .y(scope.y[d])
  240. .on("brush", brush);
  241. });
  242. console.log("render y", scope.y);
  243. var activeData = _.map(scope.data, function(d) {
  244. var t = {};
  245. _.each(scope.panel.fields, function(f) {
  246. t[f] = d[f];
  247. });
  248. return t;
  249. });
  250. scope.foregroundLines = scope.foreground
  251. .selectAll(".foregroundlines")
  252. .data(activeData, function(d, i){
  253. var id = "";
  254. _.each(d, function(v) {
  255. id += i + "_" + v;
  256. });
  257. return id;
  258. });
  259. scope.foregroundLines
  260. .enter().append("svg:path")
  261. .attr("d", path)
  262. .attr("class", "foregroundlines")
  263. .attr("style", function(d) {
  264. return "stroke:" + scope.colors(d.phpmemory) + ";";
  265. });
  266. scope.foregroundLines.exit().remove();
  267. console.log("Render Fields",scope.panel.fields);
  268. scope.traits = scope.svg.selectAll(".trait")
  269. .data(scope.panel.fields, String);
  270. scope.traits
  271. .enter().append("svg:g")
  272. .attr("class", "trait")
  273. .attr("transform", function(d) { return "translate(" + scope.x(d) + ")"; });
  274. scope.brushes = scope.svg.selectAll(".brush")
  275. .data(scope.panel.fields, String);
  276. scope.brushes
  277. .enter()
  278. .append("svg:g")
  279. .attr("class", "brush")
  280. .each(function(d) {
  281. d3.select(this)
  282. .call(scope.y[d].brush)
  283. .attr("transform", function(d) { return "translate(" + scope.x(d) + ")"; });
  284. })
  285. .selectAll("rect")
  286. .attr("x", -8)
  287. .attr("width", 16);
  288. scope.brushes
  289. .each(function(d) {
  290. d3.select(this)
  291. .call(scope.y[d].brush)
  292. .attr("transform", function(d) { return "translate(" + scope.x(d) + ")"; });
  293. });
  294. scope.axisLines = scope.svg.selectAll(".axis")
  295. .data(scope.panel.fields, String);
  296. scope.axisLines
  297. .enter()
  298. .append("svg:g")
  299. .attr("class", "axis")
  300. .each(function(d) {
  301. console.log("axis",d)
  302. d3.select(this)
  303. .call(scope.axis.scale(scope.y[d]))
  304. .attr("transform", function(d) { return "translate(" + scope.x(d) + ")"; });
  305. }).call(d3.behavior.drag()
  306. .origin(function(d) { return {x: scope.x(d)}; })
  307. .on("dragstart", dragstart)
  308. .on("drag", drag)
  309. .on("dragend", dragend))
  310. .append("svg:text")
  311. .attr("text-anchor", "middle")
  312. .attr("y", -9)
  313. .text(String);
  314. scope.brushes
  315. .exit().remove();
  316. scope.axisLines
  317. .exit().remove();
  318. scope.traits
  319. .exit().remove();
  320. dragend();
  321. /*
  322. // Add a brush for each axis.
  323. scope.brushes = scope.g.append("svg:g")
  324. .attr("class", "brush");
  325. scope.axisLines = scope.g.append("svg:g")
  326. .attr("class", "axis");
  327. //Draw the brushes
  328. //If the field is no longer in the list of actives,
  329. //remove the element. Sorta like a poor-man's enter() / exit()
  330. scope.brushes
  331. .each(function(d) {
  332. if (typeof scope.y[d] !== 'undefined') {
  333. console.log("brushes.each", d);
  334. d3.select(this).attr("style", "display").call(scope.y[d].brush);
  335. } else {
  336. console.log("none");
  337. d3.select(this).attr("style", "display:none");
  338. }
  339. })
  340. .selectAll("rect")
  341. .attr("x", -8)
  342. .attr("width", 16);
  343. //Draw the axis lines
  344. //If the field is no longer in the list of actives,
  345. //remove the element. Sorta like a poor-man's enter() / exit()
  346. scope.axisLines
  347. .each(function(d) {
  348. if (typeof scope.y[d] !== 'undefined') {
  349. d3.select(this).attr("style", "display").call(scope.axis.scale(scope.y[d]));
  350. } else {
  351. d3.select(this).attr("style", "display:none");
  352. }
  353. })
  354. .append("svg:text")
  355. .attr("text-anchor", "middle")
  356. .attr("y", -9)
  357. .text(String)
  358. .call(dragend); //call dragend so that the axis reshuffle.
  359. */
  360. }
  361. }
  362. };
  363. });