KnowledgeGraphVisualization.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. namespace App\Livewire\Integrations;
  3. use Livewire\Component;
  4. use App\Services\KnowledgeServiceApi;
  5. use App\Services\QuestionBankService;
  6. use Illuminate\Support\Facades\Http;
  7. use Illuminate\Support\Facades\Log;
  8. class KnowledgeGraphVisualization extends Component
  9. {
  10. public $selectedKpCode = null;
  11. public $graphData = [];
  12. public $nodes = [];
  13. public $edges = [];
  14. public $isLoading = false;
  15. public $layoutType = 'full'; // 'full' 或 'selected'
  16. public $filterPhase = '';
  17. public $filterCategory = '';
  18. protected $listeners = [
  19. 'nodeSelected' => 'handleNodeSelected',
  20. 'clearSelection' => 'clearSelection',
  21. ];
  22. public function mount($selectedKpCode = null)
  23. {
  24. $this->selectedKpCode = $selectedKpCode;
  25. $this->loadGraphData();
  26. }
  27. public function loadGraphData()
  28. {
  29. $this->isLoading = true;
  30. try {
  31. $knowledgeApiBase = config('services.knowledge_api.base_url', 'http://localhost:5011');
  32. $response = Http::timeout(10)
  33. ->get($knowledgeApiBase . '/graph/export');
  34. if ($response->successful()) {
  35. $data = $response->json();
  36. $allNodes = $data['nodes'] ?? [];
  37. $allEdges = $data['edges'] ?? [];
  38. // 应用筛选
  39. $filteredNodes = $this->applyFilters($allNodes);
  40. $this->nodes = $filteredNodes;
  41. $this->edges = $allEdges;
  42. // 转换为可视化格式
  43. $this->graphData = $this->formatForVisualization($this->nodes, $this->edges);
  44. // 如果指定了选中的知识点,加载详细信息
  45. if ($this->selectedKpCode) {
  46. $this->loadNodeDetails($this->selectedKpCode);
  47. }
  48. }
  49. } catch (\Exception $e) {
  50. Log::error('加载知识图谱失败', [
  51. 'error' => $e->getMessage()
  52. ]);
  53. $this->dispatch('error', message: '加载知识图谱失败:' . $e->getMessage());
  54. }
  55. $this->isLoading = false;
  56. }
  57. private function applyFilters($nodes)
  58. {
  59. $filtered = collect($nodes);
  60. if ($this->filterPhase) {
  61. $filtered = $filtered->where('phase', $this->filterPhase);
  62. }
  63. if ($this->filterCategory) {
  64. $filtered = $filtered->where('category', $this->filterCategory);
  65. }
  66. return $filtered->values()->toArray();
  67. }
  68. private function formatForVisualization($nodes, $edges)
  69. {
  70. $formattedNodes = [];
  71. $formattedEdges = [];
  72. foreach ($nodes as $node) {
  73. $formattedNodes[] = [
  74. 'id' => $node['kp_code'],
  75. 'label' => $node['cn_name'] ?? $node['kp_code'],
  76. 'kp_code' => $node['kp_code'],
  77. 'cn_name' => $node['cn_name'] ?? '',
  78. 'en_name' => $node['en_name'] ?? '',
  79. 'category' => $node['category'] ?? '',
  80. 'phase' => $node['phase'] ?? '',
  81. 'importance' => $node['importance'] ?? 0,
  82. 'description' => $node['description'] ?? '',
  83. 'skills_count' => count($node['skills'] ?? []),
  84. 'is_selected' => $this->selectedKpCode === $node['kp_code'],
  85. ];
  86. }
  87. foreach ($edges as $edge) {
  88. $formattedEdges[] = [
  89. 'from' => $edge['source'],
  90. 'to' => $edge['target'],
  91. 'type' => $edge['relation_type'] ?? '',
  92. 'direction' => $edge['relation_direction'] ?? '',
  93. 'label' => $edge['description'] ?? '',
  94. ];
  95. }
  96. return [
  97. 'nodes' => $formattedNodes,
  98. 'edges' => $formattedEdges,
  99. ];
  100. }
  101. private function loadNodeDetails($kpCode)
  102. {
  103. try {
  104. $service = app(KnowledgeServiceApi::class);
  105. $detail = $service->getKnowledgePointDetail($kpCode);
  106. if ($detail) {
  107. // 获取上游和下游节点(一级子知识点)
  108. $upstream = $service->getUpstreamNodes($kpCode, 1);
  109. $downstream = $service->getDownstreamNodes($kpCode, 1);
  110. // 获取题目统计数据
  111. $questionService = app(QuestionBankService::class);
  112. $stats = $questionService->getKnowledgePointStatistics($kpCode);
  113. // 构建详情数据
  114. $detail['upstream_nodes'] = $upstream['nodes'] ?? [];
  115. $detail['downstream_nodes'] = $downstream['nodes'] ?? [];
  116. $detail['question_stats'] = $stats;
  117. $this->dispatch('nodeDetailsLoaded', details: $detail);
  118. }
  119. } catch (\Exception $e) {
  120. Log::error('加载节点详情失败', [
  121. 'kp_code' => $kpCode,
  122. 'error' => $e->getMessage()
  123. ]);
  124. }
  125. }
  126. public function handleNodeSelected($kpCode)
  127. {
  128. $this->selectedKpCode = $kpCode;
  129. $this->loadNodeDetails($kpCode);
  130. // 刷新图谱数据以高亮选中节点
  131. $this->graphData = $this->formatForVisualization($this->nodes, $this->edges);
  132. }
  133. public function clearSelection()
  134. {
  135. $this->selectedKpCode = null;
  136. $this->graphData = $this->formatForVisualization($this->nodes, $this->edges);
  137. $this->dispatch('nodeDetailsCleared');
  138. }
  139. public function setLayoutType($type)
  140. {
  141. $this->layoutType = $type;
  142. $this->loadGraphData();
  143. }
  144. public function updatedFilterPhase()
  145. {
  146. $this->loadGraphData();
  147. }
  148. public function updatedFilterCategory()
  149. {
  150. $this->loadGraphData();
  151. }
  152. public function getFilterOptionsProperty()
  153. {
  154. $phases = collect($this->nodes)->pluck('phase')->filter()->unique()->sort()->values()->toArray();
  155. $categories = collect($this->nodes)->pluck('category')->filter()->unique()->sort()->values()->toArray();
  156. return [
  157. 'phases' => $phases,
  158. 'categories' => $categories,
  159. ];
  160. }
  161. public function render()
  162. {
  163. return view('livewire.integrations.knowledge-graph-visualization');
  164. }
  165. }