Skip to content

Commit 132fc68

Browse files
committed
Adding alphabetical file sorting
- added configuration parameter 'log_sorting_method' - Method for sorting logs into directories - added dependence of sorting options on the parameter 'log_sorting_method': - with the value ModifiedTime - ['Newest first', 'Oldest first'] - with the value Alphabetical - ['From A to Z', 'From Z to A'] - supplemented sorting of files in routes 'log-viewer.folders' and 'log-viewer.files' - supplemented tests (Failed tests did not pass before the changes) docker compose run --rm php composer test Tests: 4 failed, 248 passed (815 assertions) Duration: 2.78s Random Order Seed: 1757604409 FAIL Tests\Unit\LogIndex\LogIndexTest ⨯ it compresses chunk if gzip is available 0.01s ⨯ it can save to the cache after building up the index FAIL Tests\Feature\LogFoldersControllerTest ⨯ it folders are sorted alphabetically descending when configured 0.03s ⨯ it can get the log files 0.02s
1 parent 503c545 commit 132fc68

File tree

12 files changed

+354
-11
lines changed

12 files changed

+354
-11
lines changed

config/log-viewer.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,9 @@
288288
// Order to sort the folders. Other options: `Ascending`, `Descending`
289289
'folder_sorting_order' => SortingOrder::Descending,
290290

291+
// Method for sorting logs into directories. Other options: `Alphabetical`, `ModifiedTime`
292+
'log_sorting_method' => FolderSortingMethod::ModifiedTime,
293+
291294
// Order to sort the logs. Other options: `Ascending`, `Descending`
292295
'log_sorting_order' => SortingOrder::Descending,
293296

public/app.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/mix-manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"/app.js": "/app.js?id=74d3e481ad3e8fa14f1daaa0aa46e109",
2+
"/app.js": "/app.js?id=0c810fdd27597f4d9fff94af639da625",
33
"/app.css": "/app.css?id=5593a0331dd40729ff41e32a6035d872",
44
"/img/log-viewer-128.png": "/img/log-viewer-128.png?id=d576c6d2e16074d3f064e60fe4f35166",
55
"/img/log-viewer-32.png": "/img/log-viewer-32.png?id=f8ec67d10f996aa8baf00df3b61eea6d",

resources/js/components/FileList.vue

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@
4545
<div class="text-sm text-gray-500 dark:text-gray-400">
4646
<label for="file-sort-direction" class="sr-only">Sort direction</label>
4747
<select id="file-sort-direction" class="select" v-model="fileStore.direction">
48-
<option value="desc">Newest first</option>
49-
<option value="asc">Oldest first</option>
48+
<option v-if="!LogViewer.log_sort_by_time" value="asc">From A to Z</option>
49+
<option v-if="!LogViewer.log_sort_by_time" value="desc">From Z to A</option>
50+
<option v-if="LogViewer.log_sort_by_time" value="desc">Newest first</option>
51+
<option v-if="LogViewer.log_sort_by_time" value="asc">Oldest first</option>
5052
</select>
5153
</div>
5254
</div>

src/Http/Controllers/FilesController.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Http\Request;
66
use Illuminate\Support\Facades\Gate;
77
use Illuminate\Support\Facades\URL;
8+
use Opcodes\LogViewer\Enums\FolderSortingMethod;
89
use Opcodes\LogViewer\Facades\LogViewer;
910
use Opcodes\LogViewer\Http\Resources\LogFileResource;
1011

@@ -13,11 +14,21 @@ class FilesController
1314
public function index(Request $request)
1415
{
1516
$files = LogViewer::getFiles();
17+
$sortingMethod = config('log-viewer.defaults.log_sorting_method', FolderSortingMethod::ModifiedTime);
18+
19+
if ($sortingMethod === FolderSortingMethod::ModifiedTime) {
20+
if ($request->query('direction', 'desc') === 'asc') {
21+
$files = $files->sortByEarliestFirst();
22+
} else {
23+
$files = $files->sortByLatestFirst();
24+
}
1625

17-
if ($request->query('direction', 'desc') === 'asc') {
18-
$files = $files->sortByEarliestFirst();
1926
} else {
20-
$files = $files->sortByLatestFirst();
27+
if ($request->query('direction', 'desc') === 'asc') {
28+
$files = $files->sortAlphabeticallyAsc();
29+
} else {
30+
$files = $files->sortAlphabeticallyDesc();
31+
}
2132
}
2233

2334
return LogFileResource::collection($files);

src/Http/Controllers/FoldersController.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public function index(Request $request)
2020
$sortingMethod = config('log-viewer.defaults.folder_sorting_method', FolderSortingMethod::ModifiedTime);
2121
$sortingOrder = config('log-viewer.defaults.folder_sorting_order', SortingOrder::Descending);
2222

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

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

3233
// Still sort files inside folders by direction param
33-
$folders->each(function ($folder) use ($fileSortingOrder) {
34-
if ($fileSortingOrder === 'asc') {
35-
$folder->files()->sortByEarliestFirst();
34+
$folders->each(function ($folder) use ($fileSortingMethod, $fileSortingOrder) {
35+
if ($fileSortingMethod === FolderSortingMethod::ModifiedTime) {
36+
if ($fileSortingOrder === 'asc') {
37+
$folder->files()->sortByEarliestFirst();
38+
} else {
39+
$folder->files()->sortByLatestFirst();
40+
}
41+
3642
} else {
37-
$folder->files()->sortByLatestFirst();
43+
if ($fileSortingOrder === 'asc') {
44+
$folder->files()->sortAlphabeticallyAsc();
45+
} else {
46+
$folder->files()->sortAlphabeticallyDesc();
47+
}
3848
}
3949
});
4050
} else { // ModifiedTime

src/Http/Controllers/IndexController.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Opcodes\LogViewer\Http\Controllers;
44

5+
use Opcodes\LogViewer\Enums\FolderSortingMethod;
56
use Opcodes\LogViewer\Facades\LogViewer;
67
use Opcodes\LogViewer\LogFolder;
78
use Opcodes\LogViewer\Utils\Utils;
@@ -14,6 +15,8 @@ public function __invoke()
1415
abort(404);
1516
}
1617

18+
$log_sort_by_time = config('log-viewer.defaults.log_sorting_method') === FolderSortingMethod::ModifiedTime;
19+
1720
return view(LogViewer::getViewLayout(), [
1821
'logViewerScriptVariables' => [
1922
'headers' => (object) [],
@@ -23,6 +26,7 @@ public function __invoke()
2326
'path' => config('log-viewer.route_path'),
2427
'back_to_system_url' => config('log-viewer.back_to_system_url'),
2528
'back_to_system_label' => config('log-viewer.back_to_system_label'),
29+
'log_sort_by_time' => $log_sort_by_time,
2630
'max_log_size_formatted' => Utils::bytesForHumans(LogViewer::maxLogSize()),
2731
'show_support_link' => config('log-viewer.show_support_link', true),
2832

src/LogFileCollection.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,20 @@ public function sortByLatestFirst(): self
2828
return $this;
2929
}
3030

31+
public function sortAlphabeticallyAsc(): self
32+
{
33+
$this->items = $this->sortBy('name')->values()->toArray();
34+
35+
return $this;
36+
}
37+
38+
public function sortAlphabeticallyDesc(): self
39+
{
40+
$this->items = $this->sortByDesc('name')->values()->toArray();
41+
42+
return $this;
43+
}
44+
3145
public function latest(): ?LogFile
3246
{
3347
return $this->sortByDesc->latestTimestamp()->first();
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
use Opcodes\LogViewer\Enums\FolderSortingMethod;
4+
5+
use function Pest\Laravel\getJson;
6+
7+
beforeEach(function () {
8+
config(['log-viewer.include_files' => ['*.log']]);
9+
});
10+
11+
it('you can get alphabetically sorted default desc logs files controller', function () {
12+
config(['log-viewer.defaults.log_sorting_method' => FolderSortingMethod::Alphabetical]);
13+
14+
generateLogFiles([
15+
'one.log',
16+
'two.log',
17+
'three.log',
18+
'four.log',
19+
], randomContent: true);
20+
21+
$response = getJson(route('log-viewer.files'));
22+
23+
expect(array_column($response->json(), 'name'))->toBe([
24+
'two.log',
25+
'three.log',
26+
'one.log',
27+
'four.log',
28+
]);
29+
});
30+
31+
it('you can get alphabetically sorted asc logs files controller', function () {
32+
config(['log-viewer.defaults.log_sorting_method' => FolderSortingMethod::Alphabetical]);
33+
34+
generateLogFiles([
35+
'one.log',
36+
'two.log',
37+
'three.log',
38+
'four.log',
39+
], randomContent: true);
40+
41+
$response = getJson(route('log-viewer.files', ['direction' => 'asc']));
42+
43+
expect(array_column($response->json(), 'name'))->toBe([
44+
'four.log',
45+
'one.log',
46+
'three.log',
47+
'two.log',
48+
]);
49+
});
50+
51+
it('you can get alphabetically sorted desc logs files controller', function () {
52+
config(['log-viewer.defaults.log_sorting_method' => FolderSortingMethod::Alphabetical]);
53+
54+
generateLogFiles([
55+
'one.log',
56+
'two.log',
57+
'three.log',
58+
'four.log',
59+
], randomContent: true);
60+
61+
$response = getJson(route('log-viewer.files', ['direction' => 'desc']));
62+
63+
expect(array_column($response->json(), 'name'))->toBe([
64+
'two.log',
65+
'three.log',
66+
'one.log',
67+
'four.log',
68+
]);
69+
});
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
use Opcodes\LogViewer\Enums\FolderSortingMethod;
4+
5+
use function Pest\Laravel\getJson;
6+
7+
beforeEach(function () {
8+
config(['log-viewer.include_files' => ['*.log']]);
9+
});
10+
11+
it('you can get time sorted default desc logs files controller', function () {
12+
config(['log-viewer.defaults.log_sorting_method' => FolderSortingMethod::ModifiedTime]);
13+
14+
$names = [
15+
'one.log',
16+
'two.log',
17+
'three.log',
18+
];
19+
generateLogFiles($names, randomContent: true);
20+
array_map(function (string $name) {
21+
$this->travelTo(now()->addSecond());
22+
touch(storage_path('logs/'.$name), now()->timestamp);
23+
}, $names);
24+
25+
$response = getJson(route('log-viewer.files'));
26+
27+
expect(array_column($response->json(), 'name'))->toBe([
28+
'three.log',
29+
'two.log',
30+
'one.log',
31+
]);
32+
});
33+
34+
it('you can get time sorted desc logs files controller', function () {
35+
config(['log-viewer.defaults.log_sorting_method' => FolderSortingMethod::ModifiedTime]);
36+
37+
$names = [
38+
'one.log',
39+
'two.log',
40+
'three.log',
41+
];
42+
generateLogFiles($names, randomContent: true);
43+
array_map(function (string $name) {
44+
$this->travelTo(now()->addSecond());
45+
touch(storage_path('logs/'.$name), now()->timestamp);
46+
}, $names);
47+
48+
$response = getJson(route('log-viewer.files', ['direction' => 'desc']));
49+
// dd($response->json());
50+
51+
expect(array_column($response->json(), 'name'))->toBe([
52+
'three.log',
53+
'two.log',
54+
'one.log',
55+
]);
56+
});
57+
58+
it('you can get time sorted asc logs files controller', function () {
59+
config(['log-viewer.defaults.log_sorting_method' => FolderSortingMethod::ModifiedTime]);
60+
61+
$names = [
62+
'one.log',
63+
'two.log',
64+
'three.log',
65+
];
66+
generateLogFiles($names, randomContent: true);
67+
array_map(function (string $name) {
68+
$this->travelTo(now()->addSecond());
69+
touch(storage_path('logs/'.$name), now()->timestamp);
70+
}, $names);
71+
72+
$response = getJson(route('log-viewer.files', ['direction' => 'asc']));
73+
74+
expect(array_column($response->json(), 'name'))->toBe([
75+
'one.log',
76+
'two.log',
77+
'three.log',
78+
]);
79+
});

0 commit comments

Comments
 (0)