5
5
use BeyondCode \LaravelWebSockets \Contracts \StatisticsStore ;
6
6
use Carbon \Carbon ;
7
7
use Illuminate \Database \Eloquent \Model ;
8
+ use Illuminate \Support \Collection ;
8
9
9
10
class DatabaseStore implements StatisticsStore
10
11
{
@@ -75,12 +76,7 @@ public function getRecords(callable $processQuery = null, callable $processColle
75
76
return call_user_func ($ processCollection , $ collection );
76
77
})
77
78
->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
- ];
79
+ return $ this ->statisticToArray ($ statistic );
84
80
})
85
81
->toArray ();
86
82
}
@@ -98,6 +94,33 @@ public function getForGraph(callable $processQuery = null): array
98
94
$ this ->getRecords ($ processQuery )
99
95
);
100
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
+ {
101
124
return [
102
125
'peak_connections ' => [
103
126
'x ' => $ statistics ->pluck ('timestamp ' )->toArray (),
0 commit comments