Improve inactive subscribers task
[MAILPOET-3774]
This commit is contained in:
@@ -25,22 +25,12 @@ class InactiveSubscribersController {
|
|||||||
$this->settingsRepository = $settingsRepository;
|
$this->settingsRepository = $settingsRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function markInactiveSubscribers(int $daysToInactive, int $batchSize, ?int $startId = null) {
|
||||||
* @param int $daysToInactive
|
|
||||||
* @param int $batchSize
|
|
||||||
* @return int|bool
|
|
||||||
*/
|
|
||||||
public function markInactiveSubscribers($daysToInactive, $batchSize, $startId = null) {
|
|
||||||
$thresholdDate = $this->getThresholdDate($daysToInactive);
|
$thresholdDate = $this->getThresholdDate($daysToInactive);
|
||||||
return $this->deactivateSubscribers($thresholdDate, $batchSize, $startId);
|
return $this->deactivateSubscribers($thresholdDate, $batchSize, $startId);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function markActiveSubscribers(int $daysToInactive, int $batchSize): int {
|
||||||
* @param int $daysToInactive
|
|
||||||
* @param int $batchSize
|
|
||||||
* @return int
|
|
||||||
*/
|
|
||||||
public function markActiveSubscribers($daysToInactive, $batchSize) {
|
|
||||||
$thresholdDate = $this->getThresholdDate($daysToInactive);
|
$thresholdDate = $this->getThresholdDate($daysToInactive);
|
||||||
return $this->activateSubscribers($thresholdDate, $batchSize);
|
return $this->activateSubscribers($thresholdDate, $batchSize);
|
||||||
}
|
}
|
||||||
@@ -56,21 +46,15 @@ class InactiveSubscribersController {
|
|||||||
ORM::rawExecute($reactivateAllInactiveQuery);
|
ORM::rawExecute($reactivateAllInactiveQuery);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private function getThresholdDate(int $daysToInactive): Carbon {
|
||||||
* @param int $daysToInactive
|
|
||||||
* @return Carbon
|
|
||||||
*/
|
|
||||||
private function getThresholdDate($daysToInactive) {
|
|
||||||
$now = new Carbon();
|
$now = new Carbon();
|
||||||
return $now->subDays($daysToInactive);
|
return $now->subDays($daysToInactive);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Carbon $thresholdDate
|
|
||||||
* @param int $batchSize
|
|
||||||
* @return int|bool
|
* @return int|bool
|
||||||
*/
|
*/
|
||||||
private function deactivateSubscribers(Carbon $thresholdDate, $batchSize, $startId = null) {
|
private function deactivateSubscribers(Carbon $thresholdDate, int $batchSize, ?int $startId = null) {
|
||||||
$subscribersTable = Subscriber::$_table;
|
$subscribersTable = Subscriber::$_table;
|
||||||
$scheduledTasksTable = ScheduledTask::$_table;
|
$scheduledTasksTable = ScheduledTask::$_table;
|
||||||
$scheduledTaskSubcribresTable = ScheduledTaskSubscriber::$_table;
|
$scheduledTaskSubcribresTable = ScheduledTaskSubscriber::$_table;
|
||||||
@@ -96,11 +80,6 @@ class InactiveSubscribersController {
|
|||||||
(INDEX task_id_ids (id))
|
(INDEX task_id_ids (id))
|
||||||
SELECT DISTINCT task_id as id FROM $sendingQueuesTable as sq
|
SELECT DISTINCT task_id as id FROM $sendingQueuesTable as sq
|
||||||
JOIN $scheduledTasksTable as st ON sq.task_id = st.id
|
JOIN $scheduledTasksTable as st ON sq.task_id = st.id
|
||||||
JOIN (
|
|
||||||
SELECT so.newsletter_id
|
|
||||||
FROM $statisticsOpensTable as so
|
|
||||||
GROUP BY so.newsletter_id
|
|
||||||
) sno ON sno.newsletter_id = sq.newsletter_id
|
|
||||||
WHERE st.processed_at > '%s'
|
WHERE st.processed_at > '%s'
|
||||||
AND st.processed_at < '%s'",
|
AND st.processed_at < '%s'",
|
||||||
$thresholdDateIso, $dayAgoIso
|
$thresholdDateIso, $dayAgoIso
|
||||||
@@ -124,9 +103,13 @@ class InactiveSubscribersController {
|
|||||||
);
|
);
|
||||||
|
|
||||||
$idsToDeactivate = ORM::forTable($inactiveSubscriberIdsTmpTable)->rawQuery("
|
$idsToDeactivate = ORM::forTable($inactiveSubscriberIdsTmpTable)->rawQuery("
|
||||||
SELECT s.id FROM $inactiveSubscriberIdsTmpTable s
|
SELECT isi.id FROM $inactiveSubscriberIdsTmpTable isi
|
||||||
LEFT OUTER JOIN $statisticsOpensTable as so ON s.id = so.subscriber_id AND so.created_at > ?
|
LEFT OUTER JOIN $subscribersTable as s ON isi.id = s.id AND GREATEST(
|
||||||
WHERE so.id IS NULL",
|
COALESCE(s.last_engagement_at, 0),
|
||||||
|
COALESCE(s.last_subscribed_at, 0),
|
||||||
|
COALESCE(s.created_at, 0)
|
||||||
|
) > ?
|
||||||
|
WHERE s.id IS NULL",
|
||||||
[$thresholdDateIso]
|
[$thresholdDateIso]
|
||||||
)->findArray();
|
)->findArray();
|
||||||
|
|
||||||
@@ -149,12 +132,7 @@ class InactiveSubscribersController {
|
|||||||
return count($idsToDeactivate);
|
return count($idsToDeactivate);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private function activateSubscribers(Carbon $thresholdDate, int $batchSize): int {
|
||||||
* @param Carbon $thresholdDate
|
|
||||||
* @param int $batchSize
|
|
||||||
* @return int
|
|
||||||
*/
|
|
||||||
private function activateSubscribers(Carbon $thresholdDate, $batchSize) {
|
|
||||||
$subscribersTable = Subscriber::$_table;
|
$subscribersTable = Subscriber::$_table;
|
||||||
$statsOpensTable = StatisticsOpens::$_table;
|
$statsOpensTable = StatisticsOpens::$_table;
|
||||||
|
|
||||||
@@ -168,10 +146,14 @@ class InactiveSubscribersController {
|
|||||||
->findArray();
|
->findArray();
|
||||||
} else {
|
} else {
|
||||||
$idsToActivate = ORM::forTable($subscribersTable)->select("$subscribersTable.id")
|
$idsToActivate = ORM::forTable($subscribersTable)->select("$subscribersTable.id")
|
||||||
->leftOuterJoin($statsOpensTable, "$subscribersTable.id = $statsOpensTable.subscriber_id AND $statsOpensTable.created_at > '$thresholdDate'")
|
->leftOuterJoin($subscribersTable, "$subscribersTable.id = s2.id AND GREATEST(
|
||||||
|
COALESCE(s2.last_engagement_at, 0),
|
||||||
|
COALESCE(s2.last_subscribed_at, 0),
|
||||||
|
COALESCE(s2.created_at, 0)
|
||||||
|
) > '$thresholdDate'", 's2')
|
||||||
->whereLt("$subscribersTable.last_subscribed_at", $thresholdDate)
|
->whereLt("$subscribersTable.last_subscribed_at", $thresholdDate)
|
||||||
->where("$subscribersTable.status", Subscriber::STATUS_INACTIVE)
|
->where("$subscribersTable.status", Subscriber::STATUS_INACTIVE)
|
||||||
->whereRaw("$statsOpensTable.id IS NOT NULL")
|
->whereRaw("s2.id IS NOT NULL")
|
||||||
->limit($batchSize)
|
->limit($batchSize)
|
||||||
->groupByExpr("$subscribersTable.id")
|
->groupByExpr("$subscribersTable.id")
|
||||||
->findArray();
|
->findArray();
|
||||||
|
Reference in New Issue
Block a user