Passed
Push — settings-service ( 0cfa6c...7803f5 )
by Matias
04:04
created

SettingsService::setMemoryLimits()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
/**
4
 * @copyright Copyright (c) 2020 Matias De lellis <[email protected]>
5
 *
6
 * @author Matias De lellis <[email protected]>
7
 *
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
25
namespace OCA\FaceRecognition\Service;
26
27
use OCA\FaceRecognition\AppInfo\Application;
28
use OCA\FaceRecognition\Migration\AddDefaultFaceModel;
29
30
use OCP\IConfig;
31
32
class SettingsService {
33
34
	/*
35
	 * Settings keys and default values.
36
	 */
37
38
	/** Current Model used to analyze */
39
	const CURRENT_MODEL_KEY = 'model';
40
	/* Default values is taked from AddDefaultFaceModel */
41
42
	/** Sensitivity used to clustering */
43
	const SENSITIVITY_KEY = 'sensitivity';
44
	const MINIMUM_SENSITIVITY = '0.4';
45
	const DEFAULT_SENSITIVITY = '0.5';
46
	const MAXIMUM_SENSITIVITY = '0.6';
47
48
	/** Minimum confidence used to try to clustring faces */
49
	const MINIMUM_CONFIDENCE_KEY = 'min-confidence';
50
	const MINIMUM_MINIMUM_CONFIDENCE = '0.0';
51
	const DEFAULT_MINIMUM_CONFIDENCE = '0.9';
52
	const MAXIMUM_MINIMUM_CONFIDENCE = '1.0';
53
54
	/** Memory limit suggested for analysis */
55
	const MEMORY_LIMITS_KEY = "memory-limits";
56
	const MINIMUM_MEMORY_LIMITS = 1 * 1024 * 1024 * 1024;
57
	const DEFAULT_MEMORY_LIMITS = '-1'; // It is dynamically configured according to hardware
58
	const MAXIMUM_MEMORY_LIMITS = 4 * 1024 * 1024 * 1024;
59
60
	/** Show single persons on clustes view */
61
	const SHOW_NOT_GROUPED_KEY = 'show-not-grouped';
62
	const DEFAULT_SHOW_NOT_GROUPED = 'false';
63
64
	/** User setting what indicates if has the analysis enabled */
65
	const USER_ENABLED_KEY = 'enabled';
66
	const DEFAULT_USER_ENABLED = 'false';
67
68
	/** User setting that remember last images checked */
69
	const STALE_IMAGES_LAST_CHECKED_KEY = 'stale_images_last_checked';
70
	const DEFAULT_STALE_IMAGES_LAST_CHECKED = '0';
71
72
	/** Define if for some reason need remove old images */
73
	const STALE_IMAGES_REMOVAL_NEEDED_KEY = 'stale_images_removal_needed';
74
	const DEFAULT_STALE_IMAGES_REMOVAL_NEEDED = 'false';
75
76
	/** User setting that indicate when scan finished */
77
	const FULL_IMAGE_SCAN_DONE_KEY = 'full_image_scan_done';
78
	const DEFAULT_FULL_IMAGE_SCAN_DONE = 'false';
79
80
	/** User setting that indicate that need to recreate clusters */
81
	const USER_RECREATE_CLUSTERS_KEY = 'recreate-clusters';
82
	const DEFAULT_USER_RECREATE_CLUSTERS = 'false';
83
84
	/** User setting that indicate that is forced to create clusters */
85
	const FORCE_CREATE_CLUSTERS_KEY = 'force-create-clusters';
86
	const DEFAULT_FORCE_CREATE_CLUSTERS = 'false';
87
88
	/** Hidden setting that allows to analyze shared files */
89
	const HANDLE_SHARED_FILES_KEY = 'handle-shared-files';
90
	const DEFAULT_HANDLE_SHARED_FILES = 'false';
91
92
93
	/**
94
	 * SettingsService
95
	 */
96
97
	/** @var IConfig Config */
98
	private $config;
99
100
	/**  @var string|null */
101
	private $userId;
102
103
	/**
104
	 * @param IConfig $config
105
	 * @param string $userId
106
	 */
107
	public function __construct(IConfig $config,
108
	                            $userId)
109
	{
110
		$this->config = $config;
111
		$this->userId = $userId;
112
	}
113
114
	/*
115
	 * User settings.
116
	 */
117
	public function getUserEnabled ($userId = null): bool {
118
		$enabled = $this->config->getUserValue($userId ?? $this->userId, Application::APP_NAME, self::USER_ENABLED_KEY, self::DEFAULT_USER_ENABLED);
119
		return ($enabled === 'true');
120
	}
121
122
	public function setUserEnabled (bool $enabled, $userId = null) {
123
		$this->config->setUserValue($userId ?? $this->userId, Application::APP_NAME, self::USER_ENABLED_KEY, $enabled ? "true" : "false");
124
	}
125
126
	public function getUserFullScanDone ($userId = null): bool {
127
		$fullScanDone = $this->config->getUserValue($userId ?? $this->userId, Application::APP_NAME, self::FULL_IMAGE_SCAN_DONE_KEY, self::DEFAULT_FULL_IMAGE_SCAN_DONE);
128
		return ($fullScanDone === 'true');
129
	}
130
131
	public function setUserFullScanDone (bool $fullScanDone, $userId = null) {
132
		$this->config->setUserValue($userId ?? $this->userId, Application::APP_NAME, self::FULL_IMAGE_SCAN_DONE_KEY, $fullScanDone ? "true" : "false");
133
	}
134
135
	public function getNeedRemoveStaleImages ($userId = null): bool {
136
		$needRemoval = $this->config->getUserValue($userId ?? $this->userId, Application::APP_NAME, self::STALE_IMAGES_REMOVAL_NEEDED_KEY, self::DEFAULT_STALE_IMAGES_REMOVAL_NEEDED);
137
		return ($needRemoval === 'true');
138
	}
139
140
	public function setNeedRemoveStaleImages (bool $needRemoval, $userId = null) {
141
		$this->config->setUserValue($userId ?? $this->userId, Application::APP_NAME, self::STALE_IMAGES_REMOVAL_NEEDED_KEY, $needRemoval ? "true" : "false");
142
	}
143
144
	public function getNeedRecreateClusters ($userId = null): bool {
145
		$needRecreate = $this->config->getUserValue($userId ?? $this->userId, Application::APP_NAME, self::USER_RECREATE_CLUSTERS_KEY, self::DEFAULT_USER_RECREATE_CLUSTERS);
146
		return ($needRecreate === 'true');
147
	}
148
149
	public function setNeedRecreateClusters (bool $needRecreate, $userId = null) {
150
		$this->config->setUserValue($userId ?? $this->userId, Application::APP_NAME, self::USER_RECREATE_CLUSTERS_KEY, $needRecreate ? "true" : "false");
151
	}
152
153
	public function getForceCreateClusters ($userId = null): bool {
154
		$forceCreate = $this->config->getUserValue($userId ?? $this->userId, Application::APP_NAME, self::FORCE_CREATE_CLUSTERS_KEY, self::DEFAULT_FORCE_CREATE_CLUSTERS);
155
		return ($forceCreate === 'true');
156
	}
157
158
	public function setForceCreateClusters (bool $forceCreate, $userId = null) {
159
		$this->config->setUserValue($userId ?? $this->userId, Application::APP_NAME, self::FORCE_CREATE_CLUSTERS_KEY, $forceCreate ? "true" : "false");
160
	}
161
162
	/*
163
	 * Admin and process settings.
164
	 */
165
	public function getCurrentFaceModel(): int {
166
		return intval($this->config->getAppValue(Application::APP_NAME, self::CURRENT_MODEL_KEY, AddDefaultFaceModel::DEFAULT_FACE_MODEL_ID));
167
	}
168
169
	public function setCurrentFaceModel($model) {
170
		$this->config->setAppValue(Application::APP_NAME, self::CURRENT_MODEL_KEY, $model);
171
	}
172
173
	public function getSensitivity(): float {
174
		return floatval($this->config->getAppValue(Application::APP_NAME, self::SENSITIVITY_KEY, self::DEFAULT_SENSITIVITY));
175
	}
176
177
	public function setSensitivity($sensitivity) {
178
		$this->config->setAppValue(Application::APP_NAME, self::SENSITIVITY_KEY, $sensitivity);
179
	}
180
181
	public function getMinimumConfidence(): float {
182
		return floatval($this->config->getAppValue(Application::APP_NAME, self::MINIMUM_CONFIDENCE_KEY, self::DEFAULT_MINIMUM_CONFIDENCE));
183
	}
184
185
	public function setMinimumConfidence($confidence) {
186
		$this->config->setAppValue(Application::APP_NAME, self::MINIMUM_CONFIDENCE_KEY, $confidence);
187
	}
188
189
	public function getMemoryLimits(): int {
190
		return intval($this->config->getAppValue(Application::APP_NAME, self::MEMORY_LIMITS_KEY, self::DEFAULT_MEMORY_LIMITS));
191
	}
192
193
	public function setMemoryLimits(int $memoryLimits) {
194
		$this->config->setAppValue(Application::APP_NAME, self::MEMORY_LIMITS_KEY, strval($memoryLimits));
195
	}
196
197
	public function getShowNotGrouped(): bool {
198
		$show = $this->config->getAppValue(Application::APP_NAME, self::SHOW_NOT_GROUPED_KEY, self::DEFAULT_SHOW_NOT_GROUPED);
199
		return ($show === 'true');
200
	}
201
202
	public function setShowNotGrouped(bool $show) {
203
		$this->config->setAppValue(Application::APP_NAME, self::SHOW_NOT_GROUPED_KEY, $show ? "true" : "false");
204
	}
205
206
	/**
207
	 * The next settings are advanced preferences that are not available in gui.
208
	 * See: https://github.com/matiasdelellis/facerecognition/wiki/Settings#hidden-settings
209
	 */
210
	public function getHandleSharedFiles() {
211
		$handle = $this->config->getAppValue(Application::APP_NAME, self::HANDLE_SHARED_FILES_KEY, self::DEFAULT_HANDLE_SHARED_FILES);
212
		return ($handle === 'true');
213
	}
214
215
}
216