Commit 8d8bb212 by Vladislav

#19470 Реализация запроса изменения внутри РК.

1 parent 276b0655
......@@ -58,7 +58,7 @@ class CampaignsLoadGroups extends Command
$factory->getRequest('adgroups', 'get')
->call([
'CampaignIds' => $campaigns->pluck('external_id'),
'CampaignIds' => $campaigns->pluck('external_id')->all(),
]);
return 0;
......
......@@ -47,7 +47,7 @@ class CampaignsLoadUpdated extends Command
*/
public function handle()
{
$campaigns = Campaigns::forUpdatedSelf()->get();
$campaigns = Campaigns::forUpdatedSelf()->limit(10)->get();
if (!$campaigns->count()) {
return;
}
......@@ -66,9 +66,7 @@ class CampaignsLoadUpdated extends Command
}
$factory->getRequest('campaigns', 'get')->call([
'ids' => $campaigns->map(function (Campaigns $campaign) {
return $campaign->external_id;
})
'ids' => $campaigns->pluck('external_id')->all(),
]);
$tokens = Tokens::has('dictionaryCampaignsEnabledForExternalSynchronizedUpdatedSelf')
......@@ -84,10 +82,9 @@ class CampaignsLoadUpdated extends Command
$factory = APIRequest::getInstance(API::YANDEX);
$factory->setToken($token);
$factory->getRequest('change', 'checkCampaigns')->call([
'ids' => $token->dictionaryCampaignsEnabledForExternalSynchronizedUpdatedSelf->map(function (DictionaryCampaign $dictionaryCampaign) {
return $dictionaryCampaign->external_id;
})
$factory->getRequest('campaigns', 'get')->call([
'ids' => $token->dictionaryCampaignsEnabledForExternalSynchronizedUpdatedSelf
->pluck('external_id')->all(),
]);
}
......
<?php
namespace App\Console\Commands;
use App\Jobs\ProcessCallLimitedAPI;
use App\Models\Campaigns;
use App\Models\Pivots\DictionaryCampaign;
use App\Models\Tokens;
use App\Service\API\API;
use App\Service\Direct\CheckDictionaries;
use App\Service\Direct\GetCampaigns;
use App\Service\Requests\APIRequest;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Support\Facades\Bus;
class CampaignsLoadUpdatedChildrenAdGroups extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'campaigns:loadUpdatedChildrenAdGroups';
/**
* 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
*/
public function handle()
{
$campaigns = Campaigns::forUpdatedChildren()->limit(3000)->get();
if (!$campaigns->count()) {
return;
}
$token = Tokens::where('type', Tokens::MAIN)->first();
if (!$token) {
throw new \Exception('Не найден токен блин');
}
$factory = APIRequest::getInstance(API::YANDEX);
$factory->setToken($token);
$factory->getRequest('changes', 'check')->call([
'CampaignIds' => $campaigns->pluck('external_id')->all(),
'FieldNames' => [
'AdGroupIds',
],
"Timestamp" => $token->check_changes_ad_group_at->toIso8601ZuluString(),
]);
$tokens = Tokens::has('dictionaryCampaignsEnabledForExternalSynchronizedUpdatedChildren')
->with([
'dictionaryCampaignsEnabledForExternalSynchronizedUpdatedChildren' => function (HasManyThrough $query) {
return $query->limit(3000);
},
])
->where('type', '!=', Tokens::MAIN)
->get();
foreach ($tokens as $token) {
$factory = APIRequest::getInstance(API::YANDEX);
$factory->setToken($token);
$factory->getRequest('changes', 'check')->call([
'CampaignIds' => $token->dictionaryCampaignsEnabledForExternalSynchronizedUpdatedChildren
->pluck('external_id')->all(),
'FieldNames' => [
'AdGroupIds',
],
"Timestamp" => $token->check_changes_at->toIso8601ZuluString(),
]);
}
return 0;
}
}
......@@ -57,7 +57,7 @@ class CampaignsResume extends Command
$factory->getRequest('campaigns', 'resume')
->call([
'ids' => $token_main->campaignsEnabledDisabled->pluck('external_id'),
'ids' => $token_main->campaignsEnabledDisabled->pluck('external_id')->all(),
]);
}
*/
......@@ -76,7 +76,7 @@ class CampaignsResume extends Command
$factory->getRequest('campaigns', 'resume')
->call([
'ids' => $token->dictionaryCampaignsEnabledForExternalDisabled->pluck('external_id'),
'ids' => $token->dictionaryCampaignsEnabledForExternalDisabled->pluck('external_id')->all(),
]);
}
......
......@@ -56,7 +56,7 @@ class CampaignsSuspend extends Command
$factory->getRequest('campaigns', 'suspend')
->call([
'ids' => $token_main->campaignsNotEnabledNotDisabled->pluck('external_id'),
'ids' => $token_main->campaignsNotEnabledNotDisabled->pluck('external_id')->all(),
]);
}
*/
......@@ -75,7 +75,7 @@ class CampaignsSuspend extends Command
$factory->getRequest('campaigns', 'suspend')
->call([
'ids' => $token->dictionaryCampaignsNotEnabledForExternalNotDisabled->pluck('external_id'),
'ids' => $token->dictionaryCampaignsNotEnabledForExternalNotDisabled->pluck('external_id')->all(),
]);
}
......
......@@ -2,6 +2,7 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
/**
......@@ -21,8 +22,10 @@ use Illuminate\Database\Eloquent\Model;
* @property array|null $region_ids
* @property array|null $negative_keywords
* @property array|null $negative_keyword_shared_set_ids
* @property \Illuminate\Support\Carbon|null $updated_self
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @method static \Illuminate\Database\Eloquent\Builder|AdGroup forUpdatedSelf()
* @method static \Illuminate\Database\Eloquent\Builder|AdGroup newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|AdGroup newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|AdGroup query()
......@@ -84,6 +87,7 @@ class AdGroup extends Model
'negative_keywords',
'negative_keyword_shared_set_ids',
'tracking_params',
'updated_self',
];
protected $casts = [
......@@ -94,5 +98,12 @@ class AdGroup extends Model
'region_ids' => 'array',
'negative_keywords' => 'json',
'negative_keyword_shared_set_ids' => 'json',
'updated_self' => 'datetime',
];
public function scopeForUpdatedSelf(Builder $query)
{
$query->whereNotNull('updated_self');
}
}
......@@ -3,6 +3,7 @@
namespace App\Models;
use App\Models\Pivots\DictionaryCampaign;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
/**
......@@ -15,6 +16,8 @@ use Illuminate\Database\Eloquent\Model;
* @property string|null $type
* @property int $created_by
* @property string|null $timestamp
* @property \Illuminate\Support\Carbon|null $check_changes_ad_group
* @property \Illuminate\Support\Carbon|null $check_changes_ad_group_at
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property int $limit
......@@ -22,6 +25,7 @@ use Illuminate\Database\Eloquent\Model;
* @property-read int|null $campaigns_count
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Campaigns[] $campaignsNotEnabledNotDisabled
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Campaigns[] $campaignsEnabledDisabled
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Campaigns[] $campaignsEnabledNotDisabledUpdatedChildren
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Dictionary[] $cities
* @property-read int|null $cities_count
* @property-read \Illuminate\Database\Eloquent\Collection|DictionaryCampaign[] $dictionaryCampaigns
......@@ -32,6 +36,7 @@ use Illuminate\Database\Eloquent\Model;
* @property-read \Illuminate\Database\Eloquent\Collection|DictionaryCampaign[] $dictionaryCampaignsEnabledForExternalDisabled
* @property-read \Illuminate\Database\Eloquent\Collection|DictionaryCampaign[] $dictionaryCampaignsEnabledForExternalNeedUpdated
* @property-read \Illuminate\Database\Eloquent\Collection|DictionaryCampaign[] $dictionaryCampaignsEnabledForExternalSynchronizedUpdatedSelf
* @property-read \Illuminate\Database\Eloquent\Collection|DictionaryCampaign[] $dictionaryCampaignsEnabledForExternalSynchronizedUpdatedChildren
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Dictionary[] $campaignsForManaged
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Dictionary[] $campaignsNotForManaged
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Limits[] $limits
......@@ -62,8 +67,20 @@ class Tokens extends Model
'login',
'type',
'timestamp',
'check_changes_ad_group',
];
protected $casts = [
'check_changes_ad_group' => 'datetime',
];
public function getCheckChangesAdGroupAtAttribute()
{
return $this->check_changes_ad_group
? $this->check_changes_ad_group
: Carbon::now()->addDays(-1);
}
public function scopeFilter($query, array $filters)
{
$query->when($filters['login'] ?? null, function ($query, $search) {
......@@ -102,6 +119,11 @@ class Tokens extends Model
return $this->campaigns()->forEnabled()->disabled();
}
public function campaignsEnabledNotDisabledUpdatedChildren()
{
return $this->campaigns()->forEnabled()->notDisabled()->forUpdatedChildren();
}
public function cities()
{
return $this->hasMany(Dictionary::class, 'token_id', 'id')
......@@ -145,6 +167,11 @@ class Tokens extends Model
return $this->dictionaryCampaignsForExternal()->enabled()->synchronized()->forUpdatedSelf();
}
public function dictionaryCampaignsEnabledForExternalSynchronizedUpdatedChildren()
{
return $this->dictionaryCampaignsForExternal()->enabled()->synchronized()->forUpdatedChildren();
}
public function campaignsForManaged()
{
return $this->campaigns()->forManaged();
......
<?php
namespace App\Service\Requests\Direct;
use App\Jobs\ProcessCallLimitedAPI;
use App\Models\AdGroup;
use App\Models\Campaigns;
use App\Models\Pivots\DictionaryCampaign;
use App\Service\API\API;
use App\Service\Requests\APIRequest;
use App\Service\Requests\DirectRequest;
use Carbon\Carbon;
use Illuminate\Support\Facades\Log;
class CheckChanges extends DirectRequest
{
public function call($params = null)
{
$this->requestPrepare($params);
$process = new ProcessCallLimitedAPI($this);
dispatch($process)->onQueue('limits');
}
public function putParams($params)
{
$this->dictionaryCampaigns = $params['dictionaryCampaigns'];
$this->variables = $params['variables'];
}
public function handle($response)
{
if (isset($response['result']['NotFound'])) {
Log::debug($response['result']['NotFound']);
}
if (isset($response['result']['Unprocessed'])) {
Log::debug($response['result']['Unprocessed']);
}
if (isset($response['result']['Modified']['AdGroupIds'])) {
foreach ($response['result']['Modified']['AdGroupIds'] as $ad_group_id) {
$adGroup = AdGroup::where('external_id', $ad_group_id)->first();
if ($adGroup) {
$adGroup->update([
'updated_self' => Carbon::now(),
]);
}
}
}
if (isset($this->getParams()['CampaignIds'])) {
if ($this->getToken()->isMain()) {
Campaigns::whereIn('external_id', $this->getParams()['CampaignIds'])->update([
'updated_children' => null,
]);
} else {
DictionaryCampaign::whereIn('external_id', $this->getParams()['CampaignIds'])->update([
'updated_children' => null,
]);
}
}
$this->getToken()->update([
'check_changes' => [
Carbon::createFromTimestamp($response['result']['Timestamp']),
]
]);
$this->getToken()->update([
'timestamp' => $response['result']['Timestamp'],
]);
$this->setToken($this->getToken()->refresh());
}
private function requestPrepare($params)
{
$this->setService('changes');
$this->setMethod('check');
$this->setParams($params);
}
}
<?php
use Faker\Generator as Faker;
$factory->define(App\Models\AdGroup::class, function (Faker $faker) {
return [
'name' => $faker->name,
'external_id' => $faker->unique()->randomNumber()
];
});
......@@ -21,8 +21,10 @@ class CreateTokensTable extends Migration
$table->enum('type', ['main', 'goal'])->nullable();
$table->integer('created_by');
$table->string('timestamp')->nullable();
$table->unique('token');
$table->timestamp('check_changes_ad_group')->nullable();
$table->timestamps();
$table->unique('token');
});
}
......
......@@ -51,6 +51,7 @@ class CreateAdGroupsTable extends Migration
$table->json('region_ids')->nullable();
$table->json('negative_keywords')->nullable();
$table->json('negative_keyword_shared_set_ids')->nullable();
$table->timestamp('updated_self')->nullable();
$table->timestamps();
......
<?php
namespace Tests\Unit;
use App\Jobs\ProcessCallLimitedAPI;
use App\Models\Account;
use App\Models\AdGroup;
use App\Models\Campaigns;
use App\Models\Dictionary;
use App\Models\Tokens;
use App\Models\User;
use App\Service\Contract\API;
use App\Service\Requests\APIRequest;
use Carbon\Carbon;
use Illuminate\Support\Facades\Queue;
use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
class CheckChangesAdGroupsTest extends TestCase
{
use RefreshDatabase;
private $request;
private $user;
private $token_main;
private $token;
private $dictionary;
private $request_main;
private $request_main_params;
protected function setUp(): void
{
parent::setUp();
$account = Account::create(['name' => 'Acme Corporation']);
$this->user = factory(User::class)->create([
'account_id' => $account->id,
'first_name' => 'John',
'last_name' => 'Doe',
'email' => 'johndoe@example.com',
'owner' => true,
]);
$this->token_main = factory(Tokens::class)->create([
'type' => Tokens::MAIN,
'created_by' => $this->user->id
]);
factory(Campaigns::class)->create([
'updated_children' => Carbon::now(),
'token' => $this->token_main->getKey(),
]);
$campaign = Campaigns::first();
factory(AdGroup::class)->create([
'campaign_external_id' => $campaign->external_id,
'campaign_id' => $campaign->getKey(),
]);
$this->token = factory(Tokens::class)->create([
'created_by' => $this->user->id
]);
$this->dictionary = factory(Dictionary::class)->create([
'token_id' => $this->token,
'type' => Dictionary::CITY,
]);
$this->request_main = APIRequest::getInstance(API::YANDEX)
->setToken($this->token_main)
->getRequest('changes', 'check');
$this->request_main_params = [
'CampaignIds' => Campaigns::forUpdatedChildren()->pluck('external_id')->all(),
'FieldNames' => [
'AdGroupIds',
],
"Timestamp" => $this->token_main->check_changes_ad_group_at->toIso8601ZuluString(),
];
$this->request = APIRequest::getInstance(API::YANDEX)
->setToken($this->token)
->getRequest('changes', 'check');
}
public function testCallApi()
{
Queue::fake();
Queue::assertNothingPushed();
$this->request_main->call($this->request_main_params);
Queue::assertPushed(ProcessCallLimitedAPI::class);
}
public function testHandleApi()
{
$this->assertEquals(1, AdGroup::count());
$this->assertEquals(0, AdGroup::forUpdatedSelf()->count());
$this->assertEquals(1, Campaigns::forUpdatedChildren()->count());
$this->request_main->setParams($this->request_main_params);
$this->request_main->handle([
'result' => [
'Modified' => [
'AdGroupIds' => [
AdGroup::first()->external_id,
],
],
'Timestamp' => '2015-02-02T12:23:11Z',
]
]);
$this->assertEquals(1, AdGroup::forUpdatedSelf()->count());
$this->assertEquals(0, Campaigns::forUpdatedChildren()->count());
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!