1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BeyondCode\LaravelWebSockets\Statistics\Stores; |
4
|
|
|
|
5
|
|
|
use BeyondCode\LaravelWebSockets\Contracts\StatisticsStore; |
6
|
|
|
use Carbon\Carbon; |
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
|
9
|
|
|
class DatabaseStore implements StatisticsStore |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* The model that will interact with the database. |
13
|
|
|
* |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
protected static $model = \BeyondCode\LaravelWebSockets\Models\WebSocketsStatisticsEntry::class; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Store a new record in the database and return |
20
|
|
|
* the created instance. |
21
|
|
|
* |
22
|
|
|
* @param array $data |
23
|
|
|
* @return mixed |
24
|
|
|
*/ |
25
|
|
|
public static function store(array $data) |
26
|
|
|
{ |
27
|
|
|
return static::$model::create($data); |
|
|
|
|
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Delete records older than the given moment, |
32
|
|
|
* for a specific app id (if given), returning |
33
|
|
|
* the amount of deleted records. |
34
|
|
|
* |
35
|
|
|
* @param \Carbon\Carbon $moment |
36
|
|
|
* @param string|int|null $appId |
37
|
|
|
* @return int |
38
|
|
|
*/ |
39
|
|
|
public static function delete(Carbon $moment, $appId = null): int |
40
|
|
|
{ |
41
|
|
|
return static::$model::where('created_at', '<', $moment->toDateTimeString()) |
|
|
|
|
42
|
|
|
->when(! is_null($appId), function ($query) use ($appId) { |
43
|
|
|
return $query->whereAppId($appId); |
44
|
|
|
}) |
45
|
|
|
->delete(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Get the query result as eloquent collection. |
50
|
|
|
* |
51
|
|
|
* @param callable $processQuery |
52
|
|
|
* @return \Illuminate\Support\Collection |
53
|
|
|
*/ |
54
|
|
|
public function getRawRecords(callable $processQuery = null) |
55
|
|
|
{ |
56
|
|
|
return static::$model::query() |
|
|
|
|
57
|
|
|
->when(! is_null($processQuery), function ($query) use ($processQuery) { |
58
|
|
|
return call_user_func($processQuery, $query); |
59
|
|
|
}, function ($query) { |
60
|
|
|
return $query->latest()->limit(120); |
61
|
|
|
})->get(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get the results for a specific query. |
66
|
|
|
* |
67
|
|
|
* @param callable $processQuery |
68
|
|
|
* @param callable $processCollection |
69
|
|
|
* @return array |
70
|
|
|
*/ |
71
|
|
|
public function getRecords(callable $processQuery = null, callable $processCollection = null): array |
72
|
|
|
{ |
73
|
|
|
return $this->getRawRecords($processQuery) |
74
|
|
|
->when(! is_null($processCollection), function ($collection) use ($processCollection) { |
75
|
|
|
return call_user_func($processCollection, $collection); |
76
|
|
|
}) |
77
|
|
|
->map(function (Model $statistic) { |
78
|
|
|
return [ |
79
|
|
|
'timestamp' => (string) $statistic->created_at, |
80
|
|
|
'peak_connections_count' => $statistic->peak_connections_count, |
81
|
|
|
'websocket_messages_count' => $statistic->websocket_messages_count, |
82
|
|
|
'api_messages_count' => $statistic->api_messages_count, |
83
|
|
|
]; |
84
|
|
|
}) |
85
|
|
|
->toArray(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get the results for a specific query into a |
90
|
|
|
* format that is easily to read for graphs. |
91
|
|
|
* |
92
|
|
|
* @param callable $processQuery |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
|
|
public function getForGraph(callable $processQuery = null): array |
96
|
|
|
{ |
97
|
|
|
$statistics = collect( |
98
|
|
|
$this->getRecords($processQuery) |
99
|
|
|
); |
100
|
|
|
|
101
|
|
|
return [ |
102
|
|
|
'peak_connections' => [ |
103
|
|
|
'x' => $statistics->pluck('timestamp')->toArray(), |
104
|
|
|
'y' => $statistics->pluck('peak_connections_count')->toArray(), |
105
|
|
|
], |
106
|
|
|
'websocket_messages_count' => [ |
107
|
|
|
'x' => $statistics->pluck('timestamp')->toArray(), |
108
|
|
|
'y' => $statistics->pluck('websocket_messages_count')->toArray(), |
109
|
|
|
], |
110
|
|
|
'api_messages_count' => [ |
111
|
|
|
'x' => $statistics->pluck('timestamp')->toArray(), |
112
|
|
|
'y' => $statistics->pluck('api_messages_count')->toArray(), |
113
|
|
|
], |
114
|
|
|
]; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.