Test Failed
Push — main ( fac5ed...59bfee )
by Rafael
50:50
created

DolQueryCollector   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 43
c 0
b 0
f 0
dl 0
loc 100
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getAssets() 0 5 1
A __construct() 0 6 1
A collect() 0 29 3
A getName() 0 3 1
A getWidgets() 0 16 1
1
<?php
2
3
/* Copyright (C) 2023   Laurent Destailleur     <[email protected]>
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17
 */
18
19
/**
20
 *  \file       htdocs/debugbar/class/DataCollector/DolQueryCollector.php
21
 *  \brief      Class for debugbar collection
22
 *  \ingroup    debugbar
23
 */
24
25
namespace DoliCore\Tools\DebugBarCollector;
26
27
use DebugBar\DataCollector\AssetProvider;
28
use DebugBar\DataCollector\DataCollector;
29
use DebugBar\DataCollector\Renderable;
30
use DoliCore\Lib\TraceableDB;
31
32
/**
33
 * DolQueryCollector class
34
 *
35
 * Delete when fully migrated to Eloquent
36
 * This class is only needed for compatibility with Dolibarr.
37
 *
38
 * @package DoliCore\Tools\DebugBarCollector
39
 */
40
class DolQueryCollector extends DataCollector implements Renderable, AssetProvider
41
{
42
    /**
43
     * @var object Database handler
44
     */
45
    protected $db;
46
47
    /**
48
     * Constructor
49
     */
50
    public function __construct()
51
    {
52
        global $db;
53
54
        $db = new TraceableDB($db);
55
        $this->db = $db;
56
    }
57
58
    /**
59
     * Return collected data
60
     *
61
     * @return array  Array
62
     */
63
    public function collect()
64
    {
65
        $queries = [];
66
        $totalExecTime = 0;
67
        $totalMemoryUsage = 0;
68
        $totalFailed = 0;
69
        foreach ($this->db->queries as $query) {
70
            $queries[] = [
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 [
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('DolSQL');
115
116
        return [
117
            "$title" => [
118
                "icon" => "arrow-right",
119
                "widget" => "PhpDebugBar.Widgets.SQLQueriesWidget",
120
                "map" => "query",
121
                "default" => "[]",
122
            ],
123
            "$title:badge" => [
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 [
138
            'css' => 'widgets/sqlqueries/widget.css',
139
            'js' => 'widgets/sqlqueries/widget.js',
140
        ];
141
    }
142
}
143