Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function titleGenerator($startYear, $startMonth, $endYear, $endMonth) {
- for ($y = $startYear; $y <= $endYear; $y++) {
- for ($m = 1; $m <= 12; $m ++) {
- if( $m < $startMonth && $y == $startYear ) continue; // Prevents months before first
- yield sprintf( "%d.%02d", $y, $m ); // Yield manages to catch last title
- if( $m == $endMonth && $y == $endYear ) break 2; // Bye~
- }
- }
- }
- public function statistics_activity()
- {
- $raw = "SELECT DATE_FORMAT(created_at, '%Y') as year, DATE_FORMAT(created_at, '%m') as month, action, count(id) as count
- FROM `logs`
- WHERE `type` = 'heroes' AND action IN (1,2,3)
- group by year, month, action";
- $logs = DB::connection()->select( DB::raw($raw) );
- $firstLog = Log::first();
- $lastLog = Log::orderBy('created_at', 'desc')->first();
- $startYear = $firstLog->created_at->year;
- $startMonth = $firstLog->created_at->month;
- $endYear = $lastLog->created_at->year;
- $endMonth = $lastLog->created_at->month;
- $labels = iterator_to_array($this->titleGenerator($startYear, $startMonth, $endYear, $endMonth));
- $zeros = array_map(function($value) { return 0; }, $labels);
- $datasets = [
- [
- 'label' => "Создано",
- 'data' => $zeros,
- 'backgroundColor' => 'limegreen'
- ],
- [
- 'label' => "Изменено",
- 'data' => $zeros,
- 'backgroundColor' => 'royalblue'
- ],
- [
- 'label' => "Удалено",
- 'data' => $zeros,
- 'backgroundColor' => 'crimson'
- ],
- ];
- foreach($logs as $logBlock) {
- $position = $logBlock->action - 1;
- $title = "$logBlock->year.$logBlock->month";
- $indexMonth = array_search($title, $labels);
- $datasets[$position]['data'][$indexMonth] = $logBlock->count;
- }
- $result = [
- 'labels' => $labels,
- 'datasets' => $datasets
- ];
- return json_encode( $result, JSON_UNESCAPED_UNICODE );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement