From 90b2f3ebc21ab53cae97e30bb5207037db889824 Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Fri, 11 Sep 2020 15:48:03 +0300 Subject: [PATCH] Added helper methods for extending the store --- src/Statistics/Stores/DatabaseStore.php | 35 ++++++++++++++++++++----- 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/src/Statistics/Stores/DatabaseStore.php b/src/Statistics/Stores/DatabaseStore.php index 0de27bd..2a36529 100644 --- a/src/Statistics/Stores/DatabaseStore.php +++ b/src/Statistics/Stores/DatabaseStore.php @@ -5,6 +5,7 @@ namespace BeyondCode\LaravelWebSockets\Statistics\Stores; use BeyondCode\LaravelWebSockets\Contracts\StatisticsStore; use Carbon\Carbon; use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\Collection; class DatabaseStore implements StatisticsStore { @@ -75,12 +76,7 @@ class DatabaseStore implements StatisticsStore return call_user_func($processCollection, $collection); }) ->map(function (Model $statistic) { - return [ - 'timestamp' => (string) $statistic->created_at, - 'peak_connections_count' => $statistic->peak_connections_count, - 'websocket_messages_count' => $statistic->websocket_messages_count, - 'api_messages_count' => $statistic->api_messages_count, - ]; + return $this->statisticToArray($statistic); }) ->toArray(); } @@ -98,6 +94,33 @@ class DatabaseStore implements StatisticsStore $this->getRecords($processQuery) ); + return $this->statisticsToGraph($statistics); + } + + /** + * Turn the statistic model to an array. + * + * @param \Illuminate\Database\Eloquent\Model $statistic + * @return array + */ + protected function statisticToArray(Model $statistic): array + { + return [ + 'timestamp' => (string) $statistic->created_at, + 'peak_connections_count' => $statistic->peak_connections_count, + 'websocket_messages_count' => $statistic->websocket_messages_count, + 'api_messages_count' => $statistic->api_messages_count, + ]; + } + + /** + * Turn the statistics collection to an array used for graph. + * + * @param \Illuminate\Support\Collection $statistics + * @return array + */ + protected function statisticsToGraph(Collection $statistics): array + { return [ 'peak_connections' => [ 'x' => $statistics->pluck('timestamp')->toArray(),