Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateVisitsArchiveTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('visits_archive', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('visitable_type');
$table->unsignedBigInteger('visitable_id');
$table->string('tag');
$table->date('date');
$table->unsignedBigInteger('count');
$table->timestamps();

$table->index(['visitable_type', 'visitable_id']);
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('visits_archive');
}
}
70 changes: 70 additions & 0 deletions src/Commands/VisitsArchiveCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Awssat\Visits\Commands;

use Awssat\Visits\DataEngines\RedisEngine;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class VisitsArchiveCommand extends Command
{
protected $signature = 'visits:archive';
protected $description = '(Laravel-Visits) Archive daily visits from Redis to the database.';

public function handle()
{
if (! config('visits.archive_daily_visits')) {
$this->error('Daily visits archiving is disabled. Please enable it in config/visits.php');
return;
}

$this->info('Archiving daily visits...');

$redis = app(RedisEngine::class)
->connect(config('visits.connection'))
->setPrefix(config('visits.keys_prefix'));
$prefix = config('visits.keys_prefix');

$keys = $redis->search('*_day_daily_*', false);

foreach ($keys as $key) {
if (\Illuminate\Support\Str::endsWith($key, '_total')) {
continue;
}

$keyWithoutPrefix = substr($key, strlen($prefix) + 1);

$keyParts = explode(':', $key);
$key_parts_without_prefix = explode(':', $keyWithoutPrefix);
$keyParts = explode(':', $key);
$visitable_type = $keyParts[1];
$keyWithoutPrefix = implode(':', array_slice($keyParts, 1));

$parts = explode('_', $keyWithoutPrefix);
$date = array_pop($parts);
array_pop($parts); // remove daily
array_pop($parts); // remove day
$tag = array_pop($parts);

$visits = $redis->valueList($keyWithoutPrefix, -1, true, true);

foreach ($visits as $visitable_id => $count) {
DB::table('visits_archive')->insert([
'visitable_type' => $visitable_type,
'visitable_id' => $visitable_id,
'tag' => $tag,
'date' => $date,
'count' => $count,
'created_at' => now(),
'updated_at' => now(),
]);
}

$redis->delete($key);
$redis->delete($key . '_total');
}

$this->info('Done.');
}
}
5 changes: 5 additions & 0 deletions src/Traits/Record.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ protected function recordPeriods($inc)

$this->connection->increment($periodKey, $inc, $this->keys->id);
$this->connection->increment($periodKey . '_total', $inc);

if ($period === 'day' && config('visits.archive_daily_visits')) {
$this->connection->increment($periodKey . '_daily_' . now()->toDateString(), $inc, $this->keys->id);
$this->connection->increment($periodKey . '_daily_' . now()->toDateString() . '_total', $inc);
}
}
}

Expand Down
12 changes: 12 additions & 0 deletions src/Traits/Setters.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,16 @@ public function period($period)

return $this;
}

/**
* @param $from
* @param null $to
* @return $this
*/
public function byDate($from, $to = null)
{
$this->date = [$from, $to];

return $this;
}
}
28 changes: 27 additions & 1 deletion src/Visits.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Support\Carbon;
use Jaybizzle\CrawlerDetect\CrawlerDetect;

class Visits
Expand Down Expand Up @@ -42,7 +43,11 @@ class Visits
*/
protected $language = null;
/**
* @var mixed
* @var null|string
*/
protected $date = null;
/**
* @var null|string
*/
protected $periods;
/**
Expand Down Expand Up @@ -162,6 +167,27 @@ public function count()
return $this->connection->get($this->keys->visits."_languages:{$this->keys->id}", $this->language);
}


if ($this->date) {
$keys = [];
$from = \Carbon\Carbon::parse($this->date[0]);
$to = $this->date[1] ? \Carbon\Carbon::parse($this->date[1]) : $from;

for ($date = $from; $date->lte($to); $date->addDay()) {
$keys[] = $this->keys->period('day') . '_daily_' . $date->toDateString();
}

$total = 0;
foreach ($keys as $key) {
if ($this->keys->instanceOfModel) {
$total += intval($this->connection->get($key, $this->keys->id));
} else {
$total += intval($this->connection->get($key . '_total'));
}
}
return $total;
}

return intval(
$this->keys->instanceOfModel
? $this->connection->get($this->keys->visits, $this->keys->id)
Expand Down
12 changes: 12 additions & 0 deletions src/VisitsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ public function boot()
__DIR__.'/config/visits.php' => config_path('visits.php'),
], 'config');

if ($this->app->runningInConsole()) {
if (floatval(app()->version()) < 11.0) {
$this->loadMigrationsFrom(__DIR__.'/../database/migrations');
} else {
$this->publishesMigrations([
__DIR__.'/../database/migrations' => database_path('migrations'),
]);
}
}

if (! class_exists('CreateVisitsTable')) {
$this->publishes([
__DIR__.'/../database/migrations/create_visits_table.php.stub' => database_path('migrations/'.date('Y_m_d_His', time()).'_create_visits_table.php'),
Expand Down Expand Up @@ -89,9 +99,11 @@ public function getLocation() {
}

$this->app->bind('command.visits:clean', CleanCommand::class);
$this->app->bind('command.visits:archive', \Awssat\Visits\Commands\VisitsArchiveCommand::class);

$this->commands([
'command.visits:clean',
'command.visits:archive',
]);
}
}
11 changes: 10 additions & 1 deletion src/config/visits.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,16 @@
| stop recording specific items (can be any of these: 'country', 'refer', 'periods', 'operatingSystem', 'language')
|
*/
'global_ignore' => ['country'],
'global_ignore' => [],

