Skip to content

Commit 9507617

Browse files
committed
Add more structure to the database queries data.
1 parent 5a0f029 commit 9507617

File tree

7 files changed

+166
-196
lines changed

7 files changed

+166
-196
lines changed

collectors/db_dupes.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ public function process() {
5555
// Loop over each query
5656
foreach ( $query_ids as $query_id ) {
5757

58-
if ( isset( $dbq_data->wpdb->rows[ $query_id ]['trace'] ) ) {
58+
if ( isset( $dbq_data->rows[ $query_id ]['trace'] ) ) {
5959
/** @var QM_Backtrace */
60-
$trace = $dbq_data->wpdb->rows[ $query_id ]['trace'];
60+
$trace = $dbq_data->rows[ $query_id ]['trace'];
6161
$stack = array_column( $trace->get_filtered_trace(), 'id' );
6262
$component = $trace->get_component();
6363

@@ -69,7 +69,7 @@ public function process() {
6969
}
7070
} else {
7171
/** @var array<int, string> */
72-
$stack = $dbq_data->wpdb->rows[ $query_id ]['stack'];
72+
$stack = $dbq_data->rows[ $query_id ]['stack'];
7373
}
7474

7575
// Populate the caller counts for this query
@@ -84,9 +84,9 @@ public function process() {
8484

8585
// Populate the time for this query
8686
if ( isset( $times[ $sql ] ) ) {
87-
$times[ $sql ] += $dbq->data->wpdb->rows[ $query_id ]['ltime'];
87+
$times[ $sql ] += $dbq->data->rows[ $query_id ]['ltime'];
8888
} else {
89-
$times[ $sql ] = $dbq->data->wpdb->rows[ $query_id ]['ltime'];
89+
$times[ $sql ] = $dbq->data->rows[ $query_id ]['ltime'];
9090
}
9191
}
9292

collectors/db_queries.php

+7-29
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ public function process_db_object() {
117117
return;
118118
}
119119

120-
$rows = array();
121120
$types = array();
122121
$total_time = 0;
123122
$has_result = false;
@@ -143,8 +142,6 @@ public function process_db_object() {
143142
* } $query
144143
*/
145144
foreach ( $wpdb->queries as $query ) {
146-
$has_trace = false;
147-
$has_result = false;
148145
$callers = array();
149146

150147
if ( isset( $query['query'], $query['elapsed'], $query['debug'] ) ) {
@@ -200,27 +197,13 @@ public function process_db_object() {
200197

201198
$this->log_type( $type );
202199
$this->log_caller( $caller_name, $ltime, $type );
203-
204200
$this->maybe_log_dupe( $sql, $i );
205201

206202
if ( $component ) {
207203
$this->log_component( $component, $ltime, $type );
208204
}
209205

210-
if ( ! isset( $types[ $type ]['total'] ) ) {
211-
$types[ $type ]['total'] = 1;
212-
} else {
213-
$types[ $type ]['total']++;
214-
}
215-
216-
if ( ! isset( $types[ $type ]['callers'][ $caller ] ) ) {
217-
$types[ $type ]['callers'][ $caller ] = 1;
218-
} else {
219-
$types[ $type ]['callers'][ $caller ]++;
220-
}
221-
222206
$is_main_query = ( $request === $sql && ( false !== strpos( $stack, ' WP->main,' ) ) );
223-
224207
$row = compact( 'caller', 'caller_name', 'sql', 'ltime', 'result', 'type', 'component', 'trace', 'is_main_query' );
225208

226209
if ( ! isset( $trace ) ) {
@@ -237,24 +220,19 @@ public function process_db_object() {
237220
$this->data->expensive[] = $row;
238221
}
239222

240-
$rows[ $i ] = $row;
223+
$this->data->rows[ $i ] = $row;
241224
$i++;
242-
243225
}
244226

245-
$total_qs = count( $rows );
246-
247-
$this->data->total_qs += $total_qs;
248-
$this->data->total_time += $total_time;
249-
250-
$has_main_query = wp_list_filter( $rows, array(
227+
$has_main_query = wp_list_filter( $this->data->rows, array(
251228
'is_main_query' => true,
252229
) );
253230

254-
# @TODO put errors in here too:
255-
# @TODO proper class instead of (object)
256-
$this->data->wpdb = (object) compact( 'rows', 'types', 'has_result', 'has_trace', 'total_time', 'total_qs', 'has_main_query' );
257-
231+
$this->data->total_qs = count( $this->data->rows );
232+
$this->data->total_time = $total_time;
233+
$this->data->has_result = $has_result;
234+
$this->data->has_trace = $has_trace;
235+
$this->data->has_main_query = ! empty( $has_main_query );
258236
}
259237

260238
/**

data/db_queries.php

+17-2
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,24 @@ class QM_Data_DB_Queries extends QM_Data {
2727
public $expensive;
2828

2929
/**
30-
* @var ?stdClass
30+
* @var array<int, array<string, mixed>>
31+
*/
32+
public $rows;
33+
34+
/**
35+
* @var bool
36+
*/
37+
public $has_result;
38+
39+
/**
40+
* @var bool
41+
*/
42+
public $has_trace;
43+
44+
/**
45+
* @var bool
3146
*/
32-
public $wpdb;
47+
public $has_main_query;
3348

3449
/**
3550
* @var ?array<string, array<string, mixed>>

output/html/db_callers.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function output() {
5050
echo '<tr>';
5151
echo '<th scope="col">' . esc_html__( 'Caller', 'query-monitor' ) . '</th>';
5252

53-
foreach ( $data->types as $type_name => $type_count ) {
53+
foreach ( array_keys( $data->types ) as $type_name ) {
5454
echo '<th scope="col" class="qm-num qm-ltr qm-sortable-column" role="columnheader">';
5555
echo $this->build_sorter( $type_name ); // WPCS: XSS ok;
5656
echo '</th>';
@@ -73,7 +73,7 @@ public function output() {
7373
echo self::build_filter_trigger( 'db_queries', 'caller', $row['caller'], '<code>' . esc_html( $row['caller'] ) . '</code>' ); // WPCS: XSS ok;
7474
echo '</td>';
7575

76-
foreach ( $data->types as $type_name => $type_count ) {
76+
foreach ( array_keys( $data->types ) as $type_name ) {
7777
if ( isset( $row['types'][ $type_name ] ) ) {
7878
echo "<td class='qm-num'>" . esc_html( number_format_i18n( $row['types'][ $type_name ] ) ) . '</td>';
7979
} else {

0 commit comments

Comments
 (0)