Passed
Push — MODEL_LIB_240928 ( d6fbb6...55f3e4 )
by Rafael
49:14
created

DolQueryCollector::collect()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 29
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 23
nc 3
nop 0
dl 0
loc 29
rs 9.552
c 0
b 0
f 0
1
<?php
2
3
/* Copyright (C) 2023       Laurent Destailleur         <[email protected]>
4
 * Copyright (C) 2024       Rafael San José             <[email protected]>
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
20
namespace Dolibarr\Tools\DebugBarCollector;
21
22
use DebugBar\DataCollector\AssetProvider;
23
use DebugBar\DataCollector\DataCollector;
24
use DebugBar\DataCollector\Renderable;
25
use Dolibarr\Tools\TraceableDB;
26
27
/**
28
 *  \file       htdocs/debugbar/class/DataCollector/DolQueryCollector.php
29
 *  \brief      Class for debugbar collection
30
 *  \ingroup    debugbar
31
 */
32
33
dol_include_once('/debugbar/class/TraceableDB.php');
34
35
/**
36
 * DolQueryCollector class
37
 */
38
class DolQueryCollector extends DataCollector implements Renderable, AssetProvider
39
{
40
    /**
41
     * @var object Database handler
42
     */
43
    protected $db;
44
45
    /**
46
     * Constructor
47
     */
48
    public function __construct()
49
    {
50
        global $db;
51
52
        // Replace $db handler with new handler override by TraceableDB
53
        $db = new TraceableDB($db);
54
55
        $this->db = $db;
56
    }
57
58
    /**
59
     * Return collected data
60
     *
61
     * @return array  Array of collected data
62
     */
63
    public function collect()
64
    {
65
        $queries = array();
66
        $totalExecTime = 0;
67
        $totalMemoryUsage = 0;
68
        $totalFailed = 0;
69
        foreach ($this->db->queries as $query) {
70
            $queries[] = array(
71
                'sql' => $query['sql'],
72
                'duration' => $query['duration'],
73
                'duration_str' => round($query['duration'] * 1000, 2),
74
                'memory' => $query['memory_usage'],
75
                'is_success' => $query['is_success'],
76
                'error_code' => $query['error_code'],
77
                'error_message' => $query['error_message']
78
            );
79
            $totalExecTime += $query['duration'];
80
            $totalMemoryUsage += $query['memory_usage'];
81
            if (!$query['is_success']) {
82
                $totalFailed += 1;
83
            }
84
        }
85
86
        return array(
87
            'nb_statements' => count($queries),
88
            'nb_failed_statements' => $totalFailed,
89
            'accumulated_duration' => $totalExecTime,
90
            'memory_usage' => $totalMemoryUsage,
91
            'statements' => $queries
92
        );
93
    }
94
95
    /**
96
     *  Return collector name
97
     *
98
     * @return string  Name
99
     */
100
    public function getName()
101
    {
102
        return 'query';
103
    }
104
105
    /**
106
     *  Return widget settings
107
     *
108
     * @return array      Array
109
     */
110
    public function getWidgets()
111
    {
112
        global $langs;
113
114
        $title = $langs->transnoentities('Database');
115
116
        return array(
117
            "$title" => array(
118
                "icon" => "arrow-right",
119
                "widget" => "PhpDebugBar.Widgets.SQLQueriesWidget",
120
                "map" => "query",
121
                "default" => "[]"
122
            ),
123
            "$title:badge" => array(
124
                "map" => "query.nb_statements",
125
                "default" => 0
126
            )
127
        );
128
    }
129
130
    /**
131
     *  Return assets
132
     *
133
     * @return array   Array
134
     */
135
    public function getAssets()
136
    {
137
        return array(
138
            'css' => 'widgets/sqlqueries/widget.css',
139
            'js' => 'widgets/sqlqueries/widget.js'
140
        );
141
    }
142
}
143