55use BeyondCode \LaravelWebSockets \Contracts \StatisticsStore ;
66use Carbon \Carbon ;
77use Illuminate \Database \Eloquent \Model ;
8+ use Illuminate \Support \Collection ;
89
910class DatabaseStore implements StatisticsStore
1011{
@@ -75,12 +76,7 @@ public function getRecords(callable $processQuery = null, callable $processColle
7576 return call_user_func ($ processCollection , $ collection );
7677 })
7778 ->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 );
8480 })
8581 ->toArray ();
8682 }
@@ -98,6 +94,33 @@ public function getForGraph(callable $processQuery = null): array
9894 $ this ->getRecords ($ processQuery )
9995 );
10096
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+ {
101124 return [
102125 'peak_connections ' => [
103126 'x ' => $ statistics ->pluck ('timestamp ' )->toArray (),
0 commit comments