|
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 App\Model\Table\IncidentsTable; |
|
22
|
|
|
use App\Model\Table\ReportsTable; |
|
23
|
|
|
use Cake\Cache\Cache; |
|
24
|
|
|
use Cake\Command\Command; |
|
25
|
|
|
use Cake\Console\Arguments; |
|
26
|
|
|
use Cake\Console\ConsoleIo; |
|
27
|
|
|
use Cake\Console\ConsoleOptionParser; |
|
28
|
|
|
|
|
29
|
|
|
use function json_encode; |
|
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
|
|
|
|
|
78
|
2 |
|
$query = [ |
|
79
|
5 |
|
'group' => 'grouped_by', |
|
80
|
|
|
'order' => 'Incidents.created', |
|
81
|
|
|
]; |
|
82
|
7 |
|
if (isset($filter['limit'])) { |
|
83
|
7 |
|
$query['conditions'] = [ |
|
84
|
7 |
|
'Incidents.created >=' => $filter['limit'], |
|
85
|
|
|
]; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
7 |
|
$downloadStats = $this->Incidents->find('all', $query); |
|
89
|
7 |
|
$downloadStats->select([ |
|
90
|
7 |
|
'grouped_by' => $filter['group'], |
|
91
|
7 |
|
'date' => "DATE_FORMAT(Incidents.created, '%a %b %d %Y %T')", |
|
92
|
7 |
|
'count' => $downloadStats->func()->count('*'), |
|
93
|
|
|
]); |
|
94
|
7 |
|
$downloadStats = json_encode($downloadStats->toArray()); |
|
95
|
7 |
|
Cache::write('downloadStats_' . $filter_string, $downloadStats); |
|
96
|
|
|
} |
|
97
|
7 |
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|