Skip to content

Commit

Permalink
Clean up page_hits table where date_hit field is lower than --day…
Browse files Browse the repository at this point in the history
…s-old.
  • Loading branch information
biozshock committed Jan 24, 2024
1 parent 9f31c91 commit a70f987
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ on:
push:
branches:
- master
- '[0-9]+\-[0-9]+\-x'
- 'MTC\-[0-9]+'
pull_request:

jobs:
Expand Down
9 changes: 9 additions & 0 deletions Service/EventLogCleanup.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class EventLogCleanup
public const EMAIL_STATS = 'email_stats';
public const EMAIL_STATS_TOKENS = 'email_stats_tokens';
private const EMAIL_STATS_DEVICES = 'email_stats_devices';
public const PAGE_HITS = 'page_hits';

/**
* @var array<string, string>
Expand All @@ -37,6 +38,7 @@ class EventLogCleanup
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 WHERE '.self::PREFIX.'email_stats_devices.date_opened < DATE_SUB(NOW(),INTERVAL :daysOld DAY)',
self::PAGE_HITS => self::PREFIX.'page_hits WHERE date_hit < DATE_SUB(NOW(),INTERVAL :daysOld DAY)',
];

private array $update = [
Expand All @@ -59,6 +61,9 @@ class EventLogCleanup
self::EMAIL_STATS_DEVICES => [
'daysOld' => self::DEFAULT_DAYS,
],
self::PAGE_HITS => [
'daysOld' => self::DEFAULT_DAYS,
],
];

private array $types = [
Expand All @@ -77,6 +82,9 @@ class EventLogCleanup
self::EMAIL_STATS_DEVICES => [
'daysOld' => \PDO::PARAM_INT,
],
self::PAGE_HITS => [
'daysOld' => \PDO::PARAM_INT,
],
];

private string $dryRunMessage = ' This is a dry run.';
Expand Down Expand Up @@ -125,6 +133,7 @@ public function deleteEventLogEntries(int $daysOld, ?int $campaignId, bool $dryR
self::EMAIL_STATS => 0,
self::EMAIL_STATS_TOKENS => 0,
self::EMAIL_STATS_DEVICES => 0,
self::PAGE_HITS => 0,
];

$this->connection->beginTransaction();
Expand Down
20 changes: 16 additions & 4 deletions Tests/Service/EventLogCleanupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public function runProvider(): Generator
EventLogCleanup::CAMPAIGN_LEAD_EVENTS => true,
EventLogCleanup::EMAIL_STATS => true,
EventLogCleanup::EMAIL_STATS_TOKENS => true,
EventLogCleanup::PAGE_HITS => true,
],
[
[
Expand All @@ -83,6 +84,11 @@ public function runProvider(): Generator
['daysOld' => $daysOld],
['daysOld' => \PDO::PARAM_INT],
],
[
'SELECT COUNT(1) as cnt FROM prefix_table_page_hits WHERE date_hit < DATE_SUB(NOW(),INTERVAL :daysOld DAY)',
['daysOld' => $daysOld],
['daysOld' => \PDO::PARAM_INT],
],
[
'SELECT COUNT(1) as cnt FROM prefix_table_email_stats_devices WHERE prefix_table_email_stats_devices.date_opened < DATE_SUB(NOW(),INTERVAL :daysOld DAY)',
['daysOld' => $daysOld],
Expand All @@ -94,8 +100,8 @@ public function runProvider(): Generator
['daysOld' => \PDO::PARAM_INT],
],
],
[3, 14, 23, 55, 57],
'3 lead_event_log, 14 campaign_lead_event_log, 55 email_stats_devices and 57 email_stats rows would have been deleted. 23 email_stats_tokens will be set to NULL. This is a dry run.',
[3, 14, 23, 4, 55, 57],
'3 lead_event_log, 14 campaign_lead_event_log, 4 page_hits, 55 email_stats_devices and 57 email_stats rows would have been deleted. 23 email_stats_tokens will be set to NULL. This is a dry run.',
true,
null,
];
Expand Down Expand Up @@ -216,6 +222,7 @@ public function runProvider(): Generator
EventLogCleanup::CAMPAIGN_LEAD_EVENTS => true,
EventLogCleanup::EMAIL_STATS => true,
EventLogCleanup::EMAIL_STATS_TOKENS => true,
EventLogCleanup::PAGE_HITS => true,
],
[
[
Expand All @@ -233,6 +240,11 @@ public function runProvider(): Generator
['daysOld' => $daysOld],
['daysOld' => \PDO::PARAM_INT],
],
[
'DELETE prefix_table_page_hits FROM prefix_table_page_hits WHERE date_hit < DATE_SUB(NOW(),INTERVAL :daysOld DAY)',
['daysOld' => $daysOld],
['daysOld' => \PDO::PARAM_INT],
],
[
'DELETE prefix_table_email_stats_devices FROM prefix_table_email_stats_devices WHERE prefix_table_email_stats_devices.date_opened < DATE_SUB(NOW(),INTERVAL :daysOld DAY)',
['daysOld' => $daysOld],
Expand All @@ -244,8 +256,8 @@ public function runProvider(): Generator
['daysOld' => \PDO::PARAM_INT],
],
],
[3, 14, 32, 55, 41],
'3 lead_event_log, 14 campaign_lead_event_log, 55 email_stats_devices and 41 email_stats rows have been deleted. 32 email_stats_tokens have been set to NULL.',
[3, 14, 32, 21, 55, 41],
'3 lead_event_log, 14 campaign_lead_event_log, 21 page_hits, 55 email_stats_devices and 41 email_stats rows have been deleted. 32 email_stats_tokens have been set to NULL.',
false,
null,
];
Expand Down

0 comments on commit a70f987

Please sign in to comment.