beyondcode /
laravel-websockets
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace BeyondCode\LaravelWebSockets\Statistics\Collectors; |
||||
| 4 | |||||
| 5 | use BeyondCode\LaravelWebSockets\Contracts\ChannelManager; |
||||
| 6 | use BeyondCode\LaravelWebSockets\Contracts\StatisticsCollector; |
||||
| 7 | use BeyondCode\LaravelWebSockets\Facades\StatisticsStore; |
||||
| 8 | use BeyondCode\LaravelWebSockets\Helpers; |
||||
| 9 | use BeyondCode\LaravelWebSockets\Statistics\Statistic; |
||||
| 10 | use React\Promise\PromiseInterface; |
||||
| 11 | |||||
| 12 | class MemoryCollector implements StatisticsCollector |
||||
| 13 | { |
||||
| 14 | /** |
||||
| 15 | * The list of stored statistics. |
||||
| 16 | * |
||||
| 17 | * @var array |
||||
| 18 | */ |
||||
| 19 | protected $statistics = []; |
||||
| 20 | |||||
| 21 | /** |
||||
| 22 | * The Channel manager. |
||||
| 23 | * |
||||
| 24 | * @var \BeyondCode\LaravelWebSockets\Contracts\ChannelManager |
||||
| 25 | */ |
||||
| 26 | protected $channelManager; |
||||
| 27 | |||||
| 28 | /** |
||||
| 29 | * Initialize the logger. |
||||
| 30 | * |
||||
| 31 | * @return void |
||||
| 32 | */ |
||||
| 33 | public function __construct() |
||||
| 34 | { |
||||
| 35 | $this->channelManager = app(ChannelManager::class); |
||||
| 36 | } |
||||
| 37 | |||||
| 38 | /** |
||||
| 39 | * Handle the incoming websocket message. |
||||
| 40 | * |
||||
| 41 | * @param string|int $appId |
||||
| 42 | * @return void |
||||
| 43 | */ |
||||
| 44 | public function webSocketMessage($appId) |
||||
| 45 | { |
||||
| 46 | $this->findOrMake($appId) |
||||
| 47 | ->webSocketMessage(); |
||||
| 48 | } |
||||
| 49 | |||||
| 50 | /** |
||||
| 51 | * Handle the incoming API message. |
||||
| 52 | * |
||||
| 53 | * @param string|int $appId |
||||
| 54 | * @return void |
||||
| 55 | */ |
||||
| 56 | public function apiMessage($appId) |
||||
| 57 | { |
||||
| 58 | $this->findOrMake($appId) |
||||
| 59 | ->apiMessage(); |
||||
| 60 | } |
||||
| 61 | |||||
| 62 | /** |
||||
| 63 | * Handle the new conection. |
||||
| 64 | * |
||||
| 65 | * @param string|int $appId |
||||
| 66 | * @return void |
||||
| 67 | */ |
||||
| 68 | public function connection($appId) |
||||
| 69 | { |
||||
| 70 | $this->findOrMake($appId) |
||||
| 71 | ->connection(); |
||||
| 72 | } |
||||
| 73 | |||||
| 74 | /** |
||||
| 75 | * Handle disconnections. |
||||
| 76 | * |
||||
| 77 | * @param string|int $appId |
||||
| 78 | * @return void |
||||
| 79 | */ |
||||
| 80 | public function disconnection($appId) |
||||
| 81 | { |
||||
| 82 | $this->findOrMake($appId) |
||||
| 83 | ->disconnection(); |
||||
| 84 | } |
||||
| 85 | |||||
| 86 | /** |
||||
| 87 | * Save all the stored statistics. |
||||
| 88 | * |
||||
| 89 | * @return void |
||||
| 90 | */ |
||||
| 91 | public function save() |
||||
| 92 | { |
||||
| 93 | $this->getStatistics()->then(function ($statistics) { |
||||
| 94 | foreach ($statistics as $appId => $statistic) { |
||||
| 95 | if (! $statistic->isEnabled()) { |
||||
| 96 | continue; |
||||
| 97 | } |
||||
| 98 | |||||
| 99 | if ($statistic->shouldHaveTracesRemoved()) { |
||||
| 100 | $this->resetAppTraces($appId); |
||||
| 101 | |||||
| 102 | continue; |
||||
| 103 | } |
||||
| 104 | |||||
| 105 | $this->createRecord($statistic, $appId); |
||||
| 106 | |||||
| 107 | $this->channelManager |
||||
| 108 | ->getGlobalConnectionsCount($appId) |
||||
| 109 | ->then(function ($connections) use ($statistic) { |
||||
| 110 | $statistic->reset( |
||||
| 111 | is_null($connections) ? 0 : $connections |
||||
| 112 | ); |
||||
| 113 | }); |
||||
| 114 | } |
||||
| 115 | }); |
||||
| 116 | } |
||||
| 117 | |||||
| 118 | /** |
||||
| 119 | * Flush the stored statistics. |
||||
| 120 | * |
||||
| 121 | * @return void |
||||
| 122 | */ |
||||
| 123 | public function flush() |
||||
| 124 | { |
||||
| 125 | $this->statistics = []; |
||||
| 126 | } |
||||
| 127 | |||||
| 128 | /** |
||||
| 129 | * Get the saved statistics. |
||||
| 130 | * |
||||
| 131 | * @return PromiseInterface[array] |
||||
|
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||||
| 132 | */ |
||||
| 133 | public function getStatistics(): PromiseInterface |
||||
| 134 | { |
||||
| 135 | return Helpers::createFulfilledPromise($this->statistics); |
||||
| 136 | } |
||||
| 137 | |||||
| 138 | /** |
||||
| 139 | * Get the saved statistics for an app. |
||||
| 140 | * |
||||
| 141 | * @param string|int $appId |
||||
| 142 | * @return PromiseInterface[\BeyondCode\LaravelWebSockets\Statistics\Statistic|null] |
||||
|
0 ignored issues
–
show
|
|||||
| 143 | */ |
||||
| 144 | public function getAppStatistics($appId): PromiseInterface |
||||
| 145 | { |
||||
| 146 | return Helpers::createFulfilledPromise( |
||||
| 147 | $this->statistics[$appId] ?? null |
||||
| 148 | ); |
||||
| 149 | } |
||||
| 150 | |||||
| 151 | /** |
||||
| 152 | * Remove all app traces from the database if no connections have been set |
||||
| 153 | * in the meanwhile since last save. |
||||
| 154 | * |
||||
| 155 | * @param string|int $appId |
||||
| 156 | * @return void |
||||
| 157 | */ |
||||
| 158 | public function resetAppTraces($appId) |
||||
| 159 | { |
||||
| 160 | unset($this->statistics[$appId]); |
||||
| 161 | } |
||||
| 162 | |||||
| 163 | /** |
||||
| 164 | * Find or create a defined statistic for an app. |
||||
| 165 | * |
||||
| 166 | * @param string|int $appId |
||||
| 167 | * @return \BeyondCode\LaravelWebSockets\Statistics\Statistic |
||||
| 168 | */ |
||||
| 169 | protected function findOrMake($appId): Statistic |
||||
| 170 | { |
||||
| 171 | if (! isset($this->statistics[$appId])) { |
||||
| 172 | $this->statistics[$appId] = Statistic::new($appId); |
||||
| 173 | } |
||||
| 174 | |||||
| 175 | return $this->statistics[$appId]; |
||||
| 176 | } |
||||
| 177 | |||||
| 178 | /** |
||||
| 179 | * Create a new record using the Statistic Store. |
||||
| 180 | * |
||||
| 181 | * @param \BeyondCode\LaravelWebSockets\Statistics\Statistic $statistic |
||||
| 182 | * @param mixed $appId |
||||
| 183 | * @return void |
||||
| 184 | */ |
||||
| 185 | public function createRecord(Statistic $statistic, $appId) |
||||
|
0 ignored issues
–
show
The parameter
$appId is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||
| 186 | { |
||||
| 187 | StatisticsStore::store($statistic->toArray()); |
||||
| 188 | } |
||||
| 189 | } |
||||
| 190 |