|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Gos\Bundle\WebSocketBundle\DataCollector; |
| 4 | + |
| 5 | +use Symfony\Component\HttpFoundation\Request; |
| 6 | +use Symfony\Component\HttpFoundation\Response; |
| 7 | +use Symfony\Component\HttpKernel\DataCollector\DataCollector; |
| 8 | +use Symfony\Component\Stopwatch\Stopwatch; |
| 9 | +use Symfony\Component\Stopwatch\StopwatchEvent; |
| 10 | + |
| 11 | +class WebsocketDataCollector extends DataCollector |
| 12 | +{ |
| 13 | + /** @var array */ |
| 14 | + protected $durations; |
| 15 | + |
| 16 | + /** @var [] */ |
| 17 | + protected $count; |
| 18 | + |
| 19 | + /** @var [] */ |
| 20 | + protected $rawData; |
| 21 | + |
| 22 | + public function __construct() |
| 23 | + { |
| 24 | + $this->rawData = []; |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Collects data for the given Request and Response. |
| 29 | + * |
| 30 | + * @param Request $request A Request instance |
| 31 | + * @param Response $response A Response instance |
| 32 | + * @param \Exception $exception An Exception instance |
| 33 | + * |
| 34 | + * @api |
| 35 | + */ |
| 36 | + public function collect(Request $request, Response $response, \Exception $exception = null) |
| 37 | + { |
| 38 | + $pusherDuration = []; |
| 39 | + $pusherCount = []; |
| 40 | + $durationTotal = 0; |
| 41 | + $totalPush = 0; |
| 42 | + |
| 43 | + foreach($this->rawData as $pusherName => $durations) { |
| 44 | + if(!isset($pusherCount[$pusherName])){ |
| 45 | + $pusherCount[$pusherName] = 0; |
| 46 | + } |
| 47 | + |
| 48 | + $pusherDurationTotal = array_sum($durations); |
| 49 | + $pusherCount[$pusherName] += $count = count($durations); |
| 50 | + $totalPush += $count; |
| 51 | + $pusherDuration[$pusherName] = $pusherDurationTotal; |
| 52 | + $durationTotal += $pusherDurationTotal; |
| 53 | + } |
| 54 | + |
| 55 | + $this->data = [ |
| 56 | + 'pusher_counts' => $pusherCount, |
| 57 | + 'push_total' => $totalPush, |
| 58 | + 'durations' => $pusherDuration, |
| 59 | + 'duration_total' => $durationTotal |
| 60 | + ]; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * @return int[] |
| 65 | + */ |
| 66 | + public function getPusherCounts() |
| 67 | + { |
| 68 | + return $this->data['pusher_counts']; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * @return float |
| 73 | + */ |
| 74 | + public function getTotalDuration() |
| 75 | + { |
| 76 | + return $this->data['duration_total']; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * @return float[] |
| 81 | + */ |
| 82 | + public function getDurations() |
| 83 | + { |
| 84 | + return $this->data['durations']; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * @return int |
| 89 | + */ |
| 90 | + public function getPushTotal() |
| 91 | + { |
| 92 | + return $this->data['push_total']; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * @param StopwatchEvent $event |
| 97 | + * @param $pusherName |
| 98 | + */ |
| 99 | + public function collectData(StopwatchEvent $event, $pusherName) |
| 100 | + { |
| 101 | + if(!isset($this->rawData[$pusherName])){ |
| 102 | + $this->rawData[$pusherName] = []; |
| 103 | + } |
| 104 | + |
| 105 | + $this->rawData[$pusherName][] = $event->getDuration(); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Returns the name of the collector. |
| 110 | + * |
| 111 | + * @return string The collector name |
| 112 | + * |
| 113 | + * @api |
| 114 | + */ |
| 115 | + public function getName() |
| 116 | + { |
| 117 | + return 'websocket'; |
| 118 | + } |
| 119 | +} |
0 commit comments