fixed chart in 30 min

This commit is contained in:
2026-06-24 11:34:23 +03:30
parent 75db07dbda
commit d968a42db9
3 changed files with 279 additions and 340 deletions
+20 -90
View File
@@ -22,6 +22,7 @@ use Illuminate\Support\Facades\Mail;
use Morilog\Jalali\Jalalian; use Morilog\Jalali\Jalalian;
use App\Services\ConfigService; use App\Services\ConfigService;
use App\Services\LivePriceCacheService; use App\Services\LivePriceCacheService;
use App\Services\PriceSnapshotService;
use App\Services\ProductDisplayPriceService; use App\Services\ProductDisplayPriceService;
@@ -160,12 +161,16 @@ class ReportController extends Controller
} }
if ($config->isStore()) { if ($config->isStore()) {
return $this->appendUpPrices(array_merge([ $storePayload = array_merge([
'closed' => false, 'closed' => false,
'time' => now(), 'time' => now(),
'main_product' => 0, 'main_product' => 0,
'zaarbox' => 0, 'zaarbox' => 0,
], $this->storeLastPrice($config)), $forceRefresh); ], $this->storeLastPrice($config));
app(PriceSnapshotService::class)->recordHalfHourBuyPrice((int) ($storePayload['buy'] ?? 0));
return $this->appendUpPrices($storePayload, $forceRefresh);
} }
$closed = false; $closed = false;
@@ -389,53 +394,6 @@ class ReportController extends Controller
} }
} }
/*
|--------------------------------------------------------------------------
| ذخیره قیمت
|--------------------------------------------------------------------------
*/
$priceToStore = $priceValue;
$lastPrice = Price::latest()->first();
if (
! $closed &&
(! $lastPrice || (float) $lastPrice->price != (float) $priceToStore) &&
$priceToStore > 30_000_000
) {
$price = new Price();
$price->price = $priceToStore;
$price->time_at = Carbon::now()->toTimeString();
$price->date_at = Carbon::now()->toDateString();
$price->save();
$this->updatePrise();
$candle = PriceCandle::whereDate('date_at', date("Y-m-d"))->first();
if (!is_null($candle)) {
$candle->end = $priceToStore;
if ($priceToStore < $candle->low) {
$candle->low = $priceToStore;
} elseif ($priceToStore > $candle->high) {
$candle->high = $priceToStore;
}
$candle->save();
} else {
PriceCandle::create([
'start' => $priceToStore,
'end' => $priceToStore,
'high' => $priceToStore,
'low' => $priceToStore,
'date_at' => date('Y-m-d'),
]);
}
$lastPrice = $price;
}
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| محاسبه sell / buy | محاسبه sell / buy
@@ -457,6 +415,8 @@ class ReportController extends Controller
$config->trackDailyPriceExtreme((float) $priceValue); $config->trackDailyPriceExtreme((float) $priceValue);
} }
app(PriceSnapshotService::class)->recordHalfHourBuyPrice((int) $buy);
return $this->appendUpPrices([ return $this->appendUpPrices([
'sell' => $sell, 'sell' => $sell,
'buy' => $buy, 'buy' => $buy,
@@ -595,53 +555,23 @@ class ReportController extends Controller
} }
$lastStored = Price::latest()->first(); $lastStored = Price::latest()->first();
if ( if ($priceValue <= 0 && $lastStored && (float) $lastStored->price > 0) {
(! $lastStored || (float) $lastStored->price != (float) $priceValue) $priceValue = (float) $lastStored->price;
&& $priceValue > 30000000 $priceSource = 'database_last';
) {
$price = new Price();
$price->price = $priceValue;
$price->time_at = Carbon::now()->toTimeString();
$price->date_at = Carbon::now()->toDateString();
$price->save();
$this->updatePrise();
$candle = PriceCandle::whereDate('date_at', date('Y-m-d'))->first();
if ($candle !== null) {
$candle->end = $priceValue;
if ($priceValue < $candle->low) {
$candle->low = $priceValue;
} elseif ($priceValue > $candle->high) {
$candle->high = $priceValue;
}
$candle->save();
} else {
PriceCandle::create([
'start' => $priceValue,
'end' => $priceValue,
'high' => $priceValue,
'low' => $priceValue,
'date_at' => date('Y-m-d'),
]);
}
} }
$price = Price::orderByDesc('id')->first(); if ($priceValue <= 0) {
if (! $price) {
return []; return [];
} }
if ($priceSource === 'montzar') { $display = $config->buildTradeDisplayPrices(
$difference += (float) $config->Config('montzar_diff'); (float) $priceValue,
} elseif ($priceSource === 'nasiryan') { $priceSource
$difference += (float) (Cnt::where('key', 'nasiryan_diff')->first()['value'] ?? 0); );
}
$response = [ $response = [
'sell' => (int) ceil($price->price - $difference), 'sell' => $display['sell'],
'buy' => (int) ceil($price->price + $difference), 'buy' => $display['buy'],
'price_last' => (int) ceil($price->price + $difference), 'price_last' => $display['priceLast'],
'price' => $priceValue, 'price' => $priceValue,
'tala_ir' => $talaIr, 'tala_ir' => $talaIr,
'nasiryan' => $nasiryan, 'nasiryan' => $nasiryan,
-4
View File
@@ -78,10 +78,6 @@ class LivePriceTickService
$syncLog = app(PriceSyncLogService::class)->recordTick($source, $sourceUrl, $liquidGold, $activeProducts); $syncLog = app(PriceSyncLogService::class)->recordTick($source, $sourceUrl, $liquidGold, $activeProducts);
if ($payload !== []) {
app(PriceSnapshotService::class)->recordTick($payload);
}
app(LivePriceCacheService::class)->markTick(); app(LivePriceCacheService::class)->markTick();
// last از put داخل lastPrice؛ list بعد از sync محصولات // last از put داخل lastPrice؛ list بعد از sync محصولات
+27 -14
View File
@@ -8,6 +8,8 @@ use Carbon\Carbon;
class PriceSnapshotService class PriceSnapshotService
{ {
public const HALF_HOUR_MINUTES = 30;
/** حداقل قیمت معتبر آبشده (ریال) — برای تاریخچه ذخیره‌شده در DB */ /** حداقل قیمت معتبر آبشده (ریال) — برای تاریخچه ذخیره‌شده در DB */
public const MIN_VALID_CHART_PRICE_RIAL = 30_000_000; public const MIN_VALID_CHART_PRICE_RIAL = 30_000_000;
@@ -32,31 +34,42 @@ class PriceSnapshotService
return self::MIN_VALID_CHART_PRICE_RIAL; return self::MIN_VALID_CHART_PRICE_RIAL;
} }
/** هر تیک کران — ذخیره در prices برای نمودار (هرگز صفر ثبت نمی‌شود) */ /**
public function recordTick(array $payload): void * ذخیره نمونه قیمت خرید (buy) برای نمودار — حداکثر هر ۳۰ دقیقه یک‌بار.
*
* 1) در ۳۰ دقیقه گذشته رکوردی نباشد
* 2) با آخرین رکورد جدول prices یکی نباشد
* 3) قیمت صفر نباشد
*/
public function recordHalfHourBuyPrice(int $buyPrice): bool
{ {
$priceValue = (int) ($payload['price'] ?? 0); if ($buyPrice <= 0) {
if ($priceValue <= self::MIN_VALID_CHART_PRICE_RIAL) { return false;
$last = Price::orderByDesc('id')->first();
if (! $last || (int) $last->price <= self::MIN_VALID_CHART_PRICE_RIAL) {
return;
}
$priceValue = (int) $last->price;
} }
$lastRecorded = Price::query()->orderByDesc('id')->first(); $savedRecently = Price::query()
if ($lastRecorded && (int) $lastRecorded->price === $priceValue) { ->where('created_at', '>=', now()->subMinutes(self::HALF_HOUR_MINUTES))
return; ->exists();
if ($savedRecently) {
return false;
}
$lastPrice = Price::query()->orderByDesc('id')->first();
if ($lastPrice && (int) $lastPrice->price === $buyPrice) {
return false;
} }
Price::create([ Price::create([
'price' => $priceValue, 'price' => $buyPrice,
'date_at' => Carbon::now()->toDateString(), 'date_at' => Carbon::now()->toDateString(),
'time_at' => Carbon::now()->toTimeString(), 'time_at' => Carbon::now()->toTimeString(),
'user_id' => null, 'user_id' => null,
]); ]);
$this->touchCandle($priceValue); $this->touchCandle($buyPrice);
return true;
} }
private function touchCandle(int $priceValue): void private function touchCandle(int $priceValue): void