-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathLogController.php
More file actions
216 lines (195 loc) Β· 6.17 KB
/
LogController.php
File metadata and controls
216 lines (195 loc) Β· 6.17 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
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
<?php
/**
* @author Robin Appelman <icewind@owncloud.com>
*
* @copyright Copyright (c) 2015, ownCloud, Inc.
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OCA\LogReader\Controller;
use OCA\LogReader\Log\LogIteratorFactory;
use OCA\LogReader\Log\SearchFilter;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\IConfig;
use OCP\IRequest;
/**
* Class LogController
*
* @package OCA\LogReader\Controller
*/
class LogController extends Controller {
private $logIteratorFactory;
private $config;
public function __construct($appName,
IRequest $request,
IConfig $config,
LogIteratorFactory $logIteratorFactory
) {
parent::__construct($appName, $request);
$this->logIteratorFactory = $logIteratorFactory;
$this->config = $config;
}
/**
* @AuthorizedAdminSetting(settings=OCA\LogReader\Settings\Admin)
* @param int $count
* @param int $offset
* @param string $levels
* @return TemplateResponse
*/
public function get($count = 50, $offset = 0, $levels = '11111') {
$iterator = $this->logIteratorFactory->getLogIterator($levels);
return $this->responseFromIterator($iterator, $count, $offset);
}
/**
* @brief Gets the last item in the log, bypassing any cache.
* @return mixed
*/
private function getLastItem($levels) {
$iterator = $this->logIteratorFactory->getLogIterator($levels);
$iterator->next();
return $iterator->current();
}
/**
* @AuthorizedAdminSetting(settings=OCA\LogReader\Settings\Admin)
* @brief polls for a new log message since $lastReqId.
* This method will sleep for maximum 20 seconds before returning an empty
* result.
*
* Note that there is a race condition possible: when the user loads the
* logging page when a request isn't finished and this specific request
* is the last request in the log, then new messages of this request
* won't be polled. This is because there is no reliable way to identify
* a log message, so we have to use the reqid:
* - the key of the iterator will change when a new message is saved
* - a combination of reqid and counting the messages for that specific reqid
* will work in some cases but not when there are more than 50 messages of that
* request.
* @param $lastReqId
* @param string $levels
* @return JSONResponse
*/
public function poll($lastReqId, $levels = '11111') {
$cycles = 0;
$maxCycles = 20;
while ($this->getLastItem($levels)['reqId'] === $lastReqId) {
sleep(1);
$cycles++;
if ($cycles === $maxCycles) {
return new JSONResponse([]);
}
}
$iterator = $this->logIteratorFactory->getLogIterator($levels);
$iterator->next();
$data = [];
while ($iterator->valid()) {
$line = $iterator->current();
if ($line['reqId'] === $lastReqId) {
break;
}
if (!is_null($line)) {
$line['id'] = uniqid();
$data[] = $line;
}
$iterator->next();
}
return new JSONResponse($data);
}
/**
* @AuthorizedAdminSetting(settings=OCA\LogReader\Settings\Admin)
* @param string $query
* @param int $count
* @param int $offset
* @param string $levels
* @return TemplateResponse
*
* @NoCSRFRequired
*/
public function search($query = '', $count = 50, $offset = 0, $levels = '11111') {
$iterator = $this->logIteratorFactory->getLogIterator($levels);
$iterator = new \LimitIterator($iterator, 0, 100000); // limit the number of message we search to avoid huge search times
$iterator->rewind();
$iterator = new SearchFilter($iterator, $query);
$iterator->rewind();
return $this->responseFromIterator($iterator, $count, $offset);
}
/**
* @AuthorizedAdminSetting(settings=OCA\LogReader\Settings\Admin)
*/
public function getLevels(): JSONResponse {
return new JSONResponse($this->config->getAppValue('logreader', 'levels', '11111'));
}
/**
* @AuthorizedAdminSetting(settings=OCA\LogReader\Settings\Admin)
*/
public function getSettings(): JSONResponse {
return new JSONResponse([
'levels' => $this->config->getAppValue('logreader', 'levels', '11111'),
'dateformat' => $this->config->getSystemValue('logdateformat', \DateTime::ISO8601),
'timezone' => $this->config->getSystemValue('logtimezone', 'UTC'),
'relativedates' => (bool)$this->config->getAppValue('logreader', 'relativedates', false),
'live' => (bool)$this->config->getAppValue('logreader', 'live', true),
]);
}
/**
* @AuthorizedAdminSetting(settings=OCA\LogReader\Settings\Admin)
* @param bool $relative
*/
public function setRelative($relative) {
$this->config->setAppValue('logreader', 'relativedates', $relative);
}
/**
* @AuthorizedAdminSetting(settings=OCA\LogReader\Settings\Admin)
* @param bool $live
*/
public function setLive($live) {
$this->config->setAppValue('logreader', 'live', $live);
}
/**
* @AuthorizedAdminSetting(settings=OCA\LogReader\Settings\Admin)
*/
public function setLevels(string $levels): int {
$intLevels = array_map('intval', str_split($levels));
$minLevel = 4;
foreach ($intLevels as $level => $log) {
if ($log) {
$minLevel = $level;
break;
}
}
$this->config->setAppValue('logreader', 'levels', $levels);
return $minLevel;
}
protected function responseFromIterator(\Iterator $iterator, $count, $offset) {
$iterator->rewind();
for ($i = 0; $i < $offset; $i++) {
$iterator->next();
}
$data = [];
for ($i = 0; $i < $count && $iterator->valid(); $i++) {
$line = $iterator->current();
if (!is_null($line)) {
$line["id"] = uniqid();
$data[] = $line;
}
$iterator->next();
}
return new JSONResponse([
'data' => $data,
'remain' => $iterator->valid()
]);
}
}