Skip to content
This repository was archived by the owner on Feb 7, 2024. It is now read-only.

Commit 90b2f3e

Browse files
committed
Added helper methods for extending the store
1 parent 0996f6e commit 90b2f3e

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

src/Statistics/Stores/DatabaseStore.php

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use BeyondCode\LaravelWebSockets\Contracts\StatisticsStore;
66
use Carbon\Carbon;
77
use Illuminate\Database\Eloquent\Model;
8+
use Illuminate\Support\Collection;
89

910
class 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

Comments
 (0)