KnowledgePointStats.php 3.2 KB

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