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