Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created new drush command for audit trail logs cleaup #534

Merged
merged 10 commits into from
Dec 9, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Build Fix
  • Loading branch information
sharmasahil committed Dec 9, 2024
commit 7a0e01518ca9514e2d7890460e31a18348d0b156
175 changes: 85 additions & 90 deletions src/Command/AuditLogCleanupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,101 +10,96 @@
/**
* Custom AuditLog Clean Drush command.
*/
class AuditLogCleanupCommand extends DrushCommands
{
class AuditLogCleanupCommand extends DrushCommands {

/**
* The configuration service.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* The configuration service.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;

/**
* Constructs a AuditLogCleanupCommand object.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The configuration service.
*/
public function __construct(ConfigFactoryInterface $config_factory)
{
$this->configFactory = $config_factory;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container)
{
return new static(
$container->get('config.factory')
);
}
/**
* Constructs a AuditLogCleanupCommand object.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The configuration service.
*/
public function __construct(ConfigFactoryInterface $config_factory) {
$this->configFactory = $config_factory;
}

/**
* Removes log entries older than the configured retention period.
*
* @command tide_core:auditlog-cleanup
* @aliases tcl
* @description Cleans up audittrail logs older than the configured retention period.
*
* @option force-cleanup Skip confirmation and run the cleanup immediately.
*/
public function cleanupLogs($options = ['force-cleanup' => false])
{
// Check if the user passed the --force-cleanup option.
if (!$options['force-cleanup']) {
// If the force-cleanup flag isn't passed, ask for confirmation.
$confirmation = $this->confirmCleanup();
if (!$confirmation) {
$this->output()->writeln('<comment>Cleanup operation cancelled.</comment>');
return;
}
}
$config = $this->configFactory->get('tide_core.settings');
define('DEFAULT_LOG_RETENTION_DAYS', 30);
$log_retention_days = $config->get('log_retention_days') ?: DEFAULT_LOG_RETENTION_DAYS;
// Get current date and time.
$current_time = new DateTime();
$current_time->sub(new DateInterval("P{$log_retention_days}D"));
$threshold_timestamp = $current_time->getTimestamp();
// Connect to the database.
$database = Database::getConnection();
$deleted = $database->delete('admin_audit_trail')
->condition('created', $threshold_timestamp, '<')
->execute();
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('config.factory')
);
}

// Output the result.
$this->output()->writeln("Deleted $deleted log entries older than $log_retention_days days.");
// Run a database optimization command to recover space.
$this->optimizeDatabase();
/**
* Removes log entries older than the configured retention period.
*
* @command tide_core:auditlog-cleanup
sharmasahil marked this conversation as resolved.
Show resolved Hide resolved
* @aliases tcl
* @description Cleans up audittrail logs older than the configured retention period.
*
* @option force-cleanup Skip confirmation and run the cleanup immediately.
*/
public function cleanupLogs($options = ['force-cleanup' => FALSE]) {
// Check if the user passed the --force-cleanup option.
if (!$options['force-cleanup']) {
// If the force-cleanup flag isn't passed, ask for confirmation.
$confirmation = $this->confirmCleanup();
if (!$confirmation) {
$this->output()->writeln('<comment>Cleanup operation cancelled.</comment>');
return;
}
}

$config = $this->configFactory->get('tide_core.settings');
define('DEFAULT_LOG_RETENTION_DAYS', 30);
$log_retention_days = $config->get('log_retention_days') ?: DEFAULT_LOG_RETENTION_DAYS;
// Get current date and time.
$current_time = new DateTime();
$current_time->sub(new DateInterval("P{$log_retention_days}D"));
$threshold_timestamp = $current_time->getTimestamp();
// Connect to the database.
$database = Database::getConnection();
$deleted = $database->delete('admin_audit_trail')
->condition('created', $threshold_timestamp, '<')
->execute();

/**
* Ask for confirmation before proceeding with the cleanup.
*
* @return bool
* TRUE if the user confirms, FALSE if the user cancels.
*/
private function confirmCleanup()
{
$question = 'Are you sure you want to delete log entries older than the configured retention period? (y/n): ';
$confirmation = $this->io()->ask($question, 'n');
$confirmation = strtolower($confirmation);
// Return TRUE if the user answers 'y' or 'yes'.
return in_array($confirmation, ['y', 'yes']);
}
// Output the result.
$this->output()->writeln("Deleted $deleted log entries older than $log_retention_days days.");
// Run a database optimization command to recover space.
$this->optimizeDatabase();
}

/**
* Run database optimization (optional).
*
* @return void
* TRUE write the message.
*/
private function optimizeDatabase()
{
$database = Database::getConnection();
$database->query('OPTIMIZE TABLE {admin_audit_trail}');
$this->output()->writeln("Database optimized to recover space.");
}
/**
* Ask for confirmation before proceeding with the cleanup.
*
* @return bool
* TRUE if the user confirms, FALSE if the user cancels.
*/
private function confirmCleanup() {
$question = 'Are you sure you want to delete log entries older than the configured retention period? (y/n): ';
$confirmation = $this->io()->ask($question, 'n');
$confirmation = strtolower($confirmation);
// Return TRUE if the user answers 'y' or 'yes'.
return in_array($confirmation, ['y', 'yes']);
}

/**
* Run database optimization (optional).
*
* @return void
* TRUE write the message.
*/
private function optimizeDatabase() {
$database = Database::getConnection();
$database->query('OPTIMIZE TABLE {admin_audit_trail}');
$this->output()->writeln("Database optimized to recover space.");
}
}
Loading