dolibarr  19.0.0-dev
DolQueryCollector.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2023 Laurent Destailleur <eldy@users.sourceforge.net>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <https://www.gnu.org/licenses/>.
16  */
17 
24 use DebugBar\DataCollector\AssetProvider;
25 use DebugBar\DataCollector\DataCollector;
26 use DebugBar\DataCollector\Renderable;
27 use DebugBar\DebugBarException;
28 
29 dol_include_once('/debugbar/class/TraceableDB.php');
30 
34 class DolQueryCollector extends DataCollector implements Renderable, AssetProvider
35 {
39  protected $db;
40 
44  public function __construct()
45  {
46  global $db;
47 
48  // Replace $db handler with new handler override by TraceableDB
49  $db = new TraceableDB($db);
50 
51  $this->db = $db;
52  }
53 
59  public function collect()
60  {
61  $queries = array();
62  $totalExecTime = 0;
63  $totalMemoryUsage = 0;
64  $totalFailed = 0;
65  foreach ($this->db->queries as $query) {
66  $queries[] = array(
67  'sql' => $query['sql'],
68  'duration' => $query['duration'],
69  'duration_str' => round($query['duration'] * 1000, 2),
70  'memory' => $query['memory_usage'],
71  'is_success' => $query['is_success'],
72  'error_code' => $query['error_code'],
73  'error_message' => $query['error_message']
74  );
75  $totalExecTime += $query['duration'];
76  $totalMemoryUsage += $query['memory_usage'];
77  if (!$query['is_success']) {
78  $totalFailed += 1;
79  }
80  }
81 
82  return array(
83  'nb_statements' => count($queries),
84  'nb_failed_statements' => $totalFailed,
85  'accumulated_duration' => $totalExecTime,
86  'memory_usage' => $totalMemoryUsage,
87  'statements' => $queries
88  );
89  }
90 
96  public function getName()
97  {
98  return 'query';
99  }
100 
106  public function getWidgets()
107  {
108  global $langs;
109 
110  $title = $langs->transnoentities('Database');
111 
112  return array(
113  "$title" => array(
114  "icon" => "arrow-right",
115  "widget" => "PhpDebugBar.Widgets.SQLQueriesWidget",
116  "map" => "query",
117  "default" => "[]"
118  ),
119  "$title:badge" => array(
120  "map" => "query.nb_statements",
121  "default" => 0
122  )
123  );
124  }
125 
131  public function getAssets()
132  {
133  return array(
134  'css' => 'widgets/sqlqueries/widget.css',
135  'js' => 'widgets/sqlqueries/widget.js'
136  );
137  }
138 }
DolQueryCollector class.
getWidgets()
Return widget settings.
collect()
Return collected data.
__construct()
Constructor.
getName()
Return collector name.
getAssets()
Return assets.
TraceableDB class.
Definition: TraceableDB.php:32
if(!function_exists('dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.