added wss tools 7
This commit is contained in:
@@ -4,6 +4,7 @@ namespace App\Http\Controllers\Site;
|
|||||||
|
|
||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
use App\Services\LivePriceCacheService;
|
use App\Services\LivePriceCacheService;
|
||||||
|
use App\Services\PriceBroadcastService;
|
||||||
use App\Services\PriceStreamService;
|
use App\Services\PriceStreamService;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Symfony\Component\HttpFoundation\StreamedResponse;
|
use Symfony\Component\HttpFoundation\StreamedResponse;
|
||||||
@@ -44,6 +45,11 @@ class PriceStreamController extends Controller
|
|||||||
$send('last', $last);
|
$send('last', $last);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$list = app(PriceBroadcastService::class)->buildListPayload();
|
||||||
|
if (is_array($list) && $list !== []) {
|
||||||
|
$send('list', $list);
|
||||||
|
}
|
||||||
|
|
||||||
$lastId = $service->currentSeq();
|
$lastId = $service->currentSeq();
|
||||||
$heartbeatTicks = 0;
|
$heartbeatTicks = 0;
|
||||||
|
|
||||||
|
|||||||
@@ -33,6 +33,18 @@ class LivePriceCacheService
|
|||||||
|
|
||||||
public function put(array $body): array
|
public function put(array $body): array
|
||||||
{
|
{
|
||||||
|
$cached = Cache::get(self::FULL_RESPONSE_KEY);
|
||||||
|
$cachedBody = is_array($cached['body'] ?? null) ? $cached['body'] : null;
|
||||||
|
|
||||||
|
if ($cachedBody !== null && PriceBroadcastService::lastPriceIdentity($cachedBody) === PriceBroadcastService::lastPriceIdentity($body)) {
|
||||||
|
Cache::put(self::FULL_RESPONSE_KEY, [
|
||||||
|
'body' => $cachedBody,
|
||||||
|
'stored_at' => now(),
|
||||||
|
], now()->addSeconds(self::TTL_SECONDS + 5));
|
||||||
|
|
||||||
|
return $cachedBody;
|
||||||
|
}
|
||||||
|
|
||||||
Cache::put(self::FULL_RESPONSE_KEY, [
|
Cache::put(self::FULL_RESPONSE_KEY, [
|
||||||
'body' => $body,
|
'body' => $body,
|
||||||
'stored_at' => now(),
|
'stored_at' => now(),
|
||||||
|
|||||||
@@ -43,15 +43,34 @@ class PriceBroadcastService
|
|||||||
}
|
}
|
||||||
|
|
||||||
$lastChanged = is_array($lastPayload) && $lastPayload !== []
|
$lastChanged = is_array($lastPayload) && $lastPayload !== []
|
||||||
&& $this->rememberHashIfChanged(self::LAST_HASH_KEY, $lastPayload);
|
&& $this->rememberHashIfChanged(self::LAST_HASH_KEY, self::lastPriceIdentity($lastPayload));
|
||||||
|
|
||||||
$this->publishListIfChanged();
|
|
||||||
|
|
||||||
if ($lastChanged && is_array($lastPayload)) {
|
if ($lastChanged && is_array($lastPayload)) {
|
||||||
$stream->publish('last', $lastPayload);
|
$stream->publish('last', $lastPayload);
|
||||||
|
$this->publishListIfChanged(true);
|
||||||
|
} else {
|
||||||
|
$this->publishListIfChanged();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<string, mixed> $payload
|
||||||
|
* @return array<string, int|bool>
|
||||||
|
*/
|
||||||
|
public static function lastPriceIdentity(array $payload): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'sell' => (int) ($payload['sell'] ?? 0),
|
||||||
|
'buy' => (int) ($payload['buy'] ?? 0),
|
||||||
|
'price_last' => (int) ($payload['price_last'] ?? 0),
|
||||||
|
'price' => (int) ($payload['price'] ?? 0),
|
||||||
|
'closed' => (bool) ($payload['closed'] ?? false),
|
||||||
|
'main_product' => (int) ($payload['main_product'] ?? 0),
|
||||||
|
'montzar' => (int) ($payload['montzar'] ?? 0),
|
||||||
|
'zaarbox' => (int) ($payload['zaarbox'] ?? 0),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
public function broadcastLastIfChanged(?array $lastPayload): void
|
public function broadcastLastIfChanged(?array $lastPayload): void
|
||||||
{
|
{
|
||||||
$stream = app(PriceStreamService::class);
|
$stream = app(PriceStreamService::class);
|
||||||
@@ -63,39 +82,74 @@ class PriceBroadcastService
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (! $this->rememberHashIfChanged(self::LAST_HASH_KEY, $lastPayload)) {
|
if (! $this->rememberHashIfChanged(self::LAST_HASH_KEY, self::lastPriceIdentity($lastPayload))) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$stream->publish('last', $lastPayload);
|
$stream->publish('last', $lastPayload);
|
||||||
|
$this->publishListIfChanged(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function broadcastListIfChanged(): bool
|
public function buildListPayload(): ?array
|
||||||
|
{
|
||||||
|
return $this->buildProductListPayload();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function broadcastListIfChanged(bool $force = false): bool
|
||||||
{
|
{
|
||||||
$stream = app(PriceStreamService::class);
|
$stream = app(PriceStreamService::class);
|
||||||
if (! $stream->isEnabled()) {
|
if (! $stream->isEnabled()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->publishListIfChanged();
|
return $this->publishListIfChanged($force);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function publishListIfChanged(): bool
|
private function publishListIfChanged(bool $force = false): bool
|
||||||
{
|
{
|
||||||
$listPayload = $this->buildProductListPayload();
|
$listPayload = $this->buildProductListPayload();
|
||||||
if ($listPayload === null) {
|
if ($listPayload === null || $listPayload === []) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (! $this->rememberHashIfChanged(self::LIST_HASH_KEY, $listPayload)) {
|
if (! $force && ! $this->rememberHashIfChanged(self::LIST_HASH_KEY, $this->listIdentity($listPayload))) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($force) {
|
||||||
|
$this->rememberHashIfChanged(self::LIST_HASH_KEY, $this->listIdentity($listPayload));
|
||||||
|
}
|
||||||
|
|
||||||
app(PriceStreamService::class)->publish('list', $listPayload);
|
app(PriceStreamService::class)->publish('list', $listPayload);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<int, array<string, mixed>> $listPayload
|
||||||
|
* @return array<int, array<string, int|bool>>
|
||||||
|
*/
|
||||||
|
private function listIdentity(array $listPayload): array
|
||||||
|
{
|
||||||
|
$identity = [];
|
||||||
|
|
||||||
|
foreach ($listPayload as $product) {
|
||||||
|
if (! is_array($product)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$identity[] = [
|
||||||
|
'id' => (int) ($product['id'] ?? 0),
|
||||||
|
'price' => (int) ($product['price'] ?? 0),
|
||||||
|
'price_profit' => (int) ($product['price_profit'] ?? 0),
|
||||||
|
'show' => (bool) ($product['show'] ?? true),
|
||||||
|
'active' => (bool) ($product['active'] ?? true),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $identity;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array<string, mixed>|array<int, mixed> $payload
|
* @param array<string, mixed>|array<int, mixed> $payload
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -100,6 +100,15 @@ class PriceStreamService
|
|||||||
|
|
||||||
$key = $this->inboxKey();
|
$key = $this->inboxKey();
|
||||||
$inbox = Cache::get($key, ['seq' => 0, 'events' => []]);
|
$inbox = Cache::get($key, ['seq' => 0, 'events' => []]);
|
||||||
|
$events = is_array($inbox['events'] ?? null) ? $inbox['events'] : [];
|
||||||
|
$lastEvent = $events !== [] ? $events[array_key_last($events)] : null;
|
||||||
|
|
||||||
|
if (is_array($lastEvent)
|
||||||
|
&& ($lastEvent['event'] ?? null) === $event
|
||||||
|
&& $this->eventDataHash($event, $lastEvent['data'] ?? null) === $this->eventDataHash($event, $data)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
$inbox['seq'] = (int) ($inbox['seq'] ?? 0) + 1;
|
$inbox['seq'] = (int) ($inbox['seq'] ?? 0) + 1;
|
||||||
$inbox['events'][] = [
|
$inbox['events'][] = [
|
||||||
'id' => $inbox['seq'],
|
'id' => $inbox['seq'],
|
||||||
@@ -119,6 +128,41 @@ class PriceStreamService
|
|||||||
return self::INBOX_PREFIX.'global';
|
return self::INBOX_PREFIX.'global';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<string, mixed>|array<int, mixed> $data
|
||||||
|
*/
|
||||||
|
private function eventDataHash(string $event, mixed $data): string
|
||||||
|
{
|
||||||
|
if (! is_array($data)) {
|
||||||
|
return md5('');
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($event === 'last') {
|
||||||
|
return md5(json_encode(PriceBroadcastService::lastPriceIdentity($data), JSON_UNESCAPED_UNICODE));
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($event === 'list') {
|
||||||
|
$identity = [];
|
||||||
|
foreach ($data as $product) {
|
||||||
|
if (! is_array($product)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$identity[] = [
|
||||||
|
'id' => (int) ($product['id'] ?? 0),
|
||||||
|
'price' => (int) ($product['price'] ?? 0),
|
||||||
|
'price_profit' => (int) ($product['price_profit'] ?? 0),
|
||||||
|
'show' => (bool) ($product['show'] ?? true),
|
||||||
|
'active' => (bool) ($product['active'] ?? true),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
return md5(json_encode($identity, JSON_UNESCAPED_UNICODE));
|
||||||
|
}
|
||||||
|
|
||||||
|
return md5(json_encode($data, JSON_UNESCAPED_UNICODE));
|
||||||
|
}
|
||||||
|
|
||||||
private function normalizeHost(string $host): string
|
private function normalizeHost(string $host): string
|
||||||
{
|
{
|
||||||
$host = strtolower(trim($host));
|
$host = strtolower(trim($host));
|
||||||
|
|||||||
Reference in New Issue
Block a user