Skip to content

Commit

Permalink
Merge pull request #3 from zunnu/dev
Browse files Browse the repository at this point in the history
Pagination
  • Loading branch information
zunnu authored Oct 9, 2022
2 parents 4978009 + 1fc056f commit 288a2b4
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 37 deletions.
21 changes: 19 additions & 2 deletions src/Controller/LogReaderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@

class LogReaderController extends AppController
{
/**
* initialize method
*
* @return void
*/
public function initialize() {
parent::initialize();
}
Expand All @@ -26,10 +31,22 @@ public function index($date = null) {
}

$this->Reader = new Reader($conditions);
$this->set('logs', $this->Reader->read());
$logs = $this->Reader->read();

// paginate
$pagination = [
'limit' => !empty($conditions['limit']) ? $conditions['limit'] : 100,
'total' => sizeof($logs),
'page' => !empty($conditions['page']) ? $conditions['page'] : 1,
];
$pagination['pages'] = ceil($pagination['total'] / $pagination['limit']);
$pagination['offset'] = ($pagination['page'] * $pagination['limit']) - $pagination['limit'];
$logs = array_slice($logs, $pagination['offset'], $pagination['limit'], true);

$this->set(compact('logs', 'pagination'));
$this->set('files', $this->Reader->getFiles());
$this->set('types', $this->Reader->getLogTypes());
$this->set('selectedFiles', !empty($conditions['files']) ? $conditions['files'] : ['debug.log', 'error.log']);
$this->set('selectedFiles', !empty($conditions['files']) ? $conditions['files'] : []);
$this->set('selectedTypes', !empty($conditions['types']) ? $conditions['types'] : []);
}
}
65 changes: 57 additions & 8 deletions src/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,20 @@
use Cake\Filesystem\Folder;
use Cake\Filesystem\File;

