Skip to content
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
302c2ab
feat(album-statistics): add album_size_statistics table and model
ildyria Jan 2, 2026
4428d8b
feat(album-statistics): implement RecomputeAlbumSizeJob with propagation
ildyria Jan 2, 2026
298d610
feat(album-statistics): add event listeners for automatic recomputation
ildyria Jan 2, 2026
2bdd58d
test(album-size-statistics): add unit tests for AlbumSizeStatistics m…
ildyria Jan 3, 2026
202ea54
feat(factories): add helper methods to SizeVariantFactory
ildyria Jan 3, 2026
3baa5e7
refactor(statistics): use pre-computed album_size_statistics table
ildyria Jan 3, 2026
014731b
test(album-statistics): add comprehensive tests for RecomputeAlbumSiz…
ildyria Jan 3, 2026
a824c18
docs(tasks): mark T-004-14 complete
ildyria Jan 3, 2026
fd93bdb
docs(tasks): mark T-004-22, T-004-23, T-004-25, T-004-28 complete
ildyria Jan 3, 2026
3e89905
test(album-statistics): add propagation feature tests
ildyria Jan 3, 2026
1ec30b2
test(album-statistics): add event listener integration tests
ildyria Jan 3, 2026
18f728c
docs(tasks): mark T-004-16, T-004-17, T-004-21 complete
ildyria Jan 3, 2026
7419f7c
feat(commands): add album size statistics management commands
ildyria Jan 3, 2026
d865620
docs(tasks): mark command implementation tasks complete
ildyria Jan 3, 2026
62462e1
add album size statistics
ildyria Jan 3, 2026
04d0a3e
fix tests
ildyria Jan 3, 2026
61da698
Merge branch 'master' into album-size-statistics
ildyria Jan 3, 2026
7f1b30b
some fixes
ildyria Jan 3, 2026
49a20f9
fix phpstna
ildyria Jan 3, 2026
4d29708
improve Space speed
ildyria Jan 3, 2026
5705fca
dispatch after import
ildyria Jan 3, 2026
074e012
fix
ildyria Jan 3, 2026
4b34078
fix
ildyria Jan 3, 2026
92e16c3
fix
ildyria Jan 3, 2026
c94d0ac
improve
ildyria Jan 3, 2026
20baca6
Merge branch 'master' into album-size-statistics
ildyria Jan 3, 2026
1225c1a
stuff
ildyria Jan 3, 2026
2be87e0
fix error
ildyria Jan 3, 2026
9f62189
fix jobs count
ildyria Jan 3, 2026
b191935
fix comments
ildyria Jan 3, 2026
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
81 changes: 40 additions & 41 deletions app/Actions/Statistics/Spaces.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ final class Spaces
/**
* Return the amount of data stored on the server (optionally for a user).
*
* Uses pre-computed album_size_statistics table for <100ms performance.
* Joins user's owned albums and sums their statistics.
*
* @param int|null $owner_id
*
* @return Collection<int,array{id:int,username:string,size:int}>
Expand All @@ -32,28 +35,24 @@ public function getFullSpacePerUser(?int $owner_id = null): Collection
{
return DB::table('users')
->when($owner_id !== null, fn ($query) => $query->where('users.id', '=', $owner_id))
->joinSub(
query: DB::table('photos')->select(['photos.id', 'photos.owner_id']),
as: 'photos',
first: 'photos.owner_id',
operator: '=',
second: 'users.id',
type: 'left'
)
->joinSub(
query: DB::table('size_variants')
->select(['size_variants.photo_id', 'size_variants.filesize'])
->where('size_variants.type', '!=', 7),
as: 'size_variants',
first: 'size_variants.photo_id',
->leftJoinSub(
query: DB::table('base_albums')->select(['base_albums.id', 'base_albums.owner_id']),
as: 'base_albums',
first: 'base_albums.owner_id',
operator: '=',
second: 'photos.id',
type: 'left'
second: 'users.id'
)
->leftJoin('album_size_statistics', 'album_size_statistics.album_id', '=', 'base_albums.id')
->select(
'users.id',
'username',
DB::raw('SUM(size_variants.filesize) as size')
DB::raw('SUM(COALESCE(album_size_statistics.size_thumb, 0) +
COALESCE(album_size_statistics.size_thumb2x, 0) +
COALESCE(album_size_statistics.size_small, 0) +
COALESCE(album_size_statistics.size_small2x, 0) +
COALESCE(album_size_statistics.size_medium, 0) +
COALESCE(album_size_statistics.size_medium2x, 0) +
COALESCE(album_size_statistics.size_original, 0)) as size')
)
->groupBy('users.id', 'username')
->orderBy('users.id', 'asc')
Expand Down Expand Up @@ -144,6 +143,9 @@ public function getSpacePerSizeVariantTypePerAlbum(string $album_id): Collection
/**
* Return size statistics per album.
*
* Uses pre-computed album_size_statistics table for performance.
* Falls back to runtime calculation if statistics are missing.
*
* @param string|null $album_id
* @param int|null $owner_id
*
Expand Down Expand Up @@ -172,22 +174,19 @@ public function getSpacePerAlbum(?string $album_id = null, ?int $owner_id = null
second: 'albums.id'
)
->where('base_albums.owner_id', '=', $owner_id))
->join(PA::PHOTO_ALBUM, PA::ALBUM_ID, '=', 'albums.id')
->joinSub(
query: DB::table('size_variants')
->select(['size_variants.id', 'size_variants.photo_id', 'size_variants.filesize'])
->where('size_variants.type', '!=', 7),
as: 'size_variants',
first: 'size_variants.photo_id',
operator: '=',
second: PA::PHOTO_ID
)
->leftJoin('album_size_statistics', 'album_size_statistics.album_id', '=', 'albums.id')
->select(
'albums.id',
'albums._lft',
'albums._rgt',
DB::raw('SUM(size_variants.filesize) as size'),
)->groupBy('albums.id')
DB::raw('(COALESCE(album_size_statistics.size_thumb, 0) +
COALESCE(album_size_statistics.size_thumb2x, 0) +
COALESCE(album_size_statistics.size_small, 0) +
COALESCE(album_size_statistics.size_small2x, 0) +
COALESCE(album_size_statistics.size_medium, 0) +
COALESCE(album_size_statistics.size_medium2x, 0) +
COALESCE(album_size_statistics.size_original, 0)) as size')
)
->orderBy('albums._lft', 'asc');

return $query
Expand All @@ -203,6 +202,9 @@ public function getSpacePerAlbum(?string $album_id = null, ?int $owner_id = null
/**
* Same as above but with full size (including sub-albums).
*
* Uses pre-computed album_size_statistics table with nested set query
* to find descendants and sum their statistics.
*
* @param string|null $album_id
* @param int|null $owner_id
*
Expand All @@ -228,22 +230,19 @@ public function getTotalSpacePerAlbum(?string $album_id = null, ?int $owner_id =
->on('albums._rgt', '>=', 'descendants._rgt');
}
)
->join(PA::PHOTO_ALBUM, PA::ALBUM_ID, '=', 'descendants.id')
->joinSub(
query: DB::table('size_variants')
->select(['size_variants.id', 'size_variants.photo_id', 'size_variants.filesize'])
->where('size_variants.type', '!=', 7),
as: 'size_variants',
first: 'size_variants.photo_id',
operator: '=',
second: PA::PHOTO_ID
)
->leftJoin('album_size_statistics', 'album_size_statistics.album_id', '=', 'descendants.id')
->select(
'albums.id',
'albums._lft',
'albums._rgt',
DB::raw('SUM(size_variants.filesize) as size'),
)->groupBy('albums.id')
DB::raw('SUM(COALESCE(album_size_statistics.size_thumb, 0) +
COALESCE(album_size_statistics.size_thumb2x, 0) +
COALESCE(album_size_statistics.size_small, 0) +
COALESCE(album_size_statistics.size_small2x, 0) +
COALESCE(album_size_statistics.size_medium, 0) +
COALESCE(album_size_statistics.size_medium2x, 0) +
COALESCE(album_size_statistics.size_original, 0)) as size')
)->groupBy('albums.id', 'albums._lft', 'albums._rgt')
->orderBy('albums._lft', 'asc');

return $query
Expand Down
112 changes: 112 additions & 0 deletions app/Console/Commands/BackfillAlbumSizeStatistics.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

/**
* SPDX-License-Identifier: MIT
* Copyright (c) 2017-2018 Tobias Reich
* Copyright (c) 2018-2026 LycheeOrg.
*/

