1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PDO handler to save profiler data |
4
|
|
|
*/ |
5
|
|
|
class Xhgui_Saver_Pdo implements \Xhgui_Saver_Interface { |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @var \PDO |
9
|
|
|
*/ |
10
|
|
|
protected $connection; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* PDO constructor. |
14
|
|
|
* |
15
|
|
|
* @param string $dsn |
16
|
|
|
* @param string $userName |
17
|
|
|
* @param string $password |
18
|
|
|
* @param array $options |
19
|
|
|
*/ |
20
|
|
|
public function __construct($dsn, $userName, $password, $options = array()) |
21
|
|
|
{ |
22
|
|
|
$this->connection = new \PDO($dsn, $userName, $password, $options); |
23
|
|
|
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Save data in sql database. |
28
|
|
|
* |
29
|
|
|
* @param array $data |
30
|
|
|
* |
31
|
|
|
* @return bool |
32
|
|
|
*/ |
33
|
|
|
public function save(array $data) |
34
|
|
|
{ |
35
|
|
|
$this->connection->beginTransaction(); |
36
|
|
|
|
37
|
|
|
try { |
38
|
|
|
$id = Xhgui_Util::getId($data); |
39
|
|
|
|
40
|
|
|
$requestTime = \DateTime::createFromFormat('U u', $data['meta']['request_ts_micro']['sec'].' '.$data['meta']['request_ts_micro']['usec']); |
41
|
|
|
|
42
|
|
|
$infoStatement = $this->connection->prepare(' |
43
|
|
|
insert into profiles_info( |
44
|
|
|
id, url, simple_url, request_time, method, main_ct, main_wt, main_cpu, main_mu, main_pmu, application, version, branch,controller, action, remote_addr, session_id |
45
|
|
|
) values ( |
46
|
|
|
:id, :url, :simple_url, :request_time, :method, :main_ct, :main_wt, :main_cpu, :main_mu, :main_pmu, :application, :version, :branch, :controller, :action, :remote_addr, :session_id |
47
|
|
|
)'); |
48
|
|
|
|
49
|
|
|
// get some data from meta and save in separate column to make it easier to search/filter |
50
|
|
|
$infoStatement->execute([ |
51
|
|
|
'id' => $id, |
52
|
|
|
'url' => $data['meta']['url'], |
53
|
|
|
'simple_url' => $data['meta']['simple_url'], |
54
|
|
|
'request_time' => $requestTime->format('Y-m-d H:i:s.u'), |
55
|
|
|
'main_ct' => $data['profile']['main()']['ct'], |
56
|
|
|
'main_wt' => $data['profile']['main()']['wt'], |
57
|
|
|
'main_cpu' => $data['profile']['main()']['cpu'], |
58
|
|
|
'main_mu' => $data['profile']['main()']['mu'], |
59
|
|
|
'main_pmu' => $data['profile']['main()']['pmu'], |
60
|
|
|
'application' => !empty($data['meta']['application']) ? $data['meta']['application'] : null, |
61
|
|
|
'version' => !empty($data['meta']['version']) ? $data['meta']['version'] : null, |
62
|
|
|
'branch' => !empty($data['meta']['branch']) ? $data['meta']['branch'] : null, |
63
|
|
|
'controller' => !empty($data['meta']['controller']) ? $data['meta']['controller'] : null, |
64
|
|
|
'action' => !empty($data['meta']['action']) ? $data['meta']['action'] : null, |
65
|
|
|
'session_id' => !empty($data['meta']['session_id']) ? $data['meta']['action'] : null, |
66
|
|
|
'method' => !empty($data['meta']['method']) ? $data['meta']['method'] : Xhgui_Util::getMethod(), |
67
|
|
|
|
68
|
|
|
'remote_addr' => !empty($data['meta']['SERVER']['REMOTE_ADDR']) ? $data['meta']['SERVER']['REMOTE_ADDR'] : null, |
69
|
|
|
]); |
70
|
|
|
|
71
|
|
|
$profileStatement = $this->connection->prepare('insert into profiles(profile_id, profiles) VALUES(:id, :profiles)'); |
72
|
|
|
$profileStatement->execute([ |
73
|
|
|
'id' => $id, |
74
|
|
|
'profiles' => json_encode($data['profile']), |
75
|
|
|
]); |
76
|
|
|
|
77
|
|
|
$metaStatement = $this->connection->prepare('insert into profiles_meta(profile_id, meta) VALUES(:id, :meta)'); |
78
|
|
|
$metaStatement->execute([ |
79
|
|
|
'id' => $id, |
80
|
|
|
'meta' => json_encode($data['meta']), |
81
|
|
|
]); |
82
|
|
|
|
83
|
|
|
$this->connection->commit(); |
84
|
|
|
|
85
|
|
|
return true; |
86
|
|
|
} catch (\Exception $e) { |
87
|
|
|
$this->connection->rollBack(); |
88
|
|
|
error_log('xhgui - ' . $e->getMessage()); |
89
|
|
|
} |
90
|
|
|
return false; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|