chg: [cli] Show stats for access logs

pull/8749/head
Jakub Onderka 2022-11-12 20:15:43 +01:00
parent b3afa724f9
commit 50428e5c0b
3 changed files with 34 additions and 7 deletions

View File

@ -3,23 +3,27 @@
/**
* @property Log $Log
* @property AuditLog $AuditLog
* @property AccessLog $AccessLog
* @property Server $Server
*/
class LogShell extends AppShell
{
public $uses = ['Log', 'AuditLog', 'Server'];
public $uses = ['Log', 'AuditLog', 'AccessLog', 'Server'];
public function getOptionParser()
{
$parser = parent::getOptionParser();
$parser->addSubcommand('auditStatistics', [
'help' => __('Show statistics from audit logs.'),
'help' => __('Show statistics for audit logs.'),
]);
$parser->addSubcommand('accessStatistics', [
'help' => __('Show statistics for access logs.'),
]);
$parser->addSubcommand('statistics', [
'help' => __('Show statistics from logs.'),
'help' => __('Show statistics for application logs.'),
]);
$parser->addSubcommand('export', [
'help' => __('Export logs to compressed file in JSON Lines format (one JSON encoded line per entry).'),
'help' => __('Export application logs to compressed file in JSON Lines format (one JSON encoded line per entry).'),
'parser' => array(
'arguments' => array(
'file' => ['help' => __('Path to output file'), 'required' => true],
@ -77,7 +81,7 @@ class LogShell extends AppShell
if ($log['id'] > $lastId) {
$lastId = $log['id'];
}
$lines .= json_encode($log, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR) . "\n";
$lines .= JsonTool::encode($log) . "\n";
}
if (gzwrite($file, $lines) === false) {
$this->error("Could not write data to $path");
@ -147,4 +151,28 @@ class LogShell extends AppShell
$this->out(str_pad(__('Uncompressed size:'), 20) . CakeNumber::toReadableSize($this->AuditLog->compressionStats['bytes_uncompressed']));
$this->out(str_pad(__('Compressed size:'), 20) . CakeNumber::toReadableSize($this->AuditLog->compressionStats['bytes_compressed']));
}
public function accessStatistics()
{
$count = $this->AccessLog->find('count');
$first = $this->AccessLog->find('first', [
'recursive' => -1,
'fields' => ['created'],
'order' => ['id ASC'],
]);
$last = $this->AccessLog->find('first', [
'recursive' => -1,
'fields' => ['created'],
'order' => ['id DESC'],
]);
$this->out(str_pad(__('Count:'), 20) . $count);
$this->out(str_pad(__('First:'), 20) . $first['AccessLog']['created']);
$this->out(str_pad(__('Last:'), 20) . $last['AccessLog']['created']);
$usage = $this->Server->dbSpaceUsage()['access_logs'];
$this->out(str_pad(__('Data size:'), 20) . CakeNumber::toReadableSize($usage['data_in_bytes']));
$this->out(str_pad(__('Index size:'), 20) . CakeNumber::toReadableSize($usage['index_in_bytes']));
$this->out(str_pad(__('Reclaimable size:'), 20) . CakeNumber::toReadableSize($usage['reclaimable_in_bytes']), 2);
}
}

View File

@ -61,7 +61,6 @@ class Log extends AppModel
'registration',
'registration_error',
'remove_dead_workers',
'request',
'request_delegation',
'reset_auth_key',
'send_mail',

View File

@ -1,5 +1,5 @@
<div class="logs index">
<h2><?php echo __('Logs');?></h2>
<h2><?php echo __('Application Logs');?></h2>
<?php
if ($isSearch == 1) {
echo "<h4>" . __("Results for all log entries");