Skip to content

Commit

Permalink
Merge pull request #3016 from Roardom/revert-upsert
Browse files Browse the repository at this point in the history
(Revert) History upsert
  • Loading branch information
HDVinnie committed Aug 2, 2023
2 parents 9af50a5 + e81e2fc commit b85287f
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 30 deletions.
18 changes: 9 additions & 9 deletions app/Console/Commands/AutoUpsertHistories.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

use App\Models\History;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use Exception;

Expand Down Expand Up @@ -76,19 +75,20 @@ public function handle(): void
$histories,
['user_id', 'torrent_id'],
[
'user_id',
'torrent_id',
'agent',
'uploaded' => DB::raw('uploaded + VALUES(uploaded)'),
'actual_uploaded' => DB::raw('actual_uploaded + VALUES(actual_uploaded)'),
'uploaded',
'actual_uploaded',
'client_uploaded',
'downloaded' => DB::raw('downloaded + VALUES(downloaded)'),
'actual_downloaded' => DB::raw('actual_downloaded + VALUES(actual_downloaded)'),
'downloaded',
'actual_downloaded',
'client_downloaded',
'seeder',
'active',
// 5400 is the max announce interval defined in the announce controller
'seedtime' => DB::raw('IF(DATE_ADD(updated_at, INTERVAL 5400 SECOND) > VALUES(updated_at) AND seeder = 1 AND VALUES(seeder) = 1, seedtime + TIMESTAMPDIFF(SECOND, updated_at, VALUES(updated_at)), seedtime)'),
'immune' => DB::raw('immune AND VALUES(immune)'),
'completed_at' => DB::raw('COALESCE(completed_at, VALUES(completed_at))'),
'seedtime',
'immune',
'completed_at',
],
);
}
Expand Down
122 changes: 101 additions & 21 deletions app/Jobs/ProcessAnnounce.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
namespace App\Jobs;

