This repository has been archived by the owner on Feb 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 640
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #473 from beyondcode/refactor/custom-statistics-dr…
…ivers [2.x] Custom statistics drivers
- Loading branch information
Showing
8 changed files
with
234 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
|
||
namespace BeyondCode\LaravelWebSockets\Statistics\Drivers; | ||
|
||
use Carbon\Carbon; | ||
|
||
class DatabaseDriver implements StatisticsDriver | ||
{ | ||
/** | ||
* The model that controls the database table. | ||
* | ||
* @var \BeyondCode\LaravelWebSockets\Statistics\Models\WebSocketsStatisticsEntry|null | ||
*/ | ||
protected $record; | ||
|
||
/** | ||
* Initialize the driver. | ||
* | ||
* @param \BeyondCode\LaravelWebSockets\Statistics\Models\WebSocketsStatisticsEntry|null $record | ||
* @return void | ||
*/ | ||
public function __construct($record = null) | ||
{ | ||
$this->record = $record; | ||
} | ||
|
||
/** | ||
* Get the app ID for the stats. | ||
* | ||
* @return mixed | ||
*/ | ||
public function getAppId() | ||
{ | ||
return $this->record->app_id; | ||
} | ||
|
||
/** | ||
* Get the time value. Should be Y-m-d H:i:s. | ||
* | ||
* @return string | ||
*/ | ||
public function getTime(): string | ||
{ | ||
return Carbon::parse($this->record->created_at)->toDateTimeString(); | ||
} | ||
|
||
/** | ||
* Get the peak connection count for the time. | ||
* | ||
* @return int | ||
*/ | ||
public function getPeakConnectionCount(): int | ||
{ | ||
return $this->record->peak_connection_count ?? 0; | ||
} | ||
|
||
/** | ||
* Get the websocket messages count for the time. | ||
* | ||
* @return int | ||
*/ | ||
public function getWebsocketMessageCount(): int | ||
{ | ||
return $this->record->websocket_message_count ?? 0; | ||
} | ||
|
||
/** | ||
* Get the API message count for the time. | ||
* | ||
* @return int | ||
*/ | ||
public function getApiMessageCount(): int | ||
{ | ||
return $this->record->api_message_count ?? 0; | ||
} | ||
|
||
/** | ||
* Create a new statistic in the store. | ||
* | ||
* @param array $data | ||
* @return \BeyondCode\LaravelWebSockets\Statistics\Drivers\StatisticsDriver | ||
*/ | ||
public static function create(array $data): StatisticsDriver | ||
{ | ||
$class = config('websockets.statistics.database.model'); | ||
|
||
return new static($class::create($data)); | ||
} | ||
|
||
/** | ||
* Delete statistics from the store, | ||
* optionally by app id, returning | ||
* the number of deleted records. | ||
* | ||
* @param mixed $appId | ||
* @return int | ||
*/ | ||
public static function delete($appId = null): int | ||
{ | ||
$cutOffDate = Carbon::now()->subDay( | ||
config('websockets.statistics.delete_statistics_older_than_days') | ||
)->format('Y-m-d H:i:s'); | ||
|
||
$class = config('websockets.statistics.database.model'); | ||
|
||
return $class::where('created_at', '<', $cutOffDate) | ||
->when($appId, function ($query) use ($appId) { | ||
return $query->whereAppId($appId); | ||
}) | ||
->delete(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
namespace BeyondCode\LaravelWebSockets\Statistics\Drivers; | ||
|
||
interface StatisticsDriver | ||
{ | ||
/** | ||
* Initialize the driver with a stored record. | ||
* | ||
* @param mixed $record | ||
* @return void | ||
*/ | ||
public function __construct($record = null); | ||
|
||
/** | ||
* Get the app ID for the stats. | ||
* | ||
* @return mixed | ||
*/ | ||
public function getAppId(); | ||
|
||
/** | ||
* Get the time value. Should be Y-m-d H:i:s. | ||
* | ||
* @return string | ||
*/ | ||
public function getTime(): string; | ||
|
||
/** | ||
* Get the peak connection count for the time. | ||
* | ||
* @return int | ||
*/ | ||
public function getPeakConnectionCount(): int; | ||
|
||
/** | ||
* Get the websocket messages count for the time. | ||
* | ||
* @return int | ||
*/ | ||
public function getWebsocketMessageCount(): int; | ||
|
||
/** | ||
* Get the API message count for the time. | ||
* | ||
* @return int | ||
*/ | ||
public function getApiMessageCount(): int; | ||
|
||
/** | ||
* Create a new statistic in the store. | ||
* | ||
* @param array $data | ||
* @return \BeyondCode\LaravelWebSockets\Statistics\Drivers\StatisticsDriver | ||
*/ | ||
public static function create(array $data): StatisticsDriver; | ||
|
||
/** | ||
* Delete statistics from the store, | ||
* optionally by app id, returning | ||
* the number of deleted records. | ||
* | ||
* @param mixed $appId | ||
* @return int | ||
*/ | ||
public static function delete($appId = null): int; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters