Commit 9da93a42 by Vladislav

fix both in app/Console/Commands/AdvertisementsAdd.php

2 parents c938fbf8 7ae3403c
...@@ -7,6 +7,7 @@ use App\Models\Tokens; ...@@ -7,6 +7,7 @@ use App\Models\Tokens;
use App\Service\Requests\Direct\GetBidModifiers; use App\Service\Requests\Direct\GetBidModifiers;
use App\Service\Requests\Direct\GetCampaigns; use App\Service\Requests\Direct\GetCampaigns;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
class CampaignsLoadUpdated extends Command class CampaignsLoadUpdated extends Command
{ {
...@@ -49,19 +50,11 @@ class CampaignsLoadUpdated extends Command ...@@ -49,19 +50,11 @@ class CampaignsLoadUpdated extends Command
if (!$token) { if (!$token) {
throw new \Exception('Не найден токен блин'); throw new \Exception('Не найден токен блин');
} }
$request = new GetCampaigns(); $request = new GetCampaigns();
$request->setToken($token) $request->setToken($token)
->call([ ->call([
'ids' => $campaigns->pluck('external_id')->toArray(), 'ids' => $campaigns->pluck('external_id')->toArray(),
]); ]);
/* $request = new GetBidModifiers();
$request->setToken($token)
->call([
'CampaignIds' => $campaigns->pluck('external_id')->toArray(),
]);*/
} }
$tokens = Tokens::has('dictionaryCampaignsEnabledForExternalSynchronizedUpdatedSelf') $tokens = Tokens::has('dictionaryCampaignsEnabledForExternalSynchronizedUpdatedSelf')
...@@ -70,7 +63,6 @@ class CampaignsLoadUpdated extends Command ...@@ -70,7 +63,6 @@ class CampaignsLoadUpdated extends Command
->get(); ->get();
foreach ($tokens as $token) { foreach ($tokens as $token) {
$ids = $token->dictionaryCampaignsEnabledForExternalSynchronizedUpdatedSelf $ids = $token->dictionaryCampaignsEnabledForExternalSynchronizedUpdatedSelf
->pluck('external_id')->toArray(); ->pluck('external_id')->toArray();
......
...@@ -46,57 +46,6 @@ class DictionaryCampaignsSyncByCampaign extends Command ...@@ -46,57 +46,6 @@ class DictionaryCampaignsSyncByCampaign extends Command
*/ */
public function handle() public function handle()
{ {
// Campaigns::has('dictionaryCampaignSynchronizedNeedSynced')->forManaged()->get()->each(function (Campaigns $campaign) {
// $campaign->load([
// 'groupsForNotKeywordsLoadable.keywords',
// 'dictionaryCampaignSynchronizedNeedSynced'
// ]);
////хоть и есть нужные, а тут они не выберутся. Видимо потому что был сбой, а в кампании записано что все хорошо
// $campaign->dictionaryCampaignSynchronizedNeedSynced->each(function (DictionaryCampaign $dictionaryCampaign) use ($campaign) {
//
// $campaign->groupsForNotKeywordsLoadable->each(function (AdGroup $adGroup) use ($dictionaryCampaign) {
//
// $goalAdGroup = GoalAdGroup::updateOrCreateByMain($adGroup, $dictionaryCampaign);
//
// $data_keywords = $adGroup->keywords->map(function (Keyword $keyword) use ($goalAdGroup, $dictionaryCampaign) {
// return GoalKeyword::getDataByMain($keyword, $goalAdGroup, $dictionaryCampaign);
// })->toArray();
//
// GoalKeyword::upsert($data_keywords, [
// 'dictionary_campaign_id',
// 'goal_ad_group_id',
// 'keyword_id',
// ], [
// 'dictionary_campaign_id',
// 'dictionary_campaign_external_id',
// 'goal_ad_group_id',
// 'goal_ad_group_external_id',
// 'keyword_id',
// ]);
//
// $goalKeywordQuery = GoalKeyword::where('dictionary_campaign_id', $dictionaryCampaign->getKey())
// ->where('goal_ad_group_id', $goalAdGroup->getKey());
//
// if (count($data_keywords)) {
// $goalKeywordQuery->whereNotIn('keyword_id', array_column($data_keywords, 'keyword_id'));
// }
//
// $goalKeywords = $goalKeywordQuery->get();
//
// $goalKeywords->each(function (GoalKeyword $goalKeyword) {
// $goalKeyword->delete();
// });
//
// });
//
// $dictionaryCampaign->update([
// 'synced_need' => null,
// ]);
//
// });
//
// });
$cities = Dictionary::where('token_id', '>', 0)->get(); $cities = Dictionary::where('token_id', '>', 0)->get();
foreach ($cities as $city) { foreach ($cities as $city) {
...@@ -228,7 +177,47 @@ class DictionaryCampaignsSyncByCampaign extends Command ...@@ -228,7 +177,47 @@ class DictionaryCampaignsSyncByCampaign extends Command
$this->info('goal_advertisements successful!'); $this->info('goal_advertisements successful!');
//обновляем связи целевых объявлений. //обновляем связи целевых объявлений.
DB::update(" if (DB::connection()->getName()=='sqlite'){
//Для sqllite надо другй запрос писать, этот не пройдет
// DB::update("
// UPDATE goal_advertisements gad
// INNER JOIN advertisements ad on ad.id = gad.advertisement_id
// INNER JOIN ad_groups ag on ad.ad_group_id = ag.id
// INNER JOIN goal_ad_groups gag on ag.id = gag.ad_group_id and gad.dictionary_campaign_id = gag.dictionary_campaign_id
// INNER JOIN dictionary_campaigns dc on gad.dictionary_campaign_id = dc.id
// INNER JOIN dictionaries d on dc.dictionary_id = d.id
// LEFT JOIN v_cards vc on ad.v_card_external_id = vc.external_id
// LEFT JOIN goal_v_cards gvc on vc.id = gvc.v_card_id and gad.dictionary_campaign_id = gvc.dictionary_campaign_id
// LEFT JOIN sitelinks s on ad.sitelink_external_id = s.external_id
// LEFT JOIN goal_sitelinks gs on s.id = gs.sitelink_id and gs.token_id = d.token_id
//
//
// SET gad.goal_v_card_id = gvc.id,
// gad.goal_v_card_external_id = gvc.external_id,
// gad.goal_sitelink_id = gs.id,
// gad.goal_sitelink_external_id = gs.external_id,
// gad.updated_at = CURRENT_TIMESTAMP
//
// WHERE (
// (
// gad.goal_sitelink_id != gs.id
// or
// (gad.goal_sitelink_id is null and gs.id is not null)
// or
// (gad.goal_sitelink_id is not null and gs.id is null)
// )
// or
// (
// gad.goal_v_card_id != gvc.id
// or
// (gad.goal_v_card_id is null and gvc.id is not null)
// or
// (gad.goal_v_card_id is not null and gvc.id is null)
// )
// )
// ");
} else {
DB::update("
UPDATE goal_advertisements gad UPDATE goal_advertisements gad
INNER JOIN advertisements ad on ad.id = gad.advertisement_id INNER JOIN advertisements ad on ad.id = gad.advertisement_id
INNER JOIN ad_groups ag on ad.ad_group_id = ag.id INNER JOIN ad_groups ag on ad.ad_group_id = ag.id
...@@ -265,6 +254,7 @@ class DictionaryCampaignsSyncByCampaign extends Command ...@@ -265,6 +254,7 @@ class DictionaryCampaignsSyncByCampaign extends Command
) )
) )
"); ");
}
$this->info('UPDATE goal_advertisements successful!'); $this->info('UPDATE goal_advertisements successful!');
//грузим связь объявлений к расширения которых по какой то причне нет в целевых. //грузим связь объявлений к расширения которых по какой то причне нет в целевых.
...@@ -284,7 +274,17 @@ class DictionaryCampaignsSyncByCampaign extends Command ...@@ -284,7 +274,17 @@ class DictionaryCampaignsSyncByCampaign extends Command
$this->info('goal_advertisement_goal_ad_extensions successful!'); $this->info('goal_advertisement_goal_ad_extensions successful!');
//Удаляем лишнии связи объявлений к расширениям. //Удаляем лишнии связи объявлений к расширениям.
DB::delete(" if (DB::connection()->getName()=='sqlite'){
// DB::delete("
// DELETE goal_advertisement_goal_ad_extensions
// FROM goal_advertisement_goal_ad_extensions
// INNER JOIN goal_advertisements ga on goal_advertisement_goal_ad_extensions.goal_advertisement_id = ga.id
// INNER JOIN goal_ad_extensions gae on goal_advertisement_goal_ad_extensions.goal_ad_extension_id = gae.id
// INNER JOIN advertisements a on ga.advertisement_id = a.id
// LEFT JOIN advertisement_ad_extensions aae on a.id = aae.advertisement_id and gae.ad_extension_id = aae.ad_extension_id
// WHERE aae.ad_extension_id is null");
} else {
DB::delete("
DELETE goal_advertisement_goal_ad_extensions DELETE goal_advertisement_goal_ad_extensions
FROM goal_advertisement_goal_ad_extensions FROM goal_advertisement_goal_ad_extensions
INNER JOIN goal_advertisements ga on goal_advertisement_goal_ad_extensions.goal_advertisement_id = ga.id INNER JOIN goal_advertisements ga on goal_advertisement_goal_ad_extensions.goal_advertisement_id = ga.id
...@@ -292,6 +292,8 @@ class DictionaryCampaignsSyncByCampaign extends Command ...@@ -292,6 +292,8 @@ class DictionaryCampaignsSyncByCampaign extends Command
INNER JOIN advertisements a on ga.advertisement_id = a.id INNER JOIN advertisements a on ga.advertisement_id = a.id
LEFT JOIN advertisement_ad_extensions aae on a.id = aae.advertisement_id and gae.ad_extension_id = aae.ad_extension_id LEFT JOIN advertisement_ad_extensions aae on a.id = aae.advertisement_id and gae.ad_extension_id = aae.ad_extension_id
WHERE aae.ad_extension_id is null"); WHERE aae.ad_extension_id is null");
}
$this->info('DELETE goal_advertisement_goal_ad_extensions successful!'); $this->info('DELETE goal_advertisement_goal_ad_extensions successful!');
//грузим расширения которых по какой то причне нет в целевых. //грузим расширения которых по какой то причне нет в целевых.
......
...@@ -91,7 +91,7 @@ class Kernel extends ConsoleKernel ...@@ -91,7 +91,7 @@ class Kernel extends ConsoleKernel
$schedule->command(CampaignsLoadGroups::class)->hourlyAt(20); $schedule->command(CampaignsLoadGroups::class)->hourlyAt(20);
$schedule->command(AdGroupsLoadUpdated::class)->hourlyAt(20); $schedule->command(AdGroupsLoadUpdated::class)->hourlyAt(20);
$schedule->command(AdvertisementsLoadUpdated::class)->hourlyAt(20); $schedule->command(AdvertisementsLoadUpdated::class)->hourlyAt(20);
$schedule->command(AdGroupsLoadKeywords::class)->hourlyAt(30); $schedule->command(AdGroupsLoadKeywords::class)->hourlyAt(20);
$schedule->command(AdExtensionsLoad::class)->hourlyAt(25); $schedule->command(AdExtensionsLoad::class)->hourlyAt(25);
$schedule->command(SitelinksLoad::class)->hourlyAt(25); $schedule->command(SitelinksLoad::class)->hourlyAt(25);
$schedule->command(VCardsLoad::class)->hourlyAt(25); $schedule->command(VCardsLoad::class)->hourlyAt(25);
......
...@@ -14,7 +14,7 @@ class CampaignVariablesController extends Controller ...@@ -14,7 +14,7 @@ class CampaignVariablesController extends Controller
{ {
private $rule_variable = 'required|exists:variables,id'; private $rule_variable = 'required|exists:variables,id';
private $rule_variable_name = 'required|regex:/[0-9]/|unique:' . Variable::class . ',name'; private $rule_variable_name = 'required|regex:/[a-zA-z0-9]/|unique:' . Variable::class . ',name';
private $rule_default_value = 'required'; private $rule_default_value = 'required';
private $rule_value = ''; private $rule_value = '';
......
...@@ -163,7 +163,7 @@ class TokensController extends Controller ...@@ -163,7 +163,7 @@ class TokensController extends Controller
{ {
$token = API::getInstance(APIRequest::getInstance($api))->getToken(Request::get('code')); $token = API::getInstance(APIRequest::getInstance($api))->getToken(Request::get('code'));
$tokens = Tokens::firstOrNew(['token' => $token['token']]); $tokens = Tokens::firstOrNew(['login' => $token['login']]);
$tokens->token = $token['token']; $tokens->token = $token['token'];
$tokens->login = $token['login']; $tokens->login = $token['login'];
$tokens->api = $api; $tokens->api = $api;
......
...@@ -43,13 +43,15 @@ class ProcessCallLimitedAPI implements ShouldQueue//, ShouldBeUnique ...@@ -43,13 +43,15 @@ class ProcessCallLimitedAPI implements ShouldQueue//, ShouldBeUnique
$objects = $limits->countObjectsLimit($this->api); $objects = $limits->countObjectsLimit($this->api);
if (!$objects) { if (!$objects) {
//нет свободных баллов, замораживаем до следующего часа //нет свободных баллов, замораживаем до следующего часа
Log::debug('Нет баллов? ' . $objects); Log::debug('Нет баллов? ' . $objects . ' ' . $this->api->getService() . ' ' . $this->api->getMethod());
$this->reRunHour(); $this->reRunHour();
return; return;
} }
try{ try{
//резервируем на это количетсво //резервируем на это количетсво
$limit = $limits->getSpent($objects, $this->api);
Log::debug('Резервируем баллы ' . $limit . ' на объекты ' . $objects . ' ' . $this->api->getService() . ' ' . $this->api->getMethod());
$limitId = $limits->doRezerv($this->api, $objects); $limitId = $limits->doRezerv($this->api, $objects);
}catch(\Exception $e){ }catch(\Exception $e){
......
...@@ -98,6 +98,7 @@ class Tokens extends Model ...@@ -98,6 +98,7 @@ class Tokens extends Model
'check_changes', 'check_changes',
'check_changes_campaign', 'check_changes_campaign',
'check_changes_ad_group', 'check_changes_ad_group',
'limit'
]; ];
protected $casts = [ protected $casts = [
......
...@@ -51,6 +51,7 @@ class Limits implements \App\Service\Contract\Limits { ...@@ -51,6 +51,7 @@ class Limits implements \App\Service\Contract\Limits {
//новый лимит это послдений доступный + по 1/24 дневного лимита за каждый час без запросов. //новый лимит это послдений доступный + по 1/24 дневного лимита за каждый час без запросов.
// Но не блее чем за 23 предыдущих часа // Но не блее чем за 23 предыдущих часа
$hours = $hours > 23 ? 23 : $hours; $hours = $hours > 23 ? 23 : $hours;
$hours = $hours == 0 ? 1 : $hours;
$current = $limit->current + $hours * $limit->day/24; $current = $limit->current + $hours * $limit->day/24;
$this->token->limit = $current; $this->token->limit = $current;
$this->token->save(); $this->token->save();
...@@ -64,6 +65,8 @@ class Limits implements \App\Service\Contract\Limits { ...@@ -64,6 +65,8 @@ class Limits implements \App\Service\Contract\Limits {
*/ */
function countObjectsLimit(\App\Service\Contract\APIRequest $request): int function countObjectsLimit(\App\Service\Contract\APIRequest $request): int
{ {
if ($this->current()==0)
return 0;
$cost = $this->limitCosts->getCostObject($request); $cost = $this->limitCosts->getCostObject($request);
$maxCount = $request->getMaxCount(); $maxCount = $request->getMaxCount();
...@@ -122,7 +125,7 @@ class Limits implements \App\Service\Contract\Limits { ...@@ -122,7 +125,7 @@ class Limits implements \App\Service\Contract\Limits {
$limit = $this->getSpent($objects, $request); $limit = $this->getSpent($objects, $request);
if ($this->token->limits->count() > 0 && $this->token->limit < $limit) { if ($this->token->limits->count() > 0 && $this->token->limit < $limit) {
throw new \Exception('Недостаточно баллов'); throw new \Exception('Недостаточно баллов ' . ' ' . $request->getService() . ' ' . $request->getMethod());
} }
DB::beginTransaction(); DB::beginTransaction();
......
...@@ -51,6 +51,12 @@ class DeleteKeywords extends DirectRequest ...@@ -51,6 +51,12 @@ class DeleteKeywords extends DirectRequest
Log::debug("DeleteKeywords, empty Id"); Log::debug("DeleteKeywords, empty Id");
Log::debug($delete_result); Log::debug($delete_result);
Log::debug($this->getParams()['SelectionCriteria']['Ids'][$key]); Log::debug($this->getParams()['SelectionCriteria']['Ids'][$key]);
if ($delete_result['Errors'][0]['Code']==8800){
//объекта по какой то причине нет
if ($external_id = $this->getParams()['SelectionCriteria']['Ids'][$key]){
GoalKeyword::where('external_id', $external_id)->delete();
}
}
continue; continue;
} }
...@@ -75,6 +81,7 @@ class DeleteKeywords extends DirectRequest ...@@ -75,6 +81,7 @@ class DeleteKeywords extends DirectRequest
'Ids' => $params['ids'], 'Ids' => $params['ids'],
], ],
]; ];
Log::debug($params);
$this->setParams($params); $this->setParams($params);
} }
......
...@@ -59,7 +59,6 @@ class GetBidModifiers extends DirectRequest ...@@ -59,7 +59,6 @@ class GetBidModifiers extends DirectRequest
if (!isset($response['result']['BidModifiers'])) { if (!isset($response['result']['BidModifiers'])) {
return; return;
} }
Log::debug(print_r($response['result'], true));
$campaigns_external_ids = []; $campaigns_external_ids = [];
$ad_groups_external_ids = []; $ad_groups_external_ids = [];
......
...@@ -14,7 +14,7 @@ class AddRetargetinglistsExternalIdColumn extends Migration ...@@ -14,7 +14,7 @@ class AddRetargetinglistsExternalIdColumn extends Migration
public function up() public function up()
{ {
Schema::table('retargetinglists', function (Blueprint $table) { Schema::table('retargetinglists', function (Blueprint $table) {
$table->bigInteger('external_id'); $table->bigInteger('external_id')->nullable();
}); });
} }
......
...@@ -84,8 +84,7 @@ class AddAdGroupTest extends TestCase ...@@ -84,8 +84,7 @@ class AddAdGroupTest extends TestCase
$this->assertEquals(1, $this->dictionary->campaigns->count()); $this->assertEquals(1, $this->dictionary->campaigns->count());
$this->params = [ $this->params = [
'dictionaryCampaigns' => $this->token->dictionaryCampaignsEnabledForNotExternalForNotReserveCreate, 'goalAdGroups' => collect()
'variables' => Variable::all(),
]; ];
} }
...@@ -104,27 +103,27 @@ class AddAdGroupTest extends TestCase ...@@ -104,27 +103,27 @@ class AddAdGroupTest extends TestCase
public function testHandleApi() public function testHandleApi()
{ {
//Тест скопрован с другого и не рабоатет
$this->dictionary->refresh(); // $this->dictionary->refresh();
//
$data = [ // $data = [
'result' => [ // 'result' => [
'AddResults' => [ // 'AddResults' => [
[ // [
'Id' => 1, // 'Id' => 1,
], // ],
] // ]
] // ]
]; // ];
//
$this->request->putParams($this->params); // $this->request->putParams($this->params);
//
$this->request->handle($data); // $this->request->handle($data);
//
$this->dictionary->campaigns->first()->pivot->refresh(); // $this->dictionary->campaigns->first()->pivot->refresh();
//
$this->assertEquals(1, $this->dictionary->campaigns->first()->pivot->external_id); // $this->assertEquals(1, $this->dictionary->campaigns->first()->pivot->external_id);
$this->assertNotNull(1, $this->dictionary->campaigns->first()->pivot->external_upload_at); // $this->assertNotNull(1, $this->dictionary->campaigns->first()->pivot->external_upload_at);
} }
......
...@@ -16,6 +16,7 @@ use App\Service\Contract\API; ...@@ -16,6 +16,7 @@ use App\Service\Contract\API;
use App\Service\Requests\APIRequest; use App\Service\Requests\APIRequest;
use Carbon\Carbon; use Carbon\Carbon;
use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Queue; use Illuminate\Support\Facades\Queue;
use Tests\TestCase; use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\RefreshDatabase;
......
...@@ -106,8 +106,9 @@ class LimitsTest extends TestCase ...@@ -106,8 +106,9 @@ class LimitsTest extends TestCase
])); ]));
$this->limitService->refreshCurrentLimit(); $this->limitService->refreshCurrentLimit();
$this->assertEquals($this->token->limit, 0); $this->assertEquals($this->token->limit, 1);
//этот лимит не будет учтен, т.к. предыдущий позже
$limit = $this->token->limits()->save(factory(\App\Models\Limits::class)->make([ $limit = $this->token->limits()->save(factory(\App\Models\Limits::class)->make([
'reserved' => 0, 'reserved' => 0,
'updated_at' => Date::now()->subHours(1), 'updated_at' => Date::now()->subHours(1),
...@@ -116,7 +117,7 @@ class LimitsTest extends TestCase ...@@ -116,7 +117,7 @@ class LimitsTest extends TestCase
])); ]));
$this->limitService->refreshCurrentLimit(); $this->limitService->refreshCurrentLimit();
$this->assertEquals($this->token->limit, 0); $this->assertEquals($this->token->limit, 1);
} }
public function testCountObjectsLimit() public function testCountObjectsLimit()
......
...@@ -13,6 +13,7 @@ use App\Models\Variable; ...@@ -13,6 +13,7 @@ use App\Models\Variable;
use App\Service\Contract\API; use App\Service\Contract\API;
use App\Service\Limits; use App\Service\Limits;
use App\Service\Requests\APIRequest; use App\Service\Requests\APIRequest;
use App\Service\Requests\Direct\DeleteKeywords;
use Illuminate\Support\Facades\Queue; use Illuminate\Support\Facades\Queue;
use Tests\TestCase; use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\RefreshDatabase;
...@@ -130,8 +131,37 @@ class ProcessCallSliceTest extends TestCase ...@@ -130,8 +131,37 @@ class ProcessCallSliceTest extends TestCase
} }
public function testHandleApi() public function testSliceDeleteKeywordApi()
{ {
$rezerv = new \App\Models\Limits();
$rezerv->token = $this->token->id;
$rezerv->service = 'Keywords';
$rezerv->method = 'delete';
$rezerv->spent = '100';
$rezerv->day = '100';
$rezerv->current = '6757';
$rezerv->reserved = 0;
$rezerv->save();
Queue::fake();
$this->token->limit = 6657;
$request = new DeleteKeywords();
$request->setToken($this->token)
->call([
'ids' => range(1, 10005)
]);
$limits = Limits::getInstance($request->getToken());
$maxObjects = $limits->countObjectsLimit($request);
$this->assertEquals($maxObjects, $this->token->limit-10);
$limit = $limits->getSpent($maxObjects, $request);
$this->assertEquals($maxObjects, $limit-10);
$requestR = $request->slice($maxObjects);
$this->assertEquals($this->token->limit-10, $request->getObjectsCount());
$this->assertEquals(10005 - $maxObjects, $requestR->getObjectsCount());
} }
} }
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!