Commit dceccd2b by Евгений

Улучшение #19453

Первоначальная загрузка всех РК аккаунтов.
1 parent d4934254
<?php
namespace App\Console\Commands;
use App\Jobs\ProcessCallLimitedAPI;
use App\Models\Campaigns;
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\Support\Facades\Bus;
class LoadUpdatedCampaigns extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'campaigns: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
*/
public function handle()
{
$campaigns = Campaigns::forUpdated()->get();
if (!$campaigns)
return;
$token = Tokens::where('type', Tokens::MAIN)->first();
if (!$token){
throw new \Exception('Не найден токен блин');
}
$factory = APIRequest::getInstance(API::YANDEX);
$factory->setToken($token);
$ids = [];
foreach ($campaigns as $campaign){
$ids[] = $campaign['external_id'];
}
if (!empty($ids)){
$factory->getRequest('campaigns', 'get')->call(['ids' => $ids]);
}
return 0;
}
}
......@@ -24,5 +24,11 @@ class Campaigns extends Model
'RelevantKeywordsSettingBudgetPercent',
'RelevantKeywordsSettingOptimizeGoalId',
'AttributionModel',
'updated'
];
public function scopeForUpdated($query)
{
$query->whereNotNull('updated');
}
}
......@@ -16,6 +16,6 @@ interface APIRequest{
function chunk($objects): ?APIRequest;
function call($next = null, $response = null);
function call($params = null);
function handle($response);
}
......@@ -83,7 +83,7 @@ class APIRequest implements \App\Service\Contract\APIRequest {
throw new Exception('Я не знаю формата запрсов, чтобы его разбить');
}
function call($next = null, $response = null){
function call($params = null){
}
......
<?php
namespace App\Service\Requests\Direct;
use App\Jobs\ProcessCallLimitedAPI;
use App\Models\Campaigns;
use App\Service\API\API;
use App\Service\Requests\APIRequest;
use App\Service\Requests\DirectRequest;
class CheckCampaignsChange extends DirectRequest {
CONST SELF = 'SELF';
CONST CHILDREN = 'CHILDREN';
CONST STAT = 'STAT';
protected $next;
function call($params = null){
$this->requestPrepare($params);
$process = new ProcessCallLimitedAPI($this);
dispatch($process)->onQueue('limits');
}
function handle($response){
foreach ($response['result']['Campaigns'] as $campaign){
if ($campaign['ChangesIn']==self::SELF || $campaign['ChangesIn']==self::CHILDREN){
Campaigns::firstOrCreate([
'external_id' => $campaign['CampaignId']
],
[
'updated' => $campaign['ChangesIn']
]);
}
}
$this->getToken()->timestamp = $response['result']['Timestamp'];
$this->getToken()->save();
}
private function requestPrepare($params){
$this->setService('changes');
$this->setMethod('checkDictionaries');
$this->setParams([
"Timestamp" => $this->getToken()->timestamp
]);
}
}
......@@ -7,24 +7,18 @@ use App\Service\Requests\APIRequest;
use App\Service\Requests\DirectRequest;
class CheckDictionariesChange extends DirectRequest {
protected $next;
function call($next = null, $response = null){
$this->next = $next;
$this->requestPrepare($response);
function call($params = null){
$this->requestPrepare($params);
$process = new ProcessCallLimitedAPI($this);
dispatch($process)->onQueue('limits');
}
function handle($response){
// if ($this->next){
// $this->next->call(null, $response);
// }
$this->token->timestamp = $response['result']['Timestamp'] ?? '';
$this->token->save();
}
private function requestPrepare($response){
private function requestPrepare($params){
$this->setService('changes');
$this->setMethod('checkDictionaries');
}
......
......@@ -6,12 +6,10 @@ use App\Models\Campaigns;
use App\Service\Requests\DirectRequest;
class GetCampaigns extends DirectRequest{
protected $next;
protected $timestamp;
function call($next = null, $response = null){
$this->next = $next;
$this->requestPrepare($response);
function call($params = null){
$this->requestPrepare($params);
$process = new ProcessCallLimitedAPI($this);
dispatch($process)->onQueue('limits');
}
......@@ -35,6 +33,7 @@ class GetCampaigns extends DirectRequest{
'RelevantKeywordsSettingOptimizeGoalId' => $campaign['TextCampaign']['RelevantKeywords']['OptimizeGoalId'],
'AttributionModel' => $campaign['TextCampaign']['AttributionModel'],
'PriorityGoals' => json_encode($campaign['TextCampaign']['PriorityGoals']),
'updated' => null
];
}
Campaigns::upsert($data, [
......@@ -53,13 +52,13 @@ class GetCampaigns extends DirectRequest{
'RelevantKeywordsSettingBudgetPercent',
'RelevantKeywordsSettingOptimizeGoalId',
'AttributionModel',
'updated'
]);
}
private function requestPrepare($response){
private function requestPrepare($filter){
$this->setService('campaigns');
$this->setMethod('get');
// $this->setTimestamp($response);
$params = [
'SelectionCriteria' => [
'Types' => ["TEXT_CAMPAIGN"]
......@@ -71,11 +70,9 @@ class GetCampaigns extends DirectRequest{
"BiddingStrategy", "Settings", "CounterIds", "RelevantKeywords", "PriorityGoals", "AttributionModel"
]
];
if (!empty($filter['ids'])){
$params['SelectionCriteria']['Ids'] = $filter['ids'];
}
$this->setParams($params);
}
// private function setTimestamp($response){
// $this->timestamp = $response['result']['Timestamp'] ?? '';
// }
}
......@@ -23,7 +23,7 @@ class DirectRequest extends APIRequest {
//для запросов get ничего не разбиваем, т.к. заранее не знаем сколько чего будет
//для них только после выполнения запроса будет известно есть ли кроме $count еще данные
//а текущий запрос просто устанавливается в $count
//а вот для запросов мутатрово будем разбивать. Но тут уже будет зависеть от запроса
//а вот для запросов мутаторов будем разбивать. Но тут уже будет зависеть от запроса
//будем под каждый реализовывать делитель
if (!isset($this->params['Page'])){
$this->params['Page'] = [];
......
......@@ -20,9 +20,9 @@ class CreateTokensTable extends Migration
$table->enum('api', ['yd']);
$table->enum('type', ['main', 'goal'])->nullable();
$table->integer('created_by');
$table->timestamps()->nullable();
$table->string('timestamp')->nullable();
$table->unique('token');
$table->timestamps();
});
}
......
......@@ -16,20 +16,25 @@ class CreateCampaignsTable extends Migration
Schema::create('campaigns', function (Blueprint $table) {
$table->id();
$table->bigInteger('external_id')->unique();
$table->string('Name', 255);
$table->json('TimeTargeting');
$table->text('NegativeKeywords');
$table->text('BlockedIps');
$table->text('ExcludedSites');
$table->json('DailyBudget');
$table->string('TextCampaignStrategySearch');
$table->string('TextCampaignStrategyNetwork');
$table->json('Settings');
$table->text('CounterIds');
$table->integer('RelevantKeywordsSettingBudgetPercent');
$table->bigInteger('RelevantKeywordsSettingOptimizeGoalId');
$table->string('AttributionModel', 4);
$table->json('PriorityGoals');
$table->string('Name', 255)->nullable();
$table->json('TimeTargeting')->nullable();
$table->text('NegativeKeywords')->nullable();
$table->text('BlockedIps')->nullable();
$table->text('ExcludedSites')->nullable();
$table->json('DailyBudget')->nullable();
$table->string('TextCampaignStrategySearch')->nullable();
$table->string('TextCampaignStrategyNetwork')->nullable();
$table->json('Settings')->nullable();
$table->text('CounterIds')->nullable();
$table->integer('RelevantKeywordsSettingBudgetPercent')->nullable();
$table->bigInteger('RelevantKeywordsSettingOptimizeGoalId')->nullable();
$table->string('AttributionModel', 4)->nullable();
$table->json('PriorityGoals')->nullable();
$table->enum('updated', [
\App\Service\Requests\Direct\CheckCampaignsChange::CHILDREN,
\App\Service\Requests\Direct\CheckCampaignsChange::SELF,
\App\Service\Requests\Direct\CheckCampaignsChange::STAT,
])->nullable();
$table->timestamps();
});
}
......
......@@ -52,28 +52,4 @@ class ApiRequestTest extends TestCase
$this->expectException(\TypeError::class);
$this->request->setToken( 1 );
}
public function testChunk(){
$this->markTestIncomplete();
// $this->request->setParams([
// 'Page' => [
// "Limit" => 10,
// "Offset" => 0
// ]
// ]);
// $request = $this->request->chunk(3);
// $this->assertEquals($request->getParams()['Page']['Offset'], 3);
// $this->assertEquals($this->request->getParams()['Page']['Limit'], 3);
//
// $this->request->setParams([
// 'Page' => [
// "Limit" => 10,
// "Offset" => 0
// ]
// ]);
// $request = $this->request->chunk(20);
// $this->assertNull($request);
// $this->assertEquals($this->request->getParams()['Page']['Limit'], 10);
}
}
<?php
namespace Tests\Unit;
use App\Jobs\ProcessCallLimitedAPI;
use App\Models\Account;
use App\Models\Campaigns;
use App\Models\Tokens;
use App\Models\User;
use App\Service\Contract\API;
use App\Service\Requests\APIRequest;
use Illuminate\Support\Facades\Queue;
use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
class CheckCampaignsTest extends TestCase
{
use RefreshDatabase;
private $request;
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 = factory(Tokens::class)->create([
'created_by' => $this->user->id
]);
$this->request = APIRequest::getInstance(API::YANDEX)
->setToken($this->token)
->getRequest('change', 'CheckCampaigns');
}
public function testCallApi(){
Queue::fake();
Queue::assertNothingPushed();
$this->request->call();
Queue::assertPushed(ProcessCallLimitedAPI::class);
}
public function testHandleApi(){
$this->request->handle([
'result' => [
'Timestamp' => 'test time',
'Campaigns' => [
[
'ChangesIn' => 'SELF',
'CampaignId' => 1
],
[
'ChangesIn' => 'CHILDREN',
'CampaignId' => 2
]
]
]
]);
$this->assertEquals($this->request->getToken()->timestamp, 'test time');
$this->assertEquals(2, Campaigns::forUpdated()->count());
foreach (Campaigns::forUpdated()->get() as $c){
if ($c->external_id==1){
$this->assertEquals('SELF', $c->updated);
}else{
$this->assertEquals('CHILDREN', $c->updated);
}
}
}
}
<?php
namespace Tests\Unit;
use App\Jobs\ProcessCallLimitedAPI;
use App\Models\Account;
use App\Models\Tokens;
use App\Models\User;
use App\Service\Contract\API;
use App\Service\Requests\APIRequest;
use Illuminate\Support\Facades\Queue;
use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
class CheckDictionariesTest extends TestCase
{
use RefreshDatabase;
private $request;
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 = factory(Tokens::class)->create([
'created_by' => $this->user->id
]);
$this->request = APIRequest::getInstance(API::YANDEX)
->setToken($this->token)
->getRequest('change', 'CheckDictionaries');
}
public function testCallApi(){
Queue::fake();
Queue::assertNothingPushed();
$this->request->call();
Queue::assertPushed(ProcessCallLimitedAPI::class);
}
public function testHandleApi(){
$this->request->handle([
'result' => [
'Timestamp' => 'test time'
]
]);
$this->assertEquals($this->request->getToken()->timestamp, 'test time');
}
}
<?php
namespace Tests\Unit;
use App\Jobs\ProcessCallLimitedAPI;
use App\Models\Account;
use App\Models\Campaigns;
use App\Models\Tokens;
use App\Models\User;
use App\Service\Contract\API;
use App\Service\Requests\APIRequest;
use Illuminate\Support\Facades\Queue;
use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
class GetCampaignsTest extends TestCase
{
use RefreshDatabase;
private $request;
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 = factory(Tokens::class)->create([
'created_by' => $this->user->id
]);
$this->request = APIRequest::getInstance(API::YANDEX)
->setToken($this->token)
->getRequest('campaigns', 'get');
}
public function testCallApi(){
Queue::fake();
Queue::assertNothingPushed();
$this->request->call(['ids'=>[1,2,3]]);
Queue::assertPushed(ProcessCallLimitedAPI::class);
}
public function testHandleApi(){
$this->request->handle([
'result' => [
'Campaigns' => [
[
'Id' => 1,
'Name' => 'test',
'TimeTargeting' => ['test time schedule'],
'NegativeKeywords' => ['Items' => [1,2,3]],
'BlockedIps' => ['Items' => [1,2,3]],
'ExcludedSites' => ['Items' => [1,2,3]],
'DailyBudget' => ['Items' => [1,2,3]],
'TextCampaign' => [
'BiddingStrategy' =>[
'Search' => [
'BiddingStrategyType' => 12312
],
'Network' => [
'BiddingStrategyType' => 12312
]
],
'Settings' => [123,123,12],
'CounterIds' => ['Items' => [1,2,3]],
'RelevantKeywords' => [
'BudgetPercent' => 1,
'OptimizeGoalId' => 2,
],
'AttributionModel' => 'AL',
'PriorityGoals' => [34234,2435567,23423]
],
]
]
]
]);
$this->assertEquals(1, Campaigns::all()->count());
}
}
......@@ -9,12 +9,12 @@ class HeaderLimitsTest extends TestCase
{
public function testParse(){
$limits = new HeaderLimits(['Units' => "0/0/0"]);
$limits = new HeaderLimits(['Units' => ["0/0/0"]]);
$this->assertEquals($limits->getDayLimit(), 0);
$this->assertEquals($limits->getCurrentLimit(), 0);
$this->assertEquals($limits->getSpentLimit(), 0);
$limits = new HeaderLimits(['Units' => "1/2/3"]);
$limits = new HeaderLimits(['Units' => ["1/2/3"]]);
$this->assertEquals($limits->getDayLimit(), 3);
$this->assertEquals($limits->getCurrentLimit(), 2);
$this->assertEquals($limits->getSpentLimit(), 1);
......
......@@ -192,7 +192,7 @@ class LimitsTest extends TestCase
]));
$this->limitService->acceptRezerv(
$limit->id,
new HeaderLimits(['Units' => "1/2/3"])
new HeaderLimits(['Units' => ["1/2/3"]])
);
$this->assertEquals($this->token->limit, 2);
......@@ -205,7 +205,7 @@ class LimitsTest extends TestCase
public function testUpdateLimits(){
$this->limitService->updateLimits(
new HeaderLimits(['Units' => "1/2/3"])
new HeaderLimits(['Units' => ["1/2/3"]])
);
$this->assertEquals($this->token->limit, 2);
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!