Skip to content
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
3 changes: 3 additions & 0 deletions config/log-viewer.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,9 @@
// Order to sort the folders. Other options: `Ascending`, `Descending`
'folder_sorting_order' => SortingOrder::Descending,

// Method for sorting logs into directories. Other options: `Alphabetical`, `ModifiedTime`
'log_sorting_method' => FolderSortingMethod::ModifiedTime,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's an important distinction between a "log" and a "file". Logs are individual log entries within a file, and here we want to sort files, not logs.

Call it file_sorting_method

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, I'll correct that


// Order to sort the logs. Other options: `Ascending`, `Descending`
'log_sorting_order' => SortingOrder::Descending,

Expand Down
2 changes: 1 addition & 1 deletion public/app.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/mix-manifest.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"/app.js": "/app.js?id=74d3e481ad3e8fa14f1daaa0aa46e109",
"/app.js": "/app.js?id=0c810fdd27597f4d9fff94af639da625",
"/app.css": "/app.css?id=5593a0331dd40729ff41e32a6035d872",
"/img/log-viewer-128.png": "/img/log-viewer-128.png?id=d576c6d2e16074d3f064e60fe4f35166",
"/img/log-viewer-32.png": "/img/log-viewer-32.png?id=f8ec67d10f996aa8baf00df3b61eea6d",
Expand Down
6 changes: 4 additions & 2 deletions resources/js/components/FileList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@
<div class="text-sm text-gray-500 dark:text-gray-400">
<label for="file-sort-direction" class="sr-only">Sort direction</label>
<select id="file-sort-direction" class="select" v-model="fileStore.direction">
<option value="desc">Newest first</option>
<option value="asc">Oldest first</option>
<option v-if="!LogViewer.log_sort_by_time" value="asc">From A to Z</option>
<option v-if="!LogViewer.log_sort_by_time" value="desc">From Z to A</option>
<option v-if="LogViewer.log_sort_by_time" value="desc">Newest first</option>
<option v-if="LogViewer.log_sort_by_time" value="asc">Oldest first</option>
</select>
</div>
</div>
Expand Down
17 changes: 14 additions & 3 deletions src/Http/Controllers/FilesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\URL;
use Opcodes\LogViewer\Enums\FolderSortingMethod;
use Opcodes\LogViewer\Facades\LogViewer;
use Opcodes\LogViewer\Http\Resources\LogFileResource;

