Passed
Pull Request — master (#549)
by Matias
07:06 queued 04:58
created

ImageMapper::findAll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 2
dl 0
loc 7
ccs 0
cts 7
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017-2020, Matias De lellis <[email protected]>
4
 * @copyright Copyright (c) 2018-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
namespace OCA\FaceRecognition\Db;
25
26
use OCP\IDBConnection;
27
use OCP\IUser;
28
29
use OCP\AppFramework\Db\QBMapper;
30
use OCP\AppFramework\Db\DoesNotExistException;
31
use OCP\DB\QueryBuilder\IQueryBuilder;
32
33
class ImageMapper extends QBMapper {
34
	/** @var FaceMapper Face mapper*/
35
	private $faceMapper;
36
37 1
	public function __construct(IDBConnection $db, FaceMapper $faceMapper) {
38 1
		parent::__construct($db, 'facerecog_images', '\OCA\FaceRecognition\Db\Image');
39 1
		$this->faceMapper = $faceMapper;
40 1
	}
41
42
	/**
43
	 * @param string $userId Id of user
44
	 * @param int $imageId Id of Image to get
45
	 *
46
	 */
47 4
	public function find(string $userId, int $imageId): ?Image {
48 4
		$qb = $this->db->getQueryBuilder();
49 4
		$qb->select('id', 'file', 'is_processed', 'error', 'last_processed_time', 'processing_duration')
50 4
			->from($this->getTableName(), 'i')
51 4
			->where($qb->expr()->eq('user', $qb->createNamedParameter($userId)))
52 4
			->andWhere($qb->expr()->eq('id', $qb->createNamedParameter($imageId)));
53
		try {
54 4
			return $this->findEntity($qb);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->findEntity($qb) returns the type OCP\AppFramework\Db\Entity which includes types incompatible with the type-hinted return OCA\FaceRecognition\Db\Image|null.
Loading history...
55
		} catch (DoesNotExistException $e) {
56
			return null;
57
		}
58
	}
59
60
	/**
61
	 * @param string $userId Id of user
62
	 * @param int $modelId Id of model to get
63
	 *
64
	 */
65
	public function findAll(string $userId, int $modelId): array {
66
		$qb = $this->db->getQueryBuilder();
67
		$qb->select('id', 'file', 'is_processed', 'error', 'last_processed_time', 'processing_duration')
68
			->from($this->getTableName())
69
			->where($qb->expr()->eq('user', $qb->createNamedParameter($userId)))
70
			->andWhere($qb->expr()->eq('model', $qb->createNamedParameter($modelId)));
71
		return $this->findEntities($qb);
72
	}
73
74
	/**
75
	 * @param string $userId Id of user
76
	 * @param int $modelId Id of model
77
	 * @param int $fileId Id of file to get Image
78
	 *
79
	 */
80
	public function findFromFile(string $userId, int $modelId, int $fileId): ?Image {
81
		$qb = $this->db->getQueryBuilder();
82
		$qb->select('id', 'is_processed', 'error')
83
			->from($this->getTableName(), 'i')
84
			->where($qb->expr()->eq('user', $qb->createNamedParameter($userId)))
85
			->andwhere($qb->expr()->eq('model', $qb->createNamedParameter($modelId)))
86
			->andWhere($qb->expr()->eq('file', $qb->createNamedParameter($fileId)));
87
88
		try {
89
			return $this->findEntity($qb);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->findEntity($qb) returns the type OCP\AppFramework\Db\Entity which includes types incompatible with the type-hinted return OCA\FaceRecognition\Db\Image|null.
Loading history...
90
		} catch (DoesNotExistException $e) {
91
			return null;
92
		}
93
	}
94
95 7
	public function imageExists(Image $image): ?int {
96 7
		$qb = $this->db->getQueryBuilder();
97
		$query = $qb
98 7
			->select(['id'])
99 7
			->from($this->getTableName())
100 7
			->where($qb->expr()->eq('user', $qb->createParameter('user')))
101 7
			->andWhere($qb->expr()->eq('file', $qb->createParameter('file')))
102 7
			->andWhere($qb->expr()->eq('model', $qb->createParameter('model')))
103 7
			->setParameter('user', $image->getUser())
104 7
			->setParameter('file', $image->getFile())
105 7
			->setParameter('model', $image->getModel());
106 7
		$resultStatement = $query->execute();
107 7
		$row = $resultStatement->fetch();
108 7
		$resultStatement->closeCursor();
109 7
		return $row ? (int)$row['id'] : null;
110
	}
111
112
	public function countImages(int $model): int {
113
		$qb = $this->db->getQueryBuilder();
114
		$query = $qb
115
			->select($qb->createFunction('COUNT(' . $qb->getColumnName('id') . ')'))
116
			->from($this->getTableName())
117
			->where($qb->expr()->eq('model', $qb->createParameter('model')))
118
			->setParameter('model', $model);
119
		$resultStatement = $query->execute();
120
		$data = $resultStatement->fetch(\PDO::FETCH_NUM);
121
		$resultStatement->closeCursor();
122
123
		return (int)$data[0];
124
	}
125
126
	public function countProcessedImages(int $model): int {
127
		$qb = $this->db->getQueryBuilder();
128
		$query = $qb
129
			->select($qb->createFunction('COUNT(' . $qb->getColumnName('id') . ')'))
130
			->from($this->getTableName())
131
			->where($qb->expr()->eq('model', $qb->createParameter('model')))
132
			->andWhere($qb->expr()->eq('is_processed', $qb->createParameter('is_processed')))
133
			->setParameter('model', $model)
134
			->setParameter('is_processed', True);
135
		$resultStatement = $query->execute();
136
		$data = $resultStatement->fetch(\PDO::FETCH_NUM);
137
		$resultStatement->closeCursor();
138
139
		return (int)$data[0];
140
	}
141
142
	public function getPersonCount(string $userId, int $modelId, string $name): int {
143
		$qb = $this->db->getQueryBuilder();
144
		$query = $qb
145
			->select($qb->createFunction('COUNT(' . $qb->getColumnName('i.id') . ')'))
146
			->from($this->getTableName(), 'i')
147
			->innerJoin('i', 'facerecog_faces', 'f', $qb->expr()->eq('f.image', 'i.id'))
148
			->innerJoin('i', 'facerecog_persons', 'p', $qb->expr()->eq('f.person', 'p.id'))
149
			->where($qb->expr()->eq('p.user', $qb->createNamedParameter($userId)))
150
			->andWhere($qb->expr()->eq('model', $qb->createNamedParameter($modelId)))
151
			->andWhere($qb->expr()->eq('is_processed', $qb->createNamedParameter(True)))
152
			->andWhere($qb->expr()->eq('p.name', $qb->createNamedParameter($name)));
153
154
		$resultStatement = $query->execute();
155
		$data = $resultStatement->fetch(\PDO::FETCH_NUM);
156
		$resultStatement->closeCursor();
157
158
		return (int)$data[0];
159
	}
160
161
	public function avgProcessingDuration(int $model): int {
162
		$qb = $this->db->getQueryBuilder();
163
		$query = $qb
164
			->select($qb->createFunction('AVG(' . $qb->getColumnName('processing_duration') . ')'))
165
			->from($this->getTableName())
166
			->where($qb->expr()->eq('model', $qb->createParameter('model')))
167
			->andWhere($qb->expr()->eq('is_processed', $qb->createParameter('is_processed')))
168
			->setParameter('model', $model)
169
			->setParameter('is_processed', True);
170
		$resultStatement = $query->execute();
171
		$data = $resultStatement->fetch(\PDO::FETCH_NUM);
172
		$resultStatement->closeCursor();
173
174
		return (int)$data[0];
175
	}
176
177 3
	public function countUserImages(string $userId, int $model, bool $processed = false): int {
178 3
		$qb = $this->db->getQueryBuilder();
179
		$query = $qb
180 3
			->select($qb->createFunction('COUNT(' . $qb->getColumnName('id') . ')'))
181 3
			->from($this->getTableName())
182 3
			->where($qb->expr()->eq('user', $qb->createParameter('user')))
183 3
			->andWhere($qb->expr()->eq('model', $qb->createParameter('model')))
184 3
			->setParameter('user', $userId)
185 3
			->setParameter('model', $model);
186
187 3
		if ($processed) {
188 1
			$query->andWhere($qb->expr()->eq('is_processed', $qb->createParameter('is_processed')))
189 1
			      ->setParameter('is_processed', true);
190
		}
191
192 3
		$resultStatement = $query->execute();
193 3
		$data = $resultStatement->fetch(\PDO::FETCH_NUM);
194 3
		$resultStatement->closeCursor();
195
196 3
		return (int)$data[0];
197
	}
198
199
	/**
200
	 * @param IUser|null $user User for which to get images for. If not given, all images from instance are returned.
201
	 * @param int $modelId Model Id to get images for.
202
	 */
203 9
	public function findImagesWithoutFaces(IUser $user = null, int $modelId): array {
204 9
		$qb = $this->db->getQueryBuilder();
205
		$qb
206 9
			->select(['id', 'user', 'file', 'model'])
207 9
			->from($this->getTableName())
208 9
			->where($qb->expr()->eq('is_processed',  $qb->createParameter('is_processed')))
209 9
			->andWhere($qb->expr()->eq('model', $qb->createNamedParameter($modelId)))
210 9
			->setParameter('is_processed', false, IQueryBuilder::PARAM_BOOL);
211 9
		if (!is_null($user)) {
212 9
			$qb->andWhere($qb->expr()->eq('user', $qb->createNamedParameter($user->getUID())));
213
		}
214 9
		return $this->findEntities($qb);
215
	}
216
217 6
	public function findImages(string $userId, int $model): array {
218 6
		$qb = $this->db->getQueryBuilder();
219 6
		$qb->select('i.id', 'i.file')
220 6
			->from($this->getTableName(), 'i')
221 6
			->where($qb->expr()->eq('user', $qb->createNamedParameter($userId)))
222 6
			->andWhere($qb->expr()->eq('model', $qb->createNamedParameter($model)));
223
224 6
		$images = $this->findEntities($qb);
225 6
		return $images;
226
	}
227
228
	public function findFromPersonLike(string $userId, int $model, string $name, $offset = null, $limit = null): array {
229
		$qb = $this->db->getQueryBuilder();
230
		$qb->select('i.id', 'i.file')
231
			->from($this->getTableName(), 'i')
232
			->innerJoin('i', 'facerecog_faces', 'f', $qb->expr()->eq('f.image', 'i.id'))
233
			->innerJoin('i', 'facerecog_persons', 'p', $qb->expr()->eq('f.person', 'p.id'))
234
			->where($qb->expr()->eq('p.user', $qb->createNamedParameter($userId)))
235
			->andWhere($qb->expr()->eq('model', $qb->createNamedParameter($model)))
236
			->andWhere($qb->expr()->eq('is_processed', $qb->createNamedParameter(True)))
237
			->andWhere($qb->expr()->like($qb->func()->lower('p.name'), $qb->createParameter('query')));
238
239
		$query = '%' . $this->db->escapeLikeParameter(strtolower($name)) . '%';
240
		$qb->setParameter('query', $query);
241
242
		$qb->setFirstResult($offset);
243
		$qb->setMaxResults($limit);
244
245
		return $this->findEntities($qb);
246
	}
247
248
	public function findFromPerson(string $userId, int $model, string $name, $offset = null, $limit = null): array {
249
		$qb = $this->db->getQueryBuilder();
250
		$qb->select('i.id', 'i.file')
251
			->from($this->getTableName(), 'i')
252
			->innerJoin('i', 'facerecog_faces', 'f', $qb->expr()->eq('f.image', 'i.id'))
253
			->innerJoin('i', 'facerecog_persons', 'p', $qb->expr()->eq('f.person', 'p.id'))
254
			->where($qb->expr()->eq('p.user', $qb->createNamedParameter($userId)))
255
			->andWhere($qb->expr()->eq('model', $qb->createNamedParameter($model)))
256
			->andWhere($qb->expr()->eq('is_processed', $qb->createNamedParameter(True)))
257
			->andWhere($qb->expr()->eq('p.name', $qb->createNamedParameter($name)));
258
259
		$qb->setFirstResult($offset);
260
		$qb->setMaxResults($limit);
261
262
		return $this->findEntities($qb);
263
	}
264
265
266
	/**
267
	 * Writes to DB that image has been processed. Previously found faces are deleted and new ones are inserted.
268
	 * If there is exception, its stack trace is also updated.
269
	 *
270
	 * @param Image $image Image to be updated
271
	 * @param Face[] $faces Faces to insert
272
	 * @param int $duration Processing time, in milliseconds
273
	 * @param \Exception|null $e Any exception that happened during image processing
274
	 *
275
	 * @return void
276
	 */
277 4
	public function imageProcessed(Image $image, array $faces, int $duration, \Exception $e = null): void {
278 4
		$this->db->beginTransaction();
279
		try {
280
			// Update image itself
281
			//
282 4
			$error = null;
283 4
			if ($e !== null) {
284 1
				$error = substr($e->getMessage(), 0, 1024);
285
			}
286
287 4
			$qb = $this->db->getQueryBuilder();
288 4
			$qb->update($this->getTableName())
289 4
				->set("is_processed", $qb->createNamedParameter(true, IQueryBuilder::PARAM_BOOL))
290 4
				->set("error", $qb->createNamedParameter($error))
291 4
				->set("last_processed_time", $qb->createNamedParameter(new \DateTime(), IQueryBuilder::PARAM_DATE))
292 4
				->set("processing_duration", $qb->createNamedParameter($duration))
293 4
				->where($qb->expr()->eq('id', $qb->createNamedParameter($image->id)))
294 4
				->execute();
295
296
			// Delete all previous faces
297
			//
298 4
			$qb = $this->db->getQueryBuilder();
299 4
			$qb->delete('facerecog_faces')
300 4
				->where($qb->expr()->eq('image', $qb->createNamedParameter($image->id)))
301 4
				->execute();
302
303
			// Insert all faces
304
			//
305 4
			foreach ($faces as $face) {
306 1
				$this->faceMapper->insertFace($face, $this->db);
307
			}
308
309 4
			$this->db->commit();
310
		} catch (\Exception $e) {
311
			$this->db->rollBack();
312
			throw $e;
313
		}
314 4
	}
315
316
	/**
317
	 * Resets image by deleting all associated faces and prepares it to be processed again
318
	 *
319
	 * @param Image $image Image to reset
320
	 *
321
	 * @return void
322
	 */
323
	public function resetImage(Image $image): void {
324
		$qb = $this->db->getQueryBuilder();
325
		$qb->update($this->getTableName())
326
			->set("is_processed", $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL))
327
			->set("error", $qb->createNamedParameter(null))
328
			->set("last_processed_time", $qb->createNamedParameter(null))
329
			->where($qb->expr()->eq('user', $qb->createNamedParameter($image->getUser())))
330
			->andWhere($qb->expr()->eq('file', $qb->createNamedParameter($image->getFile())))
331
			->andWhere($qb->expr()->eq('model', $qb->createNamedParameter($image->getModel())))
332
			->execute();
333
	}
334
335
	/**
336
	 * Resets all image with error from that user and prepares it to be processed again
337
	 *
338
	 * @param string $userId User to reset errors
339
	 *
340
	 * @return void
341
	 */
342
	public function resetErrors(string $userId): void {
343
		$qb = $this->db->getQueryBuilder();
344
		$qb->update($this->getTableName())
345
			->set("is_processed", $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL))
346
			->set("error", $qb->createNamedParameter(null))
347
			->set("last_processed_time", $qb->createNamedParameter(null))
348
			->where($qb->expr()->eq('user', $qb->createNamedParameter($userId)))
349
			->andWhere($qb->expr()->isNotNull('error'))
350
			->execute();
351
	}
352
353
	/**
354
	 * Deletes all images from that user.
355
	 *
356
	 * @param string $userId User to drop images from table.
357
	 *
358
	 * @return void
359
	 */
360 28
	public function deleteUserImages(string $userId): void {
361 28
		$qb = $this->db->getQueryBuilder();
362 28
		$qb->delete($this->getTableName())
363 28
			->where($qb->expr()->eq('user', $qb->createNamedParameter($userId)))
364 28
			->execute();
365 28
	}
366
367
	/**
368
	 * Deletes all images from that user and Model
369
	 *
370
	 * @param string $userId User to drop images from table.
371
	 * @param int $modelId model to drop images from table.
372
	 *
373
	 * @return void
374
	 */
375
	public function deleteUserModel(string $userId, $modelId): void {
376
		$qb = $this->db->getQueryBuilder();
377
		$qb->delete($this->getTableName())
378
			->where($qb->expr()->eq('user', $qb->createNamedParameter($userId)))
379
			->andWhere($qb->expr()->eq('model', $qb->createNamedParameter($modelId)))
380
			->execute();
381
	}
382
383
}
384