Skip to content

Commit

Permalink
Add correct spelling for removeOldestIfMoreThan and removeOlderThan f…
Browse files Browse the repository at this point in the history
…unctions.

Fixed issue with said functions not working when using mongodb.
  • Loading branch information
danielme85 committed Jan 9, 2020
1 parent b07495b commit 97be3cd
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 14 deletions.
6 changes: 3 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,14 @@ No indexes are added per default, so if you fetch a lot of log results based on
#### Log Cleanup
There is a helper function to remove the oldest log events and keep a specified number
```php
LogToDB::removeOldestIfMoreThen(100);
LogToDB::removeOldestIfMoreThan(100);
```
Or based on date (most be valid date/datetime supported by strtotime())
http://php.net/manual/en/function.strtotime.php

```php
LogToDB::model()->removeOlderThen('2019-01-01');
LogToDB::model()->removeOlderThen('2019-01-01 23:00:00');
LogToDB::model()->removeOlderThan('2019-01-01');
LogToDB::model()->removeOlderThan('2019-01-01 23:00:00');
```

#### Processors
Expand Down
36 changes: 29 additions & 7 deletions src/Models/LogToDbCreateObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,18 +156,29 @@ private function jsonDecodeIfNotEmpty($value, $arraymode = true)
return $value;
}

/**
* Delete the oldest records based on unix_time, silly spelling version.
*
* @param int $max amount of records to keep
* @return bool
*/
public function removeOldestIfMoreThen(int $max)
{
return $this->removeOldestIfMoreThan($max);
}

/**
* Delete the oldest records based on unix_time
*
* @param int $max
* @param int $max amount of records to keep
* @return bool success
*/
public function removeOldestIfMoreThen(int $max)
public function removeOldestIfMoreThan(int $max)
{
$current = $this->count();
if ($current > $max) {
$keepers = $this->orderBy('unix_time', 'DESC')->take($max)->pluck('id')->toArray();
if ($this->whereNotIn('id', $keepers)->get()->each->delete()) {
$keepers = $this->orderBy('unix_time', 'DESC')->take($max)->pluck($this->primaryKey)->toArray();
if ($this->whereNotIn($this->primaryKey, $keepers)->get()->each->delete()) {
return true;
}
}
Expand All @@ -176,17 +187,28 @@ public function removeOldestIfMoreThen(int $max)
}

/**
* Delete records based on date.
* Delete records based on date, silly spelling version.
*
* @param string $datetime date supported by strtotime: http://php.net/manual/en/function.strtotime.php
* @return bool success
*/
public function removeOlderThen(string $datetime)
{
return $this->removeOlderThan($datetime);
}

/**
* Delete records based on date.
*
* @param string $datetime date supported by strtotime: http://php.net/manual/en/function.strtotime.php
* @return bool success
*/
public function removeOlderThan(string $datetime)
{
$unixtime = strtotime($datetime);

$keepers = $this->where('unix_time', '>=', $unixtime)->pluck('id')->toArray();
$deletes = $this->whereNotIn('id', $keepers)->get();
$keepers = $this->where('unix_time', '>=', $unixtime)->pluck($this->primaryKey)->toArray();
$deletes = $this->whereNotIn($this->primaryKey, $keepers)->get();
if (!$deletes->isEmpty()){
if ($deletes->each->delete()) {
return true;
Expand Down
41 changes: 37 additions & 4 deletions tests/LogToDbTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,11 +322,44 @@ public function testStandAloneModels() {
* @group cleanup
*/
public function testRemoves() {
$this->assertTrue(LogToDB::model()->removeOldestIfMoreThen(1));
$this->assertFalse(LogToDB::model()->removeOlderThen(date('Y-m-d')));
Log::debug("This is an test DEBUG log event");
Log::info("This is an test INFO log event");
Log::notice("This is an test NOTICE log event");

//sleep to pass time for record cleanup testing based on time next.
sleep(1);

$this->assertTrue(LogToDB::model()->removeOldestIfMoreThan(2));
$this->assertEquals(2, LogToDB::model()->count());
$this->assertTrue(LogToDB::model()->removeOlderThan(date('Y-m-d H:i:s')));
$this->assertEquals(0, LogToDB::model()->count());

//Same tests on mongodb
$this->assertTrue(LogToDB::model('mongodb')->removeOldestIfMoreThan(2));
$this->assertEquals(2, LogToDB::model('mongodb')->count());
$this->assertTrue(LogToDB::model('mongodb')->removeOlderThan(date('Y-m-d H:i:s')));
$this->assertEquals(0, LogToDB::model('mongodb')->count());


//test wrappers for silly spelling
Log::debug("This is an test DEBUG log event");
Log::info("This is an test INFO log event");
Log::notice("This is an test NOTICE log event");

//sleep to pass time for record cleanup testing based on time next.
sleep(1);

$this->assertTrue(LogToDB::model()->removeOldestIfMoreThen(2));
$this->assertEquals(2, LogToDB::model()->count());
$this->assertTrue(LogToDB::model()->removeOlderThen(date('Y-m-d H:i:s')));
$this->assertEquals(0, LogToDB::model()->count());

//Same tests on mongodb
$this->assertTrue(LogToDB::model('mongodb')->removeOldestIfMoreThen(2));
$this->assertEquals(2, LogToDB::model('mongodb')->count());
$this->assertTrue(LogToDB::model('mongodb')->removeOlderThen(date('Y-m-d H:i:s')));
$this->assertEquals(0, LogToDB::model('mongodb')->count());

$this->assertTrue(LogToDB::model('mongodb')->removeOldestIfMoreThen(1));
$this->assertFalse(LogToDB::model('mongodb')->removeOlderThen(date('Y-m-d')));
}

/**
Expand Down

0 comments on commit 97be3cd

Please sign in to comment.