Passed
Pull Request — master (#232)
by Matias
05:40 queued 04:10
created

StatsCommand::execute()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 16
c 2
b 0
f 0
nc 5
nop 2
dl 0
loc 27
ccs 0
cts 24
cp 0
crap 20
rs 9.7333
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;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Command\Command was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
use Symfony\Component\Console\Helper\Table;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Helper\Table was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
use Symfony\Component\Console\Input\InputOption;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Input\InputOption was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
28
use Symfony\Component\Console\Input\InputInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Input\InputInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
29
use Symfony\Component\Console\Output\OutputInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Output\OutputInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
	protected function configure() {
80
		$this
81
			->setName('face:stats')
82
			->setDescription('Get a summary of statistics images, faces and persons')
83
			->addOption(
84
				'user_id',
85
				'u',
86
				InputOption::VALUE_REQUIRED,
87
				'Get stats for a given user only. If not given, get stats for all users.',
88
				null
89
			)->addOption(
90
				'json',
91
				'j',
92
				InputOption::VALUE_NONE,
93
				'Print in a json format, useful to analyze it with another tool.',
94
				null
95
			);
96
	}
97
98
	/**
99
	 * @param InputInterface $input
100
	 * @param OutputInterface $output
101
	 * @return int
102
	 */
103
	protected function execute(InputInterface $input, OutputInterface $output) {
104
		$users = array();
105
106
		$userId = $input->getOption('user_id');
107
		if (!is_null($userId)) {
108
			if ($this->userManager->get($userId) === null) {
109
				$output->writeln("User with id <$userId> in unknown.");
110
				return 1;
111
			}
112
			else {
113
				$users[] = $userId;
114
			}
115
		}
116
		else {
117
			$this->userManager->callForAllUsers(function (IUser $iUser) use (&$users)  {
118
				$users[] = $iUser->getUID();
119
			});
120
		}
121
122
		if ($input->getOption('json')) {
123
			$this->printJsonStats($output, $users);
124
		}
125
		else {
126
			$this->printTabledStats($output, $users);
127
		}
128
129
		return 0;
130
	}
131
132
	private function printTabledStats(OutputInterface $output, array $users) {
133
134
		$modelId = $this->settingsService->getCurrentFaceModel();
135
136
		$stats = array();
137
		foreach ($users as $user) {
138
			$stats[] = [
139
				$user,
140
				$this->imageMapper->countUserImages($user, $modelId),
141
				$this->faceMapper->countFaces($user, $modelId),
142
				$this->personMapper->countPersons($user, $modelId)
143
			];
144
		}
145
146
		$table = new Table($output);
147
		$table->setHeaders(['User', 'Images', 'Faces', 'Persons'])->setRows($stats);
148
		$table->render();
149
	}
150
151
	private function printJsonStats(OutputInterface $output, array $users) {
152
153
		$modelId = $this->settingsService->getCurrentFaceModel();
154
155
		$stats = array();
156
		foreach ($users as $user) {
157
			$stats[] = array(
158
				'user'    => $user,
159
				'images'  => $this->imageMapper->countUserImages($user, $modelId),
160
				'faces'   => $this->faceMapper->countFaces($user, $modelId),
161
				'persons' => $this->personMapper->countPersons($user, $modelId)
162
			);
163
		}
164
165
		$output->writeln(json_encode($stats));
166
	}
167
168
}
169