Skip to content
This repository was archived by the owner on Feb 7, 2024. It is now read-only.

Commit f89278e

Browse files
committed
wip
1 parent c050fe8 commit f89278e

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed

config/websockets.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@
5454
* Here you can specify the interval in seconds at which statistics should be logged.
5555
*/
5656
'interval_in_seconds' => 60,
57+
58+
/*
59+
* When the clean-command is executed, all recorded statistics older than
60+
* the number of days specified here will be deleted.
61+
*/
62+
'delete_statistics_older_than_days' => 60
5763
],
5864

5965
/*

src/Console/CleanStatistics.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace BeyondCode\LaravelWebSockets\Console;
4+
5+
use Carbon\Carbon;
6+
use Illuminate\Console\Command;
7+
use Illuminate\Database\Eloquent\Builder;
8+
9+
class CleanStatistics extends Command
10+
{
11+
12+
protected $signature = 'websockets:clean
13+
{appId? : (optional) The app id that will be cleaned.}';
14+
15+
protected $description = 'Clean up old statistics from the websocket log.';
16+
17+
public function handle()
18+
{
19+
$this->comment('Cleaning WebSocket Statistics...');
20+
21+
$appId = $this->argument('appId');
22+
23+
$maxAgeInDays = config('websockets.statistics.delete_statistics_older_than_days');
24+
25+
$cutOffDate = Carbon::now()->subDay($maxAgeInDays)->format('Y-m-d H:i:s');
26+
27+
$webSocketsStatisticsEntryModelClass = config('websockets.statistics.model');
28+
29+
$amountDeleted = $webSocketsStatisticsEntryModelClass::where('created_at', '<', $cutOffDate)
30+
->when(! is_null($appId), function (Builder $query) use ($appId) {
31+
$query->where('app_id', $appId);
32+
})
33+
->delete();
34+
35+
$this->info("Deleted {$amountDeleted} record(s) from the WebSocket statistics.");
36+
37+
$this->comment('All done!');
38+
}
39+
}

src/WebSocketsServiceProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public function boot()
4040

4141
$this->commands([
4242
Console\StartWebSocketServer::class,
43+
Console\CleanStatistics::class,
4344
]);
4445
}
4546

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace BeyondCode\LaravelWebSockets\Tests\Commands;
4+
5+
use Artisan;
6+
use BeyondCode\LaravelWebSockets\Statistics\Models\WebSocketsStatisticsEntry;
7+
use Carbon\Carbon;
8+
use BeyondCode\LaravelWebSockets\Tests\TestCase;
9+
10+
class CleanStatisticsTest extends TestCase
11+
{
12+
13+
public function setUp()
14+
{
15+
parent::setUp();
16+
17+
Carbon::setTestNow(Carbon::create(2018, 1, 1, 00, 00, 00));
18+
19+
$this->app['config']->set('websockets.statistics.delete_statistics_older_than_days', 31);
20+
}
21+
22+
23+
/** @test */
24+
public function it_can_clean_the_statistics()
25+
{
26+
collect(range(1, 60))->each(function (int $index) {
27+
WebSocketsStatisticsEntry::create([
28+
'app_id' => 'app_id',
29+
'peak_connection_count' => 1,
30+
'websocket_message_count' => 2,
31+
'api_message_count' => 3,
32+
'created_at' => Carbon::now()->subDays($index)->startOfDay(),
33+
]);
34+
});
35+
36+
$this->assertCount(60, WebSocketsStatisticsEntry::all());
37+
38+
Artisan::call('websockets:clean');
39+
40+
$this->assertCount(31, WebSocketsStatisticsEntry::all());
41+
42+
$cutOffDate = Carbon::now()->subDays(31)->format('Y-m-d H:i:s');
43+
44+
$this->assertCount(0, WebSocketsStatisticsEntry::where('created_at', '<', $cutOffDate)->get());
45+
46+
47+
}
48+
}

0 commit comments

Comments
 (0)