use App\Models\FreeleechToken;
use App\Models\History;
use App\Models\Peer;
use App\Models\PersonalFreeleech;
use Illuminate\Bus\Queueable;
Expand Down Expand Up @@ -57,6 +58,9 @@ public function middleware(): array
*/
public function handle(): void
{
// Flag is tripped if new session is created but client reports up/down > 0
$ghost = false;

// Set Variables
$realUploaded = $this->queries['uploaded'];
$realDownloaded = $this->queries['downloaded'];
Expand All @@ -70,20 +74,48 @@ public function handle(): void
->where('user_id', '=', $this->user->id)
->first();

$uploaded = max($realUploaded - ($peer?->uploaded ?? 0), 0);
$downloaded = max($realDownloaded >= ($peer?->downloaded ?? 0), 0);

// If no Peer record found then create one
if ($peer === null) {
if ($this->queries['uploaded'] > 0 || $this->queries['downloaded'] > 0) {
$ghost = true;
$event = 'started';
$uploaded = 0;
$downloaded = 0;
}

$peer = new Peer();
}

// Get history information
$history = History::firstOrNew(
[
'torrent_id' => $this->torrent->id,
'user_id' => $this->user->id,
],
[
'uploaded' => 0,
'actual_uploaded' => 0,
'downloaded' => 0,
'actual_downloaded' => 0,
'seedtime' => 0,
'immune' => 0,
'completed_at' => null,
]
);

// Check Ghost Flag
if ($ghost) {
$uploaded = ($realUploaded >= $history->client_uploaded) ? ($realUploaded - $history->client_uploaded) : 0;
$downloaded = ($realDownloaded >= $history->client_downloaded) ? ($realDownloaded - $history->client_downloaded) : 0;
} else {
$uploaded = ($realUploaded >= $peer->uploaded) ? ($realUploaded - $peer->uploaded) : 0;
$downloaded = ($realDownloaded >= $peer->downloaded) ? ($realDownloaded - $peer->downloaded) : 0;
}

if ($history->updated_at !== null && $history->updated_at->timestamp > now()->subHours(2)->timestamp && $history->seeder && $this->queries['left'] == 0) {
$oldUpdate = $history->updated_at->timestamp;
} else {
$oldUpdate = now()->timestamp;
}

// Modification of Upload and Download (Check cache but in case redis data was lost hit DB)
$personalFreeleech = cache()->rememberForever(
'personal_freeleech:'.$this->user->id,
Expand Down Expand Up @@ -136,14 +168,36 @@ public function handle(): void
$peer->updateConnectableStateIfNeeded();
$peer->updated_at = now();

$history->agent = $this->queries['user-agent'];
$history->seeder = (int) ($this->queries['left'] == 0);
$history->client_uploaded = $realUploaded;
$history->client_downloaded = $realDownloaded;

switch ($event) {
case 'started':
$peer->active = true;

$history->active = 1;
$history->immune = (int) ($history->exists ? $history->immune && $this->group->is_immune : $this->group->is_immune);

break;
case 'completed':
$peer->active = true;

$history->active = 1;
$history->uploaded += $modUploaded;
$history->actual_uploaded += $uploaded;
$history->downloaded += $modDownloaded;
$history->actual_downloaded += $downloaded;
$history->completed_at = now();

// Seedtime allocation
if ($this->queries['left'] == 0) {
$newUpdate = $peer->updated_at->timestamp;
$diff = $newUpdate - $oldUpdate;
$history->seedtime += $diff;
}

// User Update
if ($modUploaded > 0 || $modDownloaded > 0) {
$this->user->update([
Expand All @@ -160,6 +214,19 @@ public function handle(): void
case 'stopped':
$peer->active = false;

$history->active = 0;
$history->uploaded += $modUploaded;
$history->actual_uploaded += $uploaded;
$history->downloaded += $modDownloaded;
$history->actual_downloaded += $downloaded;

// Seedtime allocation
if ($this->queries['left'] == 0) {
$newUpdate = $peer->updated_at->timestamp;
$diff = $newUpdate - $oldUpdate;
$history->seedtime += $diff;
}

// User Update
if ($modUploaded > 0 || $modDownloaded > 0) {
$this->user->update([
Expand All @@ -172,6 +239,19 @@ public function handle(): void
default:
$peer->active = true;

$history->active = 1;
$history->uploaded += $modUploaded;
$history->actual_uploaded += $uploaded;
$history->downloaded += $modDownloaded;
$history->actual_downloaded += $downloaded;

// Seedtime allocation
if ($this->queries['left'] == 0) {
$newUpdate = $peer->updated_at->timestamp;
$diff = $newUpdate - $oldUpdate;
$history->seedtime += $diff;
}

// User Update
if ($modUploaded > 0 || $modDownloaded > 0) {
$this->user->update([
Expand Down Expand Up @@ -202,22 +282,22 @@ public function handle(): void

Redis::connection('announce')->command('LPUSH', [
config('cache.prefix').':histories:batch',
serialize([
'user_id' => $this->user->id,
'torrent_id' => $this->torrent->id,
'agent' => $this->queries['user-agent'],
'uploaded' => $event === 'started' ? 0 : $modUploaded,
'actual_uploaded' => $event === 'started' ? 0 : $uploaded,
'client_uploaded' => $realUploaded,
'downloaded' => $event === 'started' ? 0 : $modDownloaded,
'actual_downloaded' => $event === 'started' ? 0 : $downloaded,
'client_downloaded' => $realDownloaded,
'seeder' => $this->queries['left'] == 0,
'active' => $event !== 'stopped',
'seedtime' => 0,
'immune' => $this->group->is_immune,
'completed_at' => $event === 'completed' ? now() : null,
])
serialize($history->only([
'user_id',
'torrent_id',
'agent',
'uploaded',
'actual_uploaded',
'client_uploaded',
'downloaded',
'actual_downloaded',
'client_downloaded',
'seeder',
'active',
'seedtime',
'immune',
'completed_at',
]))
]);

$otherSeeders = $this
Expand Down
15 changes: 15 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1500,6 +1500,21 @@ parameters:
count: 1
path: app/Http/Resources/UserEchoResource.php

-
message: "#^Property App\\\\Models\\\\History\\:\\:\\$active \\(bool\\) does not accept int\\.$#"
count: 4
path: app/Jobs/ProcessAnnounce.php

-
message: "#^Property App\\\\Models\\\\History\\:\\:\\$immune \\(bool\\) does not accept int\\.$#"
count: 1
path: app/Jobs/ProcessAnnounce.php

-
message: "#^Property App\\\\Models\\\\History\\:\\:\\$seeder \\(bool\\) does not accept int\\.$#"
count: 1
path: app/Jobs/ProcessAnnounce.php

-
message: "#^Called 'pluck' on Laravel collection, but could have been retrieved as a query\\.$#"
count: 1
Expand Down

0 comments on commit b85287f

Please sign in to comment.