-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathSettingsService.php
More file actions
164 lines (153 loc) · 4.73 KB
/
SettingsService.php
File metadata and controls
164 lines (153 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
declare(strict_types=1);
/*
* Copyright (c) 2022 The Recognize contributors.
* This file is licensed under the Affero General Public License version 3 or later. See the COPYING file.
*/
namespace OCA\Recognize\Service;
use OCA\Recognize\BackgroundJobs\SchedulerJob;
use OCA\Recognize\Classifiers\Audio\MusicnnClassifier;
use OCA\Recognize\Classifiers\Images\ClusteringFaceClassifier;
use OCA\Recognize\Classifiers\Images\ImagenetClassifier;
use OCA\Recognize\Classifiers\Images\LandmarksClassifier;
use OCA\Recognize\Classifiers\Video\MovinetClassifier;
use OCA\Recognize\Exception\Exception;
use OCP\AppFramework\Services\IAppConfig;
use OCP\BackgroundJob\IJobList;
final class SettingsService {
/** @var array<string,string> */
private const DEFAULTS = [
'tensorflow.cores' => '0',
'tensorflow.gpu' => 'false',
'tensorflow.purejs' => 'false',
'geo.enabled' => 'false',
'imagenet.enabled' => 'false',
'landmarks.enabled' => 'false',
'faces.enabled' => 'false',
'musicnn.enabled' => 'false',
'movinet.enabled' => 'false',
'node_binary' => '',
'clusterFaces.status' => 'null',
'faces.status' => 'null',
'imagenet.status' => 'null',
'landmarks.status' => 'null',
'movinet.status' => 'null',
'musicnn.status' => 'null',
'clusterFaces.lastRun' => '0',
'faces.lastFile' => '0',
'imagenet.lastFile' => '0',
'landmarks.lastFile' => '0',
'movinet.lastFile' => '0',
'musicnn.lastFile' => '0',
'faces.batchSize' => '500',
'imagenet.batchSize' => '100',
'landmarks.batchSize' => '100',
'movinet.batchSize' => '20',
'musicnn.batchSize' => '100',
'nice_binary' => '',
'nice_value' => '0',
'concurrency.enabled' => 'false',
'ffmpeg_binary' => '',
];
/** @var array<string,string> */
private const PUREJS_DEFAULTS = [
'faces.batchSize' => '50',
'imagenet.batchSize' => '20',
'landmarks.batchSize' => '20',
'movinet.batchSize' => '5',
'musicnn.batchSize' => '20',
];
public const LAZY_SETTINGS = [
'tensorflow.purejs',
'node_binary',
'nice_binary',
'nice_value',
'ffmpeg_binary',
'clusterFaces.status',
'faces.status',
'imagenet.status',
'landmarks.status',
'movinet.status',
'musicnn.status',
'faces.lastFile',
'imagenet.lastFile',
'landmarks.lastFile',
'movinet.lastFile',
'musicnn.lastFile',
'clusterFaces.lastRun',
'faces.batchSize',
'imagenet.batchSize',
'landmarks.batchSize',
'movinet.batchSize',
'musicnn.batchSize',
'concurrency.enabled'
];
private IAppConfig $config;
private IJobList $jobList;
public function __construct(IAppConfig $config, IJobList $jobList) {
$this->config = $config;
$this->jobList = $jobList;
}
/**
* @param string $key
* @return string
*/
public function getSetting(string $key): string {
if (strpos($key, 'batchSize') !== false) {
return $this->config->getAppValueString($key, $this->getSetting('tensorflow.purejs') === 'false' ? self::DEFAULTS[$key] : self::PUREJS_DEFAULTS[$key]);
}
$lazy = false;
if (in_array($key, self::LAZY_SETTINGS, true)) {
$lazy = true;
}
return $this->config->getAppValueString($key, self::DEFAULTS[$key], lazy: $lazy);
}
/**
* @param string $key
* @param string $value
* @return void
* @throws \OCA\Recognize\Exception\Exception
*/
public function setSetting(string $key, string $value): void {
if (!array_key_exists($key, self::DEFAULTS)) {
throw new Exception('Unknown settings key '.$key);
}
if ($value === 'true' && $this->config->getAppValueString($key, 'false') === 'false') {
// Additional model enabled: Schedule new crawl run for the affected mime types
switch ($key) {
case ClusteringFaceClassifier::MODEL_NAME . '.enabled':
$this->jobList->add(SchedulerJob::class, ['models' => [ClusteringFaceClassifier::MODEL_NAME]]);
break;
case ImagenetClassifier::MODEL_NAME . '.enabled':
$this->jobList->add(SchedulerJob::class, ['models' => [ImagenetClassifier::MODEL_NAME]]);
break;
case LandmarksClassifier::MODEL_NAME . '.enabled':
$this->jobList->add(SchedulerJob::class, ['models' => [LandmarksClassifier::MODEL_NAME]]);
break;
case MovinetClassifier::MODEL_NAME . '.enabled':
$this->jobList->add(SchedulerJob::class, ['models' => [MovinetClassifier::MODEL_NAME]]);
break;
case MusicnnClassifier::MODEL_NAME . '.enabled':
$this->jobList->add(SchedulerJob::class, ['models' => [MusicnnClassifier::MODEL_NAME]]);
break;
default:
break;
}
}
$lazy = false;
if (in_array($key, self::LAZY_SETTINGS, true)) {
$lazy = true;
}
$this->config->setAppValueString($key, $value, lazy: $lazy);
}
/**
* @return array
*/
public function getAll(): array {
$settings = [];
foreach (array_keys(self::DEFAULTS) as $key) {
$settings[$key] = $this->getSetting($key);
}
return $settings;
}
}