Commit 230aee20 by Евгений

Улучшение #19474

Синхронизация фраз
1 parent 44836271
......@@ -62,9 +62,9 @@ class AdGroupsLoadUpdated extends Command
$factory = APIRequest::getInstance(API::YANDEX);
$factory->setToken($token);
$factory->getRequest('AdGroups', 'get')->call([
'Ids' => $ids,
]);
// $factory->getRequest('AdGroups', 'get')->call([
// 'Ids' => $ids,
// ]);
foreach (array_chunk($ids, 1000) as $ids_limit) {
$factory = APIRequest::getInstance(API::YANDEX);
......
......@@ -42,7 +42,6 @@ class CampaignsCheckUpdatedChildrenAdGroups extends Command
public function handle()
{
if (Campaigns::forUpdatedChildren()->exists()) {
$token = Tokens::where('type', Tokens::MAIN)->first();
if (!$token) {
throw new \Exception('Не найден токен блин');
......@@ -58,7 +57,6 @@ class CampaignsCheckUpdatedChildrenAdGroups extends Command
],
'Timestamp' => $token->check_changes_ad_group_at->toIso8601ZuluString(),
]);
}
......
......@@ -10,6 +10,7 @@ use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
class ProcessCallLimitedAPI implements ShouldQueue//, ShouldBeUnique
{
......@@ -42,6 +43,7 @@ class ProcessCallLimitedAPI implements ShouldQueue//, ShouldBeUnique
$objects = $limits->countObjectsLimit($this->api);
if (!$objects) {
//нет свободных баллов, замораживаем до следующего часа
Log::debug('Нет баллов? ' . $objects);
$this->reRunHour();
return;
}
......@@ -51,6 +53,7 @@ class ProcessCallLimitedAPI implements ShouldQueue//, ShouldBeUnique
$limitId = $limits->doRezerv($this->api, $objects);
}catch(\Exception $e){
//нет свободных баллов, замораживаем до следующего часа
Log::debug($e);
$this->reRunHour();
return;
}
......
......@@ -4,6 +4,7 @@ namespace App\Service\Handlers;
use App\Jobs\ProcessCallLimitedAPI;
use App\Service\Contract\API;
use App\Service\Contract\APIRequest;
use Illuminate\Support\Facades\Log;
class AdsHandler
{
......@@ -28,17 +29,16 @@ class AdsHandler
{
$this->parse($response);
$offset = $this->limited($response);
$offset = $this->limited();
if ($offset) {
$this->request->next($offset);
dispatch(new ProcessCallLimitedAPI($this->request));
dispatch(new ProcessCallLimitedAPI($this->request))->onQueue('limits');;
}
$this->request->handle($this->response);
}
protected function limited($response)
protected function limited()
{
return false;
}
......
......@@ -3,9 +3,9 @@ namespace App\Service\Handlers;
class DirectHandler extends AdsHandler
{
public function limited($response)
public function limited()
{
return $response['result']['LimitedBy'] ?? false;
return $this->response['result']['LimitedBy'] ?? false;
}
protected function parse($response)
......
......@@ -11,6 +11,7 @@ use App\Models\Pivots\GoalKeyword;
use App\Service\Contract\APIRequest;
use App\Service\Requests\DirectRequest;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
class GetKeywords extends DirectRequest
......@@ -30,16 +31,16 @@ class GetKeywords extends DirectRequest
public function getObjectsCount()
{
$params = $this->getParams();
if (isset($params['SelectionCriteria']['CampaignIds'])) {
return -1;
}
if (isset($params['SelectionCriteria']['AdGroupIds'])) {
return -1;
}
// if (isset($params['SelectionCriteria']['CampaignIds'])) {
// return -1;
// }
// if (isset($params['SelectionCriteria']['AdGroupIds'])) {
// return -1;
// }
if (isset($params['SelectionCriteria']['Ids'])) {
return count($params['SelectionCriteria']['Ids']);
}
return -1;
return $this->getMaxCount();
}
public function slice($maxObjects): ?APIRequest
......@@ -116,6 +117,7 @@ class GetKeywords extends DirectRequest
'state' => $keyword['State'],
'status' => $keyword['Status'],
'serving_status' => $keyword['ServingStatus'],
'deleted_at' => null//не забыть убрать признак удаления, если вдруг опять пришла удаленная ранее фраза
];
$keyword = Keyword::updateOrCreate([
......@@ -156,45 +158,64 @@ class GetKeywords extends DirectRequest
}
if ($this->getToken()->isMain()) {
if (isset($this->getParams()['SelectionCriteria']['AdGroupIds'])) {
AdGroup::whereIn('external_id', $this->getParams()['SelectionCriteria']['AdGroupIds'])
->update([
'keywords_loaded_at' => Carbon::now(),
]);
}
if (count($campaign_ids_synced_need)) {
Campaigns::findMany(array_keys($campaign_ids_synced_need))->each(function (Campaigns $campaign) {
$campaign->dictionaryCampaigns()->update([
'synced_need' => Carbon::now(),
]);
});
}
$keywordQuery = Keyword::query();
if (isset($this->getParams()['SelectionCriteria']['AdGroupIds'])) {
$keywordQuery->whereIn('ad_group_external_id', $this->getParams()['SelectionCriteria']['AdGroupIds']);
} else {
$keywordQuery->whereIn('ad_group_id', $ad_groups->pluck('id'));
}
} else {
$keywordQuery = GoalKeyword::query()
->whereIn('goal_ad_group_id', $ad_groups->pluck('id'));
}
if (count($ids)) {
$keywordQuery->whereNotIn('id', $ids);
// if ($this->getToken()->isMain()) {
//
// if (isset($this->getParams()['SelectionCriteria']['AdGroupIds'])) {
// AdGroup::whereIn('external_id', $this->getParams()['SelectionCriteria']['AdGroupIds'])
// ->update([
// 'keywords_loaded_at' => Carbon::now(),
// ]);
// }
//
// if (count($campaign_ids_synced_need)) {
// Campaigns::findMany(array_keys($campaign_ids_synced_need))->each(function (Campaigns $campaign) {
// $campaign->dictionaryCampaigns()->update([
// 'synced_need' => Carbon::now(),
// ]);
// });
// }
//
// $keywordQuery = Keyword::query();
//
// if (isset($this->getParams()['SelectionCriteria']['AdGroupIds'])) {
// $keywordQuery->whereIn('ad_group_external_id', $this->getParams()['SelectionCriteria']['AdGroupIds']);
// } else {
// $keywordQuery->whereIn('ad_group_id', $ad_groups->pluck('id'));
// }
//
// } else {
// $keywordQuery = GoalKeyword::query()
// ->whereIn('goal_ad_group_id', $ad_groups->pluck('id'));
// }
//
// if (count($ids)) {
// $keywordQuery->whereNotIn('id', $ids);
// }
//
// $keywordQuery->get()->each(function ($goalKeyword) {
// /* @var $goalKeyword GoalKeyword|Keyword */
// $goalKeyword->delete();
// });
//удаление будет толко когда получаем изменения по группам, иначе это либо загрузка все фраз либо как то избранных
if ( !empty($this->getParams()['SelectionCriteria']['AdGroupIds']) &&
!isset($response['result']['LimitedBy']) ){
$ag_groups = $this->getParams()['SelectionCriteria']['AdGroupIds'];
//последний блок с данными, можно удалять те, что так и не получили
//Выбираем все те фразы, которые обновлены позже чем группа.
//это означает что этой фразы не было в результатах и в БД она по этой причине не обновилась
//надо такие пометить на удаление
//при удалении для всех таких надо будет удалить фразы из целевых и потом удалить их сами
$sql = "UPDATE keywords k
INNER JOIN ad_groups ag ON k.ad_group_id=ag.id
SET k.deleted_at = now(),
ag.keywords_loaded_at=now()
WHERE k.updated_at>ag.keywords_loaded_at
AND k.deleted_at is null
AND ag.id in (" . implode(", ", $ag_groups) . ")";
DB::update($sql);
}
$keywordQuery->get()->each(function ($goalKeyword) {
/* @var $goalKeyword GoalKeyword|Keyword */
$goalKeyword->delete();
});
} catch (\Exception $e) {
Log::debug($e);
throw $e;
......@@ -214,7 +235,7 @@ class GetKeywords extends DirectRequest
];
if (isset($filter['CampaignIds'])) {
$this->max_count = $this->max_count_CampaignIds;
// $this->max_count = $this->max_count_CampaignIds;
$params['SelectionCriteria'] = [
'CampaignIds' => $filter['CampaignIds'],
......@@ -222,7 +243,7 @@ class GetKeywords extends DirectRequest
}
if (isset($filter['AdGroupIds'])) {
$this->max_count = $this->max_count_AdGroupIds;
// $this->max_count = $this->max_count_AdGroupIds;
$params['SelectionCriteria'] = [
'AdGroupIds' => $filter['AdGroupIds'],
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!