Completed
Pull Request — master (#168)
by Deven
02:30
created

StatsShell::initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
crap 1
1
<?php
2
3
/* vim: set expandtab sw=4 ts=4 sts=4: */
4
/**
5
 * Stats Shell.
6
 *
7
 * phpMyAdmin Error reporting server
8
 * Copyright (c) phpMyAdmin project (https://www.phpmyadmin.net/)
9
 *
10
 * Licensed under The MIT License
11
 * For full copyright and license information, please see the LICENSE.txt
12
 * Redistributions of files must retain the above copyright notice.
13
 *
14
 * @copyright Copyright (c) phpMyAdmin project (https://www.phpmyadmin.net/)
15
 * @license   https://opensource.org/licenses/mit-license.php MIT License
16
 *
17
 * @see      https://www.phpmyadmin.net/
18
 */
19
20
namespace App\Shell;
21
22
use Cake\Cache\Cache;
23
use Cake\Console\Shell;
24
25
/**
26
 * Stats shell.
27
 */
28
class StatsShell extends Shell
29
{
30 1
    public function initialize()
31
    {
32 1
        parent::initialize();
33 1
        $this->loadModel('Incidents');
34 1
        $this->loadModel('Reports');
35 1
    }
36
37 1
    public function main()
38
    {
39 1
        foreach ($this->Incidents->filterTimes as $filter_string => $filter) {
40 1
            foreach ($this->Incidents->summarizableFields as $field) {
41 1
                $this->out('processing ' . $filter_string . ':' . $field);
42 1
                $entriesWithCount = $this->Reports->
43 1
                        getRelatedByField($field, 25, false, false, $filter['limit']);
44 1
                $entriesWithCount = json_encode($entriesWithCount);
45 1
                Cache::write($field . '_' . $filter_string, $entriesWithCount);
46
            }
47
            $query = array(
48 1
                'group' => 'grouped_by',
49
                'order' => 'Incidents.created',
50
            );
51 1
            if (isset($filter['limit'])) {
52 1
                $query['conditions'] = array(
53 1
                    'Incidents.created >=' => $filter['limit'],
54
                );
55
            }
56 1
            $downloadStats = $this->Incidents->find('all', $query);
57 1
            $downloadStats->select(array(
58 1
                'grouped_by' => $filter['group'],
59 1
                'date' => "DATE_FORMAT(Incidents.created, '%a %b %d %Y %T')",
60 1
                'count' => $downloadStats->func()->count('*'),
61
            ));
62 1
            $downloadStats = json_encode($downloadStats->toArray());
63 1
            Cache::write('downloadStats_' . $filter_string, $downloadStats);
64
        }
65 1
    }
66
}
67