1 | <?php |
||
2 | /** |
||
3 | * @copyright Copyright (c) 2020, Matias De lellis <[email protected]> |
||
4 | * |
||
5 | * @author Matias De lellis <[email protected]> |
||
6 | * |
||
7 | * @license GNU AGPL version 3 or any later version |
||
8 | * |
||
9 | * This program is free software: you can redistribute it and/or modify |
||
10 | * it under the terms of the GNU Affero General Public License as |
||
11 | * published by the Free Software Foundation, either version 3 of the |
||
12 | * License, or (at your option) any later version. |
||
13 | * |
||
14 | * This program is distributed in the hope that it will be useful, |
||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
17 | * GNU Affero General Public License for more details. |
||
18 | * |
||
19 | * You should have received a copy of the GNU Affero General Public License |
||
20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
||
21 | * |
||
22 | */ |
||
23 | namespace OCA\FaceRecognition\Command; |
||
24 | |||
25 | use Symfony\Component\Console\Command\Command; |
||
26 | use Symfony\Component\Console\Helper\Table; |
||
27 | use Symfony\Component\Console\Input\InputOption; |
||
28 | use Symfony\Component\Console\Input\InputInterface; |
||
29 | use Symfony\Component\Console\Output\OutputInterface; |
||
30 | |||
31 | use OCP\IUser; |
||
32 | use OCP\IUserManager; |
||
33 | |||
34 | use OCA\FaceRecognition\Db\ImageMapper; |
||
35 | use OCA\FaceRecognition\Db\FaceMapper; |
||
36 | use OCA\FaceRecognition\Db\PersonMapper; |
||
37 | |||
38 | use OCA\FaceRecognition\Service\SettingsService; |
||
39 | |||
40 | class StatsCommand extends Command { |
||
41 | |||
42 | /** @var IUserManager */ |
||
43 | protected $userManager; |
||
44 | |||
45 | /** @var ImageMapper */ |
||
46 | protected $imageMapper; |
||
47 | |||
48 | /** @var FaceMapper */ |
||
49 | protected $faceMapper; |
||
50 | |||
51 | /** @var PersonMapper */ |
||
52 | protected $personMapper; |
||
53 | |||
54 | /** @var SettingsService */ |
||
55 | private $settingsService; |
||
56 | |||
57 | /** |
||
58 | * @param IUserManager $userManager |
||
59 | * @param ImageMapper $imageMapper |
||
60 | * @param FaceMapper $faceMapper |
||
61 | * @param PersonMapper $personMapper |
||
62 | * @param SettingsService $settingsService |
||
63 | */ |
||
64 | public function __construct(IUserManager $userManager, |
||
65 | ImageMapper $imageMapper, |
||
66 | FaceMapper $faceMapper, |
||
67 | PersonMapper $personMapper, |
||
68 | SettingsService $settingsService) |
||
69 | { |
||
70 | parent::__construct(); |
||
71 | |||
72 | $this->userManager = $userManager; |
||
73 | $this->imageMapper = $imageMapper; |
||
74 | $this->faceMapper = $faceMapper; |
||
75 | $this->personMapper = $personMapper; |
||
76 | $this->settingsService = $settingsService; |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * @return void |
||
81 | */ |
||
82 | protected function configure() { |
||
83 | $this |
||
84 | ->setName('face:stats') |
||
85 | ->setDescription('Get a summary of statistics images, faces and persons') |
||
86 | ->addOption( |
||
87 | 'user_id', |
||
88 | 'u', |
||
89 | InputOption::VALUE_REQUIRED, |
||
90 | 'Get stats for a given user only. If not given, get stats for all users.', |
||
91 | null |
||
92 | )->addOption( |
||
93 | 'json', |
||
94 | 'j', |
||
95 | InputOption::VALUE_NONE, |
||
96 | 'Print in a json format, useful to analyze it with another tool.', |
||
97 | null |
||
98 | ); |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * @param InputInterface $input |
||
103 | * @param OutputInterface $output |
||
104 | * @return int |
||
105 | */ |
||
106 | protected function execute(InputInterface $input, OutputInterface $output) { |
||
107 | $users = array(); |
||
108 | |||
109 | $userId = $input->getOption('user_id'); |
||
110 | if (!is_null($userId)) { |
||
111 | if ($this->userManager->get($userId) === null) { |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
112 | $output->writeln("User with id <$userId> in unknown."); |
||
113 | return 1; |
||
114 | } |
||
115 | else { |
||
116 | $users[] = $userId; |
||
117 | } |
||
118 | } |
||
119 | else { |
||
120 | $this->userManager->callForAllUsers(function (IUser $iUser) use (&$users) { |
||
121 | $users[] = $iUser->getUID(); |
||
122 | }); |
||
123 | } |
||
124 | |||
125 | if ($input->getOption('json')) { |
||
126 | $this->printJsonStats($output, $users); |
||
127 | } |
||
128 | else { |
||
129 | $this->printTabledStats($output, $users); |
||
130 | } |
||
131 | |||
132 | return 0; |
||
133 | } |
||
134 | |||
135 | private function printTabledStats(OutputInterface $output, array $users): void { |
||
136 | |||
137 | $modelId = $this->settingsService->getCurrentFaceModel(); |
||
138 | |||
139 | $stats = array(); |
||
140 | foreach ($users as $user) { |
||
141 | $stats[] = [ |
||
142 | $user, |
||
143 | $this->imageMapper->countUserImages($user, $modelId), |
||
144 | $this->imageMapper->countUserImages($user, $modelId, true), |
||
145 | $this->faceMapper->countFaces($user, $modelId), |
||
146 | $this->personMapper->countClusters($user, $modelId), |
||
147 | $this->personMapper->countPersons($user, $modelId) |
||
148 | ]; |
||
149 | } |
||
150 | |||
151 | $table = new Table($output); |
||
152 | $table->setHeaders(['User', 'Images', 'Processed', 'Faces', 'Clusters', 'Persons'])->setRows($stats); |
||
153 | $table->render(); |
||
154 | } |
||
155 | |||
156 | private function printJsonStats(OutputInterface $output, array $users): void { |
||
157 | |||
158 | $modelId = $this->settingsService->getCurrentFaceModel(); |
||
159 | |||
160 | $stats = array(); |
||
161 | foreach ($users as $user) { |
||
162 | $stats[] = array( |
||
163 | 'user' => $user, |
||
164 | 'images' => $this->imageMapper->countUserImages($user, $modelId), |
||
165 | 'processed'=> $this->imageMapper->countUserImages($user, $modelId, true), |
||
166 | 'faces' => $this->faceMapper->countFaces($user, $modelId), |
||
167 | 'clusters' => $this->personMapper->countClusters($user, $modelId), |
||
168 | 'persons' => $this->personMapper->countPersons($user, $modelId) |
||
169 | ); |
||
170 | } |
||
171 | |||
172 | $output->writeln(json_encode($stats)); |
||
173 | } |
||
174 | |||
175 | } |
||
176 |