Skip to content

Commit fcfe79a

Browse files
committed
add log filtering
1 parent 9b58fd9 commit fcfe79a

File tree

8 files changed

+285
-12
lines changed

8 files changed

+285
-12
lines changed

appinfo/routes.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@
1111
return ['routes' => [
1212
// page
1313
['name' => 'page#index', 'url' => '/', 'verb' => 'GET'],
14+
['name' => 'log#get', 'url' => '/get', 'verb' => 'GET'],
15+
['name' => 'log#search', 'url' => '/search', 'verb' => 'GET'],
1416
]];

controller/logcontroller.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
/**
3+
* @author Robin Appelman <[email protected]>
4+
*
5+
* @copyright Copyright (c) 2015, ownCloud, Inc.
6+
* @license AGPL-3.0
7+
*
8+
* This code is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Affero General Public License, version 3,
10+
* as published by the Free Software Foundation.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU Affero General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Affero General Public License, version 3,
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>
19+
*
20+
*/
21+
22+
namespace OCA\LogReader\Controller;
23+
24+
use OCA\LogReader\Log\LogIterator;
25+
use OCA\LogReader\Log\SearchFilter;
26+
use OCP\AppFramework\Controller;
27+
use OCP\AppFramework\Http\JSONResponse;
28+
use OCP\AppFramework\Http\TemplateResponse;
29+
30+
/**
31+
* Class LogController
32+
*
33+
* @package OCA\LogReader\Controller
34+
*/
35+
class LogController extends Controller {
36+
/**
37+
* @param int $count
38+
* @param int $offset
39+
* @return TemplateResponse
40+
*/
41+
public function get($count = 50, $offset = 0) {
42+
$iterator = new LogIterator(fopen(\OC_Log_Owncloud::getLogFilePath(), 'rb'));
43+
return $this->responseFromIterator($iterator, $count, $offset);
44+
}
45+
46+
/**
47+
* @param string $query
48+
* @param int $count
49+
* @param int $offset
50+
* @return TemplateResponse
51+
*/
52+
public function search($query = '', $count = 50, $offset = 0) {
53+
$iterator = new LogIterator(fopen(\OC_Log_Owncloud::getLogFilePath(), 'rb'));
54+
$iterator = new SearchFilter($iterator, $query);
55+
return $this->responseFromIterator($iterator, $count, $offset);
56+
}
57+
58+
protected function responseFromIterator(\Iterator $iterator, $count, $offset) {
59+
for ($i = 0; $i < $offset; $i++) {
60+
$iterator->next();
61+
}
62+
$data = [];
63+
for ($i = 0; $i < $count; $i++) {
64+
$line = $iterator->current();
65+
if (!is_null($line)) {
66+
$data[] = $line;
67+
}
68+
$iterator->next();
69+
}
70+
return new JSONResponse([
71+
'data' => $data,
72+
'remain' => $iterator->valid()
73+
]);
74+
}
75+
}

controller/pagecontroller.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
<?php
22
/**
3-
* ownCloud - Notes
3+
* @author Robin Appelman <[email protected]>
44
*
5-
* This file is licensed under the Affero General Public License version 3 or
6-
* later. See the COPYING file.
5+
* @copyright Copyright (c) 2015, ownCloud, Inc.
6+
* @license AGPL-3.0
7+
*
8+
* This code is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Affero General Public License, version 3,
10+
* as published by the Free Software Foundation.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU Affero General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Affero General Public License, version 3,
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>
719
*
8-
* @author Bernhard Posselt <[email protected]>
9-
* @copyright Bernhard Posselt 2012, 2014
1020
*/
1121

