1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Contains logic for getting/creating/removing profile records. |
4
|
|
|
*/ |
5
|
|
|
class Xhgui_Profiles |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var Xhgui_StorageInterface |
9
|
|
|
*/ |
10
|
|
|
protected $storage; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Xhgui_Profiles constructor. |
14
|
|
|
* @param Xhgui_StorageInterface $storage |
15
|
|
|
*/ |
16
|
|
|
public function __construct(\Xhgui_StorageInterface $storage) |
17
|
|
|
{ |
18
|
|
|
$this->storage = $storage; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param $conditions |
23
|
|
|
* @param null $fields |
24
|
|
|
* @return mixed |
25
|
|
|
*/ |
26
|
|
|
public function query($conditions, $fields = null) |
27
|
|
|
{ |
28
|
|
|
return $this->storage->find($conditions, $fields); |
|
|
|
|
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Get a single profile run by id. |
33
|
|
|
* |
34
|
|
|
* @param string $id The id of the profile to get. |
35
|
|
|
* @return Xhgui_Profile |
36
|
|
|
* @throws Exception |
37
|
|
|
*/ |
38
|
|
|
public function get($id) |
39
|
|
|
{ |
40
|
|
|
return $this->wrap($this->storage->findOne($id)); |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Get the list of profiles for a simplified url. |
45
|
|
|
* |
46
|
|
|
* @param string $url The url to load profiles for. |
47
|
|
|
* @param array $options Pagination options to use. |
48
|
|
|
* @param array $conditions The search options. |
49
|
|
|
* @return MongoCursor |
50
|
|
|
*/ |
51
|
|
|
public function getForUrl($url, $options, $conditions = array()) |
52
|
|
|
{ |
53
|
|
|
$conditions = array_merge( |
54
|
|
|
(array)$conditions, |
55
|
|
|
array('simple_url' => $url) |
56
|
|
|
); |
57
|
|
|
$options = array_merge($options, array( |
58
|
|
|
'conditions' => $conditions, |
59
|
|
|
)); |
60
|
|
|
return $this->paginate($options); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param Xhgui_Storage_Filter $filter |
65
|
|
|
* @return array |
66
|
|
|
* @throws Exception |
67
|
|
|
*/ |
68
|
|
|
public function paginate(Xhgui_Storage_Filter $filter) |
69
|
|
|
{ |
70
|
|
|
$projection = false; |
71
|
|
|
|
72
|
|
|
if ($projection === false) { |
73
|
|
|
$result = $this->storage->find($filter, null); |
|
|
|
|
74
|
|
|
} else { |
75
|
|
|
$result = $this->storage->find($filter, $projection); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$totalRows = $this->storage->count($filter); |
79
|
|
|
$totalPages = max(ceil($totalRows / $filter->getPerPage()), 1); |
80
|
|
|
|
81
|
|
|
return array( |
82
|
|
|
'results' => $this->wrap($result), |
83
|
|
|
'sort' => $filter->getSort(), |
84
|
|
|
'direction' => $filter->getDirection(), |
85
|
|
|
'page' => $filter->getPage(), |
86
|
|
|
'perPage' => $filter->getPerPage(), |
87
|
|
|
'totalPages' => $totalPages |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get the Percentile metrics for a URL |
93
|
|
|
* |
94
|
|
|
* This will group data by date and returns only the |
95
|
|
|
* percentile + date, making the data ideal for time series graphs |
96
|
|
|
* |
97
|
|
|
* @param integer $percentile The percentile you want. e.g. 90. |
98
|
|
|
* @param string $url |
99
|
|
|
* @param array $search Search options containing startDate and or endDate |
|
|
|
|
100
|
|
|
* @return array Array of metrics grouped by date |
101
|
|
|
*/ |
102
|
|
|
public function getPercentileForUrl($percentile, $url, $filter) |
|
|
|
|
103
|
|
|
{ |
104
|
|
|
$col = '$meta.request_date'; |
105
|
|
|
|
106
|
|
|
$results = $this->storage->aggregate($filter, $col, $percentile); |
107
|
|
|
if (empty($results['result'])) { |
108
|
|
|
return array(); |
109
|
|
|
} |
110
|
|
|
$keys = array( |
111
|
|
|
'wall_times' => 'wt', |
112
|
|
|
'cpu_times' => 'cpu', |
113
|
|
|
'mu_times' => 'mu', |
114
|
|
|
'pmu_times' => 'pmu' |
115
|
|
|
); |
116
|
|
|
foreach ($results['result'] as &$result) { |
117
|
|
|
if ($result['_id'] instanceof MongoDate) { |
118
|
|
|
$result['date'] = date('Y-m-d H:i:s', $result['_id']->sec); |
119
|
|
|
} else { |
120
|
|
|
$result['date'] = $result['_id']; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
unset($result['_id']); |
124
|
|
|
$index = max(round($result['raw_index']) - 1, 0); |
125
|
|
|
foreach ($keys as $key => $out) { |
126
|
|
|
sort($result[$key]); |
127
|
|
|
$result[$out] = isset($result[$key][$index]) ? $result[$key][$index] : null; |
128
|
|
|
unset($result[$key]); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
return array_values($results['result']); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Get a paginated set of results. |
136
|
|
|
* |
137
|
|
|
* @param array $options The find options to use. |
|
|
|
|
138
|
|
|
* @return array An array of result data. |
139
|
|
|
*/ |
140
|
|
|
public function getAll($filter) |
141
|
|
|
{ |
142
|
|
|
return $this->paginate($filter); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Insert a profile run. |
147
|
|
|
* |
148
|
|
|
* Does unchecked inserts. |
149
|
|
|
* |
150
|
|
|
* @param array $profile The profile data to save. |
151
|
|
|
* @return |
152
|
|
|
*/ |
153
|
|
|
public function insert($profile) |
154
|
|
|
{ |
155
|
|
|
return $this->storage->insert($profile); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Delete a profile run. |
160
|
|
|
* |
161
|
|
|
* @param string $id The profile id to delete. |
162
|
|
|
* @return array|bool |
163
|
|
|
*/ |
164
|
|
|
public function delete($id) |
165
|
|
|
{ |
166
|
|
|
return $this->storage->remove($id); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Used to truncate a collection. |
171
|
|
|
* |
172
|
|
|
* Primarly used in test cases to reset the test db. |
173
|
|
|
* |
174
|
|
|
* @return boolean |
175
|
|
|
*/ |
176
|
|
|
public function truncate() |
177
|
|
|
{ |
178
|
|
|
return $this->storage->drop(); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Converts arrays + MongoCursors into Xhgui_Profile instances. |
183
|
|
|
* |
184
|
|
|
* @param array|MongoCursor $data The data to transform. |
185
|
|
|
* @return Xhgui_Profile|array The transformed/wrapped results. |
186
|
|
|
* @throws Exception |
187
|
|
|
*/ |
188
|
|
|
protected function wrap($data) |
189
|
|
|
{ |
190
|
|
|
if ($data === null) { |
191
|
|
|
throw new Exception('No profile data found.'); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
if (!($data instanceof \Xhgui_Storage_ResultSet)) { |
195
|
|
|
return new Xhgui_Profile($data, true); |
|
|
|
|
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
$results = array(); |
199
|
|
|
foreach ($data as $row) { |
200
|
|
|
$results[] = new Xhgui_Profile($row, true); |
201
|
|
|
} |
202
|
|
|
return $results; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @return Xhgui_StorageInterface |
207
|
|
|
*/ |
208
|
|
|
public function getStorage() |
209
|
|
|
{ |
210
|
|
|
return $this->storage; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @param Xhgui_StorageInterface $storage |
215
|
|
|
*/ |
216
|
|
|
public function setStorage($storage) |
217
|
|
|
{ |
218
|
|
|
$this->storage = $storage; |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: