Skip to content

Commit

Permalink
Merge pull request #3809 from Roardom/custom-top-10
Browse files Browse the repository at this point in the history
  • Loading branch information
HDVinnie committed May 5, 2024
2 parents f54c2fb + f9e6037 commit ca58a9a
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
33 changes: 31 additions & 2 deletions app/Http/Livewire/Top10.php
Expand Up @@ -16,11 +16,13 @@
use App\Models\Category;
use App\Models\Torrent;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Livewire\Attributes\Computed;
use Livewire\Attributes\Url;
use Livewire\Attributes\Validate;
use Livewire\Component;
use Throwable;

/**
* @property \Illuminate\Database\Eloquent\Collection<int, Torrent> $works
Expand All @@ -35,9 +37,35 @@ class Top10 extends Component
public string $metaType = 'movie_meta';

#[Url(history: true)]
#[Validate('in:day,week,month,year,all')]
#[Validate('in:day,week,month,year,all,custom')]
public string $interval = 'day';

#[Url(history: true)]
#[Validate('sometimes|date_format:Y-m-d')]
public string $from = '';

#[Url(history: true)]
#[Validate('sometimes|date_format:Y-m-d')]
public string $until = '';

public function updatingFrom(string &$value): void
{
try {
$value = Carbon::parse($value)->format('Y-m-d');
} catch (Throwable) {
$value = now()->subDay()->format('Y-m-d');
}
}

public function updatingUntil(string &$value): void
{
try {
$value = Carbon::parse($value)->format('Y-m-d');
} catch (Throwable) {
$value = now()->format('Y-m-d');
}
}

/**
* @return \Illuminate\Database\Eloquent\Collection<int, Torrent>
*/
Expand All @@ -47,7 +75,7 @@ final public function works(): Collection
$this->validate();

return cache()->remember(
'top10-'.$this->interval.'-'.$this->metaType,
'top10-'.$this->interval.'-'.($this->from ?? '').'-'.($this->to ?? '').'-'.$this->metaType,
3600,
fn () => Torrent::query()
->when(
Expand All @@ -67,6 +95,7 @@ final public function works(): Collection
->when($this->interval === 'month', fn ($query) => $query->whereBetween('history.completed_at', [now()->subMonth(), now()]))
->when($this->interval === 'year', fn ($query) => $query->whereBetween('history.completed_at', [now()->subYear(), now()]))
->when($this->interval === 'all', fn ($query) => $query->whereNotNull('history.completed_at'))
->when($this->interval === 'custom', fn ($query) => $query->whereBetween('history.completed_at', [$this->from ?: now(), $this->until ?: now()]))
->whereIn('torrents.category_id', Category::select('id')->where($this->metaType, '=', true))
// Small torrents screw the stats since users download them only to farm bon.
->where('torrents.size', '>', 1024 * 1024 * 1024)
Expand Down
29 changes: 28 additions & 1 deletion resources/views/livewire/top10.blade.php
Expand Up @@ -16,16 +16,43 @@ class="form__select"
<option value="month">Past Month</option>
<option value="year">Past Year</option>
<option value="all">All-time</option>
<option value="custom">Custom</option>
</select>
<label class="form__label form__label--floating" for="interval">Interval</label>
</div>
</div>
@if ($this->interval === 'custom')
<div class="panel__action">
<div class="form__group">
<input
id="from"
class="form__text"
name="from"
type="date"
wire:model.live="from"
/>
<label class="form__label form__label--floating" for="from">From</label>
</div>
</div>
<div class="panel__action">
<div class="form__group">
<input
id="until"
class="form__text"
name="until"
type="date"
wire:model.live="until"
/>
<label class="form__label form__label--floating" for="until">Until</label>
</div>
</div>
@endif

<div class="panel__action">
<div class="form__group">
<select
id="metaType"
class="form__select"
type="date"
name="metaType"
wire:model.live="metaType"
>
Expand Down

0 comments on commit ca58a9a

Please sign in to comment.