dolibarr 21.0.0-alpha
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
24use DebugBar\DataCollector\AssetProvider;
25use DebugBar\DataCollector\DataCollector;
26use DebugBar\DataCollector\Renderable;
27
28dol_include_once('/debugbar/class/TraceableDB.php');
29
33class DolQueryCollector extends DataCollector implements Renderable, AssetProvider
34{
38 protected $db;
39
43 public function __construct()
44 {
45 global $db;
46
47 // Replace $db handler with new handler override by TraceableDB
48 $db = new TraceableDB($db);
49
50 $this->db = $db;
51 }
52
58 public function collect()
59 {
60 $queries = array();
61 $totalExecTime = 0;
62 $totalMemoryUsage = 0;
63 $totalFailed = 0;
64 foreach ($this->db->queries as $query) {
65 $queries[] = array(
66 'sql' => $query['sql'],
67 'duration' => $query['duration'],
68 'duration_str' => round($query['duration'] * 1000, 2),
69 'memory' => $query['memory_usage'],
70 'is_success' => $query['is_success'],
71 'error_code' => $query['error_code'],
72 'error_message' => $query['error_message']
73 );
74 $totalExecTime += $query['duration'];
75 $totalMemoryUsage += $query['memory_usage'];
76 if (!$query['is_success']) {
77 $totalFailed += 1;
78 }
79 }
80
81 return array(
82 'nb_statements' => count($queries),
83 'nb_failed_statements' => $totalFailed,
84 'accumulated_duration' => $totalExecTime,
85 'memory_usage' => $totalMemoryUsage,
86 'statements' => $queries
87 );
88 }
89
95 public function getName()
96 {
97 return 'query';
98 }
99
105 public function getWidgets()
106 {
107 global $langs;
108
109 $title = $langs->transnoentities('Database');
110
111 return array(
112 "$title" => array(
113 "icon" => "arrow-right",
114 "widget" => "PhpDebugBar.Widgets.SQLQueriesWidget",
115 "map" => "query",
116 "default" => "[]"
117 ),
118 "$title:badge" => array(
119 "map" => "query.nb_statements",
120 "default" => 0
121 )
122 );
123 }
124
130 public function getAssets()
131 {
132 return array(
133 'css' => 'widgets/sqlqueries/widget.css',
134 'js' => 'widgets/sqlqueries/widget.js'
135 );
136 }
137}
DolQueryCollector class.
getWidgets()
Return widget settings.
collect()
Return collected data.
__construct()
Constructor.
getName()
Return collector name.
getAssets()
Return assets.
TraceableDB class.
if(!function_exists( 'dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.