-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created new drush command for audit trail logs cleaup
- Loading branch information
Sahil Sharma
committed
Nov 11, 2024
1 parent
e19c5a4
commit 9dc1f61
Showing
4 changed files
with
156 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
namespace Drupal\tide_core\Command; | ||
|
||
use Drush\Commands\DrushCommands; | ||
use Drupal\Core\Database\Database; | ||
use DateInterval; | ||
use DateTime; | ||
use Drupal\Core\Config\ConfigFactoryInterface; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
|
||
/** | ||
* Custom AuditLog Clean Drush command. | ||
*/ | ||
class AuditLogCleanupCommand extends DrushCommands { | ||
|
||
/** | ||
* The configuration service. | ||
* | ||
* @var \Drupal\Core\Config\ConfigFactoryInterface | ||
*/ | ||
protected $configFactory; | ||
|
||
/** | ||
* Constructs a LogCleanupCommands 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') | ||
); | ||
} | ||
|
||
/** | ||
* 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. | ||
*/ | ||
public function cleanupLogs() { | ||
$config = $this->configFactory->get('tide_core.settings'); | ||
$log_retention_days = $config->get('log_retention_days') ?: 30; | ||
|
||
// 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(); | ||
|
||
// 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 | ||
*/ | ||
private function optimizeDatabase() { | ||
$database = Database::getConnection(); | ||
$database->query('OPTIMIZE TABLE {admin_audit_trail}'); | ||
$this->output()->writeln("Database optimized to recover space."); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
namespace Drupal\tide_core\Form; | ||
|
||
use Drupal\Core\Form\ConfigFormBase; | ||
use Drupal\Core\Form\FormStateInterface; | ||
|
||
/** | ||
* Provides the configuration form for setting the log retention days. | ||
*/ | ||
class AuditTrailSettingsForm extends ConfigFormBase { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function getEditableConfigNames() { | ||
// This returns the name of the config object. | ||
return ['tide_core.settings']; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getFormId() { | ||
// The form ID for this form. | ||
return 'tide_core_log_retention_settings_form'; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function buildForm(array $form, FormStateInterface $form_state) { | ||
// Load the configuration data. | ||
$config = $this->config('tide_core.settings'); | ||
|
||
// Add a text field to specify the number of days for log retention. | ||
$form['log_retention_days'] = [ | ||
'#type' => 'number', | ||
'#title' => $this->t('Log retention days'), | ||
'#description' => $this->t('Enter the number of days after which logs should be deleted.'), | ||
'#default_value' => $config->get('log_retention_days', 30), // Default to 30 if not set. | ||
'#min' => 1, | ||
'#required' => TRUE, | ||
]; | ||
|
||
return parent::buildForm($form, $form_state); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function submitForm(array &$form, FormStateInterface $form_state) { | ||
$this->config('tide_core.settings') | ||
->set('log_retention_days', $form_state->getValue('log_retention_days')) | ||
->save(); | ||
|
||
$this->messenger()->addMessage($this->t('The log retention days have been updated.')); | ||
parent::submitForm($form, $form_state); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters