Skip to content

Commit

Permalink
Merge pull request #14 from biozshock/MTC-4373
Browse files Browse the repository at this point in the history
[MTC-4373] Allow deletion from email_stats if email expired
  • Loading branch information
oltmanns-leuchtfeuer authored Nov 28, 2023
2 parents cea2548 + 0177352 commit 9c0a881
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 139 deletions.
20 changes: 11 additions & 9 deletions Config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
return [
'name' => 'Housekeeping by Leuchtfeuer',
'description' => 'Database Cleanup Command to delete lead_event_log table entries, campaign_lead_event_log table entries, email_stats table entries where the referenced email entry is currently not published and email_stats_devices table entries.',
'version' => '3.0.0',
'version' => '3.1.0',
'author' => 'Leuchtfeuer Digital Marketing GmbH',
'services' => [
'integrations' => [
Expand All @@ -16,7 +16,7 @@
],
'housekeepingleuchtfeuer.integration.configuration' => [
'class' => \MauticPlugin\LeuchtfeuerHousekeepingBundle\Integration\Support\ConfigSupport::class,
'tags' => [
'tags' => [
'mautic.config_integration',
],
],
Expand All @@ -30,15 +30,17 @@
],
],
],
'models' => [],
'forms' => [],
'helpers' => [],
'other' => [
'models' => [],
'forms' => [],
'helpers' => [],
'other' => [
'mautic.leuchtfeuer.service.event_log_cleanup' => [
'class' => \MauticPlugin\LeuchtfeuerHousekeepingBundle\Service\EventLogCleanup::class,
'arguments' => ['database_connection',
'%mautic.db_table_prefix%',
'mautic.housekeepingleuchtfeuer.config'],
'arguments' => [
'database_connection',
'%mautic.db_table_prefix%',
'mautic.housekeepingleuchtfeuer.config',
],
],
'mautic.housekeepingleuchtfeuer.config' => [
'class' => \MauticPlugin\LeuchtfeuerHousekeepingBundle\Integration\Config::class,
Expand Down
100 changes: 67 additions & 33 deletions Service/EventLogCleanup.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@
namespace MauticPlugin\LeuchtfeuerHousekeepingBundle\Service;

use Doctrine\DBAL\Connection;
use Symfony\Component\Console\Output\OutputInterface;
use MauticPlugin\LeuchtfeuerHousekeepingBundle\Integration\Config;
use Symfony\Component\Console\Output\OutputInterface;

class EventLogCleanup
{
private const PREFIX = '%PREFIX%';

/**
* Constant used to indicate where the query can place "SET a = :a" when query is an update.
*/
private const SET = '%SET%';

private Config $config;
private Connection $connection;
private string $dbPrefix;
Expand All @@ -26,15 +31,17 @@ class EventLogCleanup
/**
* @var array<string, string>
*/
private array $queries = [
private array $queriesTemplate = [
self::CAMPAIGN_LEAD_EVENTS => self::PREFIX.'campaign_lead_event_log WHERE ('.self::PREFIX.'campaign_lead_event_log.id NOT IN (SELECT maxId FROM (SELECT MAX(clel2.id) as maxId FROM '.self::PREFIX.'campaign_lead_event_log clel2 GROUP BY lead_id, campaign_id) as maxIds) AND '.self::PREFIX.'campaign_lead_event_log.date_triggered < DATE_SUB(NOW(),INTERVAL :daysOld DAY))',
self::LEAD_EVENTS => self::PREFIX.'lead_event_log WHERE date_added < DATE_SUB(NOW(),INTERVAL :daysOld DAY)',
self::EMAIL_STATS => self::PREFIX.'email_stats LEFT JOIN '.self::PREFIX.'emails ON '.self::PREFIX.'email_stats.email_id = '.self::PREFIX.'emails.id WHERE is_published = 0 OR '.self::PREFIX.'email_stats.email_id IS NULL AND date_sent < DATE_SUB(NOW(),INTERVAL :daysOld DAY)',
self::EMAIL_STATS_TOKENS => self::PREFIX.'email_stats SET tokens = NULL WHERE date_sent < DATE_SUB(NOW(),INTERVAL :daysOld DAY) AND tokens IS NOT NULL',
self::EMAIL_STATS => self::PREFIX.'email_stats LEFT JOIN '.self::PREFIX.'emails ON '.self::PREFIX.'email_stats.email_id = '.self::PREFIX.'emails.id WHERE ('.self::PREFIX.'emails.is_published = 0 OR '.self::PREFIX.'emails.publish_down < DATE_SUB(NOW(),INTERVAL :daysOld DAY) OR '.self::PREFIX.'email_stats.email_id IS NULL) AND '.self::PREFIX.'email_stats.date_sent < DATE_SUB(NOW(),INTERVAL :daysOld DAY)',
self::EMAIL_STATS_TOKENS => self::PREFIX.'email_stats '.self::SET.' WHERE date_sent < DATE_SUB(NOW(),INTERVAL :daysOld DAY) AND tokens IS NOT NULL',
self::EMAIL_STATS_DEVICES => self::PREFIX.'email_stats_devices LEFT JOIN '.self::PREFIX.'email_stats ON '.self::PREFIX.'email_stats.id = '.self::PREFIX.'email_stats_devices.stat_id WHERE '.self::PREFIX.'email_stats.id IS NULL OR '.self::PREFIX.'email_stats.date_sent < DATE_SUB(NOW(),INTERVAL :daysOld DAY)',
];

private string $queriesTokensDryRun = self::PREFIX.'email_stats WHERE date_sent < DATE_SUB(NOW(),INTERVAL :daysOld DAY) AND tokens IS NOT NULL';
private array $update = [
self::EMAIL_STATS_TOKENS => 'SET tokens = NULL',
];

private array $params = [
self::CAMPAIGN_LEAD_EVENTS => [
Expand Down Expand Up @@ -72,11 +79,11 @@ class EventLogCleanup
],
];

private string $dryRunMessage = ' rows would have been deleted. This is a dry run.';
private string $runMessage = ' rows have been deleted.';

private string $dryRunMessageTokens = ' will be set to NULL. This is a dry run.';
private string $runMessageTokens = ' have been set to NULL.';
private string $dryRunMessage = ' This is a dry run.';
private string $dryRunDeleteMessage = ' rows would have been deleted.';
private string $runDeleteMessage = ' rows have been deleted.';
private string $dryRunUpdateMessage = ' will be set to NULL.';
private string $runUpdateMessage = ' have been set to NULL.';

public function __construct(Connection $connection, ?string $dbPrefix, Config $config)
{
Expand All @@ -103,7 +110,7 @@ public function deleteEventLogEntries(int $daysOld, ?int $campaignId, bool $dryR
if (null !== $campaignId && $operations[self::CAMPAIGN_LEAD_EVENTS]) {
$this->params[self::CAMPAIGN_LEAD_EVENTS][':cmpId'] = $campaignId;
$this->types[self::CAMPAIGN_LEAD_EVENTS][':cmpId'] = \PDO::PARAM_INT;
$this->queries[self::CAMPAIGN_LEAD_EVENTS] .= ' AND campaign_id = :cmpId';
$this->queriesTemplate[self::CAMPAIGN_LEAD_EVENTS] .= ' AND campaign_id = :cmpId';
}

if (array_key_exists(self::EMAIL_STATS, $operations) && true === $operations[self::EMAIL_STATS]) {
Expand All @@ -128,15 +135,12 @@ public function deleteEventLogEntries(int $daysOld, ?int $campaignId, bool $dryR
continue;
}

if (true === $operations[self::EMAIL_STATS_TOKENS]) {
$sql = 'SELECT * FROM '.str_replace(self::PREFIX, $this->dbPrefix, $this->queriesTokensDryRun);
$statement = $this->connection->executeQuery($sql, $this->params[$operation], $this->types[$operation]);
$result[$operation] = $statement->rowCount();
} else {
$sql = 'SELECT * FROM '.str_replace(self::PREFIX, $this->dbPrefix, $this->queries[$operation]);
$statement = $this->connection->executeQuery($sql, $this->params[$operation], $this->types[$operation]);
$result[$operation] = $statement->rowCount();
}
$queryTemplate = $this->queriesTemplate[$operation];
$queryTemplate = str_replace(self::SET, '', $queryTemplate);

$sql = 'SELECT * FROM '.str_replace(self::PREFIX, $this->dbPrefix, $queryTemplate);
$statement = $this->connection->executeQuery($sql, $this->params[$operation], $this->types[$operation]);
$result[$operation] = $statement->rowCount();

if ($output->isVerbose()) {
$output->writeln($sql);
Expand All @@ -148,14 +152,16 @@ public function deleteEventLogEntries(int $daysOld, ?int $campaignId, bool $dryR
continue;
}

if (true === $operations[self::EMAIL_STATS_TOKENS]) {
$sql = 'UPDATE '.str_replace(self::PREFIX, $this->dbPrefix, $this->queries[$operation]);
$queryTemplate = $this->queriesTemplate[$operation];
if (array_key_exists($operation, $this->update)) {
$queryTemplate = str_replace(self::SET, $this->update[$operation], $queryTemplate);
$sql = 'UPDATE '.str_replace(self::PREFIX, $this->dbPrefix, $queryTemplate);
} else {
$sql = 'DELETE '.$this->dbPrefix.$operation.' FROM '.str_replace(self::PREFIX, $this->dbPrefix, $this->queries[$operation]);
$sql = 'DELETE '.$this->dbPrefix.$operation.' FROM '.str_replace(self::PREFIX, $this->dbPrefix, $queryTemplate);
}

$statement = $this->connection->executeQuery($sql, $this->params[$operation], $this->types[$operation]);
$result[$operation] = $statement->rowCount();
$statement = $this->connection->executeQuery($sql, $this->params[$operation], $this->types[$operation]);
$result[$operation] = $statement->rowCount();

if ($output->isVerbose()) {
$output->writeln($sql);
Expand All @@ -170,13 +176,42 @@ public function deleteEventLogEntries(int $daysOld, ?int $campaignId, bool $dryR
throw $throwable;
}

$message = '';
$lastOperation = array_key_last($operations);
$operationsResult = [
'delete' => [],
'update' => [],
];
foreach ($operations as $operation => $enabled) {
if (false === $enabled) {
continue;
}

if (array_key_exists($operation, $this->update)) {
$operationsResult['update'][$operation] = $result[$operation];
} else {
$operationsResult['delete'][$operation] = $result[$operation];
}
}

$message = $this->generateMessage($operationsResult['delete'], $dryRun ? $this->dryRunDeleteMessage : $this->runDeleteMessage);
if ('' !== $updateMessage = $this->generateMessage($operationsResult['update'], $dryRun ? $this->dryRunUpdateMessage : $this->runUpdateMessage)) {
$message .= ' '.$updateMessage;
}

if ($dryRun) {
$message .= $this->dryRunMessage;
}

return $message;
}

/**
* @param non-empty-array<string, int> $result
*/
private function generateMessage(array $result, string $postfix): string
{
$message = '';
$lastOperation = array_key_last($result);
foreach ($result as $operation => $resultCount) {
if ('' !== $message) {
if ($lastOperation === $operation) {
$message .= ' and ';
Expand All @@ -185,14 +220,13 @@ public function deleteEventLogEntries(int $daysOld, ?int $campaignId, bool $dryR
}
}

$message .= $result[$operation].' '.$operation;
$message .= $resultCount.' '.$operation;
}
if (true === $operations[self::EMAIL_STATS_TOKENS]) {
$message .= $dryRun ? $this->dryRunMessageTokens : $this->runMessageTokens;
} else {
$message .= $dryRun ? $this->dryRunMessage : $this->runMessage;

if ('' === $message) {
return '';
}

return $message;
return $message.$postfix;
}
}
Loading

0 comments on commit 9c0a881

Please sign in to comment.