StatController.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. namespace App\Http\Controllers\Admin\User\Log\Login;
  3. use Illuminate\Http\Request;
  4. use App\Http\Controllers\Controller;
  5. use App\Models\LoginLog;
  6. class StatController extends Controller
  7. {
  8. private LoginLog $userLoginLog;
  9. public function __construct(LoginLog $userLoginLog)
  10. {
  11. $this->userLoginLog = $userLoginLog;
  12. }
  13. /**
  14. * 로그인 그래프
  15. * @method GET
  16. * @see /admin/user/log/login/stat
  17. * https://developers.google.com/chart/interactive/docs/basic_load_libs
  18. */
  19. public function index(Request $request)
  20. {
  21. /*
  22. * d: 일별, m: 월별, y: 년도별
  23. */
  24. $dateType = $request->get('date_type', 'd');
  25. if ($dateType !== 'm' && $dateType !== 'y') {
  26. $dateType = 'd';
  27. }
  28. $startDate = $request->get('start_date', date('Y-m-01'));
  29. $endDate = $request->get('end_date', date('Y-m-d'));
  30. $success = $request->get('success', 1);
  31. $startYear = '';
  32. $endYear = '';
  33. $startYearMonth = '';
  34. $endYearMonth = '';
  35. if ($dateType === 'y' || $dateType === 'm') {
  36. $startYear = substr($startDate, 0, 4);
  37. $endYear = substr($endDate, 0, 4);
  38. }
  39. if ($dateType === 'm') {
  40. $startMonth = substr($startDate, 5, 2);
  41. $endMonth = substr($endDate, 5, 2);
  42. $startYearMonth = ($startYear * 12 + $startMonth);
  43. $endYearMonth = ($endYear * 12 + $endMonth);
  44. }
  45. $userLoginLogData = $this->userLoginLog->getLoginSuccessCount($dateType, $startDate, $endDate, $success);
  46. $sumCount = 0;
  47. $arr = [];
  48. $max = 0;
  49. if ($userLoginLogData->total > 0) {
  50. foreach ($userLoginLogData->list as $key => $value) {
  51. $s = $value->day;
  52. if (!isset($arr[$s])) {
  53. $arr[$s] = 0;
  54. }
  55. $arr[$s] += $value->cnt;
  56. if ($arr[$s] > $max) {
  57. $max = $arr[$s];
  58. }
  59. $sumCount += $value->cnt;
  60. }
  61. }
  62. $result = [];
  63. $i = 0;
  64. $no = 0;
  65. $saveCount = -1;
  66. /*
  67. * 통계 데이터 생성
  68. */
  69. if (count($arr) > 0) {
  70. foreach ($arr as $date => $value) {
  71. $count = (int)$arr[$date];
  72. $result[$date]['count'] = $count;
  73. $i++;
  74. if ($saveCount !== $count) {
  75. $no = $i;
  76. $saveCount = $count;
  77. }
  78. $result[$date]['no'] = $no;
  79. $result[$date]['key'] = $date;
  80. $rate = ($count / $sumCount * 100);
  81. $result[$date]['rate'] = $rate;
  82. $sRate = number_format($rate, 1);
  83. $result[$date]['sRate'] = $sRate;
  84. $bar = (int)($count / $max * 100);
  85. $result[$date]['bar'] = $bar;
  86. }
  87. $data['maxValue'] = $max;
  88. $data['sumCount'] = $sumCount;
  89. }
  90. /*
  91. * 빈 배열 초기화
  92. */
  93. if ($dateType === 'y') {
  94. for ($i = $startYear; $i <= $endYear; $i++) {
  95. if (!isset($result[$i])) {
  96. $result[$i] = [
  97. 'count' => 0,
  98. 'no' => 0,
  99. 'key' => 0,
  100. 'rate' => 0,
  101. 'sRate' => 0,
  102. 'bar' => 0,
  103. ];
  104. }
  105. }
  106. } elseif ($dateType === 'm') {
  107. for ($i = $startYearMonth; $i <= $endYearMonth; $i++) {
  108. $year = floor($i / 12);
  109. if ($year * 12 == $i) $year--;
  110. $month = sprintf("%02d", ($i - ($year * 12)));
  111. $date = $year . '-' . $month;
  112. if (!isset($result[$date])) {
  113. $result[$date] = [
  114. 'count' => 0,
  115. 'no' => 0,
  116. 'key' => 0,
  117. 'rate' => 0,
  118. 'sRate' => 0,
  119. 'bar' => 0,
  120. ];
  121. }
  122. }
  123. } elseif ($dateType === 'd') {
  124. $date = $startDate;
  125. while ($date <= $endDate) {
  126. if (!isset($result[$date])) {
  127. $result[$date] = [
  128. 'count' => 0,
  129. 'no' => 0,
  130. 'key' => 0,
  131. 'rate' => 0,
  132. 'sRate' => 0,
  133. 'bar' => 0,
  134. ];
  135. }
  136. $date = date('Y-m-d', strtotime($date) + 86400);
  137. }
  138. }
  139. ksort($result);
  140. $data['result'] = $result;
  141. $data['dateType'] = $dateType;
  142. $data['startDate'] = $startDate;
  143. $data['endDate'] = $endDate;
  144. $data['success'] = $success;
  145. return view('admin.user.log.login.stat', $data);
  146. }
  147. }