-
Notifications
You must be signed in to change notification settings - Fork 312
/
FailedQueries.php
186 lines (155 loc) · 4.55 KB
/
FailedQueries.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
/**
* Failed Queries report class
*
* @since 4.4.0
* @package elasticpress
*/
namespace ElasticPress\StatusReport;
use \ElasticPress\QueryLogger;
use \ElasticPress\Utils;
defined( 'ABSPATH' ) || exit;
/**
* FailedQueries report class
*
* @package ElasticPress
*/
class FailedQueries extends Report {
/**
* The logger instance
*
* @var QueryLogger
*/
protected $query_logger;
/**
* Class constructor
*
* @param QueryLogger $query_logger The logger instance
*/
public function __construct( QueryLogger $query_logger ) {
$this->query_logger = $query_logger;
}
/**
* Return the report title
*
* @return string
*/
public function get_title() : string {
return __( 'Failed Queries', 'elasticpress' );
}
/**
* Return the report fields
*
* @return array
*/
public function get_groups() : array {
$this->maybe_clear_logs();
$logs = $this->query_logger->get_logs( false );
$labels = [
'wp_url' => esc_html__( 'Page URL', 'elasticpress' ),
'es_req' => esc_html__( 'Elasticsearch Request', 'elasticpress' ),
'request_id' => esc_html__( 'Request ID', 'elasticpress' ),
'timestamp' => esc_html__( 'Time', 'elasticpress' ),
'query_time' => esc_html__( 'Time Spent (ms)', 'elasticpress' ),
'wp_args' => esc_html__( 'WP Query Args', 'elasticpress' ),
'status_code' => esc_html__( 'HTTP Status Code', 'elasticpress' ),
'body' => esc_html__( 'Query Body', 'elasticpress' ),
'result' => esc_html__( 'Query Result', 'elasticpress' ),
];
$groups = [];
foreach ( $logs as $log ) {
list( $error, $solution ) = $this->analyze_log( $log );
$fields = [
'error' => [
'label' => __( 'Error', 'elasticpress' ),
'value' => $error,
],
'recommended_solution' => [
'label' => __( 'Recommended Solution', 'elasticpress' ),
'value' => $solution,
],
];
foreach ( $log as $field => $value ) {
// Already outputted in the title
if ( in_array( $field, [ 'wp_url', 'timestamp' ], true ) ) {
continue;
}
$fields[ $field ] = [
'label' => $labels[ $field ] ?? $field,
'value' => $value,
];
}
$groups[] = [
'title' => sprintf( '%s (%s)', $log['wp_url'], date_i18n( 'Y-m-d H:i:s', $log['timestamp'] ) ),
'fields' => $fields,
];
}
return $groups;
}
/**
* Return the output of a button to clear the logged queries
*
* @return string
*/
public function get_actions() : array {
global $wp;
$logs = $this->query_logger->get_logs( false );
if ( empty( $logs ) ) {
return [];
}
$label = __( 'Clear query log', 'elasticpress' );
$href = wp_nonce_url( add_query_arg( [ $_GET ], $wp->request ), 'ep-clear-logged-queries', '_wpnonce' ); // phpcs:ignore WordPress.Security.NonceVerification
return [
[
'href' => $href,
'label' => $label,
],
];
}
/**
* If a nonce is present, clear the logs
*/
protected function maybe_clear_logs() {
if ( empty( $_GET['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( $_GET['_wpnonce'] ), 'ep-clear-logged-queries' ) ) {
return;
}
$this->query_logger->clear_logs();
if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
$redirect_url = network_admin_url( 'admin.php?page=elasticpress-status-report' );
} else {
$redirect_url = admin_url( 'admin.php?page=elasticpress-status-report' );
}
wp_safe_redirect( $redirect_url );
exit();
}
/**
* Given a log, try to find the error and its solution
*
* @param array $log The log
* @return array The error in index 0, solution in index 1
*/
public function analyze_log( $log ) {
if ( is_array( $log['result'] ) && ! empty( $log['result']['is_wp_error'] ) ) {
return [
$log['result']['message'],
__( 'It seems WordPress was not able to complete the request. Review the error message and your configuration.', 'elasticpress' ),
];
}
$error = Utils\get_elasticsearch_error_reason( $log );
$solution = ( ! empty( $error ) ) ?
( new \ElasticPress\ElasticsearchErrorInterpreter() )->maybe_suggest_solution_for_es( $error )['solution'] :
'';
return [ $error, $solution ];
}
/**
* DEPRECATED. Given an Elasticsearch error, try to suggest a solution
*
* @deprecated 5.0.0
* @param string $error The error
* @return string
*/
protected function maybe_suggest_solution_for_es( $error ) {
_deprecated_function( __METHOD__, '5.0.0', '\ElasticPress\ElasticsearchErrorInterpreter::maybe_suggest_solution_for_es()' );
return ( new \ElasticPress\ElasticsearchErrorInterpreter() )->maybe_suggest_solution_for_es( $error )['solution'];
}
}