/**
* Reader class
*/
class Reader {
// prefixes
protected $config = [];
/*
* Class config
* Options:
* Types: Used to filter the results. Available types are in $logTypes
* Files: The files where to read the logs from
*/
protected $config = [
'types' => [],
'files' => [],
];

private $logTypes = [
'info' => 'Info',
Expand All @@ -23,6 +34,10 @@ public function __construct($config = []) {
$this->config = $config;
}

/**
* Get the date of the files
* @return array List of different dates of files
*/
public function getFileDates() {
$dates = [];
$folder = new Folder(LOGS);
Expand All @@ -42,7 +57,11 @@ public function getFileDates() {
return array_unique($dates);
}

// return the path to the model dir
/**
* Main reader function
* The files and types that are parsed need to be set in config
* @return array List of logs
*/
public function read() {
$date = !empty($this->config['date']) ? $this->config['date'] : null;
$selectedTypes = !empty($this->config['types']) ? $this->config['types'] : [];
Expand Down Expand Up @@ -83,23 +102,34 @@ public function read() {
return $logs;
}

/**
* Get logs inside file or files
* @param array $selectedFiles List of files to get the logs from
* @return array Content of the selected files
*/
private function getLogFile($selectedFiles = []) {
$folder = new Folder(LOGS);
$files = $folder->findRecursive('.*', true);
$data = [];

// load default files
if(empty($selectedFiles)) {
$selectedFiles = ['debug.log', 'error.log'];
return [];
// $selectedFiles = ['debug.log', 'error.log'];
}

if(!empty($files)) {
foreach($files as $file) {
$file = new File($file);
$info = $file->info();
$path = null;

// check if file is under a folder inside logs folder
if(strpos($info['dirname'], 'logs/') !== false) {
$path = substr($info['dirname'], strrpos($info['dirname'], '/') + 1);
}

if(!empty($selectedFiles)) {
if(!in_array($info['basename'], $selectedFiles)) {
if(!in_array((!empty($path) ? $path . '/' : '') . $info['basename'], $selectedFiles)) {
continue;
}
}
Expand Down Expand Up @@ -131,6 +161,10 @@ private function getLogFile($selectedFiles = []) {
return false;
}

/**
* Get list of log files inside the logs folder
* @return array List of files
*/
public function getFiles() {
$filesList = [];
$folder = new Folder(LOGS);
Expand All @@ -141,10 +175,16 @@ public function getFiles() {
$file = new File($file);
$date = date('Y-m-d H:i:s', $file->lastChange());
$info = $file->info();
$path = null;

// check if file is under a folder inside logs folder
if(strpos($info['dirname'], 'logs/') !== false) {
$path = substr($info['dirname'], strrpos($info['dirname'], '/') + 1);
}

if($date) {
$filesList[] = [
'name' => $info['basename'],
'name' => (!empty($path) ? $path . '/' : '') . $info['basename'],
'date' => $date,
'type' => strpos($file->name(), 'cli-debug') !== false || strpos($file->name(), 'cli-error') !== false ? 'cli' : 'app',
];
Expand All @@ -155,7 +195,12 @@ public function getFiles() {
return $filesList;
}

// move this to regex later
/**
* Parse log file content
* Move this to use regex later
* @param array $data Content of log file
* @return array Parsed data with type, date and content
*/
private function _parseData($data) {
$data = preg_split("/\r\n|\n|\r/", $data);
$buildData = [];
Expand Down Expand Up @@ -209,6 +254,10 @@ private function _parseData($data) {
return $buildData;
}

/**
* Return available log file types
* @return array
*/
public function getLogTypes() {
return $this->logTypes;
}
Expand Down
119 changes: 92 additions & 27 deletions src/Template/LogReader/index.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
}
}

$selectedFiles = json_encode($selectedFiles);
$selectedTypes = json_encode($selectedTypes);

function getBadgeClass($type) {
$type = strtolower($type);

Expand All @@ -27,6 +24,8 @@

return '';
}

$queryParams = $this->getRequest()->getQueryParams();
?>

<!DOCTYPE html>
Expand All @@ -51,8 +50,8 @@
</style>

<script type="text/javascript">
var selectedFiles = <?= $selectedFiles ?>;
var selectedTypes = <?= $selectedTypes ?>;
var selectedFiles = <?= json_encode($selectedFiles) ?>;
var selectedTypes = <?= json_encode($selectedTypes) ?>;
</script>

<body>
Expand Down Expand Up @@ -87,7 +86,21 @@
'onchange' => 'this.form.submit()',
'class' => 'form-control',
'templates' => [
'inputContainer' => '<div class="form-group col-md-6 col-lg-6">{{content}}</div>'
'inputContainer' => '<div class="form-group col-md-4 col-lg-4">{{content}}</div>'
]
]); ?>

<?= $this->Form->control('limit', [
'label' => 'Limit',
'required' => false,
'id' => 'limit',
'options' => ['25' => '25', '50' => '50', '100' => '100', '500' => '500', '1000' => '1000'],
'default' => '100',
'value' => $pagination['limit'],
'onchange' => 'this.form.submit()',
'class' => 'form-control',
'templates' => [
'inputContainer' => '<div class="form-group col-md-2 col-lg-2">{{content}}</div>'
]
]); ?>
</div>
Expand All @@ -97,27 +110,79 @@

<div class="row">
<div class="col-md-12">
<div class="table-responsive">
<table class="table table-white-bordered">
<thead class="thead-dark">
<tr>
<th width="200" scope="col">Date</th>
<th width="120" scope="col">Type</th>
<th width="1100" scope="col">Message</th>
</tr>
</thead>
<tbody>
<?php foreach ($logs as $log): ?>
<?php $badgeClass = getBadgeClass($log['type']); ?>
<tr>
<td><?= $log['date'] ?></td>
<td class="badge <?= $badgeClass ?>"><?= $log['type'] ?></td>
<td><?= $log['message'] ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php if(!empty($selectedFiles)) : ?>
<div class="table-responsive">
<table class="table table-white-bordered">
<thead class="thead-dark">
<tr>
<th width="200" scope="col">Date</th>
<th width="120" scope="col">Type</th>
<th width="1100" scope="col">Message</th>
</tr>
</thead>
<tbody>
<?php foreach ($logs as $log): ?>
<?php $badgeClass = getBadgeClass($log['type']); ?>
<tr>
<td><?= $log['date'] ?></td>
<td class="badge <?= $badgeClass ?>"><?= $log['type'] ?></td>
<td><?= $log['message'] ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>

<nav aria-label="Page navigation">
<ul class="pagination justify-content-center">
<?php
$pageLimit = $pagination['page'] + 10;
$i = $pagination['page'] - 10;
if($i < 1) $i = 1;
?>

<?php if($pagination['page'] > 1) : ?>
<li class="page-item <?= ($pagination['page'] == 1) ? 'disabled' : ''; ?>">
<?php $queryParams['page'] = 1; ?>
<a class="page-link" href="<?= '?' . http_build_query($queryParams); ?>">First</a>
</li>
<?php endif; ?>

<li class="page-item <?= ($pagination['page'] == 1) ? 'disabled' : ''; ?>">
<?php $queryParams['page'] = $pagination['page'] - 1; ?>
<a class="page-link" href="<?= '?' . http_build_query($queryParams); ?>">Previous</a>
</li>

<?php for($i; $i <= $pagination['pages']; $i++) : ?>
<?php
if($i > $pageLimit) break;
$queryParams['page'] = $i;
$buildParams = '?' . http_build_query($queryParams);
?>
<li class="page-item <?= ($pagination['page'] == $i) ? 'active' : ''; ?>">
<a class="page-link" href="<?= $buildParams ?>"><?= $i ?></a>
</li>
<?php endfor; ?>

<?php if(($pagination['page'] + 1) <= $pagination['pages']) : ?>
<?php $queryParams['page'] = ++$pagination['page']; ?>
<li class="page-item <?= ($pagination['page'] >= $pagination['pages']) ? 'disabled' : '' ?>">
<a class="page-link" href="<?= '?' . http_build_query($queryParams); ?>">Next</a>
</li>
<?php endif; ?>

<?php if($pagination['page'] < $pagination['pages']) : ?>
<?php $queryParams['page'] = $pagination['pages']; ?>
<li class="page-item <?= ($pagination['page'] >= $pagination['pages']) ? 'disabled' : '' ?>">
<a class="page-link" href="<?= '?' . http_build_query($queryParams); ?>">Last</a>
</li>
<?php endif; ?>
</ul>
</nav>

<?php else : ?>
<h3><?= __d('LogReader', 'Please select one or more files for viewing') ?></h3>
<?php endif; ?>
</div>
</div>
</div>
Expand Down

0 comments on commit 288a2b4

Please sign in to comment.