dolibarr  17.0.2
DolQueryCollector.php
1 <?php
2 
3 use DebugBar\DataCollector\AssetProvider;
4 use DebugBar\DataCollector\DataCollector;
5 use DebugBar\DataCollector\Renderable;
6 use DebugBar\DebugBarException;
7 
8 dol_include_once('/debugbar/class/TraceableDB.php');
9 
13 class DolQueryCollector extends DataCollector implements Renderable, AssetProvider
14 {
18  protected $db;
19 
23  public function __construct()
24  {
25  global $db;
26 
27  // Replace $db handler with new handler override by TraceableDB
28  $db = new TraceableDB($db);
29 
30  $this->db = $db;
31  }
32 
38  public function collect()
39  {
40  $queries = array();
41  $totalExecTime = 0;
42  $totalMemoryUsage = 0;
43  $totalFailed = 0;
44  foreach ($this->db->queries as $query) {
45  $queries[] = array(
46  'sql' => $query['sql'],
47  'duration' => $query['duration'],
48  'duration_str' => round($query['duration'] * 1000, 2),
49  'memory' => $query['memory_usage'],
50  'is_success' => $query['is_success'],
51  'error_code' => $query['error_code'],
52  'error_message' => $query['error_message']
53  );
54  $totalExecTime += $query['duration'];
55  $totalMemoryUsage += $query['memory_usage'];
56  if (!$query['is_success']) {
57  $totalFailed += 1;
58  }
59  }
60 
61  return array(
62  'nb_statements' => count($queries),
63  'nb_failed_statements' => $totalFailed,
64  'accumulated_duration' => $totalExecTime,
65  'memory_usage' => $totalMemoryUsage,
66  'statements' => $queries
67  );
68  }
69 
75  public function getName()
76  {
77  return 'query';
78  }
79 
85  public function getWidgets()
86  {
87  global $langs;
88 
89  $title = $langs->transnoentities('Database');
90 
91  return array(
92  "$title" => array(
93  "icon" => "arrow-right",
94  "widget" => "PhpDebugBar.Widgets.SQLQueriesWidget",
95  "map" => "query",
96  "default" => "[]"
97  ),
98  "$title:badge" => array(
99  "map" => "query.nb_statements",
100  "default" => 0
101  )
102  );
103  }
104 
110  public function getAssets()
111  {
112  return array(
113  'css' => 'widgets/sqlqueries/widget.css',
114  'js' => 'widgets/sqlqueries/widget.js'
115  );
116  }
117 }
DolQueryCollector\__construct
__construct()
Constructor.
Definition: DolQueryCollector.php:23
DolQueryCollector\getWidgets
getWidgets()
Return widget settings.
Definition: DolQueryCollector.php:85
DolQueryCollector\collect
collect()
Return collected data.
Definition: DolQueryCollector.php:38
db
$conf db
API class for accounts.
Definition: inc.php:41
DolQueryCollector\getAssets
getAssets()
Return assets.
Definition: DolQueryCollector.php:110
dol_include_once
if(!function_exists('dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
Definition: functions.lib.php:1032
DolQueryCollector\getName
getName()
Return collector name.
Definition: DolQueryCollector.php:75
DolQueryCollector
DolQueryCollector class.
Definition: DolQueryCollector.php:13
TraceableDB
TraceableDB class.
Definition: TraceableDB.php:10