dolibarr 21.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 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 );
76 $totalExecTime += $query['duration'];
77 $totalMemoryUsage += $query['memory_usage'];
78 if (!$query['is_success']) {
79 $totalFailed += 1;
80 }
81 }
82
83 return array(
84 'nb_statements' => count($queries),
85 'nb_failed_statements' => $totalFailed,
86 'accumulated_duration' => $totalExecTime,
87 'memory_usage' => $totalMemoryUsage,
88 'statements' => $queries
89 );
90 }
91
97 public function getName()
98 {
99 return 'query';
100 }
101
107 public function getWidgets()
108 {
109 global $langs;
110
111 $title = $langs->transnoentities('Database');
112
113 return array(
114 "$title" => array(
115 "icon" => "arrow-right",
116 "widget" => "PhpDebugBar.Widgets.SQLQueriesWidget",
117 "map" => "query",
118 "default" => "[]"
119 ),
120 "$title:badge" => array(
121 "map" => "query.nb_statements",
122 "default" => 0
123 )
124 );
125 }
126
132 public function getAssets()
133 {
134 return array(
135 'css' => 'widgets/sqlqueries/widget.css',
136 'js' => 'widgets/sqlqueries/widget.js'
137 );
138 }
139}
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.