Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
Письменов Дмитрий Иванович
/
yourroomads
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Settings
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 230aee20
authored
Jun 21, 2021
by
Евгений
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Улучшение #19474
Синхронизация фраз
1 parent
44836271
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
80 additions
and
58 deletions
app/Console/Commands/AdGroupsLoadUpdated.php
app/Console/Commands/CampaignsCheckUpdatedChildrenAdGroups.php
app/Jobs/ProcessCallLimitedAPI.php
app/Service/Handlers/AdsHandler.php
app/Service/Handlers/DirectHandler.php
app/Service/Requests/Direct/GetKeywords.php
app/Console/Commands/AdGroupsLoadUpdated.php
View file @
230aee2
...
@@ -62,9 +62,9 @@ class AdGroupsLoadUpdated extends Command
...
@@ -62,9 +62,9 @@ class AdGroupsLoadUpdated extends Command
$factory
=
APIRequest
::
getInstance
(
API
::
YANDEX
);
$factory
=
APIRequest
::
getInstance
(
API
::
YANDEX
);
$factory
->
setToken
(
$token
);
$factory
->
setToken
(
$token
);
$factory
->
getRequest
(
'AdGroups'
,
'get'
)
->
call
([
//
$factory->getRequest('AdGroups', 'get')->call([
'Ids'
=>
$ids
,
//
'Ids' => $ids,
]);
//
]);
foreach
(
array_chunk
(
$ids
,
1000
)
as
$ids_limit
)
{
foreach
(
array_chunk
(
$ids
,
1000
)
as
$ids_limit
)
{
$factory
=
APIRequest
::
getInstance
(
API
::
YANDEX
);
$factory
=
APIRequest
::
getInstance
(
API
::
YANDEX
);
...
...
app/Console/Commands/CampaignsCheckUpdatedChildrenAdGroups.php
View file @
230aee2
...
@@ -42,7 +42,6 @@ class CampaignsCheckUpdatedChildrenAdGroups extends Command
...
@@ -42,7 +42,6 @@ class CampaignsCheckUpdatedChildrenAdGroups extends Command
public
function
handle
()
public
function
handle
()
{
{
if
(
Campaigns
::
forUpdatedChildren
()
->
exists
())
{
if
(
Campaigns
::
forUpdatedChildren
()
->
exists
())
{
$token
=
Tokens
::
where
(
'type'
,
Tokens
::
MAIN
)
->
first
();
$token
=
Tokens
::
where
(
'type'
,
Tokens
::
MAIN
)
->
first
();
if
(
!
$token
)
{
if
(
!
$token
)
{
throw
new
\Exception
(
'Не найден токен блин'
);
throw
new
\Exception
(
'Не найден токен блин'
);
...
@@ -58,7 +57,6 @@ class CampaignsCheckUpdatedChildrenAdGroups extends Command
...
@@ -58,7 +57,6 @@ class CampaignsCheckUpdatedChildrenAdGroups extends Command
],
],
'Timestamp'
=>
$token
->
check_changes_ad_group_at
->
toIso8601ZuluString
(),
'Timestamp'
=>
$token
->
check_changes_ad_group_at
->
toIso8601ZuluString
(),
]);
]);
}
}
...
...
app/Jobs/ProcessCallLimitedAPI.php
View file @
230aee2
...
@@ -10,6 +10,7 @@ use Illuminate\Contracts\Queue\ShouldQueue;
...
@@ -10,6 +10,7 @@ use Illuminate\Contracts\Queue\ShouldQueue;
use
Illuminate\Foundation\Bus\Dispatchable
;
use
Illuminate\Foundation\Bus\Dispatchable
;
use
Illuminate\Queue\InteractsWithQueue
;
use
Illuminate\Queue\InteractsWithQueue
;
use
Illuminate\Queue\SerializesModels
;
use
Illuminate\Queue\SerializesModels
;
use
Illuminate\Support\Facades\Log
;
class
ProcessCallLimitedAPI
implements
ShouldQueue
//, ShouldBeUnique
class
ProcessCallLimitedAPI
implements
ShouldQueue
//, ShouldBeUnique
{
{
...
@@ -42,6 +43,7 @@ class ProcessCallLimitedAPI implements ShouldQueue//, ShouldBeUnique
...
@@ -42,6 +43,7 @@ class ProcessCallLimitedAPI implements ShouldQueue//, ShouldBeUnique
$objects
=
$limits
->
countObjectsLimit
(
$this
->
api
);
$objects
=
$limits
->
countObjectsLimit
(
$this
->
api
);
if
(
!
$objects
)
{
if
(
!
$objects
)
{
//нет свободных баллов, замораживаем до следующего часа
//нет свободных баллов, замораживаем до следующего часа
Log
::
debug
(
'Нет баллов? '
.
$objects
);
$this
->
reRunHour
();
$this
->
reRunHour
();
return
;
return
;
}
}
...
@@ -51,6 +53,7 @@ class ProcessCallLimitedAPI implements ShouldQueue//, ShouldBeUnique
...
@@ -51,6 +53,7 @@ class ProcessCallLimitedAPI implements ShouldQueue//, ShouldBeUnique
$limitId
=
$limits
->
doRezerv
(
$this
->
api
,
$objects
);
$limitId
=
$limits
->
doRezerv
(
$this
->
api
,
$objects
);
}
catch
(
\Exception
$e
){
}
catch
(
\Exception
$e
){
//нет свободных баллов, замораживаем до следующего часа
//нет свободных баллов, замораживаем до следующего часа
Log
::
debug
(
$e
);
$this
->
reRunHour
();
$this
->
reRunHour
();
return
;
return
;
}
}
...
...
app/Service/Handlers/AdsHandler.php
View file @
230aee2
...
@@ -4,6 +4,7 @@ namespace App\Service\Handlers;
...
@@ -4,6 +4,7 @@ namespace App\Service\Handlers;
use
App\Jobs\ProcessCallLimitedAPI
;
use
App\Jobs\ProcessCallLimitedAPI
;
use
App\Service\Contract\API
;
use
App\Service\Contract\API
;
use
App\Service\Contract\APIRequest
;
use
App\Service\Contract\APIRequest
;
use
Illuminate\Support\Facades\Log
;
class
AdsHandler
class
AdsHandler
{
{
...
@@ -28,17 +29,16 @@ class AdsHandler
...
@@ -28,17 +29,16 @@ class AdsHandler
{
{
$this
->
parse
(
$response
);
$this
->
parse
(
$response
);
$offset
=
$this
->
limited
(
$response
);
$offset
=
$this
->
limited
();
if
(
$offset
)
{
if
(
$offset
)
{
$this
->
request
->
next
(
$offset
);
$this
->
request
->
next
(
$offset
);
dispatch
(
new
ProcessCallLimitedAPI
(
$this
->
request
));
dispatch
(
new
ProcessCallLimitedAPI
(
$this
->
request
))
->
onQueue
(
'limits'
);
;
}
}
$this
->
request
->
handle
(
$this
->
response
);
$this
->
request
->
handle
(
$this
->
response
);
}
}
protected
function
limited
(
$response
)
protected
function
limited
()
{
{
return
false
;
return
false
;
}
}
...
...
app/Service/Handlers/DirectHandler.php
View file @
230aee2
...
@@ -3,9 +3,9 @@ namespace App\Service\Handlers;
...
@@ -3,9 +3,9 @@ namespace App\Service\Handlers;
class
DirectHandler
extends
AdsHandler
class
DirectHandler
extends
AdsHandler
{
{
public
function
limited
(
$response
)
public
function
limited
()
{
{
return
$response
[
'result'
][
'LimitedBy'
]
??
false
;
return
$
this
->
response
[
'result'
][
'LimitedBy'
]
??
false
;
}
}
protected
function
parse
(
$response
)
protected
function
parse
(
$response
)
...
...
app/Service/Requests/Direct/GetKeywords.php
View file @
230aee2
...
@@ -11,6 +11,7 @@ use App\Models\Pivots\GoalKeyword;
...
@@ -11,6 +11,7 @@ use App\Models\Pivots\GoalKeyword;
use
App\Service\Contract\APIRequest
;
use
App\Service\Contract\APIRequest
;
use
App\Service\Requests\DirectRequest
;
use
App\Service\Requests\DirectRequest
;
use
Carbon\Carbon
;
use
Carbon\Carbon
;
use
Illuminate\Support\Facades\DB
;
use
Illuminate\Support\Facades\Log
;
use
Illuminate\Support\Facades\Log
;
class
GetKeywords
extends
DirectRequest
class
GetKeywords
extends
DirectRequest
...
@@ -30,16 +31,16 @@ class GetKeywords extends DirectRequest
...
@@ -30,16 +31,16 @@ class GetKeywords extends DirectRequest
public
function
getObjectsCount
()
public
function
getObjectsCount
()
{
{
$params
=
$this
->
getParams
();
$params
=
$this
->
getParams
();
if
(
isset
(
$params
[
'SelectionCriteria'
][
'CampaignIds'
]))
{
//
if (isset($params['SelectionCriteria']['CampaignIds'])) {
return
-
1
;
//
return -1;
}
//
}
if
(
isset
(
$params
[
'SelectionCriteria'
][
'AdGroupIds'
]))
{
//
if (isset($params['SelectionCriteria']['AdGroupIds'])) {
return
-
1
;
//
return -1;
}
//
}
if
(
isset
(
$params
[
'SelectionCriteria'
][
'Ids'
]))
{
if
(
isset
(
$params
[
'SelectionCriteria'
][
'Ids'
]))
{
return
count
(
$params
[
'SelectionCriteria'
][
'Ids'
]);
return
count
(
$params
[
'SelectionCriteria'
][
'Ids'
]);
}
}
return
-
1
;
return
$this
->
getMaxCount
()
;
}
}
public
function
slice
(
$maxObjects
)
:
?
APIRequest
public
function
slice
(
$maxObjects
)
:
?
APIRequest
...
@@ -116,6 +117,7 @@ class GetKeywords extends DirectRequest
...
@@ -116,6 +117,7 @@ class GetKeywords extends DirectRequest
'state'
=>
$keyword
[
'State'
],
'state'
=>
$keyword
[
'State'
],
'status'
=>
$keyword
[
'Status'
],
'status'
=>
$keyword
[
'Status'
],
'serving_status'
=>
$keyword
[
'ServingStatus'
],
'serving_status'
=>
$keyword
[
'ServingStatus'
],
'deleted_at'
=>
null
//не забыть убрать признак удаления, если вдруг опять пришла удаленная ранее фраза
];
];
$keyword
=
Keyword
::
updateOrCreate
([
$keyword
=
Keyword
::
updateOrCreate
([
...
@@ -156,45 +158,64 @@ class GetKeywords extends DirectRequest
...
@@ -156,45 +158,64 @@ class GetKeywords extends DirectRequest
}
}
if
(
$this
->
getToken
()
->
isMain
())
{
// if ($this->getToken()->isMain()) {
//
if
(
isset
(
$this
->
getParams
()[
'SelectionCriteria'
][
'AdGroupIds'
]))
{
// if (isset($this->getParams()['SelectionCriteria']['AdGroupIds'])) {
AdGroup
::
whereIn
(
'external_id'
,
$this
->
getParams
()[
'SelectionCriteria'
][
'AdGroupIds'
])
// AdGroup::whereIn('external_id', $this->getParams()['SelectionCriteria']['AdGroupIds'])
->
update
([
// ->update([
'keywords_loaded_at'
=>
Carbon
::
now
(),
// 'keywords_loaded_at' => Carbon::now(),
]);
// ]);
}
// }
//
if
(
count
(
$campaign_ids_synced_need
))
{
// if (count($campaign_ids_synced_need)) {
Campaigns
::
findMany
(
array_keys
(
$campaign_ids_synced_need
))
->
each
(
function
(
Campaigns
$campaign
)
{
// Campaigns::findMany(array_keys($campaign_ids_synced_need))->each(function (Campaigns $campaign) {
$campaign
->
dictionaryCampaigns
()
->
update
([
// $campaign->dictionaryCampaigns()->update([
'synced_need'
=>
Carbon
::
now
(),
// 'synced_need' => Carbon::now(),
]);
// ]);
});
// });
// }
//
// $keywordQuery = Keyword::query();
//
// if (isset($this->getParams()['SelectionCriteria']['AdGroupIds'])) {
// $keywordQuery->whereIn('ad_group_external_id', $this->getParams()['SelectionCriteria']['AdGroupIds']);
// } else {
// $keywordQuery->whereIn('ad_group_id', $ad_groups->pluck('id'));
// }
//
// } else {
// $keywordQuery = GoalKeyword::query()
// ->whereIn('goal_ad_group_id', $ad_groups->pluck('id'));
// }
//
// if (count($ids)) {
// $keywordQuery->whereNotIn('id', $ids);
// }
//
// $keywordQuery->get()->each(function ($goalKeyword) {
// /* @var $goalKeyword GoalKeyword|Keyword */
// $goalKeyword->delete();
// });
//удаление будет толко когда получаем изменения по группам, иначе это либо загрузка все фраз либо как то избранных
if
(
!
empty
(
$this
->
getParams
()[
'SelectionCriteria'
][
'AdGroupIds'
])
&&
!
isset
(
$response
[
'result'
][
'LimitedBy'
])
){
$ag_groups
=
$this
->
getParams
()[
'SelectionCriteria'
][
'AdGroupIds'
];
//последний блок с данными, можно удалять те, что так и не получили
//Выбираем все те фразы, которые обновлены позже чем группа.
//это означает что этой фразы не было в результатах и в БД она по этой причине не обновилась
//надо такие пометить на удаление
//при удалении для всех таких надо будет удалить фразы из целевых и потом удалить их сами
$sql
=
"UPDATE keywords k
INNER JOIN ad_groups ag ON k.ad_group_id=ag.id
SET k.deleted_at = now(),
ag.keywords_loaded_at=now()
WHERE k.updated_at>ag.keywords_loaded_at
AND k.deleted_at is null
AND ag.id in ("
.
implode
(
", "
,
$ag_groups
)
.
")"
;
DB
::
update
(
$sql
);
}
}
$keywordQuery
=
Keyword
::
query
();
if
(
isset
(
$this
->
getParams
()[
'SelectionCriteria'
][
'AdGroupIds'
]))
{
$keywordQuery
->
whereIn
(
'ad_group_external_id'
,
$this
->
getParams
()[
'SelectionCriteria'
][
'AdGroupIds'
]);
}
else
{
$keywordQuery
->
whereIn
(
'ad_group_id'
,
$ad_groups
->
pluck
(
'id'
));
}
}
else
{
$keywordQuery
=
GoalKeyword
::
query
()
->
whereIn
(
'goal_ad_group_id'
,
$ad_groups
->
pluck
(
'id'
));
}
if
(
count
(
$ids
))
{
$keywordQuery
->
whereNotIn
(
'id'
,
$ids
);
}
$keywordQuery
->
get
()
->
each
(
function
(
$goalKeyword
)
{
/* @var $goalKeyword GoalKeyword|Keyword */
$goalKeyword
->
delete
();
});
}
catch
(
\Exception
$e
)
{
}
catch
(
\Exception
$e
)
{
Log
::
debug
(
$e
);
Log
::
debug
(
$e
);
throw
$e
;
throw
$e
;
...
@@ -214,7 +235,7 @@ class GetKeywords extends DirectRequest
...
@@ -214,7 +235,7 @@ class GetKeywords extends DirectRequest
];
];
if
(
isset
(
$filter
[
'CampaignIds'
]))
{
if
(
isset
(
$filter
[
'CampaignIds'
]))
{
$this
->
max_count
=
$this
->
max_count_CampaignIds
;
//
$this->max_count = $this->max_count_CampaignIds;
$params
[
'SelectionCriteria'
]
=
[
$params
[
'SelectionCriteria'
]
=
[
'CampaignIds'
=>
$filter
[
'CampaignIds'
],
'CampaignIds'
=>
$filter
[
'CampaignIds'
],
...
@@ -222,7 +243,7 @@ class GetKeywords extends DirectRequest
...
@@ -222,7 +243,7 @@ class GetKeywords extends DirectRequest
}
}
if
(
isset
(
$filter
[
'AdGroupIds'
]))
{
if
(
isset
(
$filter
[
'AdGroupIds'
]))
{
$this
->
max_count
=
$this
->
max_count_AdGroupIds
;
//
$this->max_count = $this->max_count_AdGroupIds;
$params
[
'SelectionCriteria'
]
=
[
$params
[
'SelectionCriteria'
]
=
[
'AdGroupIds'
=>
$filter
[
'AdGroupIds'
],
'AdGroupIds'
=>
$filter
[
'AdGroupIds'
],
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment