Skip to content

Commit

Permalink
Switch to a DTO for the HTTP API collector.
Browse files Browse the repository at this point in the history
  • Loading branch information
johnbillion committed Apr 23, 2022
1 parent 74457b5 commit ee43e20
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 40 deletions.
4 changes: 2 additions & 2 deletions classes/Collector.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ final public function id() {
}

/**
* @param string $type
* @param string|int $type
* @return void
*/
protected function log_type( $type ) {
Expand All @@ -81,7 +81,7 @@ protected function log_type( $type ) {
/**
* @param stdClass $component
* @param float $ltime
* @param string $type
* @param string|int $type
* @return void
*/
protected function log_component( $component, $ltime, $type ) {
Expand Down
2 changes: 1 addition & 1 deletion classes/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/
abstract class QM_Data implements \ArrayAccess {
/**
* @var array<string, mixed>
* @var array<string|int, mixed>
*/
public $types = array();

Expand Down
4 changes: 2 additions & 2 deletions classes/DataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final public function get_data() {
}

/**
* @param string $type
* @param string|int $type
* @return void
*/
protected function log_type( $type ) {
Expand All @@ -36,7 +36,7 @@ protected function log_type( $type ) {
/**
* @param stdClass $component
* @param float $ltime
* @param string $type
* @param string|int $type
* @return void
*/
protected function log_component( $component, $ltime, $type ) {
Expand Down
43 changes: 25 additions & 18 deletions collectors/http.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
exit;
}

class QM_Collector_HTTP extends QM_Collector {
/**
* @extends QM_DataCollector<QM_Data_HTTP>
*/
class QM_Collector_HTTP extends QM_DataCollector {

/**
* @var string
Expand All @@ -26,6 +29,10 @@ class QM_Collector_HTTP extends QM_Collector {
*/
private $info = null;

public function get_storage() {
return new QM_Data_HTTP();
}

/**
* @return void
*/
Expand Down Expand Up @@ -165,12 +172,12 @@ public function filter_http_request_args( array $args, $url ) {
// Something has triggered another HTTP request from within the `pre_http_request` filter
// (eg. WordPress Beta Tester does this). This allows for one level of nested queries.
$args['_qm_original_key'] = $args['_qm_key'];
$start = $this->data['http'][ $args['_qm_key'] ]['start'];
$start = $this->data->http[ $args['_qm_key'] ]['start'];
} else {
$start = microtime( true );
}
$key = microtime( true ) . $url;
$this->data['http'][ $key ] = array(
$this->data->http[ $key ] = array(
'url' => $url,
'args' => $args,
'start' => $start,
Expand Down Expand Up @@ -222,9 +229,9 @@ public function action_http_api_debug( $response, $action, $class, $args, $url )

case 'response':
if ( ! empty( $class ) ) {
$this->data['http'][ $args['_qm_key'] ]['transport'] = str_replace( 'wp_http_', '', strtolower( $class ) );
$this->data->http[ $args['_qm_key'] ]['transport'] = str_replace( 'wp_http_', '', strtolower( $class ) );
} else {
$this->data['http'][ $args['_qm_key'] ]['transport'] = null;
$this->data->http[ $args['_qm_key'] ]['transport'] = null;
}

$this->log_http_response( $response, $args, $url );
Expand Down Expand Up @@ -280,20 +287,20 @@ public function action_fsockopen_after_request( $headers, array $info = null ) {
* @return void
*/
public function log_http_response( $response, array $args, $url ) {
$this->data['http'][ $args['_qm_key'] ]['end'] = microtime( true );
$this->data['http'][ $args['_qm_key'] ]['response'] = $response;
$this->data['http'][ $args['_qm_key'] ]['args'] = $args;
$this->data->http[ $args['_qm_key'] ]['end'] = microtime( true );
$this->data->http[ $args['_qm_key'] ]['response'] = $response;
$this->data->http[ $args['_qm_key'] ]['args'] = $args;
if ( isset( $args['_qm_original_key'] ) ) {
$this->data['http'][ $args['_qm_original_key'] ]['end'] = $this->data['http'][ $args['_qm_original_key'] ]['start'];
$this->data['http'][ $args['_qm_original_key'] ]['response'] = new WP_Error( 'http_request_not_executed', sprintf(
$this->data->http[ $args['_qm_original_key'] ]['end'] = $this->data->http[ $args['_qm_original_key'] ]['start'];
$this->data->http[ $args['_qm_original_key'] ]['response'] = new WP_Error( 'http_request_not_executed', sprintf(
/* translators: %s: Hook name */
__( 'Request not executed due to a filter on %s', 'query-monitor' ),
'pre_http_request'
) );
}

$this->data['http'][ $args['_qm_key'] ]['info'] = $this->info;
$this->data['http'][ $args['_qm_key'] ]['transport'] = $this->transport;
$this->data->http[ $args['_qm_key'] ]['info'] = $this->info;
$this->data->http[ $args['_qm_key'] ]['transport'] = $this->transport;
$this->info = null;
$this->transport = null;
}
Expand All @@ -302,9 +309,9 @@ public function log_http_response( $response, array $args, $url ) {
* @return void
*/
public function process() {
$this->data['ltime'] = 0;
$this->data->ltime = 0;

if ( ! isset( $this->data['http'] ) ) {
if ( ! isset( $this->data->http ) ) {
return;
}

Expand All @@ -322,7 +329,7 @@ public function process() {

$home_host = (string) parse_url( home_url(), PHP_URL_HOST );

foreach ( $this->data['http'] as $key => & $http ) {
foreach ( $this->data->http as $key => & $http ) {

if ( ! isset( $http['response'] ) ) {
// Timed out
Expand All @@ -332,15 +339,15 @@ public function process() {

if ( is_wp_error( $http['response'] ) ) {
if ( ! in_array( $http['response']->get_error_code(), $silent, true ) ) {
$this->data['errors']['alert'][] = $key;
$this->data->errors['alert'][] = $key;
}
$http['type'] = -1;
} elseif ( ! $http['args']['blocking'] ) {
$http['type'] = -2;
} else {
$http['type'] = intval( wp_remote_retrieve_response_code( $http['response'] ) );
if ( $http['type'] >= 400 ) {
$this->data['errors']['warning'][] = $key;
$this->data->errors['warning'][] = $key;
}
}

Expand All @@ -355,7 +362,7 @@ public function process() {
}
}

$this->data['ltime'] += $http['ltime'];
$this->data->ltime += $http['ltime'];

$host = (string) parse_url( $http['url'], PHP_URL_HOST );

Expand Down
42 changes: 42 additions & 0 deletions data/http.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* HTTP data transfer object.
*
* @package query-monitor
*/

class QM_Data_HTTP extends QM_Data {
/**
* @var array<string, array<string, mixed>>
* @phpstan-var array<string, array{
* args: array<string, mixed>,
* component: stdClass,
* end: float,
* filtered_trace: array<int, array<string, mixed>>,
* info: array<string, mixed>|null,
* local: bool,
* ltime: float,
* redirected_to?: string,
* response: mixed[]|WP_Error,
* start: float,
* transport: string|null,
* type: int,
* url: string,
* }>
*/
public $http;

/**
* @var float
*/
public $ltime;

/**
* @var array<string, array<int, string>>
* @phpstan-var array{
* alert?: array<int, string>,
* warning?: array<int, string>,
* }
*/
public $errors;
}
28 changes: 14 additions & 14 deletions output/html/http.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ public function name() {
* @return void
*/
public function output() {

/** @var QM_Data_HTTP $data */
$data = $this->collector->get_data();

if ( ! empty( $data['http'] ) ) {
$statuses = array_keys( $data['types'] );
$components = wp_list_pluck( $data['component_times'], 'component' );
if ( ! empty( $data->http ) ) {
$statuses = array_keys( $data->types );
$components = wp_list_pluck( $data->component_times, 'component' );

usort( $statuses, 'strcasecmp' );
usort( $components, 'strcasecmp' );
Expand Down Expand Up @@ -79,7 +79,7 @@ public function output() {
echo '<tbody>';
$i = 0;

foreach ( $data['http'] as $key => $row ) {
foreach ( $data->http as $key => $row ) {
$ltime = $row['ltime'];
$i++;
$is_error = false;
Expand Down Expand Up @@ -294,8 +294,8 @@ public function output() {
echo '</tbody>';
echo '<tfoot>';

$total_stime = number_format_i18n( $data['ltime'], 4 );
$count = count( $data['http'] );
$total_stime = number_format_i18n( $data->ltime, 4 );
$count = count( $data->http );

echo '<tr>';
printf(
Expand Down Expand Up @@ -326,13 +326,13 @@ public function output() {
* @return array<int, string>
*/
public function admin_class( array $class ) {

/** @var QM_Data_HTTP $data */
$data = $this->collector->get_data();

if ( isset( $data['errors']['alert'] ) ) {
if ( isset( $data->errors['alert'] ) ) {
$class[] = 'qm-alert';
}
if ( isset( $data['errors']['warning'] ) ) {
if ( isset( $data->errors['warning'] ) ) {
$class[] = 'qm-warning';
}

Expand All @@ -345,10 +345,10 @@ public function admin_class( array $class ) {
* @return array<string, mixed[]>
*/
public function admin_menu( array $menu ) {

/** @var QM_Data_HTTP $data */
$data = $this->collector->get_data();

$count = isset( $data['http'] ) ? count( $data['http'] ) : 0;
$count = ! empty( $data->http ) ? count( $data->http ) : 0;

$title = ( empty( $count ) )
? __( 'HTTP API Calls', 'query-monitor' )
Expand All @@ -362,10 +362,10 @@ public function admin_menu( array $menu ) {
) ),
);

if ( isset( $data['errors']['alert'] ) ) {
if ( isset( $data->errors['alert'] ) ) {
$args['meta']['classname'] = 'qm-alert';
}
if ( isset( $data['errors']['warning'] ) ) {
if ( isset( $data->errors['warning'] ) ) {
$args['meta']['classname'] = 'qm-warning';
}

Expand Down
7 changes: 4 additions & 3 deletions output/raw/http.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,16 @@ public function name() {
*/
public function get_output() {
$output = array();
/** @var QM_Data_HTTP $data */
$data = $this->collector->get_data();

if ( empty( $data['http'] ) ) {
if ( empty( $data->http ) ) {
return $output;
}

$requests = array();

foreach ( $data['http'] as $http ) {
foreach ( $data->http as $http ) {
$stack = array();

if ( isset( $http['trace'] ) ) {
Expand All @@ -55,7 +56,7 @@ public function get_output() {
}

$output['total'] = count( $requests );
$output['time'] = (float) number_format_i18n( $data['ltime'], 4 );
$output['time'] = (float) number_format_i18n( $data->ltime, 4 );
$output['requests'] = $requests;

return $output;
Expand Down

0 comments on commit ee43e20

Please sign in to comment.