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

[0.x] Add time for delete visit records #5

Merged
merged 10 commits into from
Jul 31, 2023
8 changes: 8 additions & 0 deletions config/user-monitoring.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@
'expect_pages' => [
// 'home',
],

/*
* If you want to delete visit rows after some days, you can change this like 360,
* but you don't like to delete rows you can change it to 0.
*
* For this feature you need Task-Scheduling => https://laravel.com/docs/10.x/scheduling
*/
'delete_days' => 0,
],

/*
Expand Down
44 changes: 44 additions & 0 deletions src/Commands/RemoveVisitMonitoringRecordsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Binafy\LaravelUserMonitoring\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class RemoveVisitMonitoringRecordsCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'laravel-user-monitoring:remove-visit-monitoring-records';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Remove visits-monitoring records';

/**
* Execute the console command.
*/
public function handle()
{
$days = config('user-monitoring.visit_monitoring.delete_days', 0);
if ($days === 0) {
$this->error('Your delete days are 0, You can go to the config file and change it!');
return;
}

$date = now()->subDays($days)->toDateString();
$table = config('user-monitoring.visit_monitoring.table');

DB::table($table)
->where('created_at', '<', $date)
->delete();

$this->info('Records have been removed successfully!');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Illuminate\Support\Facades\DB;
use Jenssegers\Agent\Agent;

class MonitorVisitMiddleware
class VisitMonitoringMiddleware
{
/**
* Handle.
Expand Down
8 changes: 5 additions & 3 deletions src/Providers/LaravelUserMonitoringServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace Binafy\LaravelUserMonitoring\Providers;

use Binafy\LaravelUserMonitoring\Middlewares\MonitorVisitMiddleware;
use Binafy\LaravelUserMonitoring\Commands\RemoveVisitMonitoringRecordsCommand;
use Binafy\LaravelUserMonitoring\Middlewares\VisitMonitoringMiddleware;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;

Expand All @@ -18,12 +19,13 @@ public function register()
$this->loadViewsFrom(__DIR__ . '/../../resources/views/', 'LaravelUserMonitoring');
$this->loadMigrationsFrom(__DIR__ . '/../../database/migrations');
$this->mergeConfigFrom(__DIR__ . '/../../config/user-monitoring.php', 'user-monitoring');
$this->commands(RemoveVisitMonitoringRecordsCommand::class);

$this->app['router']->aliasMiddleware('monitor-visit-middleware', MonitorVisitMiddleware::class);
$this->app['router']->aliasMiddleware('monitor-visit-middleware', VisitMonitoringMiddleware::class);
$this->app->register(LaravelUserMonitoringEventServiceProvider::class);

Route::middleware('web')
->middleware(MonitorVisitMiddleware::class)
->middleware(VisitMonitoringMiddleware::class)
->group(__DIR__ . '/../../routes/web.php');
}

Expand Down
3 changes: 2 additions & 1 deletion tests/Feature/ActionMonitoringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
->toBe('products')
->and(ActionMonitoring::query()->value('action_type'))
->toBe(ActionType::ACTION_STORE)
->and($user->name)->toBe(ActionMonitoring::first()->user->name);
->and($user->name)
->toBe(ActionMonitoring::first()->user->name);

// DB Assertions
assertDatabaseCount(config('user-monitoring.action_monitoring.table'), 1);
Expand Down
84 changes: 84 additions & 0 deletions tests/Feature/DeleteVisitMonitoringRecordsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use function Pest\Laravel\artisan;
use function Pest\Laravel\assertDatabaseCount;

/*
* Use `RefreshDatabase` for delete migration data for each test.
*/
uses(RefreshDatabase::class);

test('rows are deleted after 1 day', function () {
// Set delete days
config()->set('user-monitoring.visit_monitoring.delete_days', 1);

// Store old records for 1 day age
$user = createUser();

DB::table(config('user-monitoring.visit_monitoring.table'))->insert([
[
'user_id' => $user->id,
'browser_name' => 'Chrome',
'platform' => 'Windows',
'device' => 'WebKit',
'ip' => '127.0.0.1',
'page' => 'http://localhost:8000',
'created_at' => now()->subDays(2),
],
[
'user_id' => $user->id,
'browser_name' => 'Firefox',
'platform' => 'Linux',
'device' => 'WebKit',
'ip' => '123.12.531.11',
'page' => 'http://localhost:8000/milwad',
'created_at' => now()->subDays(2),
]
]);

artisan('laravel-user-monitoring:remove-visit-monitoring-records');

// DB Assertions
assertDatabaseCount(config('user-monitoring.visit_monitoring.table'), 0);
});

test('rows are not deleted after 1 day', function () {
// Set delete days
config()->set('user-monitoring.visit_monitoring.delete_days', 1);

// Store old records for 1 day age
$user = createUser();

DB::table(config('user-monitoring.visit_monitoring.table'))->insert([
[
'user_id' => $user->id,
'browser_name' => 'Chrome',
'platform' => 'Windows',
'device' => 'WebKit',
'ip' => '127.0.0.1',
'page' => 'http://localhost:8000',
'created_at' => now(),
],
[
'user_id' => $user->id,
'browser_name' => 'Firefox',
'platform' => 'Linux',
'device' => 'WebKit',
'ip' => '123.12.531.11',
'page' => 'http://localhost:8000/milwad',
'created_at' => now(),
]
]);

artisan('laravel-user-monitoring:remove-visit-monitoring-records');

// DB Assertions
assertDatabaseCount(config('user-monitoring.visit_monitoring.table'), 2);
});

test('throw an error when days equal to 0', function () {
artisan('laravel-user-monitoring:remove-visit-monitoring-records')
->expectsOutput('Your delete days are 0, You can go to the config file and change it!');
});
4 changes: 2 additions & 2 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Tests;

use Binafy\LaravelUserMonitoring\Middlewares\MonitorVisitMiddleware;
use Binafy\LaravelUserMonitoring\Middlewares\VisitMonitoringMiddleware;
use Binafy\LaravelUserMonitoring\Providers\LaravelUserMonitoringServiceProvider;
use Illuminate\Encryption\Encrypter;
use Illuminate\Support\Facades\Route;
Expand Down Expand Up @@ -56,7 +56,7 @@ protected function setUp(): void
{
parent::setUp();

Route::middleware([MonitorVisitMiddleware::class, 'web'])->group(__DIR__ . '/SetUp/Routes/web_tests.php');
Route::middleware([VisitMonitoringMiddleware::class, 'web'])->group(__DIR__ . '/SetUp/Routes/web_tests.php');

$this->loadMigrationsFrom(__DIR__.'/SetUp/Migrations');
}
Expand Down