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 0d2540fc
authored
Jun 21, 2021
by
Vladislav
Browse files
Options
Browse Files
Download
Plain Diff
fix both in app/Console/Commands/CampaignsCheckUpdatedChildren.php, app/Service/…
…Requests/Direct/GetKeywords.php
2 parents
3e06f003
9af3a949
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
75 additions
and
52 deletions
app/Console/Commands/CampaignsCheckUpdatedChildren.php
app/Jobs/ProcessCallLimitedAPI.php
app/Service/Handlers/AdsHandler.php
app/Service/Handlers/DirectHandler.php
app/Service/Requests/Direct/GetKeywords.php
app/Console/Commands/CampaignsCheckUpdatedChildren.php
View file @
0d2540f
...
...
@@ -42,7 +42,6 @@ class CampaignsCheckUpdatedChildren extends Command
public
function
handle
()
{
if
(
Campaigns
::
forUpdatedChildren
()
->
exists
())
{
$token
=
Tokens
::
where
(
'type'
,
Tokens
::
MAIN
)
->
first
();
if
(
!
$token
)
{
throw
new
\Exception
(
'Не найден токен блин'
);
...
...
app/Jobs/ProcessCallLimitedAPI.php
View file @
0d2540f
...
...
@@ -10,6 +10,7 @@ use Illuminate\Contracts\Queue\ShouldQueue;
use
Illuminate\Foundation\Bus\Dispatchable
;
use
Illuminate\Queue\InteractsWithQueue
;
use
Illuminate\Queue\SerializesModels
;
use
Illuminate\Support\Facades\Log
;
class
ProcessCallLimitedAPI
implements
ShouldQueue
//, ShouldBeUnique
{
...
...
@@ -42,6 +43,7 @@ class ProcessCallLimitedAPI implements ShouldQueue//, ShouldBeUnique
$objects
=
$limits
->
countObjectsLimit
(
$this
->
api
);
if
(
!
$objects
)
{
//нет свободных баллов, замораживаем до следующего часа
Log
::
debug
(
'Нет баллов? '
.
$objects
);
$this
->
reRunHour
();
return
;
}
...
...
@@ -51,6 +53,7 @@ class ProcessCallLimitedAPI implements ShouldQueue//, ShouldBeUnique
$limitId
=
$limits
->
doRezerv
(
$this
->
api
,
$objects
);
}
catch
(
\Exception
$e
){
//нет свободных баллов, замораживаем до следующего часа
Log
::
debug
(
$e
);
$this
->
reRunHour
();
return
;
}
...
...
app/Service/Handlers/AdsHandler.php
View file @
0d2540f
...
...
@@ -4,6 +4,7 @@ namespace App\Service\Handlers;
use
App\Jobs\ProcessCallLimitedAPI
;
use
App\Service\Contract\API
;
use
App\Service\Contract\APIRequest
;
use
Illuminate\Support\Facades\Log
;
class
AdsHandler
{
...
...
@@ -28,17 +29,16 @@ class AdsHandler
{
$this
->
parse
(
$response
);
$offset
=
$this
->
limited
(
$response
);
$offset
=
$this
->
limited
();
if
(
$offset
)
{
$this
->
request
->
next
(
$offset
);
dispatch
(
new
ProcessCallLimitedAPI
(
$this
->
request
));
dispatch
(
new
ProcessCallLimitedAPI
(
$this
->
request
))
->
onQueue
(
'limits'
);
;
}
$this
->
request
->
handle
(
$this
->
response
);
}
protected
function
limited
(
$response
)
protected
function
limited
()
{
return
false
;
}
...
...
app/Service/Handlers/DirectHandler.php
View file @
0d2540f
...
...
@@ -3,9 +3,9 @@ namespace App\Service\Handlers;
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
)
...
...
app/Service/Requests/Direct/GetKeywords.php
View file @
0d2540f
...
...
@@ -11,6 +11,7 @@ use App\Models\Pivots\GoalKeyword;
use
App\Service\Contract\APIRequest
;
use
App\Service\Requests\DirectRequest
;
use
Carbon\Carbon
;
use
Illuminate\Support\Facades\DB
;
use
Illuminate\Support\Facades\Log
;
class
GetKeywords
extends
DirectRequest
...
...
@@ -30,16 +31,16 @@ class GetKeywords extends DirectRequest
public
function
getObjectsCount
()
{
$params
=
$this
->
getParams
();
if
(
isset
(
$params
[
'SelectionCriteria'
][
'CampaignIds'
]))
{
return
-
1
;
}
if
(
isset
(
$params
[
'SelectionCriteria'
][
'AdGroupIds'
]))
{
return
-
1
;
}
//
if (isset($params['SelectionCriteria']['CampaignIds'])) {
//
return -1;
//
}
//
if (isset($params['SelectionCriteria']['AdGroupIds'])) {
//
return -1;
//
}
if
(
isset
(
$params
[
'SelectionCriteria'
][
'Ids'
]))
{
return
count
(
$params
[
'SelectionCriteria'
][
'Ids'
]);
}
return
-
1
;
return
$this
->
getMaxCount
()
;
}
public
function
slice
(
$maxObjects
)
:
?
APIRequest
...
...
@@ -116,6 +117,7 @@ class GetKeywords extends DirectRequest
'state'
=>
$keyword
[
'State'
],
'status'
=>
$keyword
[
'Status'
],
'serving_status'
=>
$keyword
[
'ServingStatus'
],
'deleted_at'
=>
null
//не забыть убрать признак удаления, если вдруг опять пришла удаленная ранее фраза
];
$keyword
=
Keyword
::
updateOrCreate
([
...
...
@@ -156,45 +158,64 @@ class GetKeywords extends DirectRequest
}
if
(
$this
->
getToken
()
->
isMain
())
{
if
(
isset
(
$this
->
getParams
()[
'SelectionCriteria'
][
'AdGroupIds'
]))
{
AdGroup
::
whereIn
(
'external_id'
,
$this
->
getParams
()[
'SelectionCriteria'
][
'AdGroupIds'
])
->
update
([
'keywords_loaded_at'
=>
Carbon
::
now
(),
]);
}
if
(
count
(
$campaign_ids_synced_need
))
{
Campaigns
::
findMany
(
array_keys
(
$campaign_ids_synced_need
))
->
each
(
function
(
Campaigns
$campaign
)
{
$campaign
->
dictionaryCampaigns
()
->
update
([
'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
);
// if ($this->getToken()->isMain()) {
//
// if (isset($this->getParams()['SelectionCriteria']['AdGroupIds'])) {
// AdGroup::whereIn('external_id', $this->getParams()['SelectionCriteria']['AdGroupIds'])
// ->update([
// 'keywords_loaded_at' => Carbon::now(),
// ]);
// }
//
// if (count($campaign_ids_synced_need)) {
// Campaigns::findMany(array_keys($campaign_ids_synced_need))->each(function (Campaigns $campaign) {
// $campaign->dictionaryCampaigns()->update([
// '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
->
get
()
->
each
(
function
(
$goalKeyword
)
{
/* @var $goalKeyword GoalKeyword|Keyword */
$goalKeyword
->
delete
();
});
}
catch
(
\Exception
$e
)
{
Log
::
debug
(
$e
);
throw
$e
;
...
...
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