Skip to content

Commit

Permalink
Polish admin settings
Browse files Browse the repository at this point in the history
Signed-off-by: Marcel Klehr <[email protected]>
  • Loading branch information
marcelklehr committed Sep 15, 2022
1 parent 271ffcd commit 5eba30c
Show file tree
Hide file tree
Showing 20 changed files with 515 additions and 1,183 deletions.
4 changes: 3 additions & 1 deletion appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@
['name' => 'admin#reset', 'url' => '/admin/reset', 'verb' => 'GET'],
['name' => 'admin#recrawl', 'url' => '/admin/recrawl', 'verb' => 'GET'],
['name' => 'admin#count', 'url' => '/admin/count', 'verb' => 'GET'],
['name' => 'admin#countQueued', 'url' => '/admin/countQueued', 'verb' => 'GET'],
['name' => 'admin#count_missed', 'url' => '/admin/countMissed', 'verb' => 'GET'],
['name' => 'admin#avx', 'url' => '/admin/avx', 'verb' => 'GET'],
['name' => 'admin#platform', 'url' => '/admin/platform', 'verb' => 'GET'],
['name' => 'admin#musl', 'url' => '/admin/musl', 'verb' => 'GET'],
['name' => 'user#update_cluster', 'url' => '/user/cluster/{id}', 'verb' => 'POST'],
['name' => 'admin#get_setting', 'url' => '/admin/settings/{setting}', 'verb' => 'GET'],
['name' => 'admin#set_setting', 'url' => '/admin/settings/{setting}', 'verb' => 'PUT'],
],
];
21 changes: 17 additions & 4 deletions lib/BackgroundJobs/ClassifierJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,26 @@
use OCP\DB\Exception;
use OCP\Files\Config\ICachedMountInfo;
use OCP\Files\Config\IUserMountCache;
use OCP\IConfig;
use Psr\Log\LoggerInterface;

