Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add debounce #210

Merged
merged 19 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions config/front/repository.yml
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,9 @@ services:
public: true
arguments:
- "@ps_eventbus.db"

PrestaShop\Module\PsEventbus\Repository\LiveSyncRepository:
class: PrestaShop\Module\PsEventbus\Repository\LiveSyncRepository
public: true
arguments:
- "@ps_eventbus.db"
1 change: 1 addition & 0 deletions config/front/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ services:
arguments:
- '@PrestaShop\Module\PsEventbus\Repository\EventbusSyncRepository'
- '@PrestaShop\Module\PsEventbus\Repository\IncrementalSyncRepository'
- '@PrestaShop\Module\PsEventbus\Repository\LiveSyncRepository'
- '@PrestaShop\Module\PsEventbus\Service\ProxyServiceInterface'
- '@PrestaShop\Module\PsEventbus\Decorator\PayloadDecorator'

Expand Down
109 changes: 60 additions & 49 deletions ps_eventbus.php

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions sql/install.sql
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,11 @@ CREATE TABLE IF NOT EXISTS `PREFIX_eventbus_incremental_sync`
PRIMARY KEY (`type`, `id_object`, `id_shop`, `lang_iso`)
) ENGINE = ENGINE_TYPE
DEFAULT CHARSET = utf8;

CREATE TABLE IF NOT EXISTS `PREFIX_eventbus_live_sync`
fox-john marked this conversation as resolved.
Show resolved Hide resolved
(
`shop_content` VARCHAR(50) NOT NULL,
`last_change_at` DATETIME NOT NULL,
PRIMARY KEY (`shop_content`)
) ENGINE = ENGINE_TYPE
DEFAULT CHARSET = utf8;
1 change: 1 addition & 0 deletions sql/uninstall.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ DROP TABLE IF EXISTS `PREFIX_eventbus_type_sync`;
DROP TABLE IF EXISTS `PREFIX_eventbus_job`;
DROP TABLE IF EXISTS `PREFIX_eventbus_deleted_objects`;
DROP TABLE IF EXISTS `PREFIX_eventbus_incremental_sync`;
DROP TABLE IF EXISTS `PREFIX_eventbus_live_sync`;
6 changes: 3 additions & 3 deletions src/Api/LiveSyncApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ private function getClient($timeout = Config::SYNC_API_MAX_TIMEOUT)
}

/**
* @param array $shopContents
* @param string $shopContent
* @param int $shopContentId
* @param string $action
*
* @return array
*/
public function liveSync($shopContents, $shopContentId, $action)
public function liveSync(string $shopContent, int $shopContentId, string $action)
{
$rawResponse = $this->getClient(3)->sendRequest(
new Request(
Expand All @@ -83,7 +83,7 @@ public function liveSync($shopContents, $shopContentId, $action)
'User-Agent' => 'ps-eventbus/' . $this->module->version,
'Content-Type' => 'application/json',
],
'{"shopContents":' . json_encode($shopContents) . ', "shopContentId": ' . $shopContentId . ', "action": "' . $action . '"}'
'{"shopContents": ["' . $shopContent . '"], "shopContentId": ' . $shopContentId . ', "action": "' . $action . '"}'
)
);

Expand Down
6 changes: 3 additions & 3 deletions src/Api/SyncApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ public function validateJobId($jobId)
}

/**
* @param array $shopContents
* @param array $shopContent
* @param int $shopContentId
* @param string $action
*
* @return array
*/
public function liveSync($shopContents, $shopContentId, $action)
public function liveSync($shopContent, $shopContentId, $action)
{
$rawResponse = $this->getClient(3)->sendRequest(
new Request(
Expand All @@ -108,7 +108,7 @@ public function liveSync($shopContents, $shopContentId, $action)
'User-Agent' => 'ps-eventbus/' . $this->module->version,
'Content-Type' => 'application/json',
],
'{"shopContents":' . json_encode($shopContents) . ', "shopContentId": ' . $shopContentId . ', "action": "' . $action . '"}'
'{"shopContents":' . json_encode($shopContent) . ', "shopContentId": ' . $shopContentId . ', "action": "' . $action . '"}'
)
);

Expand Down
62 changes: 62 additions & 0 deletions src/Repository/LiveSyncRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace PrestaShop\Module\PsEventbus\Repository;

