Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add event logs deletion with filters #1252

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,34 @@ package-lock.json

# Ignore generated public directory from `winter:mirror public`
public
composer.json
plugins/winter/debugbar/.gitignore
plugins/winter/debugbar/icon.png
plugins/winter/debugbar/LICENSE
plugins/winter/debugbar/Plugin.php
plugins/winter/debugbar/README.md
plugins/winter/debugbar/assets/css/debugbar.css
plugins/winter/debugbar/assets/less/debugbar.less
plugins/winter/debugbar/assets/less/vendor.less
plugins/winter/debugbar/classes/JavascriptRenderer.php
plugins/winter/debugbar/classes/ServiceProvider.php
plugins/winter/debugbar/classes/WinterDebugbar.php
plugins/winter/debugbar/collectors/BackendCollector.php
plugins/winter/debugbar/collectors/CmsCollector.php
plugins/winter/debugbar/collectors/ComponentsCollector.php
plugins/winter/debugbar/collectors/ModelsCollector.php
plugins/winter/debugbar/config/config.php
plugins/winter/debugbar/lang/en/lang.php
plugins/winter/debugbar/lang/ru/lang.php
plugins/winter/debugbar/lang/sl/lang.php
plugins/winter/debugbar/middleware/InjectDebugbar.php
plugins/winter/debugbar/middleware/InterpretsAjaxExceptions.php
plugins/winter/debugbar/screenshots/collapsed.png
plugins/winter/debugbar/screenshots/expanded-queries.png
plugins/winter/debugbar/screenshots/expanded-timeline.png
plugins/winter/debugbar/twig/extension/Debug.php
plugins/winter/debugbar/twig/extension/Dump.php
plugins/winter/debugbar/twig/extension/Stopwatch.php
plugins/winter/debugbar/twig/node/StopwatchNode.php
plugins/winter/debugbar/twig/tokenparser/StopwatchTokenParser.php
plugins/winter/debugbar/updates/version.yaml
123 changes: 120 additions & 3 deletions modules/system/controllers/EventLogs.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?php namespace System\Controllers;

use App;
use Lang;
use Flash;
use BackendMenu;
use Backend\Classes\Controller;
use Backend\Facades\BackendMenu;
use Illuminate\Support\Facades\Lang;
use Illuminate\Support\Str;
use System\Classes\SettingsManager;
use System\Models\EventLog;
use Winter\Storm\Support\Facades\Flash;

/**
* Event Logs controller
Expand Down Expand Up @@ -45,6 +46,122 @@ public function index_onRefresh()
return $this->listRefresh();
}

/**
* Show a popup to ask user his choices about clearing the log
*/
public function index_onClearLog()
{
$config = $this->makeConfig([
'fields' => [
'message' => [
'type' => 'text',
'label' => 'system::lang.event_log.delete_filtered_search',
'comment' => 'system::lang.event_log.delete_filtered_search_comment',
'span' => 'full',
],
'date_start' => [
'type' => 'datepicker',
'label' => 'system::lang.event_log.delete_filtered_date_start',
'span' => 'left',
'mode' => 'datetime',
],
'date_end' => [
'type' => 'datepicker',
'label' => 'system::lang.event_log.delete_filtered_date_end',
'span' => 'right',
'mode' => 'datetime',
],
],
]);
$config->model = new EventLog;

$formWidget = $this->makeWidget('Backend\Widgets\Form', $config);
$formWidget->bindToController();

return $this->makePartial('delete_filtered_form', [
'formWidget' => $formWidget,
]);
}

