KnowledgeGraphComponent.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. namespace App\Livewire\Integrations;
  3. use Livewire\Component;
  4. use App\Services\KnowledgeServiceApi;
  5. use Illuminate\Support\Facades\Http;
  6. use Illuminate\Support\Facades\Log;
  7. class KnowledgeGraphComponent extends Component
  8. {
  9. public $selectedKpCode = null;
  10. public $graphData = [];
  11. public $nodes = [];
  12. public $edges = [];
  13. public $isLoading = false;
  14. public $showStats = false;
  15. public $statsData = [];
  16. protected $listeners = [
  17. 'kpSelected' => 'handleKpSelected',
  18. 'refreshGraph' => 'refreshGraph',
  19. ];
  20. public function mount($selectedKpCode = null)
  21. {
  22. $this->selectedKpCode = $selectedKpCode;
  23. $this->loadGraphData();
  24. }
  25. public function loadGraphData()
  26. {
  27. $this->isLoading = true;
  28. try {
  29. $service = app(KnowledgeServiceApi::class);
  30. // 获取完整图谱数据
  31. if ($this->selectedKpCode) {
  32. // 如果指定了知识点,获取该点的详细信息
  33. $nodeData = $service->getKnowledgePointDetail($this->selectedKpCode);
  34. $upstream = $service->getUpstreamNodes($this->selectedKpCode, 2);
  35. $downstream = $service->getDownstreamNodes($this->selectedKpCode, 2);
  36. $this->buildSelectedGraph($nodeData, $upstream, $downstream);
  37. } else {
  38. // 获取完整图谱
  39. $this->buildFullGraph();
  40. }
  41. // 获取统计信息
  42. $this->loadStatsData();
  43. } catch (\Exception $e) {
  44. Log::error('加载知识图谱失败', [
  45. 'kp_code' => $this->selectedKpCode,
  46. 'error' => $e->getMessage()
  47. ]);
  48. $this->dispatch('error', message: '加载知识图谱失败:' . $e->getMessage());
  49. }
  50. $this->isLoading = false;
  51. }
  52. private function buildFullGraph()
  53. {
  54. try {
  55. $knowledgeApiBase = config('services.knowledge_api.base_url', 'http://localhost:5011');
  56. $response = Http::timeout(10)
  57. ->get($knowledgeApiBase . '/graph/export');
  58. if ($response->successful()) {
  59. $data = $response->json();
  60. $this->nodes = $data['nodes'] ?? [];
  61. $this->edges = $data['edges'] ?? [];
  62. // 转换为可视化格式
  63. $this->graphData = $this->formatForVisualization($this->nodes, $this->edges);
  64. }
  65. } catch (\Exception $e) {
  66. Log::error('获取完整图谱失败', ['error' => $e->getMessage()]);
  67. }
  68. }
  69. private function buildSelectedGraph($nodeData, $upstream, $downstream)
  70. {
  71. // 构建以选中节点为中心的图谱
  72. $this->nodes = [$nodeData];
  73. $this->edges = [];
  74. // 添加上游节点
  75. if (isset($upstream['nodes'])) {
  76. $this->nodes = array_merge($this->nodes, $upstream['nodes']);
  77. }
  78. // 添加下游节点
  79. if (isset($downstream['nodes'])) {
  80. $this->nodes = array_merge($this->nodes, $downstream['nodes']);
  81. }
  82. // 转换为可视化格式
  83. $this->graphData = $this->formatForVisualization($this->nodes, $this->edges);
  84. }
  85. private function formatForVisualization($nodes, $edges)
  86. {
  87. $formattedNodes = [];
  88. $formattedEdges = [];
  89. foreach ($nodes as $node) {
  90. $formattedNodes[] = [
  91. 'id' => $node['kp_code'],
  92. 'label' => $node['cn_name'] ?? $node['kp_code'],
  93. 'kp_code' => $node['kp_code'],
  94. 'cn_name' => $node['cn_name'] ?? '',
  95. 'en_name' => $node['en_name'] ?? '',
  96. 'category' => $node['category'] ?? '',
  97. 'phase' => $node['phase'] ?? '',
  98. 'importance' => $node['importance'] ?? 0,
  99. 'description' => $node['description'] ?? '',
  100. 'skills_count' => count($node['skills'] ?? []),
  101. ];
  102. }
  103. foreach ($edges as $edge) {
  104. $formattedEdges[] = [
  105. 'from' => $edge['source'],
  106. 'to' => $edge['target'],
  107. 'type' => $edge['relation_type'] ?? '',
  108. 'direction' => $edge['relation_direction'] ?? '',
  109. 'label' => $edge['description'] ?? '',
  110. ];
  111. }
  112. return [
  113. 'nodes' => $formattedNodes,
  114. 'edges' => $formattedEdges,
  115. ];
  116. }
  117. private function loadStatsData()
  118. {
  119. try {
  120. $questionBankBase = config('services.question_bank.base_url', 'http://localhost:5015');
  121. $response = Http::timeout(10)
  122. ->get($questionBankBase . '/api/questions/statistics');
  123. if ($response->successful()) {
  124. $data = $response->json();
  125. $this->statsData = $data['by_kp'] ?? [];
  126. // 为节点添加题目数量信息
  127. foreach ($this->graphData['nodes'] as &$node) {
  128. $kpCode = $node['kp_code'];
  129. $stat = collect($this->statsData)->firstWhere('kp_code', $kpCode);
  130. $node['question_count'] = $stat['question_count'] ?? 0;
  131. $node['skills_list'] = $stat['skills_list'] ?? [];
  132. }
  133. }
  134. } catch (\Exception $e) {
  135. Log::error('获取统计数据失败', ['error' => $e->getMessage()]);
  136. }
  137. }
  138. public function handleKpSelected($kpCode)
  139. {
  140. $this->selectedKpCode = $kpCode;
  141. $this->loadGraphData();
  142. }
  143. public function refreshGraph()
  144. {
  145. $this->loadGraphData();
  146. }
  147. public function toggleStats()
  148. {
  149. $this->showStats = !$this->showStats;
  150. }
  151. public function render()
  152. {
  153. return view('livewire.integrations.knowledge-graph-component', [
  154. 'graphData' => $this->graphData,
  155. ]);
  156. }
  157. }