|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace BeyondCode\LaravelWebSockets\Statistics\Drivers; |
| 4 | + |
| 5 | +use Carbon\Carbon; |
| 6 | + |
| 7 | +class DatabaseDriver implements StatisticsDriver |
| 8 | +{ |
| 9 | + /** |
| 10 | + * The model that controls the database table. |
| 11 | + * |
| 12 | + * @var \BeyondCode\LaravelWebSockets\Statistics\Models\WebSocketsStatisticsEntry|null |
| 13 | + */ |
| 14 | + protected $record; |
| 15 | + |
| 16 | + /** |
| 17 | + * Initialize the driver. |
| 18 | + * |
| 19 | + * @param \BeyondCode\LaravelWebSockets\Statistics\Models\WebSocketsStatisticsEntry|null $record |
| 20 | + * @return void |
| 21 | + */ |
| 22 | + public function __construct($record = null) |
| 23 | + { |
| 24 | + $this->record = $record; |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Get the app ID for the stats. |
| 29 | + * |
| 30 | + * @return mixed |
| 31 | + */ |
| 32 | + public function getAppId() |
| 33 | + { |
| 34 | + return $this->record->app_id; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Get the time value. Should be Y-m-d H:i:s. |
| 39 | + * |
| 40 | + * @return string |
| 41 | + */ |
| 42 | + public function getTime(): string |
| 43 | + { |
| 44 | + return Carbon::parse($this->record->created_at)->toDateTimeString(); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Get the peak connection count for the time. |
| 49 | + * |
| 50 | + * @return int |
| 51 | + */ |
| 52 | + public function getPeakConnectionCount(): int |
| 53 | + { |
| 54 | + return $this->record->peak_connection_count ?? 0; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Get the websocket messages count for the time. |
| 59 | + * |
| 60 | + * @return int |
| 61 | + */ |
| 62 | + public function getWebsocketMessageCount(): int |
| 63 | + { |
| 64 | + return $this->record->websocket_message_count ?? 0; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Get the API message count for the time. |
| 69 | + * |
| 70 | + * @return int |
| 71 | + */ |
| 72 | + public function getApiMessageCount(): int |
| 73 | + { |
| 74 | + return $this->record->api_message_count ?? 0; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Create a new statistic in the store. |
| 79 | + * |
| 80 | + * @param array $data |
| 81 | + * @return \BeyondCode\LaravelWebSockets\Statistics\Drivers\StatisticsDriver |
| 82 | + */ |
| 83 | + public static function create(array $data): StatisticsDriver |
| 84 | + { |
| 85 | + $class = config('websockets.statistics.database.model'); |
| 86 | + |
| 87 | + return new static($class::create($data)); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Delete statistics from the store, |
| 92 | + * optionally by app id, returning |
| 93 | + * the number of deleted records. |
| 94 | + * |
| 95 | + * @param mixed $appId |
| 96 | + * @return int |
| 97 | + */ |
| 98 | + public static function delete($appId = null): int |
| 99 | + { |
| 100 | + $cutOffDate = Carbon::now()->subDay( |
| 101 | + config('websockets.statistics.delete_statistics_older_than_days') |
| 102 | + )->format('Y-m-d H:i:s'); |
| 103 | + |
| 104 | + $class = config('websockets.statistics.database.model'); |
| 105 | + |
| 106 | + return $class::where('created_at', '<', $cutOffDate) |
| 107 | + ->when($appId, function ($query) use ($appId) { |
| 108 | + return $query->whereAppId($appId); |
| 109 | + }) |
| 110 | + ->delete(); |
| 111 | + } |
| 112 | +} |
0 commit comments