Passed
Push — settings-service ( f5791d...2bf3e4 )
by Matias
05:44 queued 04:00
created

SettingService::setNeedRemoveStaleImages()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 1
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 2
ccs 0
cts 2
cp 0
crap 6
rs 10
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 SettingService {
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
	/** @var IConfig Config */
85
	private $config;
86
87
	/**  @var string|null */
88
	private $userId;
89
90
	/**
91
	 * @param IConfig $config
92
	 * @param string $userId
93
	 */
94 1
	public function __construct(IConfig $config,
95
	                            $userId)
96
	{
97 1
		$this->config = $config;
98 1
		$this->userId = $userId;
99 1
	}
100
101
	/*
102
	 * User settings.
103
	 */
104
	public function getUserEnabled ($userId = null): bool {
105
		$enabled = $this->config->getUserValue($userId ?? $this->userId, Application::APP_NAME, self::USER_ENABLED_KEY, self::DEFAULT_USER_ENABLED);
106
		return ($enabled === 'true');
107
	}
108
109
	public function setUserEnabled (bool $enabled, $userId = null) {
110
		 $this->config->setUserValue($userId ?? $this->userId, Application::APP_NAME, self::USER_ENABLED_KEY, $enabled ? "true" : "false");
111
	}
112
113
	public function getUserFullScanDone ($userId = null): bool {
114
		$fullScanDone = $this->config->getUserValue($userId ?? $this->userId, Application::APP_NAME, self::FULL_IMAGE_SCAN_DONE_KEY, self::DEFAULT_FULL_IMAGE_SCAN_DONE);
115
		return ($fullScanDone === 'true');
116
	}
117
118
	public function setUserFullScanDone (bool $fullScanDone, $userId = null) {
119
		$this->config->setUserValue($userId ?? $this->userId, Application::APP_NAME, self::FULL_IMAGE_SCAN_DONE_KEY, $fullScanDone ? "true" : "false");
120
	}
121
122
	public function getNeedRemoveStaleImages ($userId = null): bool {
123
		$needRemoval = $this->config->getUserValue($userId ?? $this->userId, Application::APP_NAME, self::STALE_IMAGES_REMOVAL_NEEDED_KEY, self::DEFAULT_STALE_IMAGES_REMOVAL_NEEDED);
124
		return ($needRemoval === 'true');
125
	}
126
127
	public function setNeedRemoveStaleImages (bool $needRemoval, $userId = null) {
128
		$this->config->setUserValue($userId ?? $this->userId, Application::APP_NAME, self::STALE_IMAGES_REMOVAL_NEEDED_KEY, $needRemoval ? "true" : "false");
129
	}
130
131
	public function getNeedRecreateClusters ($userId = null): bool {
132
		$needRecreate = $this->config->getUserValue($userId ?? $this->userId, Application::APP_NAME, self::USER_RECREATE_CLUSTERS_KEY, self::DEFAULT_USER_RECREATE_CLUSTERS);
133
		return ($needRecreate === 'true');
134
	}
135
136
	public function setNeedRecreateClusters (bool $needRecreate, $userId = null) {
137
		$this->config->setUserValue($userId ?? $this->userId, Application::APP_NAME, self::USER_RECREATE_CLUSTERS_KEY, $needRecreate ? "true" : "false");
138
	}
139
140
	/*
141
	 * Admin and process settings.
142
	 */
143
	public function getCurrentFaceModel(): int {
144
		return intval($this->config->getAppValue(Application::APP_NAME, self::CURRENT_MODEL_KEY, AddDefaultFaceModel::DEFAULT_FACE_MODEL_ID));
145
	}
146
147
	public function setCurrentFaceModel($model) {
148
		$this->config->setAppValue(Application::APP_NAME, self::CURRENT_MODEL_KEY, $model);
149
	}
150
151
	public function getSensitivity(): float {
152
		return floatval($this->config->getAppValue(Application::APP_NAME, self::SENSITIVITY_KEY, self::DEFAULT_SENSITIVITY));
153
	}
154
155
	public function setSensitivity($sensitivity) {
156
		$this->config->setAppValue(Application::APP_NAME, self::SENSITIVITY_KEY, $sensitivity);
157
	}
158
159
	public function getMinimumConfidence(): float {
160
		return floatval($this->config->getAppValue(Application::APP_NAME, self::MINIMUM_CONFIDENCE_KEY, self::DEFAULT_MINIMUM_CONFIDENCE));
161
	}
162
163
	public function setMinimumConfidence($confidence) {
164
		$this->config->setAppValue(Application::APP_NAME, self::MINIMUM_CONFIDENCE_KEY, $confidence);
165
	}
166
167
	public function getMemoryLimits(): int {
168
		return intval($this->config->getAppValue(Application::APP_NAME, self::MEMORY_LIMITS_KEY, self::DEFAULT_MEMORY_LIMITS));
169
	}
170
171
	public function setMemoryLimits(int $memoryLimits) {
172
		$this->config->setAppValue(Application::APP_NAME, self::MEMORY_LIMITS_KEY, strval($memoryLimits));
173
	}
174
175
	public function getShowNotGrouped (): bool {
176
		$show = $this->config->getAppValue(Application::APP_NAME, self::SHOW_NOT_GROUPED_KEY, self::DEFAULT_SHOW_NOT_GROUPED);
177
		return ($show === 'true');
178
	}
179
180
	public function setShowNotGrouped (bool $show) {
181
		 $this->config->setAppValue(Application::APP_NAME, self::SHOW_NOT_GROUPED_KEY, $show ? "true" : "false");
182
	}
183
184
}
185