/**
* Return informations about the log to be cleared
*/
public function index_onClearLogInfos()
{
$message = post('message');
$dateStart = post('date_start');
$dateEnd = post('date_end');

if (!$message && !$dateStart && !$dateEnd) {
return [
'#deleteFilteredList' => '',
'#deleteFilteredResults' => '',
];
}

$events = EventLog
::when($message, fn ($q) => $q->where('message', 'like', '%' . str_replace(
['\\', '%', '_'],
['\\\\', '\\%', '\\_'],
$message
) . '%'))
->when($dateStart, fn ($q) => $q->where('created_at', '>=', $dateStart))
->when($dateEnd, fn ($q) => $q->where('created_at', '<=', $dateEnd))
->orderBy('created_at', 'desc')
->get();

$config = $this->makeConfig('~/modules/system/models/eventlog/columns.yaml');
$config->model = new EventLog;

$controlList = $this->makeWidget('Backend\Widgets\Lists', $config);
$controlList->bindEvent('list.extendRecords', function ($records) use ($events, $message) {
if ($events->count() > 0) {
return $events;
}
return collect([]);
});

$controlList->bindToController();

return [
'total' => $events->count(),
'#deleteFilteredList' => $controlList->render(),
'#deleteFilteredResults' => Lang::get('system::lang.event_log.delete_filtered_results', [
'what' => mb_strtolower(Str::plural(Lang::get('system::lang.event_log.preview_title'), $events->count())),
'count' => $events->count(),
])
];
}

/**
* Delete the filtered events log
*/
public function index_onClearLogDelete()
{
$message = post('message');
$dateStart = post('date_start');
$dateEnd = post('date_end');
$eventLog = new EventLog;

$eventLog = EventLog
::when($message, fn ($q) => $q->where('message', 'like', '%' . str_replace(
['\\', '%', '_'],
['\\\\', '\\%', '\\_'],
$message
) . '%'))
->when($dateStart, fn ($q) => $q->where('created_at', '>=', $dateStart))
->when($dateEnd, fn ($q) => $q->where('created_at', '<=', $dateEnd));

$events = $eventLog->get();
$eventLog->delete();

Flash::success(Lang::get('system::lang.event_log.delete_filtered_success', [
'what' => mb_strtolower(Str::plural(Lang::get('system::lang.event_log.preview_title'), $events->count())),
'count' => $events->count(),
]));
return $this->listRefresh();
}