class LiveSyncRepository
{
/**
* @var \Db
*/
private $db;

/**
* @param \Db $db
*/
public function __construct(\Db $db)
{
$this->db = $db;
}

/**
* @param string $shopContent
*
* @return array|null
*
* @throws \PrestaShopDatabaseException
*/
public function getShopContentInfo(string $shopContent)
{
$query = '
SELECT `eb_ls`.`shop_content`, `eb_ls`.`last_change_at`
FROM `' . _DB_PREFIX_ . 'eventbus_live_sync` `eb_ls`
WHERE `eb_ls`.`shop_content` = "' . pSQL($shopContent) . '";
';

$result = $this->db->executeS($query);

if (is_array($result) && count($result) > 0) {
return $result[0];
}

return null;
}

/**
* @param string $shopContent
* @param string $lastChangeAt
*
* @return bool
*
* @throws \PrestaShopDatabaseException
*/
public function upsertDebounce(string $shopContent, string $lastChangeAt)
{
$query = '
INSERT INTO `' . _DB_PREFIX_ . 'eventbus_live_sync` (`shop_content`, `last_change_at`)
VALUES ("' . pSQL($shopContent) . '", "' . pSQL($lastChangeAt) . '")
ON DUPLICATE KEY UPDATE `last_change_at` = "' . pSQL($lastChangeAt) . '";
';

return $this->db->execute($query);
}
}
32 changes: 32 additions & 0 deletions src/Service/SynchronizationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PrestaShop\Module\PsEventbus\Provider\PaginatedApiDataProviderInterface;
use PrestaShop\Module\PsEventbus\Repository\EventbusSyncRepository;
use PrestaShop\Module\PsEventbus\Repository\IncrementalSyncRepository;
use PrestaShop\Module\PsEventbus\Repository\LiveSyncRepository;

class SynchronizationService
{
Expand All @@ -19,6 +20,10 @@ class SynchronizationService
* @var IncrementalSyncRepository
*/
private $incrementalSyncRepository;
/**
* @var LiveSyncRepository
*/
private $liveSyncRepository;
/**
* @var ProxyServiceInterface
*/
Expand All @@ -31,11 +36,13 @@ class SynchronizationService
public function __construct(
EventbusSyncRepository $eventbusSyncRepository,
IncrementalSyncRepository $incrementalSyncRepository,
LiveSyncRepository $liveSyncRepository,
ProxyServiceInterface $proxyService,
PayloadDecorator $payloadDecorator
) {
$this->eventbusSyncRepository = $eventbusSyncRepository;
$this->incrementalSyncRepository = $incrementalSyncRepository;
$this->liveSyncRepository = $liveSyncRepository;
$this->proxyService = $proxyService;
$this->payloadDecorator = $payloadDecorator;
}
Expand Down Expand Up @@ -129,6 +136,31 @@ public function handleIncrementalSync(PaginatedApiDataProviderInterface $dataPro
return $this->returnSyncResponse($data, $response, $remainingObjects);
}

/**
* @param string $shopContentName
*
* @return bool
*
* @throws \PrestaShopDatabaseException
*/
public function debounceLiveSync(string $shopContentName)
{
$dateNow = date('Y-m-d H:i:s');

$shopContent = $this->liveSyncRepository->getShopContentInfo($shopContentName);

$lastChangeAt = $shopContent != null ? (string) $shopContent['last_change_at'] : (string) $dateNow;
$diff = strtotime((string) $dateNow) - strtotime((string) $lastChangeAt);

if ($shopContent == null || $diff > 60 * 5) {
$this->liveSyncRepository->upsertDebounce($shopContentName, $dateNow);

return true;
}

return false;
}

/**
* @param array $data
* @param array $syncResponse
Expand Down
20 changes: 20 additions & 0 deletions upgrade/Upgrade-2.3.5.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/**
* @return bool
*/
function upgrade_module_2_3_5($module)
{
$db = Db::getInstance();

$query = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'eventbus_live_sync` (
`shop_content` VARCHAR(50) NOT NULL,
`last_change_at` DATETIME NOT NULL,
PRIMARY KEY (`shop_content`)
) ENGINE = ENGINE_TYPE
DEFAULT CHARSET = utf8;';

$db->execute($query);

return true;
}