Skip to content

Commit

Permalink
fix: stop loading all trade data into memory (#1141)
Browse files Browse the repository at this point in the history
* fix: stop loading all trade data into memory

* fix: remove extra '\'

* refactor: fix PHP styling

* fix: mispelling

* fix: remove json encode (now castes)

* refactor: fix PHP styling

---------

Co-authored-by: ScuffedNewt <[email protected]>
  • Loading branch information
ScuffedNewt and ScuffedNewt authored Jan 1, 2025
1 parent cb62d38 commit 2aa4551
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 52 deletions.
19 changes: 0 additions & 19 deletions app/Http/Controllers/Admin/Characters/CharacterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use App\Models\Species\Subtype;
use App\Models\Trade;
use App\Models\User\User;
use App\Models\User\UserItem;
use App\Services\CharacterManager;
use App\Services\TradeManager;
use Illuminate\Http\Request;
Expand Down Expand Up @@ -671,30 +670,12 @@ public function getTradeQueue($type) {

$openTransfersQueue = Settings::get('open_transfers_queue');

$stacks = [];
foreach ($trades->get() as $trade) {
foreach ($trade->data as $side=> $assets) {
if (isset($assets['user_items'])) {
$user_items = UserItem::with('item')->find(array_keys($assets['user_items']));
$items = [];
foreach ($assets['user_items'] as $id=> $quantity) {
$user_item = $user_items->find($id);
$user_item['quantity'] = $quantity;
array_push($items, $user_item);
}
$items = collect($items)->groupBy('item_id');
$stacks[$trade->id][$side] = $items;
}
}
}

return view('admin.masterlist.character_trades', [
'trades' => $trades->orderBy('id', 'DESC')->paginate(20),
'tradesQueue' => Settings::get('open_transfers_queue'),
'openTransfersQueue' => $openTransfersQueue,
'transferCount' => $openTransfersQueue ? CharacterTransfer::active()->where('is_approved', 0)->count() : 0,
'tradeCount' => $openTransfersQueue ? Trade::where('status', 'Pending')->count() : 0,
'stacks' => $stacks,
]);
}

Expand Down
18 changes: 0 additions & 18 deletions app/Http/Controllers/Users/TradeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,8 @@ public function getIndex($status = 'open') {
$query->where('recipient_id', Auth::user()->id)->orWhere('sender_id', Auth::user()->id);
})->where('status', ucfirst($status))->orderBy('id', 'DESC');

$stacks = [];
foreach ($trades->get() as $trade) {
foreach ($trade->data as $side=> $assets) {
if (isset($assets['user_items'])) {
$user_items = UserItem::with('item')->find(array_keys($assets['user_items']));
$items = [];
foreach ($assets['user_items'] as $id=> $quantity) {
$user_item = $user_items->find($id);
$user_item['quantity'] = $quantity;
array_push($items, $user_item);
}
$items = collect($items)->groupBy('item_id');
$stacks[$trade->id][$side] = $items;
}
}
}

return view('home.trades.index', [
'trades' => $trades->paginate(20),
'stacks' => $stacks,
]);
}

Expand Down
40 changes: 32 additions & 8 deletions app/Models/Trade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Facades\Settings;
use App\Models\Character\Character;
use App\Models\User\User;
use App\Models\User\UserItem;

class Trade extends Model {
/**
Expand All @@ -24,6 +25,16 @@ class Trade extends Model {
* @var string
*/
protected $table = 'trades';

/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'data' => 'array',
];

