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