Passed
Pull Request — master (#534)
by Matias
07:42 queued 05:25
created

SetupCommand::setupAssignedMemory()   A

Complexity

Conditions 6
Paths 3

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 18
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 26
ccs 0
cts 19
cp 0
crap 42
rs 9.0444
1
<?php
2
/**
3
 * @copyright Copyright (c) 2019-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 OCA\FaceRecognition\Model\IModel;
32
33
use OCA\FaceRecognition\Model\ModelManager;
34
35
use OCA\FaceRecognition\Service\SettingsService;
36
use OCA\FaceRecognition\Helper\MemoryLimits;
37
38
use OCP\Util as OCP_Util;
39
40
class SetupCommand extends Command {
41
42
	/** @var ModelManager */
43
	protected $modelManager;
44
45
	/** @var SettingsService */
46
	private $settingsService;
47
48
	/** @var OutputInterface */
49
	protected $logger;
50
51
	/**
52
	 * @param ModelManager $modelManager
53
	 * @param SettingsService $settingsService
54
	 */
55
	public function __construct(ModelManager    $modelManager,
56
	                            SettingsService $settingsService)
57
	{
58
		parent::__construct();
59
60
		$this->modelManager    = $modelManager;
61
		$this->settingsService = $settingsService;
62
	}
63
64
	/**
65
	 * @return void
66
	 */
67
	protected function configure() {
68
		$this
69
			->setName('face:setup')
70
			->setDescription('Basic application settings, such as maximum memory, and the model used.')
71
			->addOption(
72
				'memory',
73
				'M',
74
				InputOption::VALUE_REQUIRED,
75
				'The maximum memory assigned for image processing',
76
				-1
77
			)
78
			->addOption(
79
				'model',
80
				'm',
81
				InputOption::VALUE_REQUIRED,
82
				'The identifier number of the model to install',
83
				-1
84
			);
85
	}
86
87
	/**
88
	 * @param InputInterface $input
89
	 * @param OutputInterface $output
90
	 * @return int
91
	 */
92
	protected function execute(InputInterface $input, OutputInterface $output) {
93
		$this->logger = $output;
94
95
		$assignMemory = $input->getOption('memory');
96
		if ($assignMemory > 0) {
97
			return $this->setupAssignedMemory(OCP_Util::computerFileSize($assignMemory));
98
		}
99
100
		$modelId = $input->getOption('model');
101
		if ($modelId > 0) {
102
			return $this->setupModel($modelId);
103
		}
104
105
		$this->dumpCurrentSetup();
106
107
		return 0;
108
	}
109
110
	private function setupAssignedMemory ($assignMemory): int {
111
		$systemMemory = MemoryLimits::getSystemMemory();
112
		$this->logger->writeln("System memory: " . ($systemMemory > 0 ? $this->getHumanMemory($systemMemory) : "Unknown"));
113
		$phpMemory = MemoryLimits::getPhpMemory();
114
		$this->logger->writeln("Memory assigned to PHP: " . ($phpMemory > 0 ? $this->getHumanMemory($phpMemory) : "Unlimited"));
115
116
		$this->logger->writeln("");
117
		$availableMemory = MemoryLimits::getAvailableMemory();
118
		$this->logger->writeln("Minimum value to assign to image processing.: " . $this->getHumanMemory(SettingsService::MINIMUM_ASSIGNED_MEMORY));
119
		$this->logger->writeln("Maximum value to assign to image processing.: " . ($availableMemory > 0 ? $this->getHumanMemory($availableMemory) : "Unknown"));
120
121
		$this->logger->writeln("");
122
		if ($assignMemory > $availableMemory) {
123
			$this->logger->writeln("Cannot assign more memory than the maximum...");
124
			return 1;
125
		}
126
127
		if ($assignMemory < SettingsService::MINIMUM_ASSIGNED_MEMORY) {
128
			$this->logger->writeln("Cannot assign less memory than the minimum...");
129
			return 1;
130
		}
131
132
		$this->settingsService->setAssignedMemory ($assignMemory);
133
		$this->logger->writeln("Maximum memory assigned for image processing: " . $this->getHumanMemory($assignMemory));
134
135
		return 0;
136
	}
137
138
	private function setupModel (int $modelId): int {
139
		$this->logger->writeln("");
140
141
		$model = $this->modelManager->getModel($modelId);
142
		if (is_null($model)) {
143
			$this->logger->writeln('Invalid model Id');
144
			return 1;
145
		}
146
147
		$modelDescription = $model->getId() . ' (' . $model->getName(). ')';
148
149
		$error_message = "";
150
		if (!$model->meetDependencies($error_message)) {
151
			$this->logger->writeln('You do not meet the dependencies to install the model ' . $modelDescription);
152
			$this->logger->writeln('Summary: ' . $error_message);
153
			$this->logger->writeln('Please read the documentation for this model to continue: ' .$model->getDocumentation());
154
			return 1;
155
		}
156
157
		if ($model->isInstalled()) {
158
			$this->logger->writeln('The files of model ' . $modelDescription . ' are already installed');
159
			$this->modelManager->setDefault($modelId);
160
			$this->logger->writeln('The model ' . $modelDescription . ' was configured as default');
161
162
			return 0;
163
		}
164
165
		$this->logger->writeln('The model ' . $modelDescription . ' will be installed');
166
		$model->install();
167
		$this->logger->writeln('Install model ' . $modelDescription . ' successfully done');
168
169
		$this->modelManager->setDefault($modelId);
170
		$this->logger->writeln('The model ' . $modelDescription . ' was configured as default');
171
172
		return 0;
173
	}
174
175
	private function dumpCurrentSetup (): void {
176
		$this->logger->writeln("Current setup:");
177
		$this->logger->writeln('');
178
		$this->logger->writeln("Minimum value to assign to image processing.: " . $this->getHumanMemory(SettingsService::MINIMUM_ASSIGNED_MEMORY));
179
		$availableMemory = MemoryLimits::getAvailableMemory();
180
		$this->logger->writeln("Maximum value to assign to image processing.: " . ($availableMemory > 0 ? $this->getHumanMemory($availableMemory) : "Unknown"));
181
		$assignedMemory = $this->settingsService->getAssignedMemory();
182
		$this->logger->writeln("Maximum memory assigned for image processing: " . ($assignedMemory > 0 ? $this->getHumanMemory($assignedMemory) : "Pending configuration"));
183
184
		$this->logger->writeln('');
185
		$this->logger->writeln("Available models:");
186
		$table = new Table($this->logger);
187
		$table->setHeaders(['Id', 'Enabled', 'Name', 'Description']);
188
189
		$currentModel = $this->modelManager->getCurrentModel();
190
		$modelId = (!is_null($currentModel)) ? $currentModel->getId() : -1;
191
192
		$models = $this->modelManager->getAllModels();
193
		foreach ($models as $model) {
194
			$table->addRow([
195
				$model->getId(),
196
				($model->getId() === $modelId) ? '*' : '',
197
				$model->getName(),
198
				$model->getDescription()
199
			]);
200
		}
201
		$table->render();
202
	}
203
204
	private function getHumanMemory ($memory): string {
205
		return OCP_Util::humanFileSize($memory) . " (" . intval($memory) . "B)";
206
	}
207
208
}
209