Commit 9d038eb4 by Vladislav

#20444 Загрузка и синхронизация условий нацеливания на аудиторию

1 parent d8ba4995
......@@ -4,6 +4,7 @@ namespace App\Console\Commands;
use App\Models\Campaigns;
use App\Models\Tokens;
use App\Service\Requests\Direct\GetAudienceTargets;
use App\Service\Requests\Direct\GetKeywords;
use Illuminate\Console\Command;
......@@ -65,6 +66,11 @@ class AdGroupsLoadKeywords extends Command
->call([
'AdGroupIds' => $ids_limit,
]);
$request = new GetAudienceTargets();
$request->setToken($token)
->call([
'AdGroupIds' => $ids_limit,
]);
}
return 0;
......
......@@ -5,6 +5,7 @@ namespace App\Console\Commands;
use App\Models\AdGroup;
use App\Models\Tokens;
use App\Service\Requests\Direct\GetAdGroups;
use App\Service\Requests\Direct\GetAudienceTargets;
use App\Service\Requests\Direct\GetBidModifiers;
use App\Service\Requests\Direct\GetKeywords;
use Illuminate\Console\Command;
......@@ -58,12 +59,10 @@ class AdGroupsLoadUpdated extends Command
throw new \Exception('Не найден токен блин');
}
$ids = $adGroups->pluck('external_id')->toArray();
$request = new GetAdGroups();
$request->setToken($token)
->call([
'Ids' => $ids,
'Ids' => $adGroups->pluck('external_id')->toArray(),
]);
/*$request = new GetBidModifiers();
......@@ -72,11 +71,19 @@ class AdGroupsLoadUpdated extends Command
'AdGroupIds' => $ids,
]);*/
foreach (array_chunk($ids, 1000) as $ids_limit) {
foreach ($adGroups->chunk(1000) as $adGroups_chunk) {
$adGroupIds = $adGroups_chunk->pluck('external_id')->toArray();
$request = new GetKeywords();
$request->setToken($token)
->call([
'AdGroupIds' => $ids_limit,
'AdGroupIds' => $adGroupIds,
]);
$request = new GetAudienceTargets();
$request->setToken($token)
->call([
'AdGroupIds' => $adGroupIds,
]);
}
......
<?php
namespace App\Console\Commands;
use App\Models\Pivots\GoalAudienceTarget;
use App\Models\Tokens;
use App\Service\Requests\Direct\AddAudienceTargets;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Relations\HasMany;
class AudienceTargetsAdd extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'audiencetargets:add';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Добавление не созданных условий нацеливания на аудиторию с целевого аккаунта';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
* @throws \Exception
*/
public function handle()
{
$tokens = Tokens::whereHas('dictionaryCampaignsEnabledForExternalSynchronized.goalAudienceTargetsForNotExternalForNotReserveCreate.audienceTarget')
->where('type', '!=', Tokens::MAIN)
->get();
foreach ($tokens as $token) {
$token->load([
'dictionaryCampaignsEnabledForExternalSynchronized.goalAudienceTargetsForNotExternalForNotReserveCreate' => function (HasMany $query) {
return $query->has('audienceTarget');
},
'dictionaryCampaignsEnabledForExternalSynchronized.goalAudienceTargetsForNotExternalForNotReserveCreate.audienceTarget',
]);
$goalAudienceTargets = $token->dictionaryCampaignsEnabledForExternalSynchronized->pluck('goalAudienceTargetsForNotExternalForNotReserveCreate')
->collapse();
foreach (array_chunk($goalAudienceTargets->pluck('id')->toArray(), 1000) as $items) {
GoalAudienceTarget::whereIn('id', $items)
->update([
'reserve_create_at' => Carbon::now(),
]);
}
$request = new AddAudienceTargets();
$request->setToken($token)
->call([
'goalAudienceTargets' => $goalAudienceTargets,
]);
}
return 0;
}
}
......@@ -305,6 +305,25 @@ class DictionaryCampaignsSyncByCampaign extends Command
");
$this->info('goal_retargetinglists successful!');
//грузим условия нацеливания на аудиторию которых по какой то причне нет в целевых.
DB::insert("
INSERT INTO goal_audience_targets(dictionary_campaign_external_id, dictionary_campaign_id,
goal_ad_group_external_id, goal_ad_group_id, goal_retargetinglist_external_id, goal_retargetinglist_id,
audience_target_id, created_at, updated_at)
SELECT dc.external_id, dc.id, gag.external_id, gag.id, gr.external_id, gr.id, aut.id, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
FROM audience_targets aut
INNER JOIN campaigns c on aut.campaign_id = c.id
INNER JOIN dictionary_campaigns dc on dc.campaign_id = c.id
INNER JOIN dictionaries d on dc.dictionary_id = d.id
LEFT JOIN retargetinglists r on aut.retargetinglist_id = r.id
LEFT JOIN goal_retargetinglists gr on r.id = gr.retargetinglist_id AND d.token_id = gr.token_id
LEFT JOIN ad_groups ag on c.id = ag.campaign_id AND aut.ad_group_id = ag.id
LEFT JOIN goal_ad_groups gag on dc.id = gag.dictionary_campaign_id AND ag.id = gag.ad_group_id
LEFT JOIN goal_audience_targets gaut on aut.id = gaut.audience_target_id and gaut.dictionary_campaign_id = dc.id
WHERE gaut.audience_target_id is null
");
$this->info('goal_bid_modifiers successful!');
//грузим условия ретаргетинга и подбора аудитории которых по какой то причне нет в целевых.
DB::insert("
INSERT INTO goal_bid_modifiers(dictionary_campaign_external_id, goal_ad_group_external_id, dictionary_campaign_id,
......
......@@ -43,19 +43,19 @@ class NegativeKeywordSharedSetsUpdate extends Command
*/
public function handle()
{
$tokens = Tokens::whereHas('goalNegativeKeywordSharedSetsForExternalForNotReserveUpdate.negativeKeywordSharedSet')
$tokens = Tokens::whereHas('goalNegativeKeywordSharedSetsForExternalForNotReserveUpdateNeedUpdated.negativeKeywordSharedSet')
->where('type', '!=', Tokens::MAIN)
->get();
foreach ($tokens as $token) {
$token->load([
'goalNegativeKeywordSharedSetsForExternalForNotReserveUpdate' => function (HasMany $query) {
'goalNegativeKeywordSharedSetsForExternalForNotReserveUpdateNeedUpdated' => function (HasMany $query) {
return $query->has('negativeKeywordSharedSet');
},
'goalNegativeKeywordSharedSetsForExternalForNotReserveUpdate.negativeKeywordSharedSet',
'goalNegativeKeywordSharedSetsForExternalForNotReserveUpdateNeedUpdated.negativeKeywordSharedSet',
]);
$goalNegativeKeywordSharedSets = $token->goalNegativeKeywordSharedSetsForExternalForNotReserveUpdate;
$goalNegativeKeywordSharedSets = $token->goalNegativeKeywordSharedSetsForExternalForNotReserveUpdateNeedUpdated;
foreach (array_chunk($goalNegativeKeywordSharedSets->pluck('id')->toArray(), 1000) as $items) {
GoalNegativeKeywordSharedSet::whereIn('id', $items)
......
......@@ -2,10 +2,9 @@
namespace App\Console\Commands;
use App\Models\Pivots\GoalSitelink;
use App\Models\Pivots\GoalRetargetinglist;
use App\Models\Tokens;
use App\Service\Requests\Direct\AddRetargetinglists;
use App\Service\Requests\Direct\AddSitelinks;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Relations\HasMany;
......@@ -59,7 +58,7 @@ class RetargetinglistsAdd extends Command
$goalRetargetinglists = $token->goalRetargetinglistsForNotExternalForNotReserveCreate;
foreach (array_chunk($goalRetargetinglists->pluck('id')->toArray(), 1000) as $items) {
GoalSitelink::whereIn('id', $items)
GoalRetargetinglist::whereIn('id', $items)
->update([
'reserve_create_at' => Carbon::now(),
]);
......
<?php
namespace App\Console\Commands;
use App\Models\Pivots\GoalRetargetinglist;
use App\Models\Tokens;
use App\Service\Requests\Direct\UpdateRetargetinglists;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Relations\HasMany;
class RetargetinglistsUpdate extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'retargetinglists:update';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Изменение созданных условия ретаргетинга и подбора аудитории с целевого аккаунта';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
* @throws \Exception
*/
public function handle()
{
$tokens = Tokens::whereHas('goalRetargetinglistsForExternalForNotReserveUpdateNeedUpdated.retargetinglist')
->where('type', '!=', Tokens::MAIN)
->get();
foreach ($tokens as $token) {
$token->load([
'goalRetargetinglistsForExternalForNotReserveUpdateNeedUpdated' => function (HasMany $query) {
return $query->has('retargetinglist');
},
'goalRetargetinglistsForExternalForNotReserveUpdateNeedUpdated.retargetinglist',
]);
$goalRetargetinglists = $token->goalRetargetinglistsForExternalForNotReserveUpdateNeedUpdated;
foreach (array_chunk($goalRetargetinglists->pluck('id')->toArray(), 1000) as $items) {
GoalRetargetinglist::whereIn('id', $items)
->update([
'reserve_update_at' => Carbon::now(),
]);
}
$request = new UpdateRetargetinglists();
$request->setToken($token)
->call([
'goalRetargetinglists' => $goalRetargetinglists,
]);
}
return 0;
}
}
......@@ -10,6 +10,7 @@ use App\Console\Commands\AdGroupsUpdate;
use App\Console\Commands\AdvertisementsAdd;
use App\Console\Commands\AdvertisementsLoadUpdated;
use App\Console\Commands\AdvertisementsUpdate;
use App\Console\Commands\AudienceTargetsAdd;
use App\Console\Commands\BidModifiersAdd;
use App\Console\Commands\BidModifiersDelete;
use App\Console\Commands\BidModifiersUpdate;
......@@ -34,6 +35,7 @@ use App\Console\Commands\NegativeKeywordSharedSetsUpdate;
use App\Console\Commands\RefreshLimits;
use App\Console\Commands\RetargetinglistsAdd;
use App\Console\Commands\RetargetinglistsLoad;
use App\Console\Commands\RetargetinglistsUpdate;
use App\Console\Commands\SitelinksAdd;
use App\Console\Commands\SitelinksLoad;
use App\Console\Commands\VCardsAdd;
......@@ -69,7 +71,7 @@ class Kernel extends ConsoleKernel
$schedule->command(CampaignsCheckUpdatedChildren::class)->hourlyAt(10);
$schedule->command(RetargetinglistsLoad::class)->hourlyAt(5);
$schedule->command(RetargetinglistsAdd::class)->hourlyAt(10);
$schedule->command(RetargetinglistsUpdate::class)->hourlyAt(10);
$schedule->command(NegativeKeywordSharedSetsLoad::class)->hourlyAt(10);
$schedule->command(NegativeKeywordSharedSetsAdd::class)->hourlyAt(15);
......@@ -95,6 +97,9 @@ class Kernel extends ConsoleKernel
$schedule->command(AdGroupsAdd::class)->hourlyAt(35);
$schedule->command(AdGroupsUpdate::class)->hourlyAt(35);
$schedule->command(RetargetinglistsAdd::class)->hourlyAt(40);
$schedule->command(AudienceTargetsAdd::class)->hourlyAt(45);
$schedule->command(BidModifiersDelete::class)->hourlyAt(30);
$schedule->command(BidModifiersAdd::class)->hourlyAt(35);
......
<?php
namespace App\Models;
use App\Models\Pivots\GoalAudienceTarget;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Collection;
class AudienceTarget extends Model
{
use SoftDeletes;
const STRATEGY_PRIORITY_LOW = 'LOW';
const STRATEGY_PRIORITY_NORMAL = 'NORMAL';
const STRATEGY_PRIORITY_HIGH = 'HIGH';
protected $table = 'audience_targets';
protected $fillable = [
'external_id',
'ad_group_external_id',
'campaign_external_id',
'retargetinglist_external_id',
'ad_group_id',
'campaign_id',
'retargetinglist_id',
'interest_external_id',
'context_bid',
'strategy_priority',
];
/**
* @return Collection
*/
static public function getPropertiesWatch()
{
return collect([
'ad_group_external_id',
'campaign_external_id',
'retargetinglist_external_id',
'interest_external_id',
'context_bid',
'strategy_priority',
]);
}
public function adGroup()
{
return $this->belongsTo(AdGroup::class, 'ad_group_id');
}
public function campaign()
{
return $this->belongsTo(Campaigns::class, 'campaign_id');
}
public function retargetinglist()
{
return $this->belongsTo(Retargetinglist::class, 'retargetinglist_id');
}
public function goalAudienceTargets()
{
return $this->hasMany(GoalAudienceTarget::class, 'audience_target_id');
}
}
......@@ -521,4 +521,14 @@ class DictionaryCampaign extends Pivot
return $this->goalVCards()->forNotExternal()->forNotReserveCreate();
}
public function goalAudienceTargets()
{
return $this->hasMany(GoalAudienceTarget::class, 'dictionary_campaign_id');
}
public function goalAudienceTargetsForNotExternalForNotReserveCreate()
{
return $this->goalAudienceTargets()->forNotExternal()->forNotReserveCreate();
}
}
......@@ -242,4 +242,9 @@ class GoalAdGroup extends Pivot
return $this->hasMany(GoalBidModifier::class, 'goal_ad_group_id');
}
public function goalAudienceTargets()
{
return $this->hasMany(GoalAudienceTarget::class, 'ad_group_id');
}
}
<?php
namespace App\Models\Pivots;
use App\Models\AudienceTarget;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\Pivot;
use Illuminate\Database\Eloquent\SoftDeletes;
class GoalAudienceTarget extends Pivot
{
use SoftDeletes;
protected $table = 'goal_audience_targets';
protected $fillable = [
'external_id',
'goal_ad_group_external_id',
'dictionary_campaign_external_id',
'goal_retargetinglist_external_id',
'goal_ad_group_id',
'dictionary_campaign_id',
'goal_retargetinglist_id',
'audience_target_id',
'external_upload_at',
'external_updated_at',
'updated_need',
'reserve_create_at',
'reserve_update_at',
];
protected $casts = [
'external_upload_at' => 'datetime',
'external_updated_at' => 'datetime',
'updated_need' => 'datetime',
'reserve_create_at' => 'datetime',
'reserve_update_at' => 'datetime',
];
public $incrementing = true;
static public function getWithPivot()
{
return [
'id',
'external_id',
'goal_ad_group_external_id',
'dictionary_campaign_external_id',
'goal_ad_group_id',
'dictionary_campaign_id',
'audience_target_id',
'external_upload_at',
'external_updated_at',
'updated_need',
'reserve_create_at',
'reserve_update_at',
];
}
/**
* @param Builder $query
* @return Builder
*/
public function scopeForExternal($query)
{
return $query->whereNotNull('external_id');
}
/**
* @param Builder $query
* @return Builder
*/
public function scopeForNotExternal($query)
{
return $query->whereNull('external_id');
}
/**
* @param Builder $query
* @return Builder
*/
public function scopeForNotReserveCreate($query)
{
return $query->whereNull('reserve_create_at');
}
/**
* @param Builder $query
* @return Builder
*/
public function scopeNeedUpdated($query)
{
return $query->whereNotNull('updated_need');
}
public function goalAdGroup()
{
return $this->belongsTo(GoalAdGroup::class, 'goal_ad_group_id');
}
public function goalRetargetinglist()
{
return $this->belongsTo(GoalRetargetinglist::class, 'goal_retargetinglist_id');
}
public function audienceTarget()
{
return $this->belongsTo(AudienceTarget::class, 'audience_target_id');
}
public function dictionaryCampaign()
{
return $this->belongsTo(DictionaryCampaign::class, 'dictionary_campaign_id');
}
}
......@@ -48,17 +48,6 @@ class GoalBidModifier extends Pivot
{
use SoftDeletes;
const LEVEL_CAMPAIGN = 'CAMPAIGN';
const LEVEL_AD_GROUP = 'AD_GROUP';
const TYPE_MOBILE_ADJUSTMENT = 'MOBILE_ADJUSTMENT';
const TYPE_DESKTOP_ADJUSTMENT = 'DESKTOP_ADJUSTMENT';
const TYPE_DEMOGRAPHICS_ADJUSTMENT = 'DEMOGRAPHICS_ADJUSTMENT';
const TYPE_RETARGETING_ADJUSTMENT = 'RETARGETING_ADJUSTMENT';
const TYPE_REGIONAL_ADJUSTMENT = 'REGIONAL_ADJUSTMENT';
const TYPE_VIDEO_ADJUSTMENT = 'VIDEO_ADJUSTMENT';
const TYPE_SMART_AD_ADJUSTMENT = 'SMART_AD_ADJUSTMENT';
protected $table = 'goal_bid_modifiers';
protected $fillable = [
......@@ -69,14 +58,6 @@ class GoalBidModifier extends Pivot
'dictionary_campaign_id',
'bid_modifier_id',
'level',
'type',
'mobile_adjustment',
'desktop_adjustment',
'demographics_adjustment',
'retargeting_adjustment',
'external_upload_at',
'external_updated_at',
'updated_need',
......@@ -85,11 +66,6 @@ class GoalBidModifier extends Pivot
];
protected $casts = [
'mobile_adjustment' => 'json',
'desktop_adjustment' => 'json',
'demographics_adjustment' => 'json',
'retargeting_adjustment' => 'json',
'external_upload_at' => 'datetime',
'external_updated_at' => 'datetime',
'updated_need' => 'datetime',
......@@ -110,14 +86,6 @@ class GoalBidModifier extends Pivot
'dictionary_campaign_id',
'bid_modifier_id',
'level',
'type',
'mobile_adjustment',
'desktop_adjustment',
'demographics_adjustment',
'retargeting_adjustment',
'external_upload_at',
'external_updated_at',
'updated_need',
......
......@@ -99,4 +99,9 @@ class GoalRetargetinglist extends Pivot
return $this->belongsTo(Retargetinglist::class, 'retargetinglist_id');
}
public function goalAudienceTargets()
{
return $this->hasMany(GoalAudienceTarget::class, 'goal_retargetinglist_id');
}
}
......@@ -61,7 +61,7 @@ use Illuminate\Database\Eloquent\Model;
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Pivots\GoalAdExtension[] $goalAdExtensionsForNotExternalForNotReserveCreate
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Pivots\GoalNegativeKeywordSharedSet[] $goalNegativeKeywordSharedSets
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Pivots\GoalNegativeKeywordSharedSet[] $goalNegativeKeywordSharedSetsForNotExternalForNotReserveCreate
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Pivots\GoalNegativeKeywordSharedSet[] $goalNegativeKeywordSharedSetsForExternalForNotReserveUpdate
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Pivots\GoalNegativeKeywordSharedSet[] $goalNegativeKeywordSharedSetsForExternalForNotReserveUpdateNeedUpdated
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Limits[] $limits
* @property-read int|null $limits_count
* @property string|null $timestamp
......@@ -285,9 +285,9 @@ class Tokens extends Model
return $this->goalNegativeKeywordSharedSets()->forNotExternal()->forNotReserveCreate();
}
public function goalNegativeKeywordSharedSetsForExternalForNotReserveUpdate()
public function goalNegativeKeywordSharedSetsForExternalForNotReserveUpdateNeedUpdated()
{
return $this->goalNegativeKeywordSharedSets()->forExternal()->forNotReserveUpdate();
return $this->goalNegativeKeywordSharedSets()->forExternal()->forNotReserveUpdate()->needUpdated();
}
public function goalSitelinks()
......@@ -310,6 +310,11 @@ class Tokens extends Model
return $this->goalRetargetinglists()->forNotExternal()->forNotReserveCreate();
}
public function goalRetargetinglistsForExternalForNotReserveUpdateNeedUpdated()
{
return $this->goalRetargetinglists()->forExternal()->forNotReserveUpdate()->needUpdated();
}
public function goalAdImages()
{
return $this->hasMany(GoalAdImage::class, 'token_id');
......
......@@ -92,6 +92,10 @@ class AddAdGroups extends DirectRequest
'goal_ad_group_external_id' => $external_id,
]);
$goalAdGroup->goalAudienceTargets()->update([
'goal_ad_group_external_id' => $external_id,
]);
}
} catch (\Exception $e) {
Log::debug($e);
......
<?php
namespace App\Service\Requests\Direct;
use App\Jobs\ProcessCallLimitedAPI;
use App\Models\AudienceTarget;
use App\Models\Pivots\GoalAudienceTarget;
use App\Service\Contract\APIRequest;
use App\Service\Requests\DirectRequest;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Log;
class AddAudienceTargets extends DirectRequest
{
protected $max_count = 30;
protected $timestamp;
/* @var Collection|AudienceTarget[] */
protected $goalAudienceTargets;
public function call($params = null)
{
$this->requestPrepare($params);
$process = new ProcessCallLimitedAPI($this);
dispatch($process)->onQueue('limits');
}
public function getObjectsCount()
{
return count($this->getParams()['AudienceTargets']);
}
public function slice($maxObjects): ?APIRequest
{
$splinter = $this->sliceByKey($maxObjects, 'AudienceTargets');
$splinter->putParams([
'goalAudienceTargets' => $this->goalAudienceTargets->slice($maxObjects)->values(),
]);
$this->putParams([
'goalAudienceTargets' => $this->goalAudienceTargets->slice(0, $maxObjects),
]);
return $splinter;
}
public function handle($response)
{
try {
if (!isset($response['result']['AddResults'])) {
return;
}
foreach ($response['result']['AddResults'] as $key => $add_result) {
$goalAudienceTarget = $this->goalAudienceTargets->get($key);
if (!isset($add_result['Id'])) {
Log::debug("AddAudienceTarget, empty Id");
Log::debug($add_result);
Log::debug($this->getParams()['AudienceTargets'][$key]);
GoalAudienceTarget::where('id', $goalAudienceTarget->getKey())
->update([
'reserve_create_at' => null,
]);
continue;
}
$external_id = (string)$add_result['Id'];
GoalAudienceTarget::where('id', $goalAudienceTarget->getKey())
->update([
'external_id' => $external_id,
'external_upload_at' => Carbon::now(),
'reserve_create_at' => null,
]);
}
} catch (\Exception $e) {
Log::debug($e);
throw $e;
}
}
public function failed()
{
GoalAudienceTarget::whereIn('id', $this->goalAudienceTargets->pluck('id')->toArray())
->update([
'reserve_create_at' => null,
]);
}
public function putParams($params)
{
$this->goalAudienceTargets = $params['goalAudienceTargets'];
}
private function requestPrepare($params)
{
$this->setService('AudienceTargets');
$this->setMethod('add');
$this->putParams($params);
$this->setParams([
'AudienceTargets' => $this->goalAudienceTargets->map(function (GoalAudienceTarget $goalAudienceTarget) {
$data = [
'AdGroupId' => $goalAudienceTarget->goal_ad_group_external_id,
];
if ($goalAudienceTarget->goal_retargetinglist_external_id) {
$data['RetargetingListId'] = $goalAudienceTarget->goal_retargetinglist_external_id;
}
if ($goalAudienceTarget->audienceTarget->interest_external_id) {
$data['InterestId'] = $goalAudienceTarget->audienceTarget->interest_external_id;
}
if ($goalAudienceTarget->audienceTarget->context_bid) {
$data['ContextBid'] = $goalAudienceTarget->audienceTarget->context_bid;
}
if ($goalAudienceTarget->audienceTarget->strategy_priority) {
$data['StrategyPriority'] = $goalAudienceTarget->audienceTarget->strategy_priority;
}
return $data;
})->toArray(),
]);
}
}
......@@ -101,6 +101,10 @@ class AddCampaigns extends DirectRequest
'dictionary_campaign_external_id' => $external_id,
]);
$dictionaryCampaign->goalAudienceTargets()->update([
'dictionary_campaign_external_id' => $external_id,
]);
}
} catch (\Exception $e) {
Log::debug($e);
......
......@@ -4,7 +4,6 @@ namespace App\Service\Requests\Direct;
use App\Jobs\ProcessCallLimitedAPI;
use App\Models\Pivots\GoalRetargetinglist;
use App\Models\Pivots\GoalSitelink;
use App\Models\Variable;
use App\Service\Contract\APIRequest;
use App\Service\Requests\DirectRequest;
......@@ -57,14 +56,15 @@ class AddRetargetinglists extends DirectRequest
foreach ($response['result']['AddResults'] as $key => $add_result) {
$goalSitelink = $this->goalRetargetinglists->get($key);
$goalRetargetinglist = $this->goalRetargetinglists->get($key);
if (!isset($add_result['Id'])) {
Log::debug("AddRetargetinglist, empty Id");
Log::debug($add_result);
Log::debug($this->getParams()['RetargetingLists'][$key]);
$goalSitelink->update([
GoalRetargetinglist::where('id', $goalRetargetinglist->getKey())
->update([
'reserve_create_at' => null,
]);
......@@ -73,12 +73,17 @@ class AddRetargetinglists extends DirectRequest
$external_id = (string)$add_result['Id'];
$goalSitelink->update([
GoalRetargetinglist::where('id', $goalRetargetinglist->getKey())
->update([
'external_id' => $external_id,
'external_upload_at' => Carbon::now(),
'reserve_create_at' => null,
]);
$goalRetargetinglist->goalAudienceTargets()->update([
'goal_retargetinglist_external_id' => $external_id,
]);
}
} catch (\Exception $e) {
Log::debug($e);
......@@ -88,7 +93,7 @@ class AddRetargetinglists extends DirectRequest
public function failed()
{
GoalSitelink::whereIn('id', $this->goalRetargetinglists->pluck('id')->toArray())
GoalRetargetinglist::whereIn('id', $this->goalRetargetinglists->pluck('id')->toArray())
->update([
'reserve_create_at' => null,
]);
......
<?php
namespace App\Service\Requests\Direct;
use App\Jobs\ProcessCallLimitedAPI;
use App\Models\AdExtension;
use App\Models\AdGroup;
use App\Models\Advertisement;
use App\Models\AudienceTarget;
use App\Models\Campaigns;
use App\Models\Retargetinglist;
use App\Models\Sitelink;
use App\Service\Contract\APIRequest;
use App\Service\Requests\DirectRequest;
use Carbon\Carbon;
use Illuminate\Support\Facades\Log;
class GetAudienceTargets extends DirectRequest
{
protected $max_count = -1;
protected $max_count_Ids = 10000;
protected $max_count_AdGroupIds = 1000;
protected $max_count_RetargetingListIds = 1000;
protected $max_count_CampaignIds = 100;
function call($params = null)
{
$this->requestPrepare($params);
$process = new ProcessCallLimitedAPI($this);
dispatch($process)->onQueue('limits');
}
public function getObjectsCount()
{
$params = $this->getParams();
if (isset($params['SelectionCriteria']['CampaignIds'])) {
return count($params['SelectionCriteria']['CampaignIds']);
}
if (isset($params['SelectionCriteria']['RetargetingListIds'])) {
return count($params['SelectionCriteria']['RetargetingListIds']);
}
if (isset($params['SelectionCriteria']['AdGroupIds'])) {
return count($params['SelectionCriteria']['AdGroupIds']);
}
if (isset($params['SelectionCriteria']['Ids'])) {
return count($params['SelectionCriteria']['Ids']);
}
return -1;
}
public function slice($maxObjects): ?APIRequest
{
$params = $this->getParams();
if (isset($params['SelectionCriteria']['CampaignIds'])) {
return $this->sliceByKey($maxObjects, ['SelectionCriteria', 'CampaignIds']);
}
if (isset($params['SelectionCriteria']['RetargetingListIds'])) {
return $this->sliceByKey($maxObjects, ['SelectionCriteria', 'RetargetingListIds']);
}
if (isset($params['SelectionCriteria']['AdGroupIds'])) {
return $this->sliceByKey($maxObjects, ['SelectionCriteria', 'AdGroupIds']);
}
if (isset($params['SelectionCriteria']['Ids'])) {
return $this->sliceByKey($maxObjects, ['SelectionCriteria', 'Ids']);
}
return null;
}
function handle($response)
{
try {
if (!isset($response['result']['AudienceTargets'])) {
return;
}
$ad_group_external_ids = [];
$campaign_external_ids = [];
$retargetinglist_external_ids = [];
foreach ($response['result']['AudienceTargets'] as $audience_target) {
$ad_group_external_id = (string)$audience_target['AdGroupId'];
$campaign_external_id = (string)$audience_target['CampaignId'];
$retargetinglist_external_id = (string)$audience_target['RetargetingListId'];
if (!isset($ad_group_external_ids[$ad_group_external_id])) {
$ad_group_external_ids[$ad_group_external_id] = true;
}
if (!isset($campaign_external_ids[$campaign_external_id])) {
$campaign_external_ids[$campaign_external_id] = true;
}
if (!isset($retargetinglist_external_ids[$retargetinglist_external_id])) {
$retargetinglist_external_ids[$retargetinglist_external_id] = true;
}
}
if ($this->getToken()->isMain()) {
$adGroups = AdGroup::whereIn('external_id', array_keys($ad_group_external_ids))
->get()
->keyBy('external_id');
$campaigns = Campaigns::whereIn('external_id', array_keys($campaign_external_ids))
->get()
->keyBy('external_id');
$retargetinglists = Retargetinglist::whereIn('external_id', array_keys($retargetinglist_external_ids))
->get()
->keyBy('external_id');
}
foreach ($response['result']['AudienceTargets'] as $audience_target) {
$external_id = (string)$audience_target['Id'];
$ad_group_external_id = (string)$audience_target['AdGroupId'];
$campaign_external_id = (string)$audience_target['CampaignId'];
$retargetinglist_external_id = (string)$audience_target['RetargetingListId'];
if ($this->getToken()->isMain()) {
/* @var $adGroup AdGroup|null */
$adGroup = $adGroups->get($ad_group_external_id);
if (!$adGroup) {
continue;
}
/* @var $campaign Campaigns|null */
$campaign = $campaigns->get($campaign_external_id);
if (!$campaign) {
continue;
}
if ($retargetinglist_external_id) {
/* @var $retargetinglist Retargetinglist|null */
$retargetinglist = $retargetinglists->get($retargetinglist_external_id);
if (!$retargetinglist) {
continue;
}
}
$interest_external_id = (string)$audience_target['InterestId'];
$data = [
'external_id' => $external_id,
'ad_group_external_id' => $adGroup->external_id,
'ad_group_id' => $adGroup->getKey(),
'campaign_external_id' => $campaign->external_id,
'campaign_id' => $campaign->getKey(),
'retargetinglist_external_id' => $retargetinglist_external_id ? $retargetinglist->external_id : null,
'retargetinglist_id' => $retargetinglist_external_id ? $retargetinglist->getKey() : null,
'interest_external_id' => $interest_external_id ? $interest_external_id : null,
'context_bid' => (string)$audience_target['ContextBid'],
'strategy_priority' => $audience_target['StrategyPriority'],
];
$audienceTarget = AudienceTarget::updateOrCreate([
'external_id' => $external_id
], $data);
if (!$audienceTarget->wasRecentlyCreated && $audienceTarget->wasChanged($audienceTarget::getPropertiesWatch()->toArray())) {
$audienceTarget->goalAudienceTargets()->forExternal()->update([
'updated_need' => Carbon::now(),
]);
}
} else {
//
}
}
} catch (\Exception $e) {
Log::debug($e);
throw $e;
}
}
private function requestPrepare($filter)
{
$this->setService('AudienceTargets');
$this->setMethod('get');
$params = [
"FieldNames" => [
"Id", "AdGroupId", "CampaignId",
"RetargetingListId", "InterestId",
"ContextBid", "StrategyPriority",
],
'SelectionCriteria' => [
'States' => [
"ON", "SUSPENDED",
],
]
];
if (isset($filter['Ids'])) {
$this->max_count = $this->max_count_Ids;
$params['SelectionCriteria']['Ids'] = $filter['Ids'];
}
if (isset($filter['AdGroupIds'])) {
$this->max_count = $this->max_count_AdGroupIds;
$params['SelectionCriteria']['AdGroupIds'] = $filter['AdGroupIds'];
}
if (isset($filter['RetargetingListIds'])) {
$this->max_count = $this->max_count_RetargetingListIds;
$params['SelectionCriteria']['RetargetingListIds'] = $filter['RetargetingListIds'];
}
if (isset($filter['CampaignIds'])) {
$this->max_count = $this->max_count_CampaignIds;
$params['SelectionCriteria']['CampaignIds'] = $filter['CampaignIds'];
}
$this->setParams($params);
}
}
......@@ -61,12 +61,12 @@ class GetNegativeKeywordSharedSets extends DirectRequest
'associated' => $negative_keyword_shared_sets['Associated'] === 'YES',
];
$adExtension = NegativeKeywordSharedSet::updateOrCreate([
$negativeKeywordSharedSet = NegativeKeywordSharedSet::updateOrCreate([
'external_id' => $external_id
], $data);
if ($adExtension->wasChanged($adExtension::getPropertiesWatch()->toArray())) {
$adExtension->goalNegativeKeywordSharedSets()->forExternal()->update([
if (!$negativeKeywordSharedSet->wasRecentlyCreated && $negativeKeywordSharedSet->wasChanged($negativeKeywordSharedSet::getPropertiesWatch()->toArray())) {
$negativeKeywordSharedSet->goalNegativeKeywordSharedSets()->forExternal()->update([
'updated_need' => Carbon::now(),
]);
}
......
......@@ -59,12 +59,12 @@ class GetRetargetinglists extends DirectRequest
'rules' => $retargeting_list['Rules'],
];
$adExtension = Retargetinglist::updateOrCreate([
$retargetinglist = Retargetinglist::updateOrCreate([
'external_id' => $external_id
], $data);
if ($adExtension->wasChanged($adExtension::getPropertiesWatch()->toArray())) {
$adExtension->goalRetargetinglists()->forExternal()->update([
if (!$retargetinglist->wasRecentlyCreated && $retargetinglist->wasChanged($retargetinglist::getPropertiesWatch()->toArray())) {
$retargetinglist->goalRetargetinglists()->forExternal()->update([
'updated_need' => Carbon::now(),
]);
}
......
......@@ -63,7 +63,7 @@ class GetSitelinks extends DirectRequest
'external_id' => $external_id
], $data);
if ($sitelink->wasChanged($sitelink::getPropertiesWatch()->toArray())) {
if (!$sitelink->wasRecentlyCreated && $sitelink->wasChanged($sitelink::getPropertiesWatch()->toArray())) {
$sitelink->goalSitelinks()->forExternal()->update([
'updated_need' => Carbon::now(),
]);
......
......@@ -89,7 +89,7 @@ class GetVCards extends DirectRequest
'external_id' => $external_id
], $data);
if ($adExtension->wasChanged($adExtension::getPropertiesWatch()->toArray())) {
if (!$adExtension->wasRecentlyCreated && $adExtension->wasChanged($adExtension::getPropertiesWatch()->toArray())) {
$adExtension->goalVCards()->forExternal()->update([
'updated_need' => Carbon::now(),
]);
......
......@@ -50,6 +50,7 @@ class UpdateAdGroups extends DirectRequest
$goalAdGroup->goalKeywords()->delete();
$goalAdGroup->goalAdvertisements()->delete();
$goalAdGroup->goalBidModifiers()->delete();
$goalAdGroup->goalAudienceTargets()->delete();
$goalAdGroup->forceDelete();
} else {
......
<?php
namespace App\Service\Requests\Direct;
use App\Jobs\ProcessCallLimitedAPI;
use App\Models\Pivots\GoalRetargetinglist;
use App\Models\Variable;
use App\Service\Contract\APIRequest;
use App\Service\Requests\DirectRequest;
use App\Service\StrReplaceByVariables;
use Illuminate\Support\Facades\Log;
class UpdateRetargetinglists extends DirectRequest
{
protected $max_count = 1000;
protected $timestamp;
public function call($params = null)
{
$this->requestPrepare($params);
$process = new ProcessCallLimitedAPI($this);
dispatch($process)->onQueue('limits');
}
public function getObjectsCount()
{
return count($this->getParams()['RetargetingLists']);
}
public function slice($maxObjects): ?APIRequest
{
$splinter = $this->sliceByKey($maxObjects, 'RetargetingLists');
return $splinter;
}
public function handle($response)
{
try {
if (!isset($response['result']['UpdateResults'])) {
return;
}
foreach ($response['result']['UpdateResults'] as $key => $update_result) {
if (!isset($update_result['Id'])) {
Log::debug("AddRetargetinglist, empty Id");
Log::debug($update_result);
Log::debug($this->getParams()['RetargetingLists'][$key]);
GoalRetargetinglist::where('external_id', $this->getParams()['RetargetingLists'][$key]['Id'])
->update([
'reserve_update_at' => null,
]);
continue;
}
$external_id = (string)$update_result['Id'];
GoalRetargetinglist::where('external_id', $external_id)
->update([
'updated_need' => null,
'reserve_update_at' => null,
]);
}
} catch (\Exception $e) {
Log::debug($e);
throw $e;
}
}
public function failed()
{
GoalRetargetinglist::whereIn('external_id', array_column($this->getParams()['RetargetingLists'], 'Id'))
->update([
'reserve_update_at' => null,
]);
}
private function requestPrepare($params)
{
$this->setService('RetargetingLists');
$this->setMethod('update');
$variables = Variable::all();
$lists = [];
$this->setParams([
'RetargetingLists' => $params['goalRetargetinglists']->map(function (GoalRetargetinglist $goalRetargetinglist) use ($variables, &$lists) {
if (!isset($lists[$goalRetargetinglist->dictionary_campaign_id])) {
$list = Variable::getListVariablesByDictionaryCampaign($goalRetargetinglist->dictionary_campaign_id, $variables);
$lists[$goalRetargetinglist->dictionary_campaign_id] = $list;
} else {
$list = $lists[$goalRetargetinglist->dictionary_campaign_id];
}
return [
'Id' => $goalRetargetinglist->external_id,
'Name' => StrReplaceByVariables::getInstance($goalRetargetinglist->retargetinglist->name, $list)->get(),
'Description' => StrReplaceByVariables::getInstance($goalRetargetinglist->retargetinglist->description, $list)->get(),
'Rules' => $goalRetargetinglist->retargetinglist->rules,
];
})->toArray(),
]);
}
}
<?php
use App\Models\AudienceTarget;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateAudienceTargetsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('audience_targets', function (Blueprint $table) {
$table->id();
$table->bigInteger('external_id')->nullable();
$table->bigInteger('ad_group_external_id')->unsigned();
$table->bigInteger('campaign_external_id')->unsigned();
$table->bigInteger('retargetinglist_external_id')->unsigned()->nullable();
$table->bigInteger('ad_group_id')->unsigned();
$table->bigInteger('campaign_id')->unsigned();
$table->bigInteger('retargetinglist_id')->unsigned()->nullable();
$table->bigInteger('interest_external_id')->unsigned()->nullable();
$table->bigInteger('context_bid');
$table->enum('strategy_priority', [
AudienceTarget::STRATEGY_PRIORITY_LOW,
AudienceTarget::STRATEGY_PRIORITY_NORMAL,
AudienceTarget::STRATEGY_PRIORITY_HIGH,
]);
$table->foreign('ad_group_id')->references('id')->on('ad_groups')
->cascadeOnDelete();
$table->foreign('campaign_id')->references('id')->on('campaigns')
->cascadeOnDelete();
$table->foreign('retargetinglist_id')->references('id')->on('retargetinglists')
->cascadeOnDelete();
$table->softDeletes();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('audience_targets');
}
}
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateGoalAudienceTargetsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('goal_audience_targets', function (Blueprint $table) {
$table->id();
$table->bigInteger('external_id')->nullable();
$table->bigInteger('goal_ad_group_external_id')->unsigned()->nullable();
$table->bigInteger('dictionary_campaign_external_id')->unsigned()->nullable();
$table->bigInteger('goal_retargetinglist_external_id')->unsigned()->nullable();
$table->bigInteger('audience_target_id')->unsigned();
$table->bigInteger('goal_ad_group_id')->unsigned();
$table->bigInteger('dictionary_campaign_id')->unsigned();
$table->bigInteger('goal_retargetinglist_id')->unsigned()->nullable();
$table->foreign('audience_target_id')->references('id')->on('audience_targets')
->cascadeOnDelete();
$table->foreign('goal_ad_group_id')->references('id')->on('goal_ad_groups')
->cascadeOnDelete();
$table->foreign('dictionary_campaign_id')->references('id')->on('dictionary_campaigns')
->cascadeOnDelete();
$table->foreign('goal_retargetinglist_id')->references('id')->on('goal_retargetinglists')
->cascadeOnDelete();
$table->timestamp('external_upload_at')->nullable();
$table->timestamp('external_updated_at')->nullable();
$table->timestamp('updated_need')->nullable();
$table->timestamp('reserve_create_at')->nullable();
$table->timestamp('reserve_update_at')->nullable();
$table->softDeletes();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('goal_audience_targets');
}
}
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddGoalRetargetinglistsReserveUpdateAtColumn extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('goal_retargetinglists', function (Blueprint $table) {
$table->timestamp('reserve_update_at')->nullable();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!