Passed
Push — master ( 083ff7...c84473 )
by William
03:02
created

StatsController::initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
ccs 0
cts 5
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Stats controller handling stats preview.
5
 *
6
 * phpMyAdmin Error reporting server
7
 * Copyright (c) phpMyAdmin project (https://www.phpmyadmin.net/)
8
 *
9
 * Licensed under The MIT License
10
 * For full copyright and license information, please see the LICENSE.txt
11
 * Redistributions of files must retain the above copyright notice.
12
 *
13
 * @copyright Copyright (c) phpMyAdmin project (https://www.phpmyadmin.net/)
14
 * @license   https://opensource.org/licenses/mit-license.php MIT License
15
 *
16
 * @see      https://www.phpmyadmin.net/
17
 */
18
19
namespace App\Controller;
20
21
use Cake\Cache\Cache;
22
use Cake\ORM\TableRegistry;
23
use function json_decode;
24
use function json_encode;
25
use App\Model\Table\IncidentsTable;
26
27
/**
28
 * Stats controller handling stats preview.
29
 *
30
 * @property IncidentsTable $Incidents
31
 */
32
class StatsController extends AppController
33
{
34
    /**
35
     * Initialization hook method.
36
     *
37
     * Use this method to add common initialization code like loading components.
38
     *
39
     * @return void Nothing
40
     */
41
    public function initialize(): void
42
    {
43
        parent::initialize();
44
        $this->viewBuilder()->setHelpers(['Reports']);
45
        $this->loadModel('Incidents');
46
    }
47
48
    public function stats(): void
49
    {
50
        $filter = $this->getTimeFilter();
51
        $relatedEntries = [];
52
        $filter_string = $this->request->getQuery('filter');
53
        if (! $filter_string) {
54
            $filter_string = 'all_time';
55
        }
56
        $entriesWithCount = [];
57
        //Cache::clear();
58
        foreach ($this->Incidents->summarizableFields as $field) {
59
            $entriesWithCount = Cache::read($field . '_' . $filter_string);
60
            if ($entriesWithCount === false) {
61
                $entriesWithCount = TableRegistry::getTableLocator()->get('Reports')->
62
                        getRelatedByField($field, 25, false, false, $filter['limit']);
63
                $entriesWithCount = json_encode($entriesWithCount);
64
                Cache::write($field . '_' . $filter_string, $entriesWithCount);
65
            }
66
            $relatedEntries[$field] = json_decode($entriesWithCount, true);
67
        }
68
        $this->set('related_entries', $relatedEntries);
69
        $this->set('columns', $this->Incidents->summarizableFields);
70
        $this->set('filter_times', $this->Incidents->filterTimes);
71
        $this->set('selected_filter', $this->request->getQuery('filter'));
72
73
        $query = [
74
            'group' => 'grouped_by',
75
            'order' => 'Incidents.created',
76
        ];
77
78
        if (isset($filter['limit'])) {
79
            $query['conditions'] = [
80
                'Incidents.created >=' => $filter['limit'],
81
            ];
82
        }
83
84
        $this->Incidents->recursive = -1;
85
        $downloadStats = Cache::read('downloadStats_' . $filter_string);
86
        if ($downloadStats === false) {
87
            $downloadStats = $this->Incidents->find('all', $query);
88
            $downloadStats->select([
89
                'grouped_by' => $filter['group'],
90
                'date' => "DATE_FORMAT(Incidents.created, '%a %b %d %Y %T')",
91
                'count' => $downloadStats->func()->count('*'),
92
            ]);
93
            $downloadStats = json_encode($downloadStats->toArray());
94
            Cache::write('downloadStats_' . $filter_string, $downloadStats);
95
        }
96
        $this->set('download_stats', json_decode($downloadStats, true));
97
    }
98
99
    /**
100
     * @return mixed I am not sure about the type
101
     */
102
    protected function getTimeFilter()
103
    {
104
        if ($this->request->getQuery('filter')) {
105
            $filter = $this->Incidents->filterTimes[$this->request->getQuery('filter')];
106
        }
107
        if (isset($filter)) {
108
            return $filter;
109
        }
110
111
        return $this->Incidents->filterTimes['all_time'];
112
    }
113
}
114