/*
|--------------------------------------------------------------------------
| Archive daily visits
|--------------------------------------------------------------------------
|
| If you want to archive daily visits, you need to enable this option.
|
*/
'archive_daily_visits' => true,
];

55 changes: 55 additions & 0 deletions tests/Feature/ArchiveDailyVisitsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Awssat\Visits\Tests\Feature;

use Awssat\Visits\Tests\TestCase;
use Awssat\Visits\Tests\Post;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Carbon;

class ArchiveDailyVisitsTest extends TestCase
{
use RefreshDatabase;

public function setUp(): void
{
parent::setUp();

$this->app['config']['database.redis.client'] = 'predis';
$this->app['config']['database.redis.options.prefix'] = '';
$this->app['config']['database.redis.laravel-visits'] = [
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 3,
];

$this->redis = Redis::connection('laravel-visits');

if (count($keys = $this->redis->keys($this->app['config']['visits.keys_prefix'] . ':testing:*'))) {
$this->redis->del($keys);
}
}

/** @test */
public function it_does_not_record_daily_visits_when_disabled()
{
$this->app['config']['visits.archive_daily_visits'] = false;

$post = Post::create(['id' => 1]);
visits($post)->increment();

$this->assertCount(0, $this->redis->keys('*_day_daily_*'));
}

/** @test */
public function it_throws_an_exception_when_the_archive_command_is_run_while_disabled()
{
$this->app['config']['visits.archive_daily_visits'] = false;

$this->artisan('visits:archive')
->expectsOutput('Daily visits archiving is disabled. Please enable it in config/visits.php')
->assertExitCode(0);
}
}
45 changes: 45 additions & 0 deletions tests/Feature/ByDateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Awssat\Visits\Tests\Feature;

use Awssat\Visits\Tests\TestCase;
use Awssat\Visits\Tests\Post;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Carbon;

class ByDateTest extends TestCase
{
use RefreshDatabase;

public function setUp(): void
{
parent::setUp();

$this->app['config']['database.redis.client'] = 'predis';
$this->app['config']['database.redis.options.prefix'] = '';
$this->app['config']['database.redis.laravel-visits'] = [
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 3,
];

$this->app['config']['visits.engine'] = \Awssat\Visits\DataEngines\RedisEngine::class;
}

/** @test */
public function it_can_get_visits_by_date()
{
Carbon::setTestNow(Carbon::create(2023, 1, 1));
$post = Post::create(['id' => 1]);
visits($post)->increment();

Carbon::setTestNow(Carbon::create(2023, 1, 2));
visits($post)->increment();
visits($post)->increment();

$this->assertEquals(1, visits($post)->byDate('2023-01-01')->count());
$this->assertEquals(2, visits($post)->byDate('2023-01-02')->count());
$this->assertEquals(3, visits($post)->byDate('2023-01-01', '2023-01-02')->count());
}
}
54 changes: 54 additions & 0 deletions tests/Feature/VisitsArchiveCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Awssat\Visits\Tests\Feature;

use Awssat\Visits\Tests\TestCase;
use Awssat\Visits\Tests\Post;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Carbon;

class VisitsArchiveCommandTest extends TestCase
{
use RefreshDatabase;

public function setUp(): void
{
parent::setUp();

$this->app['config']['database.redis.client'] = 'predis';
$this->app['config']['database.redis.options.prefix'] = '';
$this->app['config']['database.redis.laravel-visits'] = [
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 3,
];

$this->redis = Redis::connection('laravel-visits');

if (count($keys = $this->redis->keys($this->app['config']['visits.keys_prefix'] . ':testing:*'))) {
$this->redis->del($keys);
}
}

/** @test */
public function it_archives_daily_visits()
{
Carbon::setTestNow(Carbon::create(2023, 1, 1));

$post = Post::create(['id' => 1]);
visits($post)->increment();

$this->artisan('visits:archive')->assertExitCode(0);

$this->assertDatabaseHas('visits_archive', [
'visitable_type' => 'posts',
'visitable_id' => 1,
'tag' => 'visits',
'date' => '2023-01-01',
'count' => 1,
]);
}
}
Loading