public function index_onEmptyLog()
{
EventLog::truncate();
Expand Down
118 changes: 118 additions & 0 deletions modules/system/controllers/eventlogs/_delete_filtered_form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<div id="deleteFilteredPopup">
<?= Form::open(['id' => 'deleteFiltered']) ?>
<div class="modal-header">
<button type="button" class="close" data-dismiss="popup">&times;</button>
<h4 class="modal-title"><?= e(trans('system::lang.event_log.delete_filtered_link')) ?></h4>
</div>

<div class="modal-body clearfix">
<?php if ($this->fatalError): ?>
<p class="flash-message static error"><?= e($fatalError) ?></p>
<?php endif ?>

<?= $formWidget->render(); ?>

<div class="loading-indicator-container">
<div class="loading-indicator hidden">
<span></span>
<div><?= e(trans('system::lang.event_log.delete_filtered_loading')) ?></div>
</div>

<div id="deleteFilteredResults" class="form-group span-full"></div>
<div id="deleteFilteredList" class="form-group span-full"></div>
</div>

</div>

<div class="modal-footer">
<button
type="button"
class="btn btn-default"
data-dismiss="popup">
<?= e(trans('backend::lang.form.cancel')) ?>
</button>

<button
data-name="submit"
class="btn btn-success hidden"
data-request="onClearLogDelete"
data-request-confirm="<?= e(trans('system::lang.event_log.delete_filtered_confirm')) ?>"
data-request-success="$(this).trigger('close.oc.popup');"
data-stripe-load-indicator>
<?= e(trans('system::lang.event_log.delete_filtered_link')) ?>
</button>
</div>

<?= Form::close() ?>
</div>

<script>
$('#deleteFilteredPopup').on('popupComplete', function() {

var $loadingIndicator = $('#deleteFilteredPopup .loading-indicator-container .loading-indicator');
var $resultsInfos = $('#deleteFilteredResults');
var $resultsList = $('#deleteFilteredList');

var $inputMessage = $('#deleteFiltered :input[name="message"]');
$inputMessage.defaultValue = $inputMessage.val();

function refreshResults () {
showLoadingIndicator();

Snowboard.request('#deleteFiltered', 'onClearLogInfos', {
success: (data) => {
const total = data.total;

if (total > 0) {
$('#deleteFiltered button[data-name="submit"]').removeClass('hidden');
} else {
$('#deleteFiltered button[data-name="submit"]').addClass('hidden');
}

hideLoadingIndicator();
},
error: (data) => {
hideLoadingIndicator()
},
});
};

function showLoadingIndicator () {
$loadingIndicator.removeClass('hidden');
$resultsInfos.addClass('hidden');
$resultsList.addClass('hidden');
}

function hideLoadingIndicator () {
$loadingIndicator.addClass('hidden');
$resultsInfos.removeClass('hidden');
$resultsList.removeClass('hidden');
}

function delay(callback, ms) {
var timer = 0;
return function() {
var context = this, args = arguments;
clearTimeout(timer);
timer = setTimeout(function () {
callback.apply(context, args);
}, ms || 0);
};
}

// Monitor change on message field
$inputMessage.keyup(delay(function (e) {
if (e.target.value != e.target.defaultValue) {
e.target.defaultValue = this.value;
refreshResults();
}
}, 750));

// Monitor change on datepicker fields
$('#deleteFiltered').on('change.oc.formwidget', '.field-datepicker', delay(function (e) {
if (e.target.name != 'message') {
refreshResults();
}
}, 400));
})
</script>
9 changes: 9 additions & 0 deletions modules/system/controllers/eventlogs/_list_toolbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@
class="btn btn-primary wn-icon-refresh">
<?= e(trans('backend::lang.list.refresh')) ?>
</a>
<a
href="javascript:;"
data-handler="onClearLog"
data-control="popup"
data-size="huge"
data-attach-loading
class="btn btn-default wn-icon-filter-circle-xmark">
<?= e(trans('system::lang.event_log.delete_filtered_link')) ?>
</a>
<a
href="javascript:;"
data-request="onEmptyLog"
Expand Down
9 changes: 9 additions & 0 deletions modules/system/lang/en/lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,15 @@
'message' => 'Message',
'level' => 'Level',
'preview_title' => 'Event',
'delete_filtered_confirm' => 'Delete the filtered events?',
'delete_filtered_date_start' => 'Starting date',
'delete_filtered_date_end' => 'Ending date',
'delete_filtered_link' => 'Delete filtered events',
'delete_filtered_loading' => 'Search for events matching your criteria',
'delete_filtered_search' => 'Messages containing',
'delete_filtered_success' => ':count :what successfully deleted',
'delete_filtered_search_comment' => 'select all event log messages containing the following text',
'delete_filtered_results' => '<b>:count</b> :what matching your criteria',
],
'request_log' => [
'hint' => 'This log displays a list of browser requests that may require attention. For example, if a visitor opens a CMS page that cannot be found, a record is created with the status code 404.',
Expand Down
9 changes: 9 additions & 0 deletions modules/system/lang/fr/lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,15 @@
'message' => 'Message',
'level' => 'Niveau',
'preview_title' => 'Évènement',
'delete_filtered_confirm' => 'Supprimer les évènement filtrés?',
'delete_filtered_date_start' => 'Depuis le',
'delete_filtered_date_end' => 'Jusqu\'au',
'delete_filtered_link' => 'Supprimer des évènements',
'delete_filtered_loading' => 'Recherche des évènements correspondant à vos critères',
'delete_filtered_search' => 'Message contenant',
'delete_filtered_success' => ':count :what supprimés avec succès',
'delete_filtered_search_comment' => 'sélectionner tous les messages du journal des événements contenant le texte suivant',
'delete_filtered_results' => '<b>:count</b> :what correspondant à votre recherche',
],
'request_log' => [
'hint' => 'Ce journal affiche une liste de requêtes potentiellement suspectes. Par exemple, si un visiteur ouvre une page introuvable du CMS, une ligne avec le code statut 404 est alors créée.',
Expand Down
1 change: 1 addition & 0 deletions plugins/hounddd/testbatches
Submodule testbatches added at 2fba53
Loading