Passed
Push — enconde-from-orig ( 13bb2b...c256b7 )
by Matias
05:03
created

FaceManagementService   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Test Coverage

Coverage 25%

Importance

Changes 4
Bugs 0 Features 1
Metric Value
eloc 44
dl 0
loc 141
ccs 13
cts 52
cp 0.25
rs 10
c 4
b 0
f 1
wmc 16

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A getEligiblesUserId() 0 10 2
A resetClusters() 0 4 2
A resetClustersForUser() 0 5 1
A hasData() 0 11 4
A resetImageErrors() 0 5 2
A resetAllForUser() 0 6 1
A resetAll() 0 4 2
A hasDataForUser() 0 3 1
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
	/**
107
	 * Deletes all faces, images and persons found. IF no user is given, resetting is executed for all users.
108
	 *
109
	 * @param IUser|null $user Optional user to execute resetting for
110
	 */
111
	public function resetAll(IUser $user = null) {
112
		$eligible_users = $this->getEligiblesUserId($user);
113
		foreach($eligible_users as $user) {
114
			$this->resetAllForUser($user);
115
		}
116
	}
117
118
	/**
119
	 * Deletes all faces, images and persons found for a given user.
120
	 *
121
	 * @param string $user ID of user to execute resetting for
122
	 */
123 28
	public function resetAllForUser(string $userId) {
124 28
		$this->faceMapper->deleteUserFaces($userId);
125 28
		$this->personMapper->deleteUserPersons($userId);
126 28
		$this->imageMapper->deleteUserImages($userId);
127
128 28
		$this->settingsService->setUserFullScanDone(false, $userId);
129 28
	}
130
131
	/**
132
	 * Reset error in images in order to re-analyze again.
133
	 * If no user is given, resetting is executed for all users.
134
	 *
135
	 * @param IUser|null $user Optional user to execute resetting for
136
	 */
137
	public function resetImageErrors(IUser $user = null) {
138
		$eligible_users = $this->getEligiblesUserId($user);
139
		foreach($eligible_users as $userId) {
140
			$this->imageMapper->resetErrors($userId);
141
			$this->settingsService->setUserFullScanDone(false, $userId);
142
		}
143
	}
144
145
	/**
146
	 * Eliminate all faces relations with person.
147
	 * If no user is given, resetting is executed for all users.
148
	 *
149
	 * @param IUser|null $user Optional user to execute resetting for
150
	 */
151
	public function resetClusters(IUser $user = null) {
152
		$eligible_users = $this->getEligiblesUserId($user);
153
		foreach($eligible_users as $user) {
154
			$this->resetClustersForUser($user);
155
		}
156
	}
157
158
	/**
159
	 * Eliminate all faces relations with person.
160
	 *
161
	 * @param string $user ID of user to execute resetting for
162
	 */
163
	public function resetClustersForUser(string $userId) {
164
		$model = $this->settingsService->getCurrentFaceModel();
165
166
		$this->faceMapper->unsetPersonsRelationForUser($userId, $model);
167
		$this->personMapper->deleteUserPersons($userId);
168
	}
169
170
	/**
171
	 * Get an array with the eligibles users taking into account the user argument,
172
	 * or all users.
173
	 *
174
	 * @param IUser|null $user Optional user to get specific user.
175
	 */
176
	private function getEligiblesUserId(IUser $user = null): array {
177
		$eligible_users = array();
178
		if (is_null($user)) {
179
			$this->userManager->callForAllUsers(function (IUser $user) use (&$eligible_users) {
180
				$eligible_users[] = $user->getUID();
181
			});
182
		} else {
183
			$eligible_users[] = $user->getUID();
184
		}
185
		return $eligible_users;
186
	}
187
188
}