1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BeyondCode\LaravelWebSockets\Statistics\Drivers; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
|
8
|
|
|
class DatabaseDriver implements StatisticsDriver |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The model that controls the database table. |
12
|
|
|
* |
13
|
|
|
* @var \BeyondCode\LaravelWebSockets\Statistics\Models\WebSocketsStatisticsEntry|null |
14
|
|
|
*/ |
15
|
|
|
protected $record; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Initialize the driver. |
19
|
|
|
* |
20
|
|
|
* @param \BeyondCode\LaravelWebSockets\Statistics\Models\WebSocketsStatisticsEntry|null $record |
21
|
|
|
* @return void |
|
|
|
|
22
|
|
|
*/ |
23
|
|
|
public function __construct($record = null) |
24
|
|
|
{ |
25
|
|
|
$this->record = $record; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Get the app ID for the stats. |
30
|
|
|
* |
31
|
|
|
* @return mixed |
32
|
|
|
*/ |
33
|
|
|
public function getAppId() |
34
|
|
|
{ |
35
|
|
|
return $this->record->app_id; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Get the time value. Should be Y-m-d H:i:s. |
40
|
|
|
* |
41
|
|
|
* @return string |
42
|
|
|
*/ |
43
|
|
|
public function getTime(): string |
44
|
|
|
{ |
45
|
|
|
return Carbon::parse($this->record->created_at)->toDateTimeString(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Get the peak connection count for the time. |
50
|
|
|
* |
51
|
|
|
* @return int |
52
|
|
|
*/ |
53
|
|
|
public function getPeakConnectionCount(): int |
54
|
|
|
{ |
55
|
|
|
return $this->record->peak_connection_count ?? 0; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get the websocket messages count for the time. |
60
|
|
|
* |
61
|
|
|
* @return int |
62
|
|
|
*/ |
63
|
|
|
public function getWebsocketMessageCount(): int |
64
|
|
|
{ |
65
|
|
|
return $this->record->websocket_message_count ?? 0; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get the API message count for the time. |
70
|
|
|
* |
71
|
|
|
* @return int |
72
|
|
|
*/ |
73
|
|
|
public function getApiMessageCount(): int |
74
|
|
|
{ |
75
|
|
|
return $this->record->api_message_count ?? 0; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Create a new statistic in the store. |
80
|
|
|
* |
81
|
|
|
* @param array $data |
82
|
|
|
* @return \BeyondCode\LaravelWebSockets\Statistics\Drivers\StatisticsDriver |
83
|
|
|
*/ |
84
|
|
|
public static function create(array $data): StatisticsDriver |
85
|
|
|
{ |
86
|
|
|
$class = config('websockets.statistics.database.model'); |
87
|
|
|
|
88
|
|
|
return new static($class::create($data)); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get the records to show to the dashboard. |
93
|
|
|
* |
94
|
|
|
* @param mixed $appId |
95
|
|
|
* @param \Illuminate\Http\Request|null $request |
96
|
|
|
* @return array |
97
|
|
|
*/ |
98
|
|
|
public static function get($appId, ?Request $request): array |
99
|
|
|
{ |
100
|
|
|
$class = config('websockets.statistics.database.model'); |
101
|
|
|
|
102
|
|
|
$statistics = $class::whereAppId($appId) |
103
|
|
|
->latest() |
104
|
|
|
->limit(120) |
105
|
|
|
->get() |
106
|
|
|
->map(function ($statistic) { |
107
|
|
|
return [ |
108
|
|
|
'timestamp' => (string) $statistic->created_at, |
109
|
|
|
'peak_connection_count' => $statistic->peak_connection_count, |
110
|
|
|
'websocket_message_count' => $statistic->websocket_message_count, |
111
|
|
|
'api_message_count' => $statistic->api_message_count, |
112
|
|
|
]; |
113
|
|
|
})->reverse(); |
114
|
|
|
|
115
|
|
|
return [ |
116
|
|
|
'peak_connections' => [ |
117
|
|
|
'x' => $statistics->pluck('timestamp'), |
118
|
|
|
'y' => $statistics->pluck('peak_connection_count'), |
119
|
|
|
], |
120
|
|
|
'websocket_message_count' => [ |
121
|
|
|
'x' => $statistics->pluck('timestamp'), |
122
|
|
|
'y' => $statistics->pluck('websocket_message_count'), |
123
|
|
|
], |
124
|
|
|
'api_message_count' => [ |
125
|
|
|
'x' => $statistics->pluck('timestamp'), |
126
|
|
|
'y' => $statistics->pluck('api_message_count'), |
127
|
|
|
], |
128
|
|
|
]; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Delete statistics from the store, |
133
|
|
|
* optionally by app id, returning |
134
|
|
|
* the number of deleted records. |
135
|
|
|
* |
136
|
|
|
* @param mixed $appId |
137
|
|
|
* @return int |
138
|
|
|
*/ |
139
|
|
|
public static function delete($appId = null): int |
140
|
|
|
{ |
141
|
|
|
$cutOffDate = Carbon::now()->subDay( |
142
|
|
|
config('websockets.statistics.delete_statistics_older_than_days') |
143
|
|
|
)->format('Y-m-d H:i:s'); |
144
|
|
|
|
145
|
|
|
$class = config('websockets.statistics.database.model'); |
146
|
|
|
|
147
|
|
|
return $class::where('created_at', '<', $cutOffDate) |
148
|
|
|
->when($appId, function ($query) use ($appId) { |
149
|
|
|
return $query->whereAppId($appId); |
150
|
|
|
}) |
151
|
|
|
->delete(); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.