namespace App\Console\Commands;

use App\Jobs\RecomputeAlbumSizeJob;
use App\Models\Album;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

/**
* Backfill album size statistics for all albums (T-004-35, T-004-36, FR-004-04).
*
* This command dispatches RecomputeAlbumSizeJob for each album to populate
* the album_size_statistics table with pre-computed size data.
*/
class BackfillAlbumSizeStatistics extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'lychee:backfill-album-sizes
{--dry-run : Preview without making changes}
{--chunk=1000 : Number of albums to process per batch}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Backfill size statistics (per-variant sizes) for all albums';

/**
* Execute the console command.
*/
public function handle(): int
{
$dry_run = $this->option('dry-run');
$chunk_size = (int) $this->option('chunk');

if ($chunk_size < 1) {
$this->error('Chunk size must be at least 1');

return Command::FAILURE;
}

$this->info('Starting album size statistics backfill...');
if ($dry_run) {
$this->warn('DRY RUN MODE - No changes will be made');
}

// Get total count
$total = Album::query()->count();
$this->info("Found {$total} albums to process");

if ($total === 0) {
$this->info('No albums to process');

return Command::SUCCESS;
}

// Process albums ordered by _lft ASC (leaf-to-root order)
// This ensures child albums are computed before parents
$bar = $this->output->createProgressBar($total);
$bar->start();

$processed = 0;

Album::query()
->orderBy('_lft', 'asc')
->chunk($chunk_size, function (\Illuminate\Database\Eloquent\Collection $albums) use ($dry_run, &$processed, $bar): void {
/** @var Album $album */
foreach ($albums as $album) {
if (!$dry_run) {
// Dispatch job to recompute size statistics for this album
RecomputeAlbumSizeJob::dispatch($album->id);
}

$processed++;
$bar->advance();

// Log progress at intervals (TE-004-03)
if ($processed % 100 === 0) {
$percentage = round(($processed / $bar->getMaxSteps()) * 100, 2);
Log::info("Backfilled album sizes: {$processed}/{$bar->getMaxSteps()} albums ({$percentage}%)");
}
}
});

$bar->finish();
$this->newLine(2);

if ($dry_run) {
$this->info("DRY RUN: Would have dispatched {$processed} jobs");
} else {
$this->info("Dispatched {$processed} jobs to recompute album size statistics");
$this->info('Jobs will be processed by the queue worker');
$this->warn('Note: This operation may take some time for large galleries');
}

Log::info("Album size backfill completed: {$processed} albums processed");

return Command::SUCCESS;
}
}
64 changes: 64 additions & 0 deletions app/Console/Commands/RecomputeAlbumSizes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

