nananako

Activity

Apr 9th, 2021 (edited)
854
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function titleGenerator($startYear, $startMonth, $endYear, $endMonth) {
  2.         for ($y = $startYear; $y <= $endYear; $y++) {
  3.             for ($m = 1; $m <= 12; $m ++) {
  4.                 if( $m < $startMonth && $y == $startYear ) continue; // Prevents months before first
  5.                 yield sprintf( "%d.%02d", $y, $m );                  // Yield manages to catch last title
  6.                 if( $m == $endMonth && $y == $endYear ) break 2;     // Bye~
  7.             }
  8.         }
  9.     }
  10.  
  11.  
  12.     public function statistics_activity()
  13.     {
  14.         $raw = "SELECT DATE_FORMAT(created_at, '%Y') as year, DATE_FORMAT(created_at, '%m') as month, action, count(id) as count
  15.        FROM `logs`
  16.        WHERE `type` = 'heroes' AND action IN (1,2,3)
  17.        group by year, month, action";
  18.  
  19.         $logs = DB::connection()->select( DB::raw($raw) );
  20.  
  21.  
  22.         $firstLog = Log::first();
  23.         $lastLog  = Log::orderBy('created_at', 'desc')->first();
  24.  
  25.         $startYear  = $firstLog->created_at->year;
  26.         $startMonth = $firstLog->created_at->month;
  27.  
  28.         $endYear  = $lastLog->created_at->year;
  29.         $endMonth = $lastLog->created_at->month;
  30.  
  31.         $labels = iterator_to_array($this->titleGenerator($startYear, $startMonth, $endYear, $endMonth));
  32.  
  33.  
  34.         $zeros = array_map(function($value) { return 0; }, $labels);
  35.         $datasets = [
  36.             [
  37.                 'label' => "Создано",
  38.                 'data' => $zeros,
  39.                 'backgroundColor' => 'limegreen'
  40.             ],
  41.             [
  42.                 'label' => "Изменено",
  43.                 'data' => $zeros,
  44.                 'backgroundColor' => 'royalblue'
  45.             ],
  46.             [
  47.                 'label' => "Удалено",
  48.                 'data' => $zeros,
  49.                 'backgroundColor' => 'crimson'
  50.             ],
  51.         ];
  52.  
  53.  
  54.         foreach($logs as $logBlock) {
  55.             $position = $logBlock->action - 1;
  56.             $title = "$logBlock->year.$logBlock->month";
  57.             $indexMonth = array_search($title, $labels);
  58.  
  59.             $datasets[$position]['data'][$indexMonth] = $logBlock->count;
  60.         }
  61.  
  62.  
  63.         $result = [
  64.             'labels'   => $labels,
  65.             'datasets' => $datasets
  66.         ];
  67.  
  68.         return json_encode( $result, JSON_UNESCAPED_UNICODE );
  69.     }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×