HttpClientPanel.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\httpclient\debug;
  8. use yii\debug\Panel;
  9. use yii\di\Instance;
  10. use yii\httpclient\Client;
  11. use yii\log\Logger;
  12. use Yii;
  13. /**
  14. * Debugger panel that collects and displays HTTP requests performed.
  15. *
  16. * @property \yii\httpclient\Client $httpClient Note that the type of this property differs in getter and
  17. * setter. See [[getHttpClient()]] and [[setHttpClient()]] for details.
  18. * @property array $methods This property is read-only.
  19. * @property array $types This property is read-only.
  20. *
  21. * @author Paul Klimov <klimov.paul@gmail.com>
  22. * @since 2.0
  23. */
  24. class HttpClientPanel extends Panel
  25. {
  26. /**
  27. * @var array current HTTP request timings
  28. */
  29. private $_timings;
  30. /**
  31. * @var array HTTP requests info extracted to array as models, to use with data provider.
  32. */
  33. private $_models;
  34. /**
  35. * @var \yii\httpclient\Client|array|string
  36. */
  37. private $_httpClient = 'yii\httpclient\Client';
  38. /**
  39. * @param array $httpClient
  40. */
  41. public function setHttpClient($httpClient)
  42. {
  43. $this->_httpClient = $httpClient;
  44. }
  45. /**
  46. * @return \yii\httpclient\Client
  47. */
  48. public function getHttpClient()
  49. {
  50. if (!is_object($this->_httpClient)) {
  51. $this->_httpClient = Instance::ensure($this->_httpClient, Client::className());
  52. }
  53. return $this->_httpClient;
  54. }
  55. /**
  56. * @inheritdoc
  57. */
  58. public function init()
  59. {
  60. $this->actions['request-execute'] = [
  61. 'class' => 'yii\httpclient\debug\RequestExecuteAction',
  62. 'panel' => $this,
  63. ];
  64. }
  65. /**
  66. * @inheritdoc
  67. */
  68. public function getName()
  69. {
  70. return 'HTTP Client';
  71. }
  72. /**
  73. * @inheritdoc
  74. */
  75. public function getSummary()
  76. {
  77. $timings = $this->calculateTimings();
  78. $queryCount = count($timings);
  79. if ($queryCount === 0) {
  80. return '';
  81. }
  82. $queryTime = number_format($this->getTotalRequestTime($timings) * 1000) . ' ms';
  83. return Yii::$app->view->render('@yii/httpclient/debug/views/summary', [
  84. 'timings' => $this->calculateTimings(),
  85. 'panel' => $this,
  86. 'queryCount' => $queryCount,
  87. 'queryTime' => $queryTime,
  88. ]);
  89. }
  90. /**
  91. * @inheritdoc
  92. */
  93. public function getDetail()
  94. {
  95. $searchModel = new SearchModel();
  96. $dataProvider = $searchModel->search(Yii::$app->request->getQueryParams(), $this->getModels());
  97. return Yii::$app->view->render('@yii/httpclient/debug/views/detail', [
  98. 'panel' => $this,
  99. 'dataProvider' => $dataProvider,
  100. 'searchModel' => $searchModel,
  101. ]);
  102. }
  103. /**
  104. * Calculates given request profile timings.
  105. *
  106. * @return array timings [token, category, timestamp, traces, nesting level, elapsed time]
  107. */
  108. public function calculateTimings()
  109. {
  110. if ($this->_timings === null) {
  111. $this->_timings = Yii::getLogger()->calculateTimings(isset($this->data['messages']) ? $this->data['messages'] : []);
  112. }
  113. return $this->_timings;
  114. }
  115. /**
  116. * @inheritdoc
  117. */
  118. public function save()
  119. {
  120. $target = $this->module->logTarget;
  121. $messages = $target->filterMessages($target->messages, Logger::LEVEL_PROFILE, [
  122. 'yii\httpclient\Transport::*',
  123. 'yii\httpclient\CurlTransport::*',
  124. 'yii\httpclient\StreamTransport::*',
  125. ]);
  126. return ['messages' => $messages];
  127. }
  128. /**
  129. * Returns an array of models that represents logs of the current request.
  130. * Can be used with data providers such as \yii\data\ArrayDataProvider.
  131. * @return array models
  132. */
  133. protected function getModels()
  134. {
  135. if ($this->_models === null) {
  136. $this->_models = [];
  137. $timings = $this->calculateTimings();
  138. foreach ($timings as $seq => $dbTiming) {
  139. $this->_models[] = [
  140. 'method' => $this->getRequestMethod($dbTiming['info']),
  141. 'type' => $this->getRequestType($dbTiming['category']),
  142. 'request' => $dbTiming['info'],
  143. 'duration' => ($dbTiming['duration'] * 1000), // in milliseconds
  144. 'trace' => $dbTiming['trace'],
  145. 'timestamp' => ($dbTiming['timestamp'] * 1000), // in milliseconds
  146. 'seq' => $seq,
  147. ];
  148. }
  149. }
  150. return $this->_models;
  151. }
  152. /**
  153. * Returns HTTP request method.
  154. *
  155. * @param string $timing timing procedure string
  156. * @return string request method such as GET, POST, PUT, etc.
  157. */
  158. protected function getRequestMethod($timing)
  159. {
  160. $timing = ltrim($timing);
  161. preg_match('/^([a-zA-z]*)/', $timing, $matches);
  162. return count($matches) ? $matches[0] : '';
  163. }
  164. /**
  165. * Returns request type.
  166. *
  167. * @param string $category
  168. * @return string request type such as 'normal', 'batch'
  169. */
  170. protected function getRequestType($category)
  171. {
  172. return (stripos($category, '::batchSend') === false) ? 'normal' : 'batch';
  173. }
  174. /**
  175. * Returns total request time.
  176. *
  177. * @param array $timings
  178. * @return int total time
  179. */
  180. protected function getTotalRequestTime($timings)
  181. {
  182. $queryTime = 0;
  183. foreach ($timings as $timing) {
  184. $queryTime += $timing['duration'];
  185. }
  186. return $queryTime;
  187. }
  188. /**
  189. * Returns array request methods
  190. *
  191. * @return array
  192. */
  193. public function getMethods()
  194. {
  195. return array_reduce(
  196. $this->_models,
  197. function ($result, $item) {
  198. $result[$item['method']] = $item['method'];
  199. return $result;
  200. },
  201. []
  202. );
  203. }
  204. /**
  205. * Returns array request types
  206. *
  207. * @return array
  208. */
  209. public function getTypes()
  210. {
  211. return [
  212. 'normal' => 'Normal',
  213. 'batch' => 'Batch',
  214. ];
  215. }
  216. }