Просмотр файла app/Http/Controllers/CounterController.php

Размер файла: 1.63Kb
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace App\Http\Controllers;
  6.  
  7. use App\Models\Counter24;
  8. use App\Models\Counter31;
  9. use Illuminate\View\View;
  10.  
  11. class CounterController extends Controller
  12. {
  13. /**
  14. * Главная страница
  15. *
  16. * @return View
  17. */
  18. public function index(): View
  19. {
  20. $count = statsCounter();
  21. $online = statsOnline();
  22.  
  23. $counts31 = [];
  24. $counters = Counter31::query()
  25. ->orderByDesc('period')
  26. ->limit(30)
  27. ->get();
  28.  
  29. for ($i = 0; $i < 30; $i++) {
  30. $curDate = date('Y-m-d 00:00:00', strtotime("-$i day", SITETIME));
  31.  
  32. $cnt = $counters->first(static function ($item) use ($curDate) {
  33. return $item->period === $curDate;
  34. });
  35.  
  36. $counts31['hits'][] = $cnt->hits ?? 0;
  37. $counts31['hosts'][] = $cnt->hosts ?? 0;
  38. $counts31['labels'][] = date('M j', strtotime($curDate));
  39. }
  40.  
  41. $counts24 = [];
  42. $counters = Counter24::query()
  43. ->orderByDesc('period')
  44. ->limit(24)
  45. ->get();
  46.  
  47. for ($i = 0; $i < 24; $i++) {
  48. $curHour = date('Y-m-d H:00:00', strtotime("-$i hour", SITETIME));
  49.  
  50. $cnt = $counters->first(static function ($item) use ($curHour) {
  51. return $item->period === $curHour;
  52. });
  53.  
  54. $counts24['hits'][] = $cnt->hits ?? 0;
  55. $counts24['hosts'][] = $cnt->hosts ?? 0;
  56. $counts24['labels'][] = date('H', strtotime($curHour));
  57. }
  58.  
  59. return view('counters/index', compact('count', 'online', 'counts24', 'counts31'));
  60. }
  61. }