laravel-websockets/src/Dashboard/Http/Controllers/DashboardApiController.php

47 lines
1.4 KiB
PHP
Raw Normal View History

2018-12-03 21:59:50 +00:00
<?php
namespace BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers;
class DashboardApiController
{
2020-08-18 17:21:22 +00:00
/**
* Get statistics for an app ID.
*
* @param mixed $appId
* @return \Illuminate\Http\Response
*/
2018-12-03 21:59:50 +00:00
public function getStatistics($appId)
{
2020-08-18 17:21:22 +00:00
$model = config('websockets.statistics.model');
$statistics = $model::where('app_id', $appId)
->latest()
->limit(120)
->get();
2018-12-03 21:59:50 +00:00
2018-12-04 08:03:27 +00:00
$statisticData = $statistics->map(function ($statistic) {
2018-12-03 21:59:50 +00:00
return [
2018-12-04 21:22:33 +00:00
'timestamp' => (string) $statistic->created_at,
2018-12-04 08:03:27 +00:00
'peak_connection_count' => $statistic->peak_connection_count,
'websocket_message_count' => $statistic->websocket_message_count,
'api_message_count' => $statistic->api_message_count,
2018-12-03 21:59:50 +00:00
];
2018-12-04 00:03:47 +00:00
})->reverse();
2018-12-03 21:59:50 +00:00
return [
2018-12-04 00:03:47 +00:00
'peak_connections' => [
2018-12-04 08:03:27 +00:00
'x' => $statisticData->pluck('timestamp'),
'y' => $statisticData->pluck('peak_connection_count'),
],
'websocket_message_count' => [
'x' => $statisticData->pluck('timestamp'),
'y' => $statisticData->pluck('websocket_message_count'),
],
'api_message_count' => [
'x' => $statisticData->pluck('timestamp'),
'y' => $statisticData->pluck('api_message_count'),
2018-12-04 21:22:33 +00:00
],
2018-12-03 21:59:50 +00:00
];
}
2018-12-04 21:22:33 +00:00
}