Skip to content

Commit

Permalink
Merge pull request #2 from christyjacob4/master
Browse files Browse the repository at this point in the history
Delete Logs Older than X Seconds
  • Loading branch information
eldadfux authored Dec 16, 2020
2 parents a3aa468 + b75c72a commit c86455f
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/Audit/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,13 @@ abstract public function getLogsByResource(string $resource): array;
* @return array
*/
abstract public function getLogsByUserAndActions(string $userId, array $actions): array;

/**
* Delete all logs older than $seconds seconds
*
* @param int $seconds
*
* @return bool
*/
abstract public function deleteLogsOlderThan(int $seconds): bool;
}
19 changes: 19 additions & 0 deletions src/Audit/Adapters/MySQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,25 @@ public function getLogsByUserAndActions(string $userId, array $actions):array
return $st->fetchAll();
}

/**
* Delete logs older than $seconds seconds
*
* @param int $seconds
*
* @return bool
*/
public function deleteLogsOlderThan(int $seconds):bool
{
$st = $this->getPDO()->prepare('DELETE
FROM `'.$this->getNamespace().'.audit.audit`
WHERE (UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(`time`)) > :seconds');

$st->bindValue(':seconds', $seconds, PDO::PARAM_INT);
$st->execute();

return ('00000' == $st->errorCode()) ? true : false;
}

/**
* @return PDO
*/
Expand Down
12 changes: 12 additions & 0 deletions src/Audit/Audit.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,16 @@ public function getLogsByUserAndActions(string $userId, array $actions): array
{
return $this->adapter->getLogsByUserAndActions($userId, $actions);
}

/**
* Delete all logs older than $seconds seconds
*
* @param int $seconds
*
* @return bool
*/
public function deleteLogsOlderThan(int $seconds): bool
{
return $this->adapter->deleteLogsOlderThan($seconds);
}
}
33 changes: 33 additions & 0 deletions tests/Audit/AuditTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,37 @@ public function testGetLogsByResource()
$this->assertEquals(1, \count($logs1));
$this->assertEquals(2, \count($logs2));
}

public function testDeleteLogsOlderThan() {
sleep(3);
// First delete all the logs
$status = $this->audit->deleteLogsOlderThan(1);
$this->assertEquals($status, true);

// Check that all logs have been deleted
$logs = $this->audit->getLogsByUser('userId');
$this->assertEquals(0, \count($logs));

// Add three sample logs
$userId = 'userId';
$userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36';
$ip = '127.0.0.1';
$location = 'US';
$data = ['key1' => 'value1','key2' => 'value2'];

$this->assertEquals($this->audit->log($userId, 'update', 'database/document/1', $userAgent, $ip, $location, $data), true);
sleep(5);
$this->assertEquals($this->audit->log($userId, 'update', 'database/document/2', $userAgent, $ip, $location, $data), true);
sleep(5);
$this->assertEquals($this->audit->log($userId, 'delete', 'database/document/2', $userAgent, $ip, $location, $data), true);
sleep(5);

// DELETE logs older than 10 seconds and check that status is true
$status = $this->audit->deleteLogsOlderThan(10);
$this->assertEquals($status, true);

// Check if 1 log has been deleted
$logs = $this->audit->getLogsByUser('userId');
$this->assertEquals(2, \count($logs));
}
}

0 comments on commit c86455f

Please sign in to comment.