Commit 36a3b3d4 by Vladislav

#19471 Получение кеша групп для шаблона

1 parent 0fcb4492
<?php
namespace App\Console\Commands;
use App\Models\Tokens;
use App\Service\API\API;
use App\Service\Requests\APIRequest;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
class AdGroupsLoadUpdated extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'adgroups:loadUpdated';
/**
* 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()
{
$token = Tokens::has('campaignsAdGroupsForUpdatedSelf')
->with([
'campaignsAdGroupsForUpdatedSelf' => function (HasManyThrough $query) {
return $query->limit(10);
},
])->where('type', '!=', Tokens::MAIN)
->first();
if (!$token) {
throw new \Exception('Не найден токен блин');
}
$factory = APIRequest::getInstance(API::YANDEX);
$factory->setToken($token);
$factory->getRequest('adgroups', 'get')
->call([
'CampaignIds' => $token->campaignsAdGroupsForUpdatedSelf->pluck('campaign_external_id')->all(),
]);
return 0;
}
}
......@@ -101,6 +101,17 @@ class AdGroup extends Model
'updated_self' => 'datetime',
];
public static function boot()
{
parent::boot();
static::updated(function (Campaigns $campaign) {
//
});
}
public function scopeForUpdatedSelf(Builder $query)
{
$query->whereNotNull('updated_self');
......
......@@ -23,6 +23,9 @@ use Illuminate\Database\Eloquent\Model;
* @property int $limit
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Campaigns[] $campaigns
* @property-read int|null $campaigns_count
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\AdGroup[] $campaignsAdGroups
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\AdGroup[] $campaignsAdGroupsForUpdatedSelf
* @property-read int|null $campaigns_ad_groups_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
......@@ -109,6 +112,16 @@ class Tokens extends Model
->orderBy('updated_at', 'DESC');
}
public function campaignsAdGroups()
{
return $this->hasManyThrough(Campaigns::class, AdGroup::class, 'token', 'campaign_id');
}
public function campaignsAdGroupsForUpdatedSelf()
{
return $this->campaignsAdGroups()->forUpdatedSelf();
}
public function campaignsNotEnabledNotDisabled()
{
return $this->campaigns()->forEnabled(false)->notDisabled();
......
......@@ -67,6 +67,7 @@ class GetAdGroups extends DirectRequest
'negative_keywords' => $ad_group['NegativeKeywords'] ? json_encode($ad_group['NegativeKeywords']) : null,
'negative_keyword_shared_set_ids' => $ad_group['NegativeKeywordSharedSetIds'] ? json_encode($ad_group['NegativeKeywordSharedSetIds']) : null,
'tracking_params' => $ad_group['TrackingParams'],
'updated_self' => null,
];
}
......@@ -86,6 +87,7 @@ class GetAdGroups extends DirectRequest
'negative_keywords',
'negative_keyword_shared_set_ids',
'tracking_params',
'updated_self',
]);
DB::commit();
} catch (\Exception $e) {
......
......@@ -13,7 +13,7 @@ return [
|
*/
'default' => env('QUEUE_CONNECTION', 'sync'),
'default' => env('QUEUE_CONNECTION', 'redis'),
/*
|--------------------------------------------------------------------------
......@@ -59,7 +59,7 @@ return [
'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
],
'redis' => [
'c' => [
'driver' => 'redis',
'connection' => 'default',
'queue' => env('REDIS_QUEUE', 'default'),
......
......@@ -10,6 +10,7 @@ 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;
......@@ -97,6 +98,7 @@ class GetAdGroupTest extends TestCase
{
$this->assertEquals(1, Campaigns::count());
$this->assertEquals(0, AdGroup::count());
$this->request->handle($this->data);
......@@ -115,5 +117,20 @@ class GetAdGroupTest extends TestCase
$this->assertEquals($ad_group->negative_keywords, $this->data['result']['AdGroups'][0]['NegativeKeywords']);
$this->assertEquals($ad_group->negative_keyword_shared_set_ids, $this->data['result']['AdGroups'][0]['NegativeKeywordSharedSetIds']);
$this->assertEquals($ad_group->tracking_params, $this->data['result']['AdGroups'][0]['TrackingParams']);
$ad_group->update([
'updated_self' => Carbon::now(),
]);
$ad_group = AdGroup::first();
$this->assertNotNull($ad_group->updated_self);
$this->request->handle($this->data);
$ad_group = AdGroup::first();
$this->assertNull($ad_group->updated_self);
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!