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