1222
namespace OCA\LogReader\Controller;
@@ -21,7 +31,6 @@
2131
*/
2232
class PageController extends Controller {
2333
/**
24-
* @NoAdminRequired
2534
* @NoCSRFRequired
2635
*
2736
* @return TemplateResponse

js/App.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import {LogProvider} from './Providers/LogProvider.js';
66
import {LogTable} from './Components/LogTable.js';
77
import {SideBar} from './Components/SideBar.js';
88

9+
import {LogSearch} from './Search.js';
10+
911
export class App extends Component {
1012
state = {
1113
'entries': [],
@@ -18,6 +20,7 @@ export class App extends Component {
1820
this.logProvider.on('entries', entries => {
1921
this.setState({entries});
2022
});
23+
OCA.Search.logreader = new LogSearch(this.logProvider);
2124
this.logProvider.load();
2225
}
2326

js/Providers/LogProvider.js

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,37 @@ export class LogProvider extends EventEmitter {
55

66
cachedEntries = [];
77

8-
_limit = 0;
9-
108
constructor (limit = 50) {
119
super();
10+
this.baseLimit = limit;
1211
this.loading = false;
1312
this.limit = limit;
13+
this.searchQuery = '';
14+
}
15+
16+
reset () {
17+
this.limit = this.baseLimit;
18+
this.cachedEntries = [];
19+
this.loading = false;
1420
}
1521

1622
get entries () {
1723
return cachedEntries;
1824
}
1925

26+
set query (newQuery) {
27+
console.log(newQuery);
28+
if (newQuery !== this.searchQuery) {
29+
this.searchQuery = newQuery;
30+
this.reset();
31+
this.load();
32+
}
33+
}
34+
35+
get query () {
36+
return this.searchQuery;
37+
}
38+
2039
async load () {
2140
this.loading = true;
2241
if (this.cachedEntries.length >= this.limit) {
@@ -30,9 +49,17 @@ export class LogProvider extends EventEmitter {
3049
}
3150

3251
loadEntries (offset, count = 50) {
33-
return $.get(OC.generateUrl('/settings/admin/log/entries'), {
34-
offset,
35-
count
36-
});
52+
if (this.searchQuery) {
53+
return $.get(OC.generateUrl('/apps/logreader/search'), {
54+
offset,
55+
count,
56+
query: this.query
57+
});
58+
} else {
59+
return $.get(OC.generateUrl('/apps/logreader/get'), {
60+
offset,
61+
count
62+
});
63+
}
3764
}
3865
}

js/Search.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
export class LogSearch {
2+
/**
3+
* @param {LogProvider} provider
4+
*/
5+
constructor (provider) {
6+
this.provider = provider;
7+
this.initialize();
8+
}
9+
10+
initialize () {
11+
OC.Plugins.register('OCA.Search', this);
12+
}
13+
14+
attach (search) {
15+
search.setFilter('logreader', _.debounce((query) => {
16+
if (query.length >= 3 || query === '') {
17+
this.provider.query = query;
18+
}
19+
}, 250));
20+
}
21+
}

log/logiterator.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
/**
3+
* @author Robin Appelman <[email protected]>
4+
*
5+
* @copyright Copyright (c) 2015, ownCloud, Inc.
6+
* @license AGPL-3.0
7+
*
8+
* This code is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Affero General Public License, version 3,
10+
* as published by the Free Software Foundation.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU Affero General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Affero General Public License, version 3,
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>
19+
*
20+
*/
21+
22+
namespace OCA\LogReader\Log;
23+
24+
class LogIterator implements \Iterator {
25+
/**
26+
* @var resource
27+
*/
28+
private $handle;
29+
30+
/**
31+
* @var int
32+
*/
33+
private $position = 0;
34+
35+
/**
36+
* @var string
37+
*/
38+
private $lastLine;
39+
40+
private $currentKey = -1;
41+
42+
/**
43+
* @param resource $handle
44+
*/
45+
public function __construct($handle) {
46+
$this->handle = $handle;
47+
$this->rewind();
48+
$this->next();
49+
}
50+
51+
function rewind() {
52+
fseek($this->handle, 0, SEEK_END);
53+
$this->position = ftell($this->handle);
54+
$this->currentKey = 0;
55+
}
56+
57+
function current() {
58+
return json_decode($this->lastLine);
59+
}
60+
61+
function key() {
62+
return $this->currentKey;
63+
}
64+
65+
function next() {
66+
$line = '';
67+
// Loop through each character of the file looking for new lines
68+
while ($this->position >= 0) {
69+
fseek($this->handle, $this->position);
70+
$ch = fgetc($this->handle);
71+
if ($ch == "\n" || $this->position === 0) {
72+
if ($line != '') {
73+
// Add the first character if at the start of the file,
74+
// because it doesn't hit the else in the loop
75+
if ($this->position === 0) {
76+
$line = $ch . $line;
77+
}
78+
$this->lastLine = $line;
79+
$this->currentKey++;
80+
return;
81+
}
82+
} else {
83+
$line = $ch . $line;
84+
}
85+
$this->position--;
86+
}
87+
}
88+
89+
function valid() {
90+
return $this->position >= 0;
91+
}
92+
}

log/searchfilter.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/**
3+
* @author Robin Appelman <[email protected]>
4+
*
5+
* @copyright Copyright (c) 2015, ownCloud, Inc.
6+
* @license AGPL-3.0
7+
*
8+
* This code is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Affero General Public License, version 3,
10+
* as published by the Free Software Foundation.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU Affero General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Affero General Public License, version 3,
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>
19+
*
20+
*/
21+
22+
namespace OCA\LogReader\Log;
23+
24+
class SearchFilter extends \FilterIterator {
25+
/**
26+
* @var string
27+
*/
28+
private $query;
29+
30+
/**
31+
* @param LogIterator $iterator
32+
* @param string $query
33+
*/
34+
public function __construct(LogIterator $iterator, $query) {
35+
parent::__construct($iterator);
36+
$this->query = strtolower($query);
37+
}
38+
39+
public function accept() {
40+
$value = $this->getInnerIterator()->current();
41+
$value = strtolower($value->message);
42+
return strpos($value, $this->query) !== false;
43+
}
44+
}

0 commit comments

Comments
 (0)