/**
* SPDX-License-Identifier: MIT
* Copyright (c) 2017-2018 Tobias Reich
* Copyright (c) 2018-2026 LycheeOrg.
*/

namespace App\Console\Commands;

use App\Jobs\RecomputeAlbumSizeJob;
use App\Models\Album;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

/**
* Manually recompute size statistics for a specific album (T-004-39, T-004-40, CLI-004-02).
*
* This command is useful for manual recovery when statistics drift out of sync.
*/
class RecomputeAlbumSizes extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'lychee:recompute-album-sizes {album_id : The ID of the album to recompute}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Manually recompute size statistics for a specific album';

/**
* Execute the console command.
*/
public function handle(): int
{
$album_id = $this->argument('album_id');

// Validate album exists
$album = Album::find($album_id);
if ($album === null) {
$this->error("Album with ID '{$album_id}' not found");

return Command::FAILURE;
}

$this->info("Recomputing size statistics for album: {$album->title} (ID: {$album_id})");

// Dispatch job
RecomputeAlbumSizeJob::dispatch($album_id);

$this->info('Job dispatched successfully');
$this->info('Statistics will be updated by the queue worker');

Log::info("Manual recompute triggered for album {$album_id} via CLI");

return Command::SUCCESS;
}
}
Loading
Loading