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
|
|
|
|
70
|
|
|
if ($projection === false) { |
71
|
|
|
$result = $this->storage->find($filter, null); |
|
|
|
|
72
|
|
|
} else { |
73
|
|
|
$result = $this->storage->find($filter, $projection); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$totalRows = $this->storage->count($filter); |
77
|
|
|
$totalPages = max(ceil($totalRows / $filter->getPerPage()), 1); |
78
|
|
|
|
79
|
|
|
return array( |
80
|
|
|
'results' => $this->wrap($result), |
81
|
|
|
'sort' => $filter->getSort(), |
82
|
|
|
'direction' => $filter->getDirection(), |
83
|
|
|
'page' => $filter->getPage(), |
84
|
|
|
'perPage' => $filter->getPerPage(), |
85
|
|
|
'totalPages' => $totalPages |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Get the Percentile metrics for a URL |
91
|
|
|
* |
92
|
|
|
* This will group data by date and returns only the |
93
|
|
|
* percentile + date, making the data ideal for time series graphs |
94
|
|
|
* |
95
|
|
|
* @param integer $percentile The percentile you want. e.g. 90. |
96
|
|
|
* @param string $url |
97
|
|
|
* @param array $search Search options containing startDate and or endDate |
|
|
|
|
98
|
|
|
* @return array Array of metrics grouped by date |
99
|
|
|
*/ |
100
|
|
|
public function getPercentileForUrl($percentile, $url, $filter) |
|
|
|
|
101
|
|
|
{ |
102
|
|
|
$col = '$meta.request_date'; |
103
|
|
|
|
104
|
|
|
$results = $this->storage->aggregate($filter, $col, $percentile); |
105
|
|
|
if (empty($results['result'])) { |
106
|
|
|
return array(); |
107
|
|
|
} |
108
|
|
|
$keys = array( |
109
|
|
|
'wall_times' => 'wt', |
110
|
|
|
'cpu_times' => 'cpu', |
111
|
|
|
'mu_times' => 'mu', |
112
|
|
|
'pmu_times' => 'pmu' |
113
|
|
|
); |
114
|
|
|
foreach ($results['result'] as &$result) { |
115
|
|
|
if ($result['_id'] instanceof MongoDate) { |
116
|
|
|
$result['date'] = date('Y-m-d H:i:s', $result['_id']->sec); |
117
|
|
|
} else { |
118
|
|
|
$result['date'] = $result['_id']; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
unset($result['_id']); |
122
|
|
|
$index = max(round($result['raw_index']) - 1, 0); |
123
|
|
|
foreach ($keys as $key => $out) { |
124
|
|
|
sort($result[$key]); |
125
|
|
|
$result[$out] = isset($result[$key][$index]) ? $result[$key][$index] : null; |
126
|
|
|
unset($result[$key]); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
return array_values($results['result']); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Get a paginated set of results. |
134
|
|
|
* |
135
|
|
|
* @param array $options The find options to use. |
|
|
|
|
136
|
|
|
* @return array An array of result data. |
137
|
|
|
*/ |
138
|
|
|
public function getAll($filter) |
139
|
|
|
{ |
140
|
|
|
return $this->paginate($filter); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Insert a profile run. |
145
|
|
|
* |
146
|
|
|
* Does unchecked inserts. |
147
|
|
|
* |
148
|
|
|
* @param array $profile The profile data to save. |
149
|
|
|
* @return |
150
|
|
|
*/ |
151
|
|
|
public function insert($profile) |
152
|
|
|
{ |
153
|
|
|
return $this->storage->insert($profile); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Delete a profile run. |
158
|
|
|
* |
159
|
|
|
* @param string $id The profile id to delete. |
160
|
|
|
* @return array|bool |
161
|
|
|
*/ |
162
|
|
|
public function delete($id) |
163
|
|
|
{ |
164
|
|
|
return $this->storage->remove($id); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Used to truncate a collection. |
169
|
|
|
* |
170
|
|
|
* Primarly used in test cases to reset the test db. |
171
|
|
|
* |
172
|
|
|
* @return boolean |
173
|
|
|
*/ |
174
|
|
|
public function truncate() |
175
|
|
|
{ |
176
|
|
|
return $this->storage->drop(); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Converts arrays + MongoCursors into Xhgui_Profile instances. |
181
|
|
|
* |
182
|
|
|
* @param array|MongoCursor $data The data to transform. |
183
|
|
|
* @return Xhgui_Profile|array The transformed/wrapped results. |
184
|
|
|
* @throws Exception |
185
|
|
|
*/ |
186
|
|
|
protected function wrap($data) |
187
|
|
|
{ |
188
|
|
|
if ($data === null) { |
189
|
|
|
throw new Exception('No profile data found.'); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
if (!($data instanceof \Xhgui_Storage_ResultSet)) { |
193
|
|
|
return new Xhgui_Profile($data, true); |
|
|
|
|
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
$results = array(); |
197
|
|
|
foreach ($data as $row) { |
198
|
|
|
$results[] = new Xhgui_Profile($row, true); |
199
|
|
|
} |
200
|
|
|
return $results; |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
This check looks for function calls that miss required arguments.