1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* A Searcher for a MongoDB backend. |
5
|
|
|
*/ |
6
|
|
|
class Xhgui_Searcher_Mongo implements Xhgui_Searcher_Interface |
|
|
|
|
7
|
|
|
{ |
8
|
|
|
protected $_collection; |
9
|
|
|
|
10
|
|
|
protected $_watches; |
11
|
|
|
|
12
|
|
|
protected $_mapper; |
13
|
|
|
|
14
|
|
|
public function __construct(MongoDb $db) |
15
|
|
|
{ |
16
|
|
|
$this->_collection = $db->results; |
|
|
|
|
17
|
|
|
$this->_watches = $db->watches; |
|
|
|
|
18
|
|
|
$this->_mapper = new Xhgui_Db_Mapper(); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* {@inheritdoc} |
23
|
|
|
*/ |
24
|
|
|
public function latest() |
25
|
|
|
{ |
26
|
|
|
$cursor = $this->_collection->find() |
27
|
|
|
->sort(array('meta.request_date' => -1)) |
28
|
|
|
->limit(1); |
29
|
|
|
$result = $cursor->getNext(); |
30
|
|
|
return $this->_wrap($result); |
|
|
|
|
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
|
|
public function query($conditions, $limit, $fields = []) |
37
|
|
|
{ |
38
|
|
|
$result = $this->_collection->find($conditions, $fields) |
39
|
|
|
->limit($limit); |
40
|
|
|
|
41
|
|
|
return iterator_to_array($result); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
|
|
public function get($id) |
48
|
|
|
{ |
49
|
|
|
return $this->_wrap($this->_collection->findOne(array( |
|
|
|
|
50
|
|
|
'_id' => new MongoId($id) |
51
|
|
|
))); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
|
|
public function getForUrl($url, $options, $conditions = []) |
58
|
|
|
{ |
59
|
|
|
$conditions = array_merge( |
60
|
|
|
(array)$conditions, |
61
|
|
|
array('simple_url' => $url) |
62
|
|
|
); |
63
|
|
|
$options = array_merge($options, array( |
64
|
|
|
'conditions' => $conditions, |
65
|
|
|
)); |
66
|
|
|
return $this->paginate($options); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
|
|
public function getPercentileForUrl($percentile, $url, $search = []) |
73
|
|
|
{ |
74
|
|
|
$result = $this->_mapper->convert(array( |
75
|
|
|
'conditions' => $search + array('simple_url' => $url) |
76
|
|
|
)); |
77
|
|
|
$match = $result['conditions']; |
78
|
|
|
|
79
|
|
|
$col = '$meta.request_date'; |
80
|
|
View Code Duplication |
if (!empty($search['limit']) && $search['limit'][0] == "P") { |
|
|
|
|
81
|
|
|
$col = '$meta.request_ts'; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$results = $this->_collection->aggregate(array( |
85
|
|
|
array('$match' => $match), |
86
|
|
|
array( |
87
|
|
|
'$project' => array( |
88
|
|
|
'date' => $col, |
89
|
|
|
'profile.main()' => 1 |
90
|
|
|
) |
91
|
|
|
), |
92
|
|
|
array( |
93
|
|
|
'$group' => array( |
94
|
|
|
'_id' => '$date', |
95
|
|
|
'row_count' => array('$sum' => 1), |
96
|
|
|
'wall_times' => array('$push' => '$profile.main().wt'), |
97
|
|
|
'cpu_times' => array('$push' => '$profile.main().cpu'), |
98
|
|
|
'mu_times' => array('$push' => '$profile.main().mu'), |
99
|
|
|
'pmu_times' => array('$push' => '$profile.main().pmu'), |
100
|
|
|
) |
101
|
|
|
), |
102
|
|
|
array( |
103
|
|
|
'$project' => array( |
104
|
|
|
'date' => '$date', |
105
|
|
|
'row_count' => '$row_count', |
106
|
|
|
'raw_index' => array( |
107
|
|
|
'$multiply' => array( |
108
|
|
|
'$row_count', |
109
|
|
|
$percentile / 100 |
110
|
|
|
) |
111
|
|
|
), |
112
|
|
|
'wall_times' => '$wall_times', |
113
|
|
|
'cpu_times' => '$cpu_times', |
114
|
|
|
'mu_times' => '$mu_times', |
115
|
|
|
'pmu_times' => '$pmu_times', |
116
|
|
|
) |
117
|
|
|
), |
118
|
|
|
array('$sort' => array('_id' => 1)), |
119
|
|
|
), |
120
|
|
|
array('cursor' => array('batchSize' => 0)) |
121
|
|
|
); |
122
|
|
|
|
123
|
|
|
if (empty($results['result'])) { |
124
|
|
|
return []; |
125
|
|
|
} |
126
|
|
|
$keys = array( |
127
|
|
|
'wall_times' => 'wt', |
128
|
|
|
'cpu_times' => 'cpu', |
129
|
|
|
'mu_times' => 'mu', |
130
|
|
|
'pmu_times' => 'pmu' |
131
|
|
|
); |
132
|
|
|
foreach ($results['result'] as &$result) { |
133
|
|
|
$result['date'] = ($result['_id'] instanceof MongoDate) ? date('Y-m-d H:i:s', $result['_id']->sec) : $result['_id']; |
134
|
|
|
unset($result['_id']); |
135
|
|
|
$index = max(round($result['raw_index']) - 1, 0); |
136
|
|
|
foreach ($keys as $key => $out) { |
137
|
|
|
sort($result[$key]); |
138
|
|
|
$result[$out] = isset($result[$key][$index]) ? $result[$key][$index] : null; |
139
|
|
|
unset($result[$key]); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
return $results['result']; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* {@inheritdoc} |
147
|
|
|
*/ |
148
|
|
|
public function getAvgsForUrl($url, $search = []) |
149
|
|
|
{ |
150
|
|
|
$match = array('meta.simple_url' => $url); |
151
|
|
|
if (isset($search['date_start'])) { |
152
|
|
|
$match['meta.request_date']['$gte'] = (string)$search['date_start']; |
153
|
|
|
} |
154
|
|
|
if (isset($search['date_end'])) { |
155
|
|
|
$match['meta.request_date']['$lte'] = (string)$search['date_end']; |
156
|
|
|
} |
157
|
|
|
$results = $this->_collection->aggregate(array( |
158
|
|
|
array('$match' => $match), |
159
|
|
|
array( |
160
|
|
|
'$project' => array( |
161
|
|
|
'date' => '$meta.request_date', |
162
|
|
|
'profile.main()' => 1, |
163
|
|
|
) |
164
|
|
|
), |
165
|
|
|
array( |
166
|
|
|
'$group' => array( |
167
|
|
|
'_id' => '$date', |
168
|
|
|
'avg_wt' => array('$avg' => '$profile.main().wt'), |
169
|
|
|
'avg_cpu' => array('$avg' => '$profile.main().cpu'), |
170
|
|
|
'avg_mu' => array('$avg' => '$profile.main().mu'), |
171
|
|
|
'avg_pmu' => array('$avg' => '$profile.main().pmu'), |
172
|
|
|
) |
173
|
|
|
), |
174
|
|
|
array('$sort' => array('_id' => 1)) |
175
|
|
|
), |
176
|
|
|
array('cursor' => array('batchSize' => 0)) |
177
|
|
|
); |
178
|
|
|
if (empty($results['result'])) { |
179
|
|
|
return []; |
180
|
|
|
} |
181
|
|
|
foreach ($results['result'] as $i => $result) { |
182
|
|
|
$results['result'][$i]['date'] = $result['_id']; |
183
|
|
|
unset($results['result'][$i]['_id']); |
184
|
|
|
} |
185
|
|
|
return $results['result']; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* {@inheritdoc} |
190
|
|
|
*/ |
191
|
|
|
public function getAll($options = []) |
192
|
|
|
{ |
193
|
|
|
return $this->paginate($options); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* {@inheritdoc} |
198
|
|
|
*/ |
199
|
|
|
public function delete($id) |
200
|
|
|
{ |
201
|
|
|
$this->_collection->remove(array('_id' => new MongoId($id)), []); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* {@inheritdoc} |
206
|
|
|
*/ |
207
|
|
|
public function truncate() |
208
|
|
|
{ |
209
|
|
|
return $this->_collection->drop(); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* {@inheritdoc} |
214
|
|
|
*/ |
215
|
|
View Code Duplication |
public function saveWatch(array $data) |
|
|
|
|
216
|
|
|
{ |
217
|
|
|
if (empty($data['name'])) { |
218
|
|
|
return false; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
if (!empty($data['removed']) && isset($data['_id'])) { |
222
|
|
|
$this->_watches->remove( |
223
|
|
|
array('_id' => new MongoId($data['_id'])), |
224
|
|
|
array('w' => 1) |
225
|
|
|
); |
226
|
|
|
return true; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
if (empty($data['_id'])) { |
230
|
|
|
$this->_watches->insert( |
231
|
|
|
$data, |
232
|
|
|
array('w' => 1) |
233
|
|
|
); |
234
|
|
|
return true; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
|
238
|
|
|
$data['_id'] = new MongoId($data['_id']); |
239
|
|
|
$this->_watches->update( |
240
|
|
|
array('_id' => $data['_id']), |
241
|
|
|
$data, |
242
|
|
|
array('w' => 1) |
243
|
|
|
); |
244
|
|
|
return true; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* {@inheritdoc} |
249
|
|
|
*/ |
250
|
|
|
public function getAllWatches() |
251
|
|
|
{ |
252
|
|
|
$cursor = $this->_watches->find(); |
253
|
|
|
return array_values(iterator_to_array($cursor)); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* {@inheritdoc} |
258
|
|
|
*/ |
259
|
|
|
public function truncateWatches() |
260
|
|
|
{ |
261
|
|
|
$this->_watches->drop(); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
private function paginate($options) |
265
|
|
|
{ |
266
|
|
|
$opts = $this->_mapper->convert($options); |
267
|
|
|
|
268
|
|
|
$totalRows = $this->_collection->find( |
269
|
|
|
$opts['conditions'], |
270
|
|
|
array('_id' => 1))->count(); |
271
|
|
|
|
272
|
|
|
$totalPages = max(ceil($totalRows / $opts['perPage']), 1); |
273
|
|
|
$page = 1; |
274
|
|
|
if (isset($options['page'])) { |
275
|
|
|
$page = min(max($options['page'], 1), $totalPages); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
$projection = false; |
279
|
|
|
if (isset($options['projection'])) { |
280
|
|
|
if ($options['projection'] === true) { |
281
|
|
|
$projection = array('meta' => 1, 'profile.main()' => 1); |
282
|
|
|
} else { |
283
|
|
|
$projection = $options['projection']; |
284
|
|
|
} |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
if ($projection === false) { |
288
|
|
|
$cursor = $this->_collection->find($opts['conditions']) |
289
|
|
|
->sort($opts['sort']) |
290
|
|
|
->skip((int)($page - 1) * $opts['perPage']) |
291
|
|
|
->limit($opts['perPage']); |
292
|
|
|
} else { |
293
|
|
|
$cursor = $this->_collection->find($opts['conditions'], $projection) |
294
|
|
|
->sort($opts['sort']) |
295
|
|
|
->skip((int)($page - 1) * $opts['perPage']) |
296
|
|
|
->limit($opts['perPage']); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
return array( |
300
|
|
|
'results' => $this->_wrap($cursor), |
301
|
|
|
'sort' => $opts['sort'], |
302
|
|
|
'direction' => $opts['direction'], |
303
|
|
|
'page' => $page, |
304
|
|
|
'perPage' => $opts['perPage'], |
305
|
|
|
'totalPages' => $totalPages |
306
|
|
|
); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* Converts arrays + MongoCursors into Xhgui_Profile instances. |
311
|
|
|
* |
312
|
|
|
* @param array|MongoCursor $data The data to transform. |
313
|
|
|
* |
314
|
|
|
* @return Xhgui_Profile[] The transformed/wrapped results. |
315
|
|
|
*/ |
316
|
|
|
protected function _wrap($data) |
317
|
|
|
{ |
318
|
|
|
if ($data === null) { |
319
|
|
|
throw new Exception('No profile data found.'); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
if (is_array($data)) { |
323
|
|
|
return new Xhgui_Profile($data); |
324
|
|
|
} |
325
|
|
|
$results = []; |
326
|
|
|
foreach ($data as $row) { |
327
|
|
|
$results[] = new Xhgui_Profile($row); |
328
|
|
|
} |
329
|
|
|
return $results; |
330
|
|
|
} |
331
|
|
|
} |
332
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.