Expand All @@ -13,11 +14,21 @@ class FilesController
public function index(Request $request)
{
$files = LogViewer::getFiles();
$sortingMethod = config('log-viewer.defaults.log_sorting_method', FolderSortingMethod::ModifiedTime);

if ($sortingMethod === FolderSortingMethod::ModifiedTime) {
if ($request->query('direction', 'desc') === 'asc') {
$files = $files->sortByEarliestFirst();
} else {
$files = $files->sortByLatestFirst();
}

if ($request->query('direction', 'desc') === 'asc') {
$files = $files->sortByEarliestFirst();
} else {
$files = $files->sortByLatestFirst();
if ($request->query('direction', 'desc') === 'asc') {
$files = $files->sortAlphabeticallyAsc();
} else {
$files = $files->sortAlphabeticallyDesc();
}
}

return LogFileResource::collection($files);
Expand Down
18 changes: 14 additions & 4 deletions src/Http/Controllers/FoldersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function index(Request $request)
$sortingMethod = config('log-viewer.defaults.folder_sorting_method', FolderSortingMethod::ModifiedTime);
$sortingOrder = config('log-viewer.defaults.folder_sorting_order', SortingOrder::Descending);

$fileSortingMethod = config('log-viewer.defaults.log_sorting_method', FolderSortingMethod::ModifiedTime);
$fileSortingOrder = $request->query('direction', 'desc');

if ($sortingMethod === FolderSortingMethod::Alphabetical) {
Expand All @@ -30,11 +31,20 @@ public function index(Request $request)
}

// Still sort files inside folders by direction param
$folders->each(function ($folder) use ($fileSortingOrder) {
if ($fileSortingOrder === 'asc') {
$folder->files()->sortByEarliestFirst();
$folders->each(function ($folder) use ($fileSortingMethod, $fileSortingOrder) {
if ($fileSortingMethod === FolderSortingMethod::ModifiedTime) {
if ($fileSortingOrder === 'asc') {
$folder->files()->sortByEarliestFirst();
} else {
$folder->files()->sortByLatestFirst();
}

} else {
$folder->files()->sortByLatestFirst();
if ($fileSortingOrder === 'asc') {
$folder->files()->sortAlphabeticallyAsc();
} else {
$folder->files()->sortAlphabeticallyDesc();
}
}
});
} else { // ModifiedTime
Expand Down
4 changes: 4 additions & 0 deletions src/Http/Controllers/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Opcodes\LogViewer\Http\Controllers;

use Opcodes\LogViewer\Enums\FolderSortingMethod;
use Opcodes\LogViewer\Facades\LogViewer;
use Opcodes\LogViewer\LogFolder;
use Opcodes\LogViewer\Utils\Utils;
Expand All @@ -14,6 +15,8 @@ public function __invoke()
abort(404);
}

$log_sort_by_time = config('log-viewer.defaults.log_sorting_method') === FolderSortingMethod::ModifiedTime;

return view(LogViewer::getViewLayout(), [
'logViewerScriptVariables' => [
'headers' => (object) [],
Expand All @@ -23,6 +26,7 @@ public function __invoke()
'path' => config('log-viewer.route_path'),
'back_to_system_url' => config('log-viewer.back_to_system_url'),
'back_to_system_label' => config('log-viewer.back_to_system_label'),
'log_sort_by_time' => $log_sort_by_time,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here, call it files_sort_by_time instead of logs, as we're sorting files, not log entries.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll try to get it done as quickly as possible

'max_log_size_formatted' => Utils::bytesForHumans(LogViewer::maxLogSize()),
'show_support_link' => config('log-viewer.show_support_link', true),

Expand Down
14 changes: 14 additions & 0 deletions src/LogFileCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ public function sortByLatestFirst(): self
return $this;
}

public function sortAlphabeticallyAsc(): self
{
$this->items = $this->sortBy('name')->values()->toArray();

return $this;
}

public function sortAlphabeticallyDesc(): self
{
$this->items = $this->sortByDesc('name')->values()->toArray();

return $this;
}

public function latest(): ?LogFile
{
return $this->sortByDesc->latestTimestamp()->first();
Expand Down
69 changes: 69 additions & 0 deletions tests/Feature/LogFilesControllerAlphaSortTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

use Opcodes\LogViewer\Enums\FolderSortingMethod;

use function Pest\Laravel\getJson;

beforeEach(function () {
config(['log-viewer.include_files' => ['*.log']]);
});

it('you can get alphabetically sorted default desc logs files controller', function () {
config(['log-viewer.defaults.log_sorting_method' => FolderSortingMethod::Alphabetical]);

generateLogFiles([
'one.log',
'two.log',
'three.log',
'four.log',
], randomContent: true);

$response = getJson(route('log-viewer.files'));

expect(array_column($response->json(), 'name'))->toBe([
'two.log',
'three.log',
'one.log',
'four.log',
]);
});

it('you can get alphabetically sorted asc logs files controller', function () {
config(['log-viewer.defaults.log_sorting_method' => FolderSortingMethod::Alphabetical]);

generateLogFiles([
'one.log',
'two.log',
'three.log',
'four.log',
], randomContent: true);

$response = getJson(route('log-viewer.files', ['direction' => 'asc']));

expect(array_column($response->json(), 'name'))->toBe([
'four.log',
'one.log',
'three.log',
'two.log',
]);
});

it('you can get alphabetically sorted desc logs files controller', function () {
config(['log-viewer.defaults.log_sorting_method' => FolderSortingMethod::Alphabetical]);

generateLogFiles([
'one.log',
'two.log',
'three.log',
'four.log',
], randomContent: true);

$response = getJson(route('log-viewer.files', ['direction' => 'desc']));

expect(array_column($response->json(), 'name'))->toBe([
'two.log',
'three.log',
'one.log',
'four.log',
]);
});
79 changes: 79 additions & 0 deletions tests/Feature/LogFilesControllerTimeSortTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

use Opcodes\LogViewer\Enums\FolderSortingMethod;

use function Pest\Laravel\getJson;

beforeEach(function () {
config(['log-viewer.include_files' => ['*.log']]);
});

it('you can get time sorted default desc logs files controller', function () {
config(['log-viewer.defaults.log_sorting_method' => FolderSortingMethod::ModifiedTime]);

$names = [
'one.log',
'two.log',
'three.log',
];
generateLogFiles($names, randomContent: true);
array_map(function (string $name) {
$this->travelTo(now()->addSecond());
touch(storage_path('logs/'.$name), now()->timestamp);
}, $names);

$response = getJson(route('log-viewer.files'));

expect(array_column($response->json(), 'name'))->toBe([
'three.log',
'two.log',
'one.log',
]);
});

it('you can get time sorted desc logs files controller', function () {
config(['log-viewer.defaults.log_sorting_method' => FolderSortingMethod::ModifiedTime]);

$names = [
'one.log',
'two.log',
'three.log',
];
generateLogFiles($names, randomContent: true);
array_map(function (string $name) {
$this->travelTo(now()->addSecond());
touch(storage_path('logs/'.$name), now()->timestamp);
}, $names);

$response = getJson(route('log-viewer.files', ['direction' => 'desc']));
// dd($response->json());

expect(array_column($response->json(), 'name'))->toBe([
'three.log',
'two.log',
'one.log',
]);
});

it('you can get time sorted asc logs files controller', function () {
config(['log-viewer.defaults.log_sorting_method' => FolderSortingMethod::ModifiedTime]);

$names = [
'one.log',
'two.log',
'three.log',
];
generateLogFiles($names, randomContent: true);
array_map(function (string $name) {
$this->travelTo(now()->addSecond());
touch(storage_path('logs/'.$name), now()->timestamp);
}, $names);

$response = getJson(route('log-viewer.files', ['direction' => 'asc']));

expect(array_column($response->json(), 'name'))->toBe([
'one.log',
'two.log',
'three.log',
]);
});
70 changes: 70 additions & 0 deletions tests/Feature/LogFoldersControllerAlphaSortTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

use Opcodes\LogViewer\Enums\FolderSortingMethod;

use function Pest\Laravel\getJson;

beforeEach(function () {
clearGeneratedLogFiles();
config(['log-viewer.include_files' => ['*/**.log']]);
});

it('you can get alphabetically sorted default desc logs folders controller', function () {
config(['log-viewer.defaults.log_sorting_method' => FolderSortingMethod::Alphabetical]);

generateLogFiles([
'sub/one.log',
'sub/two.log',
'sub/three.log',
'sub/four.log',
], randomContent: true);

$response = getJson(route('log-viewer.folders'));

expect(array_column($response->json()[0]['files'], 'name'))->toBe([
'two.log',
'three.log',
'one.log',
'four.log',
]);
});

it('you can get alphabetically sorted asc logs folders controller', function () {
config(['log-viewer.defaults.log_sorting_method' => FolderSortingMethod::Alphabetical]);

generateLogFiles([
'sub/one.log',
'sub/two.log',
'sub/three.log',
'sub/four.log',
], randomContent: true);

$response = getJson(route('log-viewer.folders', ['direction' => 'asc']));

expect(array_column($response->json()[0]['files'], 'name'))->toBe([
'four.log',
'one.log',
'three.log',
'two.log',
]);
});

it('you can get alphabetically sorted desc logs folders controller', function () {
config(['log-viewer.defaults.log_sorting_method' => FolderSortingMethod::Alphabetical]);

generateLogFiles([
'sub/one.log',
'sub/two.log',
'sub/three.log',
'sub/four.log',
], randomContent: true);

$response = getJson(route('log-viewer.folders', ['direction' => 'desc']));

expect(array_column($response->json()[0]['files'], 'name'))->toBe([
'two.log',
'three.log',
'one.log',
'four.log',
]);
});
Loading
Loading