Completed
Push — master ( b2b3ad...48dda4 )
by Matias
16s queued 12s
created

FaceManagementService::resetClusters()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 3
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 6
rs 10
1
<?php
2
/**
3
 * @copyright Copyright (c) 2020, Matias De lellis <[email protected]>
4
 * @copyright Copyright (c) 2019, Branko Kokanovic <[email protected]>
5
 *
6
 * @author Branko Kokanovic <[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 OCP\IUser;
28
use OCP\IUserManager;
29
30
use OCA\FaceRecognition\Db\FaceMapper;
31
use OCA\FaceRecognition\Db\ImageMapper;
32
use OCA\FaceRecognition\Db\PersonMapper;
33
34
use OCA\FaceRecognition\Service\SettingsService;
35
36
/**
37
 * Background service. Both command and cron job are calling this service for long-running background operations.
38
 * Background processing for face recognition is comprised of several steps, called tasks. Each task is independent,
39
 * idempotent, DI-aware logic unit that yields. Since tasks are non-preemptive, they should yield from time to time, so we son't end up
40
 * working for more than given timeout.
41
 *
42
 * Tasks can be seen as normal sequential functions, but they are easier to work with,
43
 * reason about them and test them independently. Other than that, they are really glorified functions.
44
 */
45
class FaceManagementService {
46
47
	/** @var IUserManager */
48
	private $userManager;
49
50
	/** @var FaceMapper */
51
	private $faceMapper;
52
53
	/** @var ImageMapper */
54
	private $imageMapper;
55
56
	/** @var PersonMapper */
57
	private $personMapper;
58
59
	/** @var SettingsService */
60
	private $settingsService;
61
62 2
	public function __construct(IUserManager    $userManager,
63
	                            FaceMapper      $faceMapper,
64
	                            ImageMapper     $imageMapper,
65
	                            PersonMapper    $personMapper,
66
	                            SettingsService $settingsService)
67
	{
68 2
		$this->userManager     = $userManager;
69 2
		$this->faceMapper      = $faceMapper;
70 2
		$this->imageMapper     = $imageMapper;
71 2
		$this->personMapper    = $personMapper;
72 2
		$this->settingsService = $settingsService;
73 2
	}
74
75
	/**
76
	 * Check if the current model has data on db
77
	 *
78
	 * @param IUser|null $user Optional user to check
79
	 * @param Int $modelId Optional model to check
80
	 */
81
	public function hasData(IUser $user = null, int $modelId = -1) {
82
		if ($modelId === -1) {
83
			$modelId = $this->settingsService->getCurrentFaceModel();
84
		}
85
		$eligible_users = $this->getEligiblesUserId($user);
86
		foreach ($eligible_users as $userId) {
87
			if ($this->hasDataForUser($userId, $modelId)) {
88
				return true;
89
			}
90
		}
91
		return false;
92
	}
93
94
	/**
95
	 * Check if the current model has data on db for user
96
	 *
97
	 * @param string $user ID of user to check
98
	 * @param Int $modelId model to check
99
	 */
100
	public function hasDataForUser(string $userId, int $modelId) {
101
		$facesCount = $this->faceMapper->countFaces($userId, $modelId);
102
		return ($facesCount > 0);
103
	}
104
105
	/**
106
	 * Deletes all faces, images and persons found. IF no user is given, resetting is executed for all users.
107
	 *
108
	 * @param IUser|null $user Optional user to execute resetting for
109
	 */
110
	public function resetAll(IUser $user = null) {
111
		$eligible_users = $this->getEligiblesUserId($user);
112
		foreach($eligible_users as $user) {
113
			$this->resetAllForUser($user);
114
		}
115
	}
116
117
	/**
118
	 * Deletes all faces, images and persons found for a given user.
119
	 *
120
	 * @param string $user ID of user to execute resetting for
121
	 */
122 28
	public function resetAllForUser(string $userId) {
123 28
		$this->faceMapper->deleteUserFaces($userId);
124 28
		$this->personMapper->deleteUserPersons($userId);
125 28
		$this->imageMapper->deleteUserImages($userId);
126
127 28
		$this->settingsService->setUserFullScanDone(false, $userId);
128 28
	}
129
130
	/**
131
	 * Deletes all faces, images and persons found. If no user is given, resetting is executed for all users.
132
	 *
133
	 * @param IUser|null $user Optional user to execute resetting for
134
	 * @param Int $modelId Optional model to clean
135
	 */
136
	public function resetModel(IUser $user = null, int $modelId = -1) {
137
		if ($modelId === -1) {
138
			$modelId = $this->settingsService->getCurrentFaceModel();
139
		}
140
		$eligible_users = $this->getEligiblesUserId($user);
141
		foreach($eligible_users as $userId) {
142
			$this->resetModelForUser($userId, $modelId);
143
		}
144
	}
145
146
	/**
147
	 * Deletes all faces, images and persons found for a given user.
148
	 *
149
	 * @param string $user ID of user to execute resetting for
150
	 * @param Int $modelId model to clean
151
	 */
152
	public function resetModelForUser(string $userId, $modelId) {
153
		$this->personMapper->deleteUserModel($userId, $modelId);
154
		$this->faceMapper->deleteUserModel($userId, $modelId);
155
		$this->imageMapper->deleteUserModel($userId, $modelId);
156
157
		$this->settingsService->setUserFullScanDone(false, $userId);
158
	}
159
160
	/**
161
	 * Reset error in images in order to re-analyze again.
162
	 * If no user is given, resetting is executed for all users.
163
	 *
164
	 * @param IUser|null $user Optional user to execute resetting for
165
	 */
166
	public function resetImageErrors(IUser $user = null) {
167
		$eligible_users = $this->getEligiblesUserId($user);
168
		foreach($eligible_users as $userId) {
169
			$this->imageMapper->resetErrors($userId);
170
			$this->settingsService->setUserFullScanDone(false, $userId);
171
		}
172
	}
173
174
	/**
175
	 * Eliminate all faces relations with person.
176
	 * If no user is given, resetting is executed for all users.
177
	 *
178
	 * @param IUser|null $user Optional user to execute resetting for
179
	 */
180
	public function resetClusters(IUser $user = null) {
181
		$eligible_users = $this->getEligiblesUserId($user);
182
		foreach($eligible_users as $user) {
183
			$this->resetClustersForUser($user);
184
		}
185
	}
186
187
	/**
188
	 * Eliminate all faces relations with person.
189
	 *
190
	 * @param string $user ID of user to execute resetting for
191
	 */
192
	public function resetClustersForUser(string $userId) {
193
		$model = $this->settingsService->getCurrentFaceModel();
194
195
		$this->faceMapper->unsetPersonsRelationForUser($userId, $model);
196
		$this->personMapper->deleteUserPersons($userId);
197
	}
198
199
	/**
200
	 * Get an array with the eligibles users taking into account the user argument,
201
	 * or all users.
202
	 *
203
	 * @param IUser|null $user Optional user to get specific user.
204
	 */
205
	private function getEligiblesUserId(IUser $user = null): array {
206
		$eligible_users = array();
207
		if (is_null($user)) {
208
			$this->userManager->callForAllUsers(function (IUser $user) use (&$eligible_users) {
209
				$eligible_users[] = $user->getUID();
210
			});
211
		} else {
212
			$eligible_users[] = $user->getUID();
213
		}
214
		return $eligible_users;
215
	}
216
217
}