abstract class ClassifierJob extends Job {
private LoggerInterface $logger;
private QueueService $queue;
private IUserMountCache $userMountCache;
private IJobList $jobList;
/**
* @var \OCP\IConfig
*/
private IConfig $config;

public function __construct(ITimeFactory $time, LoggerInterface $logger, QueueService $queue, IUserMountCache $userMountCache, IJobList $jobList) {
public function __construct(ITimeFactory $time, LoggerInterface $logger, QueueService $queue, IUserMountCache $userMountCache, IJobList $jobList, IConfig $config) {
parent::__construct($time);
$this->logger = $logger;
$this->queue = $queue;
$this->userMountCache = $userMountCache;
$this->jobList = $jobList;
$this->config = $config;
}

protected function runClassifier(string $model, $argument) {
Expand All @@ -32,7 +38,8 @@ protected function runClassifier(string $model, $argument) {
try {
$files = $this->queue->getFromQueue($model, $storageId, $rootId, $this->getBatchSize());
} catch (Exception $e) {
$this->logger->error('Cannot retrieve items from imagenet queue', ['exception' => $e]);
$this->config->setAppValue('recognize', $model.'.status', 'false');
$this->logger->error('Cannot retrieve items from '.$model.' queue', ['exception' => $e]);
return;
}

Expand All @@ -44,7 +51,12 @@ protected function runClassifier(string $model, $argument) {
\OC_Util::setupFS($mounts[0]->getUser()->getUID());
}

$this->classify($files);
try {
$this->classify($files);
} catch(\Throwable $e) {
$this->config->setAppValue('recognize', $model.'.status', 'false');
throw $e;
}

try {
// If there is at least one file left in the queue, reschedule this job
Expand All @@ -54,7 +66,8 @@ protected function runClassifier(string $model, $argument) {
$this->jobList->remove(static::class, $argument);
}
} catch (Exception $e) {
$this->logger->error('Cannot retrieve items from imagenet queue', ['exception' => $e]);
$this->config->setAppValue('recognize', $model.'.status', 'false');
$this->logger->error('Cannot retrieve items from '.$model.' queue', ['exception' => $e]);
return;
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/BackgroundJobs/ClassifyFacesJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ClassifyFacesJob extends ClassifierJob {
private ClusteringFaceClassifier $faces;

public function __construct(ITimeFactory $time, LoggerInterface $logger, QueueService $queue, IConfig $config, ClusteringFaceClassifier $faceClassifier, IUserMountCache $mountCache, IJobList $jobList) {
parent::__construct($time, $logger, $queue, $mountCache, $jobList);
parent::__construct($time, $logger, $queue, $mountCache, $jobList, $config);
$this->config = $config;
$this->faces = $faceClassifier;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/BackgroundJobs/ClassifyImagenetJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ClassifyImagenetJob extends ClassifierJob {
private ImagenetClassifier $imagenet;

public function __construct(ITimeFactory $time, LoggerInterface $logger, QueueService $queue, IConfig $config, ImagenetClassifier $imagenet, IUserMountCache $mountCache, IJobList $jobList) {
parent::__construct($time, $logger, $queue, $mountCache, $jobList);
parent::__construct($time, $logger, $queue, $mountCache, $jobList, $config);
$this->config = $config;
$this->imagenet = $imagenet;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/BackgroundJobs/ClassifyLandmarksJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ClassifyLandmarksJob extends ClassifierJob {
private LandmarksClassifier $landmarks;

public function __construct(ITimeFactory $time, LoggerInterface $logger, QueueService $queue, IConfig $config, LandmarksClassifier $landmarks, IUserMountCache $mountCache, IJobList $jobList) {
parent::__construct($time, $logger, $queue, $mountCache, $jobList);
parent::__construct($time, $logger, $queue, $mountCache, $jobList, $config);
$this->config = $config;
$this->landmarks = $landmarks;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/BackgroundJobs/ClassifyMovinetJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ClassifyMovinetJob extends ClassifierJob {
private MovinetClassifier $movinet;

public function __construct(ITimeFactory $time, LoggerInterface $logger, QueueService $queue, IConfig $config, MovinetClassifier $movinet, IUserMountCache $mountCache, IJobList $jobList) {
parent::__construct($time, $logger, $queue, $mountCache, $jobList);
parent::__construct($time, $logger, $queue, $mountCache, $jobList, $config);
$this->config = $config;
$this->movinet = $movinet;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/BackgroundJobs/ClassifyMusicnnJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ClassifyMusicnnJob extends ClassifierJob {
private MusicnnClassifier $musicnn;

public function __construct(ITimeFactory $time, LoggerInterface $logger, QueueService $queue, IConfig $config, MusicnnClassifier $musicnn, IUserMountCache $mountCache, IJobList $jobList) {
parent::__construct($time, $logger, $queue, $mountCache, $jobList);
parent::__construct($time, $logger, $queue, $mountCache, $jobList, $config);
$this->config = $config;
$this->musicnn = $musicnn;
}
Expand Down
1 change: 1 addition & 0 deletions lib/Classifiers/Audio/MusicnnClassifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function classify(array $queueFiles): void {

foreach ($classifierProcess as $queueFile => $results) {
$this->tagManager->assignTags($queueFile->getFileId(), $results);
$this->config->setAppValue('recognize', self::MODEL_NAME.'.status', 'true');
}
}
}
1 change: 1 addition & 0 deletions lib/Classifiers/Images/ClusteringFaceClassifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public function classify(array $queueFiles): void {
}
$usersToCluster[] = $userId;
}
$this->config->setAppValue('recognize', self::MODEL_NAME.'.status', 'true');
}
}

Expand Down
1 change: 1 addition & 0 deletions lib/Classifiers/Images/ImagenetClassifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public function classify(array $queueFiles): void {
$landmarkTags = array_filter($results, function ($tagName) {
return in_array($tagName, LandmarksClassifier::PRECONDITION_TAGS);
});
$this->config->setAppValue('recognize', self::MODEL_NAME.'.status', 'true');
if (count($landmarkTags) > 0) {
try {
$this->queue->insertIntoQueue(LandmarksClassifier::MODEL_NAME, $queueFile);
Expand Down
1 change: 1 addition & 0 deletions lib/Classifiers/Images/LandmarksClassifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public function classify(array $queueFiles): void {

foreach ($classifierProcess as $queueFile => $results) {
$this->tagManager->assignTags($queueFile->getFileId(), $results);
$this->config->setAppValue('recognize', self::MODEL_NAME.'.status', 'true');
}
}
}
1 change: 1 addition & 0 deletions lib/Classifiers/Video/MovinetClassifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function classify(array $queueFiles): void {

foreach ($classifierProcess as $queueFile => $results) {
$this->tagManager->assignTags($queueFile->getFileId(), $results);
$this->config->setAppValue('recognize', self::MODEL_NAME.'.status', 'false');
}
}
}
37 changes: 36 additions & 1 deletion lib/Controller/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
namespace OCA\Recognize\Controller;

use OCA\Recognize\BackgroundJobs\SchedulerJob;
use OCA\Recognize\Service\QueueService;
use OCA\Recognize\Service\TagManager;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\JSONResponse;
use OCP\BackgroundJob\IJobList;
use OCP\IConfig;
use OCP\IRequest;

class AdminController extends Controller {
Expand All @@ -15,11 +17,21 @@ class AdminController extends Controller {
* @var \OCP\BackgroundJob\IJobList
*/
private IJobList $jobList;
/**
* @var \OCP\IConfig
*/
private IConfig $config;
/**
* @var \OCA\Recognize\Service\QueueService
*/
private QueueService $queue;

public function __construct($appName, IRequest $request, TagManager $tagManager, IJobList $jobList) {
public function __construct($appName, IRequest $request, TagManager $tagManager, IJobList $jobList, IConfig $config, QueueService $queue) {
parent::__construct($appName, $request);
$this->tagManager = $tagManager;
$this->jobList = $jobList;
$this->config = $config;
$this->queue = $queue;
}

public function reset(): JSONResponse {
Expand All @@ -42,6 +54,21 @@ public function countMissed(): JSONResponse {
return new JSONResponse(['count' => $count]);
}

public function countQueued(): JSONResponse {
$imagenetCount = $this->queue->count('imagenet');
$facesCount = $this->queue->count('faces');
$landmarksCount = $this->queue->count('landmarks');
$movinetCount = $this->queue->count('movinet');
$musicnnCount = $this->queue->count('musicnn');
return new JSONResponse([
'imagenet' => $imagenetCount,
'faces' => $facesCount,
'landmarks' => $landmarksCount,
'movinet' => $movinetCount,
'musicnn' => $musicnnCount
]);
}

public function avx(): JSONResponse {
try {
$cpuinfo = file_get_contents('/proc/cpuinfo');
Expand Down Expand Up @@ -80,4 +107,12 @@ public function musl(): JSONResponse {
$ldd = trim(implode("\n", $output));
return new JSONResponse(['musl' => str_contains($ldd, 'musl')]);
}

public function setSetting(string $setting, $value) {
$this->config->setAppValue('recognize', $setting, $value);
}

public function getSetting(string $setting) {
return new JSONResponse(['value' => $this->config->getAppValue('recognize', $setting)]);
}
}
2 changes: 1 addition & 1 deletion lib/Db/FaceDetectionMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function findByFileIdWithTitle(int $fileId) {
*/
public function findByFileIdAndClusterId(int $fileId, int $clusterId) : FaceDetection {
$qb = $this->db->getQueryBuilder();
$qb->select(array_map(fn($c)=> 'd.'.$c, FaceDetection::$columns))
$qb->select(array_map(fn ($c) => 'd.'.$c, FaceDetection::$columns))
->from('recognize_face_detections', 'd')
->leftJoin('d', 'recognize_face_clusters', 'c', $qb->expr()->eq('d.cluster_id', 'c.id'))
->where($qb->expr()->eq('d.file_id', $qb->createPositionalParameter($fileId, IQueryBuilder::PARAM_INT)))
Expand Down
8 changes: 8 additions & 0 deletions lib/Db/QueueMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,12 @@ public function clearQueue(string $model) {
$qb = $this->db->getQueryBuilder();
$qb->delete($this->getQueueTable($model))->executeStatement();
}

public function count(string $model) : int {
$qb = $this->db->getQueryBuilder();
$result = $qb->select($qb->func()->count('id'))
->from($this->getQueueTable($model))
->executeQuery();
return $result->fetchOne(\PDO::FETCH_COLUMN);
}
}
4 changes: 4 additions & 0 deletions lib/Service/QueueService.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,8 @@ public function removeFileFromAllQueues(int $fileId) : void {
public function clearQueue(string $model) {
$this->queueMapper->clearQueue($model);
}

public function count(string $model): int {
return $this->queueMapper->count($model);
}
}
4 changes: 2 additions & 2 deletions lib/Settings/AdminSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use OCP\Settings\ISettings;

class AdminSettings implements ISettings {
public const SETTINGS = ['tensorflow.cores', 'tensorflow.gpu', 'tensorflow.purejs', 'geo.enabled', 'imagenet.enabled', 'landmarks.enabled', 'faces.enabled', 'musicnn.enabled', 'movinet.enabled', 'node_binary', 'audio.status', 'images.status', 'video.status'];
public const SETTINGS = ['tensorflow.cores', 'tensorflow.gpu', 'tensorflow.purejs', 'geo.enabled', 'imagenet.enabled', 'landmarks.enabled', 'faces.enabled', 'musicnn.enabled', 'movinet.enabled', 'node_binary', 'faces.status', 'imagenet.status', 'landmarks.status', 'movinet.status', 'musicnn.status'];

/**
* @var \OCP\AppFramework\Services\IInitialState
Expand All @@ -36,7 +36,7 @@ public function __construct(IInitialState $initialState, IConfig $config) {
public function getForm(): TemplateResponse {
$settings = [];
foreach (self::SETTINGS as $setting) {
$settings[$setting] = $this->config->getAppValue('recognize', $setting);
$settings[$setting] = $this->config->getAppValue('recognize', $setting, 'null');
}
$this->initialState->provideInitialState('settings', $settings);
return new TemplateResponse('recognize', 'admin');
Expand Down
Loading

0 comments on commit 5eba30c

Please sign in to comment.