forked from matomo-org/plugin-QueuedTracking
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSettings.php
256 lines (214 loc) · 10.6 KB
/
Settings.php
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\QueuedTracking;
use Piwik\Cache;
use Piwik\Config;
use Piwik\Plugins\QueuedTracking\Settings\NumWorkers;
use Piwik\Settings\Storage\StaticStorage;
use Piwik\Settings\SystemSetting;
/**
* Defines Settings for QueuedTracking.
*/
class Settings extends \Piwik\Plugin\Settings
{
/** @var SystemSetting */
public $redisHost;
/** @var SystemSetting */
public $redisPort;
/** @var SystemSetting */
public $redisTimeout;
/** @var SystemSetting */
public $redisPassword;
/** @var SystemSetting */
public $redisDatabase;
/** @var SystemSetting */
public $queueEnabled;
/** @var SystemSetting */
public $numRequestsToProcess;
/** @var SystemSetting */
public $numQueueWorkers;
/** @var SystemSetting */
public $processDuringTrackingRequest;
private $staticStorage;
protected function init()
{
$this->staticStorage = new StaticStorage('QueuedTracking');
$this->createRedisHostSetting();
$this->createRedisPortSetting();
$this->createRedisTimeoutSetting();
$this->createRedisDatabaseSetting();
$this->createRedisPasswordSetting();
$this->createQueueEnabledSetting();
$this->createNumberOfQueueWorkerSetting();
$this->createNumRequestsToProcessSetting();
$this->createProcessInTrackingRequestSetting();
}
private function createRedisHostSetting()
{
$this->redisHost = new SystemSetting('redisHost', 'Redis host');
$this->redisHost->readableByCurrentUser = true;
$this->redisHost->type = static::TYPE_STRING;
$this->redisHost->uiControlType = static::CONTROL_TEXT;
$this->redisHost->uiControlAttributes = array('size' => 300);
$this->redisHost->defaultValue = '127.0.0.1';
$this->redisHost->inlineHelp = 'Remote host of the Redis server. Max 300 characters are allowed.';
$this->redisHost->validate = function ($value) {
if (strlen($value) > 300) {
throw new \Exception('Max 300 characters allowed');
}
};
$this->addSetting($this->redisHost);
}
private function createRedisPortSetting()
{
$this->redisPort = new SystemSetting('redisPort', 'Redis port');
$this->redisPort->readableByCurrentUser = true;
$this->redisPort->type = static::TYPE_INT;
$this->redisPort->uiControlType = static::CONTROL_TEXT;
$this->redisPort->uiControlAttributes = array('size' => 5);
$this->redisPort->defaultValue = '6379';
$this->redisPort->inlineHelp = 'Port the Redis server is running on. Value should be between 1 and 65535.';
$this->redisPort->validate = function ($value) {
if ($value < 1) {
throw new \Exception('Port has to be at least 1');
}
if ($value >= 65535) {
throw new \Exception('Port should be max 65535');
}
};
$this->addSetting($this->redisPort);
}
private function createRedisTimeoutSetting()
{
$this->redisTimeout = new SystemSetting('redisTimeout', 'Redis timeout');
$this->redisTimeout->readableByCurrentUser = true;
$this->redisTimeout->type = static::TYPE_FLOAT;
$this->redisTimeout->uiControlType = static::CONTROL_TEXT;
$this->redisTimeout->uiControlAttributes = array('size' => 5);
$this->redisTimeout->inlineHelp = 'Redis connection timeout in seconds. "0.0" meaning unlimited. Can be a float eg "2.5" for a connection timeout of 2.5 seconds.';
$this->redisTimeout->defaultValue = 0.0;
$this->redisTimeout->validate = function ($value) {
if (!is_numeric($value)) {
throw new \Exception('Timeout should be numeric, eg "0.1"');
}
if (strlen($value) > 5) {
throw new \Exception('Max 5 characters allowed');
}
};
// we do not expose this one to the UI currently. That's on purpose
$this->redisTimeout->setStorage($this->staticStorage);
}
private function createNumberOfQueueWorkerSetting()
{
$this->numQueueWorkers = new NumWorkers('numQueueWorkers', 'Number of queue workers');
$this->numQueueWorkers->readableByCurrentUser = true;
$this->numQueueWorkers->type = static::TYPE_INT;
$this->numQueueWorkers->uiControlType = static::CONTROL_TEXT;
$this->numQueueWorkers->uiControlAttributes = array('size' => 5);
$this->numQueueWorkers->inlineHelp = 'Number of allowed maximum queue workers. Accepts a number between 1 and 16. Best practice is to set the number of CPUs you want to make available for queue processing. Be aware you need to make sure to start the workers manually. We recommend to not use 9-15 workers, rather use 8 or 16 as the queue might not be distributed evenly into different queues. DO NOT USE more than 1 worker if you make use the UserId feature when tracking see https://github.com/piwik/piwik/issues/7691';
$this->numQueueWorkers->defaultValue = 1;
$this->numQueueWorkers->validate = function ($value) {
if (!is_numeric($value)) {
throw new \Exception('Number of queue workers should be an integer, eg "6"');
}
$value = (int) $value;
if ($value > 16 || $value < 1) {
throw new \Exception('Only 1-16 workers allowed');
}
};
$this->addSetting($this->numQueueWorkers);
}
private function createRedisPasswordSetting()
{
$this->redisPassword = new SystemSetting('redisPassword', 'Redis password');
$this->redisPassword->readableByCurrentUser = true;
$this->redisPassword->type = static::TYPE_STRING;
$this->redisPassword->uiControlType = static::CONTROL_PASSWORD;
$this->redisPassword->uiControlAttributes = array('size' => 100);
$this->redisPassword->inlineHelp = 'Password set on the Redis server, if any. Redis can be instructed to require a password before allowing clients to execute commands.';
$this->redisPassword->defaultValue = '';
$this->redisPassword->validate = function ($value) {
if (strlen($value) > 100) {
throw new \Exception('Max 100 characters allowed');
}
};
$this->addSetting($this->redisPassword);
}
private function createRedisDatabaseSetting()
{
$this->redisDatabase = new SystemSetting('redisDatabase', 'Redis database');
$this->redisDatabase->readableByCurrentUser = true;
$this->redisDatabase->type = static::TYPE_INT;
$this->redisDatabase->uiControlType = static::CONTROL_TEXT;
$this->redisDatabase->uiControlAttributes = array('size' => 5);
$this->redisDatabase->defaultValue = 0;
$this->redisDatabase->inlineHelp = 'In case you are using Redis for caching make sure to use a different database.';
$this->redisDatabase->validate = function ($value) {
if (!is_numeric($value) || false !== strpos($value, '.')) {
throw new \Exception('The database has to be an integer');
}
if (strlen($value) > 5) {
throw new \Exception('Max 5 digits allowed');
}
};
$this->addSetting($this->redisDatabase);
}
private function createQueueEnabledSetting()
{
$self = $this;
$this->queueEnabled = new SystemSetting('queueEnabled', 'Queue enabled');
$this->queueEnabled->readableByCurrentUser = true;
$this->queueEnabled->type = static::TYPE_BOOL;
$this->queueEnabled->uiControlType = static::CONTROL_CHECKBOX;
$this->queueEnabled->inlineHelp = 'If enabled, all tracking requests will be written into a queue instead of the directly into the database. Requires a Redis server and phpredis PHP extension.';
$this->queueEnabled->defaultValue = false;
$this->queueEnabled->validate = function ($value) use ($self) {
$value = (bool) $value;
if ($value) {
$host = $self->redisHost->getValue();
$port = $self->redisPort->getValue();
$timeout = $self->redisTimeout->getValue();
$password = $self->redisPassword->getValue();
$systemCheck = new SystemCheck();
$systemCheck->checkRedisIsInstalled();
$systemCheck->checkConnectionDetails($host, $port, $timeout, $password);
}
};
$this->addSetting($this->queueEnabled);
}
private function createNumRequestsToProcessSetting()
{
$this->numRequestsToProcess = new SystemSetting('numRequestsToProcess', 'Number of requests that are processed in one batch');
$this->numRequestsToProcess->readableByCurrentUser = true;
$this->numRequestsToProcess->type = static::TYPE_INT;
$this->numRequestsToProcess->uiControlType = static::CONTROL_TEXT;
$this->numRequestsToProcess->uiControlAttributes = array('size' => 3);
$this->numRequestsToProcess->inlineHelp = 'Defines how many requests will be picked out of the queue and processed at once. Enter a number which is >= 1.';
$this->numRequestsToProcess->defaultValue = 25;
$this->numRequestsToProcess->validate = function ($value, $setting) {
if (!is_numeric($value)) {
throw new \Exception('Value should be a number');
}
if ((int) $value < 1) {
throw new \Exception('Number should be 1 or higher');
}
};
$this->addSetting($this->numRequestsToProcess);
}
private function createProcessInTrackingRequestSetting()
{
$this->processDuringTrackingRequest = new SystemSetting('processDuringTrackingRequest', 'Process during tracking request');
$this->processDuringTrackingRequest->readableByCurrentUser = true;
$this->processDuringTrackingRequest->type = static::TYPE_BOOL;
$this->processDuringTrackingRequest->uiControlType = static::CONTROL_CHECKBOX;
$this->processDuringTrackingRequest->inlineHelp = 'If enabled, we will process all requests within a queue during a normal tracking request once there are enough requests in the queue. This will not slow down the tracking request. If disabled, you have to setup a cronjob that executes the "./console queuedtracking:process" console command eg every minute to process the queue.';
$this->processDuringTrackingRequest->defaultValue = true;
$this->addSetting($this->processDuringTrackingRequest);
}
}