Skip to content

Commit c4d3d77

Browse files
authored
Merge pull request #176 from denydias/cursor-pagination
Implement a high performance paginator
2 parents fad77d4 + 82f943f commit c4d3d77

File tree

7 files changed

+58
-22
lines changed

7 files changed

+58
-22
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ LARAVEL_LOGGER_USER_MODEL=App\User
171171
LARAVEL_LOGGER_USER_ID_FIELD=id
172172
LARAVEL_LOGGER_DISABLE_ROUTES=false
173173
LARAVEL_LOGGER_PAGINATION_ENABLED=true
174+
LARAVEL_LOGGER_CURSOR_PAGINATION_ENABLED=false
174175
LARAVEL_LOGGER_PAGINATION_PER_PAGE=25
175176
LARAVEL_LOGGER_DATATABLES_ENABLED=true
176177
LARAVEL_LOGGER_ENABLE_SEARCH=true
@@ -286,6 +287,10 @@ Route::group(['prefix' => 'activity', 'namespace' => 'jeremykenedy\LaravelLogger
286287

287288
adding dynamic search fields (description , user, URL , method and ip address)
288289

290+
### High Performance Paginator
291+
292+
When dealing with millions activity records, default behavior of not paginate records or [Laravel's paginator](https://laravel.com/docs/pagination#paginating-eloquent-results) (enabled `LARAVEL_LOGGER_PAGINATION_ENABLED=true`) may lead to huge performance penalties. For that use case you may set `LARAVEL_LOGGER_CURSOR_PAGINATION_ENABLED=true` to enable [Laravel's Cursor Pagination](https://laravel.com/docs/pagination#cursor-pagination) feature. This will heavily improve Laravel Logger page loading time. If you choose to do so it's advisable to read [Cursor vs. Offset Pagination](https://laravel.com/docs/pagination#cursor-vs-offset-pagination) section on Laravel's documentation to get acquainted with Cursor Pagination limitations.
293+
289294
##### .env file
290295
add these configurations to your .env file to control the logging search
291296
```

src/App/Http/Controllers/LaravelLoggerController.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,14 @@ private function mapAdditionalDetails($collectionItems)
6767
*/
6868
public function showAccessLog(Request $request)
6969
{
70-
if (config('LaravelLogger.loggerPaginationEnabled')) {
70+
if (config('LaravelLogger.loggerCursorPaginationEnabled')) {
71+
$activities = config('LaravelLogger.defaultActivityModel')::orderBy('created_at', 'desc');
72+
if (config('LaravelLogger.enableSearch')) {
73+
$activities = $this->searchActivityLog($activities, $request);
74+
}
75+
$activities = $activities->cursorPaginate(config('LaravelLogger.loggerPaginationPerPage'))->withQueryString();
76+
$totalActivities = 0;
77+
} elseif (config('LaravelLogger.loggerPaginationEnabled')) {
7178
$activities = config('LaravelLogger.defaultActivityModel')::orderBy('created_at', 'desc');
7279
if (config('LaravelLogger.enableSearch')) {
7380
$activities = $this->searchActivityLog($activities, $request);
@@ -76,7 +83,6 @@ public function showAccessLog(Request $request)
7683
$totalActivities = $activities->total();
7784
} else {
7885
$activities = config('LaravelLogger.defaultActivityModel')::orderBy('created_at', 'desc');
79-
8086
if (config('LaravelLogger.enableSearch')) {
8187
$activities = $this->searchActivityLog($activities, $request);
8288
}
@@ -122,7 +128,12 @@ public function showAccessLogEntry(Request $request, $id)
122128
$eventTime = Carbon::parse($activity->created_at);
123129
$timePassed = $eventTime->diffForHumans();
124130

125-
if (config('LaravelLogger.loggerPaginationEnabled')) {
131+
if (config('LaravelLogger.loggerCursorPaginationEnabled')) {
132+
$userActivities = config('LaravelLogger.defaultActivityModel')::where('userId', $activity->userId)
133+
->orderBy('created_at', 'desc')
134+
->cursorPaginate(config('LaravelLogger.loggerPaginationPerPage'));
135+
$totalUserActivities = 0;
136+
} elseif (config('LaravelLogger.loggerPaginationEnabled')) {
126137
$userActivities = config('LaravelLogger.defaultActivityModel')::where('userId', $activity->userId)
127138
->orderBy('created_at', 'desc')
128139
->paginate(config('LaravelLogger.loggerPaginationPerPage'));
@@ -175,7 +186,12 @@ public function clearActivityLog(Request $request)
175186
*/
176187
public function showClearedActivityLog()
177188
{
178-
if (config('LaravelLogger.loggerPaginationEnabled')) {
189+
if (config('LaravelLogger.loggerCursorPaginationEnabled')) {
190+
$activities = config('LaravelLogger.defaultActivityModel')::onlyTrashed()
191+
->orderBy('created_at', 'desc')
192+
->paginate(config('LaravelLogger.loggerPaginationPerPage'));
193+
$totalActivities = 0;
194+
} elseif (config('LaravelLogger.loggerPaginationEnabled')) {
179195
$activities = config('LaravelLogger.defaultActivityModel')::onlyTrashed()
180196
->orderBy('created_at', 'desc')
181197
->paginate(config('LaravelLogger.loggerPaginationPerPage'));

src/config/laravel-logger.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
|--------------------------------------------------------------------------
9191
*/
9292
'loggerPaginationEnabled' => env('LARAVEL_LOGGER_PAGINATION_ENABLED', true),
93+
'loggerCursorPaginationEnabled' => env('LARAVEL_LOGGER_CURSOR_PAGINATION_ENABLED', false),
9394
'loggerPaginationPerPage' => env('LARAVEL_LOGGER_PAGINATION_PER_PAGE', 25),
9495

9596
/*

src/resources/views/logger/activity-log-cleared.blade.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,11 @@
7171
<div style="display: flex; justify-content: space-between; align-items: center;">
7272
<span>
7373
{!! trans('LaravelLogger::laravel-logger.dashboardCleared.title') !!}
74-
<sup class="label">
75-
{{ $totalActivities }} {!! trans('LaravelLogger::laravel-logger.dashboardCleared.subtitle') !!}
76-
</sup>
74+
@if(! config('LaravelLogger.loggerCursorPaginationEnabled'))
75+
<sup class="label">
76+
{{ $totalActivities }} {!! trans('LaravelLogger::laravel-logger.dashboardCleared.subtitle') !!}
77+
</sup>
78+
@endif
7779
</span>
7880
<div class="btn-group pull-right btn-group-xs">
7981
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">

src/resources/views/logger/activity-log-item.blade.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -339,9 +339,11 @@
339339
<ul class="list-group">
340340
<li class="list-group-item list-group-item-info">
341341
{!! trans('LaravelLogger::laravel-logger.drilldown.title-user-activity') !!}
342-
<span class="badge">
343-
{{ $totalUserActivities }} {!! trans('LaravelLogger::laravel-logger.dashboard.subtitle') !!}
344-
</span>
342+
@if(! config('LaravelLogger.loggerCursorPaginationEnabled'))
343+
<span class="badge">
344+
{{ $totalUserActivities }} {!! trans('LaravelLogger::laravel-logger.dashboard.subtitle') !!}
345+
</span>
346+
@endif
345347
</li>
346348
<li class="list-group-item">
347349
@include('LaravelLogger::logger.partials.activity-table', ['activities' => $userActivities])

src/resources/views/logger/activity-log.blade.php

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,13 @@
8181

8282
<span>
8383
{!! trans('LaravelLogger::laravel-logger.dashboard.title') !!}
84-
<small>
85-
<sup class="label label-default">
86-
{{ $totalActivities }} {!! trans('LaravelLogger::laravel-logger.dashboard.subtitle') !!}
87-
</sup>
88-
</small>
84+
@if(! config('LaravelLogger.loggerCursorPaginationEnabled'))
85+
<small>
86+
<sup class="label label-default">
87+
{{ $totalActivities }} {!! trans('LaravelLogger::laravel-logger.dashboard.subtitle') !!}
88+
</sup>
89+
</small>
90+
@endif
8991
</span>
9092

9193
<div class="btn-group pull-right btn-group-xs">
@@ -120,12 +122,14 @@
120122

121123
@else
122124
{!! trans('LaravelLogger::laravel-logger.dashboard.title') !!}
123-
<span class="pull-right label label-default">
124-
{{ $totalActivities }}
125-
<span class="hidden-sms">
126-
{!! trans('LaravelLogger::laravel-logger.dashboard.subtitle') !!}
127-
</span>
128-
</span>
125+
@if(! config('LaravelLogger.loggerCursorPaginationEnabled'))
126+
<span class="pull-right label label-default">
127+
{{ $totalActivities }}
128+
<span class="hidden-sms">
129+
{!! trans('LaravelLogger::laravel-logger.dashboard.subtitle') !!}
130+
</span>
131+
</span>
132+
@endif
129133
@endif
130134

131135
</div>

src/resources/views/logger/partials/activity-table.blade.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,13 @@
253253
</table>
254254
</div>
255255

256-
@if(config('LaravelLogger.loggerPaginationEnabled'))
256+
@if(config('LaravelLogger.loggerCursorPaginationEnabled'))
257+
<div class="text-center">
258+
<div class="d-flex justify-content-center">
259+
{!! $activities->links() !!}
260+
</div>
261+
</div>
262+
@elseif(config('LaravelLogger.loggerPaginationEnabled'))
257263
<div class="text-center">
258264
<div class="d-flex justify-content-center">
259265
{!! $activities->links('vendor.pagination.bootstrap-4') !!}

0 commit comments

Comments
 (0)