1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class Xhgui_Searcher_Pdo implements Xhgui_Searcher_Interface |
4
|
|
|
{ |
5
|
|
|
/** |
6
|
|
|
* @var PDO |
7
|
|
|
*/ |
8
|
|
|
private $pdo; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @var string |
12
|
|
|
*/ |
13
|
|
|
private $table; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param PDO $pdo An open database connection |
17
|
|
|
* @param string $table Table name where Xhgui profiles are stored |
18
|
|
|
*/ |
19
|
|
|
public function __construct(PDO $pdo, $table) |
20
|
|
|
{ |
21
|
|
|
$this->pdo = $pdo; |
22
|
|
|
$this->table = $table; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* {@inheritdoc} |
27
|
|
|
*/ |
28
|
|
View Code Duplication |
public function latest() |
|
|
|
|
29
|
|
|
{ |
30
|
|
|
$stmt = $this->pdo->query(" |
31
|
|
|
SELECT |
32
|
|
|
id, |
33
|
|
|
profile, |
34
|
|
|
url, |
35
|
|
|
SERVER, |
36
|
|
|
GET, |
37
|
|
|
ENV, |
38
|
|
|
simple_url, |
39
|
|
|
request_ts, |
40
|
|
|
request_ts_micro, |
41
|
|
|
request_date |
42
|
|
|
FROM {$this->table} |
43
|
|
|
ORDER BY request_date ASC |
44
|
|
|
LIMIT 1 |
45
|
|
|
"); |
46
|
|
|
|
47
|
|
|
$row = $stmt->fetch(PDO::FETCH_ASSOC); |
48
|
|
|
if (false === $row) { |
49
|
|
|
throw new Exception('No profile available yet.'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return new Xhgui_Profile([ |
53
|
|
|
'_id' => $row['id'], |
54
|
|
|
'meta' => [ |
55
|
|
|
'url' => $row['url'], |
56
|
|
|
'SERVER' => json_decode($row['SERVER'], true), |
57
|
|
|
'get' => json_decode($row['GET'], true), |
58
|
|
|
'env' => json_decode($row['ENV'], true), |
59
|
|
|
'simple_url' => $row['simple_url'], |
60
|
|
|
'request_ts' => (int) $row['request_ts'], |
61
|
|
|
'request_ts_micro' => $row['request_ts_micro'], |
62
|
|
|
'request_date' => $row['request_date'], |
63
|
|
|
], |
64
|
|
|
'profile' => json_decode($row['profile'], true) |
65
|
|
|
]); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
*/ |
71
|
|
|
public function query($conditions, $limit, $fields = []) |
72
|
|
|
{ |
73
|
|
|
// TODO: Implement query() method. |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
|
View Code Duplication |
public function get($id) |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
$stmt = $this->pdo->prepare(" |
82
|
|
|
SELECT |
83
|
|
|
profile, |
84
|
|
|
url, |
85
|
|
|
SERVER, |
86
|
|
|
GET, |
87
|
|
|
ENV, |
88
|
|
|
simple_url, |
89
|
|
|
request_ts, |
90
|
|
|
request_ts_micro, |
91
|
|
|
request_date |
92
|
|
|
FROM {$this->table} |
93
|
|
|
WHERE id = :id |
94
|
|
|
"); |
95
|
|
|
|
96
|
|
|
$stmt->execute(['id' => $id]); |
97
|
|
|
|
98
|
|
|
if (false === $row = $stmt->fetch(PDO::FETCH_ASSOC)) { |
99
|
|
|
throw new Exception('No profile data found.'); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return new Xhgui_Profile([ |
103
|
|
|
'_id' => $id, |
104
|
|
|
'meta' => [ |
105
|
|
|
'url' => $row['url'], |
106
|
|
|
'SERVER' => json_decode($row['SERVER'], true), |
107
|
|
|
'get' => json_decode($row['GET'], true), |
108
|
|
|
'env' => json_decode($row['ENV'], true), |
109
|
|
|
'simple_url' => $row['simple_url'], |
110
|
|
|
'request_ts' => (int) $row['request_ts'], |
111
|
|
|
'request_ts_micro' => $row['request_ts_micro'], |
112
|
|
|
'request_date' => $row['request_date'], |
113
|
|
|
], |
114
|
|
|
'profile' => json_decode($row['profile'], true) |
115
|
|
|
]); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* {@inheritdoc} |
120
|
|
|
*/ |
121
|
|
|
public function getForUrl($url, $options, $conditions = array()) |
122
|
|
|
{ |
123
|
|
|
// TODO: Implement getForUrl() method. |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* {@inheritdoc} |
128
|
|
|
*/ |
129
|
|
|
public function getPercentileForUrl($percentile, $url, $search = array()) |
130
|
|
|
{ |
131
|
|
|
// TODO: Implement getPercentileForUrl() method. |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* {@inheritdoc} |
136
|
|
|
*/ |
137
|
|
|
public function getAvgsForUrl($url, $search = array()) |
138
|
|
|
{ |
139
|
|
|
// TODO: Implement getAvgsForUrl() method. |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* {@inheritdoc} |
144
|
|
|
*/ |
145
|
|
|
public function getAll($options = array()) |
146
|
|
|
{ |
147
|
|
|
$sort = $options['sort']; |
|
|
|
|
148
|
|
|
$direction = $options['direction']; |
|
|
|
|
149
|
|
|
$page = $options['page']; |
|
|
|
|
150
|
|
|
$perPage = $options['perPage']; |
|
|
|
|
151
|
|
|
|
152
|
|
|
$stmt = $this->pdo->query(" |
153
|
|
|
SELECT |
154
|
|
|
id, |
155
|
|
|
url, |
156
|
|
|
SERVER, |
157
|
|
|
GET, |
158
|
|
|
ENV, |
159
|
|
|
simple_url, |
160
|
|
|
request_ts, |
161
|
|
|
request_ts_micro, |
162
|
|
|
request_date, |
163
|
|
|
main_wt, |
164
|
|
|
main_ct, |
165
|
|
|
main_cpu, |
166
|
|
|
main_mu, |
167
|
|
|
main_pmu |
168
|
|
|
FROM {$this->table} |
169
|
|
|
ORDER BY request_ts DESC |
170
|
|
|
", PDO::FETCH_ASSOC); |
171
|
|
|
|
172
|
|
|
$results = []; |
173
|
|
|
foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) { |
174
|
|
|
$results[] = new Xhgui_Profile([ |
175
|
|
|
'_id' => $row['id'], |
176
|
|
|
'meta' => [ |
177
|
|
|
'url' => $row['url'], |
178
|
|
|
'SERVER' => json_decode($row['SERVER'], true), |
179
|
|
|
'get' => json_decode($row['GET'], true), |
180
|
|
|
'env' => json_decode($row['ENV'], true), |
181
|
|
|
'simple_url' => $row['simple_url'], |
182
|
|
|
'request_ts' => $row['request_ts'], |
183
|
|
|
'request_ts_micro' => $row['request_ts_micro'], |
184
|
|
|
'request_date' => $row['request_date'], |
185
|
|
|
], |
186
|
|
|
'profile' => [ |
187
|
|
|
'main()' => [ |
188
|
|
|
'wt' => (int) $row['main_wt'], |
189
|
|
|
'ct' => (int) $row['main_ct'], |
190
|
|
|
'cpu' => (int) $row['main_cpu'], |
191
|
|
|
'mu' => (int) $row['main_mu'], |
192
|
|
|
'pmu' => (int) $row['main_pmu'], |
193
|
|
|
] |
194
|
|
|
] |
195
|
|
|
]); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
return array( |
199
|
|
|
'results' => $results, |
200
|
|
|
'sort' => 'meta.request_ts', |
201
|
|
|
'direction' => 'desc', |
202
|
|
|
'page' => 1, |
203
|
|
|
'perPage' => count($results), |
204
|
|
|
'totalPages' => 1 |
205
|
|
|
); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* {@inheritdoc} |
210
|
|
|
*/ |
211
|
|
|
public function delete($id) |
212
|
|
|
{ |
213
|
|
|
$stmt = $this->pdo->prepare(" |
214
|
|
|
DELETE FROM {$this->table} |
215
|
|
|
WHERE id = :id |
216
|
|
|
"); |
217
|
|
|
|
218
|
|
|
$stmt->execute(['id' => $id]); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* {@inheritdoc} |
223
|
|
|
*/ |
224
|
|
|
public function truncate() |
225
|
|
|
{ |
226
|
|
|
return is_int( |
227
|
|
|
$this->pdo->exec("DELETE FROM {$this->table}") |
228
|
|
|
); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* {@inheritdoc} |
233
|
|
|
*/ |
234
|
|
|
public function saveWatch(array $data) |
235
|
|
|
{ |
236
|
|
|
return true; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* {@inheritdoc} |
241
|
|
|
*/ |
242
|
|
|
public function getAllWatches() |
243
|
|
|
{ |
244
|
|
|
return []; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* {@inheritdoc} |
249
|
|
|
*/ |
250
|
|
|
public function truncateWatches() |
251
|
|
|
{ |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.