/**
* Whether the model contains timestamps to be saved and updated.
*
Expand Down Expand Up @@ -107,21 +118,34 @@ public function getIsConfirmableAttribute() {
}

/**
* Get the data attribute as an associative array.
* Gets the URL of the trade.
*
* @return array
* @return string
*/
public function getDataAttribute() {
return json_decode($this->attributes['data'], true);
public function getUrlAttribute() {
return url('trades/'.$this->id);
}

/**
* Gets the URL of the trade.
* Gets the stacks of the trade keyed by sender and recipient.
*
* @return string
* @return array
*/
public function getUrlAttribute() {
return url('trades/'.$this->id);
public function getStacksAttribute() {
$stacks = [];
foreach ($this->data as $side => $assets) {
if (isset($assets['user_items'])) {
$user_items = UserItem::with('item')->find(array_keys($assets['user_items']));
$items = $user_items->map(function ($user_item) use ($assets) {
$user_item['quantity'] = $assets['user_items'][$user_item->id];

return $user_item;
});
$stacks[$side] = $items->groupBy('item_id');
}
}

return $stacks;
}

/**********************************************************************************************
Expand Down
6 changes: 4 additions & 2 deletions app/Services/TradeManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ public function createTrade($data, $user) {
]);

if ($assetData = $this->handleTradeAssets($trade, $data, $user)) {
$trade->data = json_encode(['sender' => getDataReadyAssets($assetData['sender'])]);
$trade->data = [
'sender' => getDataReadyAssets($assetData['sender']),
];
$trade->save();

// send a notification
Expand Down Expand Up @@ -103,7 +105,7 @@ public function editTrade($data, $user) {
$tradeData = $trade->data;
$isSender = ($trade->sender_id == $user->id);
$tradeData[$isSender ? 'sender' : 'recipient'] = getDataReadyAssets($assetData[$isSender ? 'sender' : 'recipient']);
$trade->data = json_encode($tradeData);
$trade->data = $tradeData;
$trade->{'is_'.($isSender ? 'sender' : 'recipient').'_confirmed'} = 0;
$trade->{'is_'.($isSender ? 'recipient' : 'sender').'_trade_confirmed'} = 0;
$trade->save();
Expand Down
14 changes: 9 additions & 5 deletions resources/views/home/trades/_trade.blade.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
<div class="card mb-3">
<div class="card-header">
<h2 class="mb-0"><span
class="float-right badge badge-{{ $trade->status == 'Pending' || $trade->status == 'Open' || $trade->status == 'Canceled' ? 'secondary' : ($trade->status == 'Completed' ? 'success' : 'danger') }}">{{ $trade->status }}</span><a
href="{{ $trade->url }} ">Trade (#{{ $trade->id }})</a></h2>
<h2 class="mb-0">
<span class="float-right badge badge-{{ $trade->status == 'Pending' || $trade->status == 'Open' || $trade->status == 'Canceled' ? 'secondary' : ($trade->status == 'Completed' ? 'success' : 'danger') }}">{{ $trade->status }}</span>
<a href="{{ $trade->url }} ">Trade (#{{ $trade->id }})</a>
</h2>
@if ($trade->staff)
Processed by {!! $trade->staff->displayName !!}
@endif
</div>
<div class="card-body">
@if ($trade->comments)
Expand All @@ -29,7 +33,7 @@ class="float-right badge badge-{{ $trade->status == 'Pending' || $trade->status
'user' => $trade->sender,
'data' => isset($trade->data['sender']) ? parseAssetData($trade->data['sender']) : null,
'trade' => $trade,
'stacks' => isset($stacks[$trade->id]['sender']) ? $stacks[$trade->id]['sender'] : null,
'stacks' => isset($trade->stacks['sender']) ? $trade->stacks['sender'] : [],
])
</div>
<div class="col-md-6">
Expand All @@ -51,7 +55,7 @@ class="float-right badge badge-{{ $trade->status == 'Pending' || $trade->status
'user' => $trade->recipient,
'data' => isset($trade->data['recipient']) ? parseAssetData($trade->data['recipient']) : null,
'trade' => $trade,
'stacks' => isset($stacks[$trade->id]['recipient']) ? $stacks[$trade->id]['recipient'] : null,
'stacks' => isset($trade->stacks['recipient']) ? $trade->stacks['recipient'] : [],
])
</div>
</div>
Expand Down

0 comments on commit 2aa4551

Please sign in to comment.