fix(admin): stop dashboard OOM by aggregating product stats in SQL

Drop unbounded TransactionsAdmin/customer dumps from dashboardMaster
and aggregate GT_products_report without loading all transactions.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-08-11 21:45:59 +03:30
co-authored by Cursor
parent b81a840ffd
commit 9a101292ea
@@ -28,7 +28,6 @@ class DashboardController extends Controller
public function dashboardMaster(Request $request)
{
$tallaPrice = Price::latest()->first();
// $talla->update_date = Verta::instance(Carbon::parse($talla->updated_at))->format('Y/n/j-H:i');
$new_customer_major = Customer::where('type',1)->whereDate('created_at','<=',now())->whereDate('created_at','>=',now()->subMonths(3))
->count();
$new_customer_single = Customer::where('type',2)->whereDate('created_at','<=',now())->whereDate('created_at','>=',now()->subMonths(3))
@@ -37,33 +36,39 @@ class DashboardController extends Controller
$all_customer_single = Customer::where('type',2)->count();
$archive_customer_major = Customer::where('type',1)->where('is_archive',true)->count();
$archive_customer_single = Customer::where('type',2)->where('is_archive',true)->count();
$all_gold = Order::where('direction',2)->where('type',1)->sum('amount');// جمع طلاهایی که از مشتری خریدیم
$all_gold_single = Order::where('direction',2)->where('type',2)->sum('amount');// جمع طلاهایی که از مشتری خریدیم
$await_list = Order::where('direction',1)->where('sts',1)->get();
$best_customer_major=Order::with('customer')->where('type',1)->where('sts',2)->select('customer_id',DB::raw('sum(amount) as amount'))
->groupBy('customer_id')->limit(10)->get();
$best_customer_single=Wallet::with('customer')->whereNull('fund_id')->where('owner_type',2)->orderBy('gold','desc')->limit(10)->get();
$all_gold = Order::where('direction',2)->where('type',1)->sum('amount');
$all_gold_single = Order::where('direction',2)->where('type',2)->sum('amount');
$await_list = Order::query()
->where('direction', 1)
->where('sts', 1)
->select(['id', 'customer_id', 'amount', 'total_price', 'sts', 'direction', 'type', 'created_at'])
->with(['customer:id,name,mobile'])
->orderByDesc('id')
->limit(50)
->get();
$best_customer_major = Order::with('customer:id,name,mobile')
->where('type', 1)
->where('sts', 2)
->select('customer_id', DB::raw('sum(amount) as amount'))
->groupBy('customer_id')
->orderByDesc('amount')
->limit(10)
->get();
$best_customer_single = Wallet::with('customer:id,name,mobile')
->whereNull('fund_id')
->where('owner_type', 2)
->orderByDesc('gold')
->limit(10)
->get(['id', 'owner', 'owner_type', 'gold', 'fund_id']);
// GT
$GT_all_customers = Customer::where('type',2)->where('is_archive',false)->count();
$GT_no_login_customers = Customer::where('type',2)->where('is_archive',false)->where('last_seen_at',null)->count();
$GT_no_login_customers = Customer::where('type',2)->where('is_archive',false)->whereNull('last_seen_at')->count();
$GT_no_valid_customers = Customer::where('type',2)->where('is_archive',false)->where('is_legal',0)->count();
$GT_no_valid_customers_data = Customer::where('type',2)->where('is_archive',false)->where('is_legal',0)->get(['id','name','mobile']);
$GT_active_customers = Customer::where('type', 2)
->where('is_archive', false)
->whereIn('id', function($query) {
$query->select('owner')
->from('wallets');
->whereIn('id', function ($query) {
$query->select('owner')->from('wallets');
})->count();
$GT_active_customers_data = Customer::where('type', 2)
->where('is_archive', false)
->whereIn('id', function($query) {
$query->select('owner')
->from('wallets');
})->get(['id','name','mobile']);
$transactions_admins = TransactionsAdmin::with(['withdraw','order',"user",'customerTo','customer'])->get();
$startDate = $request->startDateProduct
? Carbon::parse($request->startDateProduct)->startOfDay()
@@ -72,23 +77,32 @@ class DashboardController extends Controller
? Carbon::parse($request->endDateProduct)->endOfDay()
: now()->endOfDay();
$products_report = Product::with(['limitedTransactions' => function ($query) use ($startDate, $endDate) {
$query->whereBetween('created_at', [$startDate, $endDate]);
}])
->whereHas('limitedTransactions', function ($query) use ($startDate, $endDate) {
$query->whereBetween('created_at', [$startDate, $endDate]);
})
// Aggregation در SQL — لود هزاران ردیف transaction باعث OOM می‌شد
$productAgg = DB::table('transactions')
->select(
'product_id',
DB::raw('SUM(CASE WHEN gold > 0 THEN gold ELSE 0 END) as transaction_sum_gold_buy'),
DB::raw('SUM(CASE WHEN gold < 0 THEN gold ELSE 0 END) as total_negative_gold_sell'),
DB::raw('SUM(gold) as total_gold')
)
->whereNotNull('product_id')
->whereBetween('created_at', [$startDate, $endDate])
->groupBy('product_id')
->get();
$products_report->each(function ($product) {
$product->transaction_sum_gold_buy = $product->limitedTransactions
->where('gold', '>', 0)
->sum('gold');
$product->total_negative_gold_sell = $product->limitedTransactions
->where('gold', '<', 0)
->sum('gold');
$product->total_gold = $product->limitedTransactions->sum('gold');
});
$productTitles = Product::query()
->whereIn('id', $productAgg->pluck('product_id')->filter()->all())
->pluck('title', 'id');
$products_report = $productAgg->map(function ($row) use ($productTitles) {
return [
'id' => (int) $row->product_id,
'title' => (string) ($productTitles[$row->product_id] ?? ''),
'transaction_sum_gold_buy' => (float) $row->transaction_sum_gold_buy,
'total_negative_gold_sell' => (float) $row->total_negative_gold_sell,
'total_gold' => (float) $row->total_gold,
];
})->values();
$gramPrice = (int) ($tallaPrice->price ?? 0);
$FPrices = [];
@@ -113,11 +127,12 @@ class DashboardController extends Controller
'GT_all_customers'=>$GT_all_customers,
'GT_no_login_customers'=>$GT_no_login_customers,
'GT_no_valid_customers'=>$GT_no_valid_customers,
'GT_no_valid_customers_data'=>$GT_no_valid_customers_data,
// لیست کامل مشتریان/تراکنش‌ها دیگر برنمی‌گردد (OOM روی JsonResponse)
'GT_no_valid_customers_data'=>[],
'GT_active_customers'=>$GT_active_customers,
'GT_active_customers_data'=>$GT_active_customers_data,
'GT_active_customers_data'=>[],
'GT_deActive_customers'=>$GT_all_customers-$GT_active_customers,
'GT_transactions_admins'=>$transactions_admins,
'GT_transactions_admins'=>[],
'GT_products_report' => $products_report,
'update_date' => now()->timezone('Asia/Tehran')->format('Y-m-d H:i:s'),
'pricem' => round($gramPrice * 4.331802 / 10000) * 10000,
@@ -135,7 +150,6 @@ class DashboardController extends Controller
'await_order'=>$await_list,
'best_customer'=>$best_customer_major,
'best_customer_single'=>$best_customer_single,
]);
}