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

StatsShell::defaultName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Stats Shell.
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\Shell;
20
21
use Cake\Cache\Cache;
22
use Cake\Command\Command;
23
use Cake\Console\Arguments;
24
use Cake\Console\ConsoleIo;
25
use Cake\Console\ConsoleOptionParser;
26
27
use function json_encode;
28
use App\Model\Table\IncidentsTable;
29
use App\Model\Table\ReportsTable;
30
31
/**
32
 * Stats shell.
33
 *
34
 * @property IncidentsTable $Incidents
35
 * @property ReportsTable $Reports
36
 */
37
class StatsShell extends Command
38
{
39
    protected const NAME = 'stats';
40
41
    /**
42
     * The name of this command.
43
     *
44
     * @var string
45
     */
46
    protected $name = self::NAME;
47
48 21
    public static function defaultName(): string
49
    {
50 21
        return self::NAME;
51
    }
52
53 7
    protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser
54
    {
55
        return $parser
56 7
            ->setCommand($this->name)
57 7
            ->setDescription('Build stats');
58
    }
59
60 7
    public function initialize(): void
61
    {
62 7
        parent::initialize();
63 7
        $this->loadModel('Incidents');
64 7
        $this->loadModel('Reports');
65 7
    }
66
67 7
    public function execute(Arguments $args, ConsoleIo $io)
68
    {
69 7
        foreach ($this->Incidents->filterTimes as $filter_string => $filter) {
70 7
            foreach ($this->Incidents->summarizableFields as $field) {
71 7
                $io->out('processing ' . $filter_string . ':' . $field);
72 7
                $entriesWithCount = $this->Reports->
73 7
                        getRelatedByField($field, 25, false, false, $filter['limit']);
74 7
                $entriesWithCount = json_encode($entriesWithCount);
75 7
                Cache::write($field . '_' . $filter_string, $entriesWithCount);
76
            }
77 2
            $query = [
78 5
                'group' => 'grouped_by',
79
                'order' => 'Incidents.created',
80
            ];
81 7
            if (isset($filter['limit'])) {
82 7
                $query['conditions'] = [
83 7
                    'Incidents.created >=' => $filter['limit'],
84
                ];
85
            }
86 7
            $downloadStats = $this->Incidents->find('all', $query);
87 7
            $downloadStats->select([
88 7
                'grouped_by' => $filter['group'],
89 7
                'date' => "DATE_FORMAT(Incidents.created, '%a %b %d %Y %T')",
90 7
                'count' => $downloadStats->func()->count('*'),
91
            ]);
92 7
            $downloadStats = json_encode($downloadStats->toArray());
93 7
            Cache::write('downloadStats_' . $filter_string, $downloadStats);
94
        }
95 7
    }
96
}
97