stream.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. */
  15. solrAdminApp.controller('StreamController',
  16. function($scope, $routeParams, $location, Query, Constants) {
  17. $scope.resetMenu("stream", Constants.IS_COLLECTION_PAGE);
  18. $scope.stream = {
  19. wt: 'json',
  20. expr: $scope.expr,
  21. indent: 'on'
  22. };
  23. $scope.qt = "stream";
  24. $scope.doExplanation = false
  25. $scope.doStream = function() {
  26. var params = {};
  27. params.core = $routeParams.core;
  28. params.handler = $scope.qt;
  29. params.expr = [$scope.expr]
  30. if($scope.doExplanation){
  31. params.explain = [$scope.doExplanation]
  32. }
  33. $scope.lang = "json";
  34. $scope.response = null;
  35. $scope.url = "";
  36. var url = Query.url(params);
  37. Query.query(params, function(data) {
  38. var jsonData = JSON.parse(data.toJSON().data);
  39. if (undefined != jsonData["explanation"]) {
  40. $scope.showExplanation = true;
  41. streamGraphSubController($scope, jsonData["explanation"])
  42. delete jsonData["explanation"]
  43. } else {
  44. $scope.showExplanation = false;
  45. }
  46. data.data = JSON.stringify(jsonData,null,2);
  47. $scope.lang = "json";
  48. $scope.response = data;
  49. $scope.url = url;
  50. $scope.hostPortContext = $location.absUrl().substr(0,$location.absUrl().indexOf("#")); // For display only
  51. });
  52. };
  53. if ($location.search().expr) {
  54. $scope.expr = $location.search()["expr"];
  55. $scope.doStream();
  56. }
  57. }
  58. );
  59. var streamGraphSubController = function($scope, explanation) {
  60. $scope.showGraph = true;
  61. $scope.pos = 0;
  62. $scope.rows = 8;
  63. $scope.resetGraph = function() {
  64. $scope.pos = 0;
  65. $scope.initGraph();
  66. }
  67. $scope.initGraph = function(explanation) {
  68. data = explanation
  69. var leafCount = 0;
  70. var maxDepth = 0;
  71. var rootNode = {};
  72. leafCount = 0;
  73. let recurse = function(dataNode, depth) {
  74. if (depth > maxDepth) {
  75. maxDepth = depth;
  76. }
  77. let graphNode = {
  78. name: dataNode.expressionNodeId,
  79. implementingClass: 'unknown',
  80. data: {}
  81. };
  82. ["expressionNodeId", "expressionType", "functionName", "implementingClass", "expression", "note", "helpers"].forEach(function(key) {
  83. graphNode.data[key] = dataNode[key];
  84. });
  85. if (dataNode.children && dataNode.children.length > 0) {
  86. graphNode.children = [];
  87. dataNode.children.forEach(function(n) {
  88. graphNode.children.push(recurse(n, depth + 1));
  89. });
  90. } else {
  91. ++leafCount;
  92. }
  93. return graphNode;
  94. }
  95. $scope.showPaging = false;
  96. $scope.isRadial = false;
  97. $scope.explanationData = recurse(data, 1);
  98. $scope.depth = maxDepth + 1;
  99. $scope.leafCount = leafCount;
  100. };
  101. $scope.initGraph(explanation);
  102. };
  103. solrAdminApp.directive('explanationGraph', function(Constants) {
  104. return {
  105. restrict: 'EA',
  106. scope: {
  107. data: "=",
  108. leafCount: "=",
  109. depth: "="
  110. },
  111. link: function(scope, element, attrs) {
  112. var helper_path_class = function(p) {
  113. var classes = ['link'];
  114. return classes.join(' ');
  115. };
  116. var helper_node_class = function(d) {
  117. var classes = ['node'];
  118. if (d.data && d.data.expressionType) {
  119. classes.push(d.data.expressionType);
  120. }
  121. return classes.join(' ');
  122. };
  123. var helper_node_text = function(d) {
  124. if (d.data && d.data.functionName) {
  125. return d.data.functionName;
  126. }
  127. return d.name
  128. };
  129. var helper_tooltip = function(d) {
  130. return [
  131. "Function: " + d.data.functionName,
  132. "Type: " + d.data.expressionType,
  133. "Class: " + d.data.implementingClass.replace("org.apache.solr.client.solrj.io", "o.a.s.c.s.i"),
  134. "=============",
  135. d.data.expression
  136. ].join("\n");
  137. }
  138. scope.$watch("data", function(newValue, oldValue) {
  139. if (newValue) {
  140. flatGraph(element, scope.data, scope.depth, scope.leafCount);
  141. }
  142. });
  143. var flatGraph = function(element, graphData, depth, leafCount) {
  144. var w = 100 + (depth * 100),
  145. h = leafCount * 40;
  146. var tree = d3.layout.tree().size([h, w]);
  147. var diagonal = d3.svg.diagonal().projection(function(d) {
  148. return [d.y * .7, d.x];
  149. });
  150. d3.select('#canvas', element).html('');
  151. var vis = d3.select('#canvas', element).append('svg')
  152. .attr('width', w)
  153. .attr('height', h)
  154. .append('g')
  155. .attr('transform', 'translate(25, 0)');
  156. var nodes = tree.nodes(graphData);
  157. var link = vis.selectAll('path.link')
  158. .data(tree.links(nodes))
  159. .enter().append('path')
  160. .attr('class', helper_path_class)
  161. .attr('d', diagonal);
  162. var node = vis.selectAll('g.node')
  163. .data(nodes)
  164. .enter().append('g')
  165. .attr('class', helper_node_class)
  166. .attr('transform', function(d) {
  167. return 'translate(' + d.y * .7 + ',' + d.x + ')';
  168. })
  169. node.append('circle')
  170. .attr('r', 4.5);
  171. node.append('title')
  172. .text(helper_tooltip);
  173. node.append('text')
  174. .attr('dx', function(d) {
  175. return 8;
  176. })
  177. .attr('dy', function(d) {
  178. return 5;
  179. })
  180. .attr('text-anchor', function(d) {
  181. return 'start';
  182. })
  183. .text(helper_node_text)
  184. };
  185. }
  186. };
  187. })