StatsController   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 43
dl 0
loc 85
ccs 0
cts 61
cp 0
rs 10
c 1
b 1
f 0
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getTimeFilter() 0 11 3
A initialize() 0 5 1
B stats() 0 53 6
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 App\Model\Table\IncidentsTable;
22
use Cake\Cache\Cache;
23
use Cake\ORM\TableRegistry;
24
25
use function json_decode;
26
use function json_encode;
27
28
/**
29
 * Stats controller handling stats preview.
30
 *
31
 * @property IncidentsTable $Incidents
32
 */
33
class StatsController extends AppController
34
{
35
    /**
36
     * Initialization hook method.
37
     *
38
     * Use this method to add common initialization code like loading components.
39
     *
40
     * @return void Nothing
41
     */
42
    public function initialize(): void
43
    {
44
        parent::initialize();
45
        $this->viewBuilder()->setHelpers(['Reports']);
46
        $this->loadModel('Incidents');
47
    }
48
49
    public function stats(): void
50
    {
51
        $filter = $this->getTimeFilter();
52
        $relatedEntries = [];
53
        $filter_string = $this->request->getQuery('filter');
54
        if (! $filter_string) {
55
            $filter_string = 'all_time';
56
        }
57
58
        $entriesWithCount = [];
59
        //Cache::clear();
60
        foreach ($this->Incidents->summarizableFields as $field) {
61
            $entriesWithCount = Cache::read($field . '_' . $filter_string);
62
            if ($entriesWithCount === null) {
63
                $entriesWithCount = TableRegistry::getTableLocator()->get('Reports')->
64
                        getRelatedByField($field, 25, false, false, $filter['limit']);
65
                $entriesWithCount = json_encode($entriesWithCount);
66
                Cache::write($field . '_' . $filter_string, $entriesWithCount);
67
            }
68
69
            $relatedEntries[$field] = json_decode($entriesWithCount, true);
70
        }
71
72
        $this->set('related_entries', $relatedEntries);
73
        $this->set('columns', $this->Incidents->summarizableFields);
74
        $this->set('filter_times', $this->Incidents->filterTimes);
75
        $this->set('selected_filter', $this->request->getQuery('filter'));
76
77
        $query = [
78
            'group' => 'grouped_by',
79
            'order' => 'Incidents.created',
80
        ];
81
82
        if (isset($filter['limit'])) {
83
            $query['conditions'] = [
84
                'Incidents.created >=' => $filter['limit'],
85
            ];
86
        }
87
88
        $this->Incidents->recursive = -1;
89
        $downloadStats = Cache::read('downloadStats_' . $filter_string);
90
        if ($downloadStats === null) {
91
            $downloadStats = $this->Incidents->find('all', $query);
92
            $downloadStats->select([
93
                'grouped_by' => $filter['group'],
94
                'date' => "DATE_FORMAT(Incidents.created, '%a %b %d %Y %T')",
95
                'count' => $downloadStats->func()->count('*'),
96
            ]);
97
            $downloadStats = json_encode($downloadStats->toArray());
98
            Cache::write('downloadStats_' . $filter_string, $downloadStats);
99
        }
100
101
        $this->set('download_stats', json_decode($downloadStats, true));
102
    }
103
104
    /**
105
     * @return mixed I am not sure about the type
106
     */
107
    protected function getTimeFilter()
108
    {
109
        if ($this->request->getQuery('filter')) {
110
            $filter = $this->Incidents->filterTimes[$this->request->getQuery('filter')];
111
        }
112
113
        if (isset($filter)) {
114
            return $filter;
115
        }
116
117
        return $this->Incidents->filterTimes['all_time'];
118
    }
119
}
120