KnowledgePointStats.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace App\Filament\Pages\Statistics;
  3. use App\Services\QuestionBankService;
  4. use BackedEnum;
  5. use Filament\Pages\Page;
  6. use UnitEnum;
  7. use Livewire\Attributes\Computed;
  8. use Illuminate\Support\Facades\Cache;
  9. class KnowledgePointStats extends Page
  10. {
  11. protected static ?string $title = '知识点题目统计';
  12. protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-chart-bar';
  13. protected static ?string $navigationLabel = '知识点统计';
  14. protected static string|UnitEnum|null $navigationGroup = null;
  15. protected static ?int $navigationSort = 8;
  16. protected static bool $shouldRegisterNavigation = false;
  17. protected string $view = 'filament.pages.knowledge-point-stats';
  18. public ?string $selectedKpCode = null;
  19. public bool $showDetails = false;
  20. #[Computed(cache: false)]
  21. public function knowledgePointStatistics(): array
  22. {
  23. $cacheKey = 'knowledge-point-statistics';
  24. if ($this->selectedKpCode) {
  25. $cacheKey .= '-' . $this->selectedKpCode;
  26. }
  27. return Cache::remember(
  28. $cacheKey,
  29. now()->addMinutes(10),
  30. function () {
  31. $service = app(QuestionBankService::class);
  32. if ($this->selectedKpCode) {
  33. $stats = $service->getKnowledgePointStatistics($this->selectedKpCode);
  34. return [$stats]; // 返回数组格式
  35. }
  36. return $service->getKnowledgePointStatistics();
  37. }
  38. );
  39. }
  40. #[Computed(cache: false)]
  41. public function knowledgePointOptions(): array
  42. {
  43. try {
  44. $knowledgeApiBase = config('services.knowledge_api.base_url', 'http://localhost:5011');
  45. $response = \Illuminate\Support\Facades\Http::timeout(10)
  46. ->get($knowledgeApiBase . '/graph/export');
  47. if ($response->successful()) {
  48. $data = $response->json();
  49. $nodes = $data['nodes'] ?? [];
  50. $options = [];
  51. foreach ($nodes as $node) {
  52. $code = $node['kp_code'] ?? null;
  53. $name = $node['cn_name'] ?? null;
  54. if ($code && $name) {
  55. $options[$code] = $name;
  56. }
  57. }
  58. ksort($options);
  59. return $options;
  60. }
  61. } catch (\Exception $e) {
  62. \Log::error('Failed to get knowledge points: ' . $e->getMessage());
  63. }
  64. return [];
  65. }
  66. public function updatedSelectedKpCode(): void
  67. {
  68. $this->showDetails = false;
  69. Cache::forget('knowledge-point-statistics-' . $this->selectedKpCode);
  70. }
  71. public function toggleDetails(): void
  72. {
  73. $this->showDetails = !$this->showDetails;
  74. }
  75. public function getTotalQuestions(array $stats): int
  76. {
  77. return $stats['total_questions'] ?? 0;
  78. }
  79. public function getDirectQuestions(array $stats): int
  80. {
  81. return $stats['direct_questions'] ?? 0;
  82. }
  83. public function getChildrenQuestions(array $stats): int
  84. {
  85. return $stats['children_questions'] ?? 0;
  86. }
  87. public function getSkillQuestions(array $stats): int
  88. {
  89. return $stats['skills_total_questions'] ?? 0;
  90. }
  91. }