KnowledgeGraphComponent.php 5.4 KB

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