2e86c939
xu
“首次提交”
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\debug\models\timeline;
use yii\data\ArrayDataProvider;
use yii\debug\panels\TimelinePanel;
/**
* DataProvider implements a data provider based on a data array.
*
* @property array $rulers This property is read-only.
*
* @author Dmitriy Bashkarev <dmitriy@bashkarev.com>
* @since 2.0.8
*/
class DataProvider extends ArrayDataProvider
{
/**
* @var TimelinePanel
*/
protected $panel;
/**
* DataProvider constructor.
* @param TimelinePanel $panel
* @param array $config
*/
public function __construct(TimelinePanel $panel, $config = [])
{
$this->panel = $panel;
parent::__construct($config);
}
/**
* @inheritdoc
*/
protected function prepareModels()
{
if (($models = $this->allModels) === null) {
return [];
}
$child = [];
foreach ($models as $key => &$model) {
$model['timestamp'] *= 1000;
$model['duration'] *= 1000;
$model['child'] = 0;
$model['css']['width'] = $this->getWidth($model);
$model['css']['left'] = $this->getLeft($model);
$model['css']['color'] = $this->getColor($model);
foreach ($child as $id => $timestamp) {
if ($timestamp > $model['timestamp']) {
++$models[$id]['child'];
} else {
unset($child[$id]);
}
}
$child[$key] = $model['timestamp'] + $model['duration'];
}
return $models;
}
/**
* Getting HEX color based on model duration
* @param array $model
* @return string
*/
public function getColor($model)
{
$width = isset($model['css']['width']) ? $model['css']['width'] : $this->getWidth($model);
foreach ($this->panel->colors as $percent => $color) {
if ($width >= $percent) {
return $color;
}
}
return '#d6e685';
}
/**
* Returns the offset left item, percentage of the total width
* @param array $model
* @return float
*/
public function getLeft($model)
{
return $this->getTime($model) / ($this->panel->duration / 100);
}
/**
* Returns item duration, milliseconds
* @param array $model
* @return float
*/
public function getTime($model)
{
return $model['timestamp'] - $this->panel->start;
}
/**
* Returns item width percent of the total width
* @param array $model
* @return float
*/
public function getWidth($model)
{
return $model['duration'] / ($this->panel->duration / 100);
}
/**
* Returns item, css class
* @param array $model
* @return string
*/
public function getCssClass($model)
{
$class = 'time';
$class .= (($model['css']['left'] > 15) && ($model['css']['left'] + $model['css']['width'] > 50)) ? ' right' : ' left';
return $class;
}
/**
* ruler items, key milliseconds, value offset left
* @param int $line number of columns
* @return array
*/
public function getRulers($line = 10)
{
if ($line == 0) {
return [];
}
$data = [0];
$percent = ($this->panel->duration / 100);
$row = $this->panel->duration / $line;
$precision = $row > 100 ? -2 : -1;
for ($i = 1; $i < $line; $i++) {
$ms = round($i * $row, $precision);
$data[$ms] = $ms / $percent;
}
return $data;
}
/**
* ```php
* [
* 0 => string, memory usage (MB)
* 1 => float, Y position (percent)
* ]
* @param array $model
* @return array|null
*/
public function getMemory($model)
{
if (empty($model['memory'])) {
return null;
}
return [
sprintf('%.2f MB', $model['memory'] / 1048576),
$model['memory'] / ($this->panel->memory / 100)
];
}
}
|