TimelinePanel.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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\debug\panels;
  8. use Yii;
  9. use yii\debug\Panel;
  10. use yii\debug\models\search\Timeline;
  11. use yii\base\InvalidConfigException;
  12. /**
  13. * Debugger panel that collects and displays timeline data.
  14. *
  15. * @property array $colors color indicators
  16. * @property float $duration request duration, milliseconds. This property is read-only.
  17. * @property float $start timestamp of starting request. This property is read-only.
  18. *
  19. * @author Dmitriy Bashkarev <dmitriy@bashkarev.com>
  20. * @since 2.0.7
  21. */
  22. class TimelinePanel extends Panel
  23. {
  24. /**
  25. * @var array Color indicators item profile.
  26. *
  27. * - keys: percentages of time request
  28. * - values: hex color
  29. */
  30. private $_colors = [
  31. 20 => '#1e6823',
  32. 10 => '#44a340',
  33. 1 => '#8cc665'
  34. ];
  35. /**
  36. * @var array log messages extracted to array as models, to use with data provider.
  37. */
  38. private $_models;
  39. /**
  40. * @var float Start request, timestamp (obtained by microtime(true))
  41. */
  42. private $_start;
  43. /**
  44. * @var float End request, timestamp (obtained by microtime(true))
  45. */
  46. private $_end;
  47. /**
  48. * @var float Request duration, milliseconds
  49. */
  50. private $_duration;
  51. /**
  52. * @inheritdoc
  53. */
  54. public function init()
  55. {
  56. if (!isset($this->module->panels['profiling'])) {
  57. throw new InvalidConfigException('Unable to determine the profiling panel');
  58. }
  59. parent::init();
  60. }
  61. /**
  62. * @inheritdoc
  63. */
  64. public function getName()
  65. {
  66. return 'Timeline';
  67. }
  68. /**
  69. * @inheritdoc
  70. */
  71. public function getDetail()
  72. {
  73. $searchModel = new Timeline();
  74. $dataProvider = $searchModel->search(Yii::$app->request->getQueryParams(), $this);
  75. return Yii::$app->view->render('panels/timeline/detail', [
  76. 'panel' => $this,
  77. 'dataProvider' => $dataProvider,
  78. 'searchModel' => $searchModel,
  79. ]);
  80. }
  81. /**
  82. * @inheritdoc
  83. */
  84. public function load($data)
  85. {
  86. if (!isset($data['start']) || empty($data['start'])) {
  87. throw new \RuntimeException('Unable to determine request start time');
  88. }
  89. $this->_start = $data['start'] * 1000;
  90. if (!isset($data['end']) || empty($data['end'])) {
  91. throw new \RuntimeException('Unable to determine request end time');
  92. }
  93. $this->_end = $data['end'] * 1000;
  94. if (isset($this->module->panels['profiling']->data['time'])) {
  95. $this->_duration = $this->module->panels['profiling']->data['time'] * 1000;
  96. } else {
  97. $this->_duration = $this->_end - $this->_start;
  98. }
  99. if ($this->_duration <= 0) {
  100. throw new \RuntimeException('Duration cannot be zero');
  101. }
  102. }
  103. /**
  104. * @inheritdoc
  105. */
  106. public function save()
  107. {
  108. return [
  109. 'start' => YII_BEGIN_TIME,
  110. 'end' => microtime(true),
  111. ];
  112. }
  113. /**
  114. * Sets color indicators.
  115. * key: percentages of time request, value: hex color
  116. * @param array $colors
  117. */
  118. public function setColors($colors)
  119. {
  120. krsort($colors);
  121. $this->_colors = $colors;
  122. }
  123. /**
  124. * Color indicators item profile,
  125. * key: percentages of time request, value: hex color
  126. * @return array
  127. */
  128. public function getColors()
  129. {
  130. return $this->_colors;
  131. }
  132. /**
  133. * Start request, timestamp (obtained by microtime(true))
  134. * @return float
  135. */
  136. public function getStart()
  137. {
  138. return $this->_start;
  139. }
  140. /**
  141. * Request duration, milliseconds
  142. * @return float
  143. */
  144. public function getDuration()
  145. {
  146. return $this->_duration;
  147. }
  148. /**
  149. * Returns an array of models that represents logs of the current request.
  150. * Can be used with data providers, such as \yii\data\ArrayDataProvider.
  151. *
  152. * @param bool $refresh if need to build models from log messages and refresh them.
  153. * @return array models
  154. */
  155. protected function getModels($refresh = false)
  156. {
  157. if ($this->_models === null || $refresh) {
  158. $this->_models = [];
  159. if (isset($this->module->panels['profiling']->data['messages'])) {
  160. $this->_models = Yii::getLogger()->calculateTimings($this->module->panels['profiling']->data['messages']);
  161. }
  162. }
  163. return $this->_models;
  164. }
  165. }