Skip to content

Commit 6b6a04c

Browse files
stats - final adjustments
1 parent 56545d9 commit 6b6a04c

11 files changed

+145
-34
lines changed

Root_AdminActions.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ private function _get_handler( $action ) {
4747
'licensing' => 'Licensing_AdminActions',
4848
'extensions' => 'Extensions_AdminActions',
4949
'default' => 'Generic_AdminActions_Default',
50-
'support' => 'Support_AdminActions'
50+
'support' => 'Support_AdminActions',
51+
'ustats' => 'UsageStatistics_AdminActions'
5152
);
5253
$handlers = apply_filters( 'w3tc_admin_actions', $handlers );
5354
}

UsageStatistics_AdminActions.php

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
namespace W3TC;
3+
4+
5+
6+
class UsageStatistics_AdminActions {
7+
private $_config = null;
8+
9+
10+
11+
public function __construct() {
12+
$this->_config = Dispatcher::config();
13+
}
14+
15+
16+
17+
public function w3tc_ustats_note_disable() {
18+
$this->_config->set( 'stats.enabled', false );
19+
$this->_config->save();
20+
21+
Util_Admin::redirect( array(), true );
22+
}
23+
}

UsageStatistics_GeneralPage_View.php

+27-10
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@
2727
'key' => 'stats.slot_seconds',
2828
'label' => __( 'Slot time (seconds):', 'w3-total-cache' ),
2929
'control' => 'textbox',
30+
'textbox_type' => 'number',
3031
'description' =>
3132
'The duration of time in seconds to collect statistics per interval.'
3233
) );
3334
Util_Ui::config_item( array(
3435
'key' => 'stats.slots_count',
3536
'label' => __( 'Slots collected:', 'w3-total-cache' ),
3637
'control' => 'textbox',
38+
'textbox_type' => 'number',
3739
'description' =>
3840
'The number of intervals that are represented in the graph.'
3941
) );
@@ -51,14 +53,6 @@
5153
'disabled' => ( $is_pro ? null : true ),
5254
'description' => __( 'Enable collecting statistics from an Access Log. This provides much more precise statistics.', 'w3-total-cache' )
5355
) );
54-
Util_Ui::config_item( array(
55-
'key' => 'stats.access_log.filename',
56-
'label' => __( 'Access Log Filename:', 'w3-total-cache' ),
57-
'control' => 'textbox',
58-
'textbox_size' => 60,
59-
'description' =>
60-
'Where your access log is located.'
61-
) );
6256
Util_Ui::config_item( array(
6357
'key' => 'stats.access_log.webserver',
6458
'label' => __( 'Webserver:', 'w3-total-cache' ),
@@ -67,8 +61,16 @@
6761
'apache' => 'Apache',
6862
'nginx' => 'Nginx'
6963
),
70-
'description' =>
71-
'Webserver type generating access logs.'
64+
'description' => 'Webserver type generating access logs.'
65+
) );
66+
Util_Ui::config_item( array(
67+
'key' => 'stats.access_log.filename',
68+
'label' => __( 'Access Log Filename:', 'w3-total-cache' ),
69+
'control' => 'textbox',
70+
'textbox_size' => 60,
71+
'description' => 'Where your access log is located.',
72+
'control_after' =>
73+
'<input type="button" class="button" id="ustats_access_log_test" value="Test" /><span id="ustats_access_log_test_result" style="padding-left: 20px"></span>'
7274
) );
7375
Util_Ui::config_item( array(
7476
'key' => 'stats.access_log.format',
@@ -100,4 +102,19 @@
100102
}
101103
jQuery('#stats__access_log__format').val(v);
102104
});
105+
106+
jQuery('#ustats_access_log_test').click(function() {
107+
var params = {
108+
action: 'w3tc_ajax',
109+
_wpnonce: w3tc_nonce,
110+
w3tc_action: 'ustats_access_log_test',
111+
filename: jQuery('#stats__access_log__filename').val()
112+
};
113+
114+
jQuery.post(ajaxurl, params, function(data) {
115+
jQuery('#ustats_access_log_test_result').text(data);
116+
}).fail(function() {
117+
jQuery('#ustats_access_log_test_result').text('Check failed');
118+
});
119+
});
103120
</script>

UsageStatistics_Page_View.css

+6-1
Original file line numberDiff line numberDiff line change
@@ -94,5 +94,10 @@ td.ustats_td_value {
9494
}
9595
.ustats_php_php_requests_pagecache_miss_level2 {
9696
width: 50%;
97-
white-space: nowrap;
97+
}
98+
99+
@media (max-width: 1500px) {
100+
.ustats_php_php_requests_pagecache_miss_level2 {
101+
width: 100%
102+
}
98103
}

UsageStatistics_Page_View.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,10 @@
8181
);
8282
$this->summary_item(
8383
'php_php_requests_pagecache_miss',
84-
'Not cached'
84+
'Not cached',
85+
false,
86+
'',
87+
'#990000'
8588
);
8689
echo '<div class="ustats_php_php_requests_pagecache_miss_level2_wrap">';
8790
$this->summary_item(
@@ -328,6 +331,8 @@
328331
<div class="ustats_dbcache_time_ms">
329332
Time taken: <span></span> ms
330333
</div>
334+
335+
<a href="?page=w3tc_stats&view=db_requests">Slowest requests (in debug mode only)</a>
331336
</div>
332337
<div class="ustats_block_chart">
333338
Time taken for database activity
@@ -346,8 +351,6 @@
346351
<div class="ustats_wpdb_calls_per_second">
347352
Calls/sec: <span></span>
348353
</div>
349-
350-
<a href="?page=w3tc_stats&view=db_requests">Slowest requests (in debug mode only)</a>
351354
</div>
352355
<div class="ustats_block_chart">
353356
Requests

UsageStatistics_Page_View_NoDebugMode.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<?php Util_Ui::postbox_header( __( 'Usage Statistics', 'w3-total-cache' ) ); ?>
1111

1212
<p>
13-
That data are collected only when Debug Mode enabled.
13+
Usage Statistics is collected only when Debug Mode is enabled.
1414
</p>
1515

1616
<a href="admin.php?page=w3tc_general#debug" class="button-primary">Enable it here</a>

UsageStatistics_Plugin_Admin.php

+40-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ function run() {
99

1010
add_filter( 'w3tc_admin_menu', array( $this, 'w3tc_admin_menu' ) );
1111
add_action( 'w3tc_ajax_ustats_get', array( $this, 'w3tc_ajax_ustats_get' ) );
12+
add_action( 'w3tc_ajax_ustats_access_log_test',
13+
array( $this, 'w3tc_ajax_ustats_access_log_test' ) );
1214

1315
add_filter( 'w3tc_usage_statistics_summary_from_history', array(
1416
'W3TC\UsageStatistics_Sources',
@@ -33,9 +35,26 @@ function run() {
3335
'admin_print_scripts_w3tc_stats'
3436
) );
3537

38+
add_action( 'w3tc_config_ui_save', array(
39+
$this,
40+
'w3tc_config_ui_save'
41+
), 10, 2 );
42+
3643
add_filter( 'w3tc_notes', array( $this, 'w3tc_notes' ) );
3744
}
3845

46+
#
47+
48+
49+
public function w3tc_config_ui_save( $config, $old_config ) {
50+
if ( $config->get( 'stats.slot_seconds' ) !=
51+
$old_config->get( 'stats.slot_seconds' ) ) {
52+
// flush all stats otherwise will be inconsistent
53+
$storage = new UsageStatistics_StorageWriter();
54+
$storage->reset();
55+
}
56+
}
57+
3958

4059

4160
public function w3tc_notes( $notes ) {
@@ -45,8 +64,13 @@ public function w3tc_notes( $notes ) {
4564
if ( $c->get_boolean( 'stats.enabled' ) &&
4665
!$state_master->get_boolean( 'common.hide_note_stats_enabled' ) ) {
4766
$notes['stats_enabled'] = sprintf(
48-
__( 'You’re running stats, it’s using Resources and not recommend to run continuously. %s',
67+
__( 'W3 Total Cache: Statistics collection is currently enabled. This consumes additional resources, and is not recommended to be run continuously. %s %s',
4968
'w3-total-cache' ),
69+
Util_Ui::button_link(
70+
__( 'Disable statistics', 'w3-total-cache' ),
71+
Util_Ui::url( array( 'w3tc_ustats_note_disable' => 'y' ) ),
72+
false, 'button',
73+
'w3tc_note_stats_disable' ),
5074
Util_Ui::button_hide_note2( array(
5175
'w3tc_default_config_state_master' => 'y',
5276
'key' => 'common.hide_note_stats_enabled',
@@ -83,4 +107,19 @@ public function w3tc_ajax_ustats_get() {
83107
echo json_encode( $summary );
84108
exit();
85109
}
110+
111+
112+
113+
public function w3tc_ajax_ustats_access_log_test() {
114+
$filename = $_REQUEST['filename'];
115+
116+
$filename = str_replace( '://', '/', $filename );
117+
$h = @fopen( $filename, 'rb' );
118+
if ( !$h ) {
119+
echo 'Failed to open file ' . $filename;
120+
} else {
121+
echo 'Success';
122+
}
123+
exit();
124+
}
86125
}

UsageStatistics_Source_AccessLog.php

+11-12
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,18 @@ static public function w3tc_usage_statistics_summary_from_history( $summary, $hi
6767

6868

6969

70-
public function __construct() {
71-
$c = Dispatcher::config();
72-
$accesslog_format = $c->get_string( 'stats.access_log.format' );
73-
$webserver = $c->get_string( 'stats.access_log.webserver' );
70+
/**
71+
* array( 'webserver', 'format', 'filename' )
72+
*/
73+
public function __construct( $data ) {
74+
$format = $data['format'];
75+
$webserver = $data['webserver'];
76+
$this->accesslog_filename = str_replace( '://', '/', $data['filename'] );
7477

7578
if ( $webserver == 'nginx' ) {
76-
$line_regexp = $this->logformat_to_regexp_nginx( $accesslog_format );
79+
$line_regexp = $this->logformat_to_regexp_nginx( $format );
7780
} else {
78-
$line_regexp = $this->logformat_to_regexp_apache( $accesslog_format );
81+
$line_regexp = $this->logformat_to_regexp_apache( $format );
7982
}
8083

8184
$this->line_regexp = apply_filters( 'w3tc_ustats_access_log_format_regexp',
@@ -92,14 +95,10 @@ public function w3tc_usage_statistics_history_set( $history ) {
9295
}
9396

9497
$this->history = $history;
95-
96-
$this->setup_history_item( count( $history ) - 1 );
97-
98-
$c = Dispatcher::config();
99-
$accesslog_filename = $c->get_string( 'stats.access_log.filename' );
10098
$this->min_time = time();
99+
$this->setup_history_item( count( $history ) - 1 );
101100

102-
$h = @fopen( $accesslog_filename, 'rb' );
101+
$h = @fopen( $this->accesslog_filename, 'rb' );
103102
if ( !$h ) {
104103
error_log( 'Failed to open access log for usage statisics collection' );
105104
return $history;

UsageStatistics_Sources.php

+12-2
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,12 @@ static public function w3tc_usage_statistics_summary_from_history( $summary, $hi
5656

5757
// access log data
5858
if ( $c->get_boolean( 'stats.access_log.enabled' ) ) {
59-
$o = new UsageStatistics_Source_AccessLog();
59+
$o = new UsageStatistics_Source_AccessLog( array(
60+
'webserver' => $c->get_string( 'stats.access_log.webserver' ),
61+
'filename' => $c->get_string( 'stats.access_log.filename' ),
62+
'format' => $c->get_string( 'stats.access_log.format' )
63+
) );
64+
6065
$summary = $o->w3tc_usage_statistics_summary_from_history(
6166
$summary, $history );
6267
}
@@ -111,7 +116,12 @@ static public function w3tc_usage_statistics_history_set( $history ) {
111116
$c = Dispatcher::config();
112117
if ( $c->get_boolean( 'stats.access_log.enabled' ) ) {
113118
// read access log
114-
$o = new UsageStatistics_Source_AccessLog();
119+
$o = new UsageStatistics_Source_AccessLog( array(
120+
'webserver' => $c->get_string( 'stats.access_log.webserver' ),
121+
'filename' => $c->get_string( 'stats.access_log.filename' ),
122+
'format' => $c->get_string( 'stats.access_log.format' )
123+
) );
124+
115125
$history = $o->w3tc_usage_statistics_history_set( $history );
116126
}
117127

UsageStatistics_StorageWriter.php

+11
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@ public function __construct() {
4848

4949

5050

51+
public function reset() {
52+
if ( !is_null( $this->cache_storage ) ) {
53+
$this->cache_storage->set( 'hotspot_endtime', 0 );
54+
}
55+
56+
update_site_option( 'w3tc_stats_hotspot_start', time() );
57+
update_site_option( 'w3tc_stats_history', '' );
58+
}
59+
60+
61+
5162
public function counter_add( $metric, $value ) {
5263
if ( !is_null( $this->cache_storage ) ) {
5364
$this->cache_storage->counter_add( $metric, $value );

Util_Ui.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,9 @@ static public function label( $id, $text ) {
352352
* @param bool $disabled
353353
* @param int $size
354354
*/
355-
static public function textbox( $id, $name, $value, $disabled = false, $size = 40 ) {
356-
echo '<input class="enabled" type="text" id="' . esc_attr( $id ) .
355+
static public function textbox( $id, $name, $value, $disabled = false, $size = 40, $type = 'text' ) {
356+
echo '<input class="enabled" type="' . esc_attr( $type ) .
357+
'" id="' . esc_attr( $id ) .
357358
'" name="' . esc_attr( $name ) .
358359
'" value="' . esc_attr( $value ) . '" ';
359360
disabled( $disabled );
@@ -597,7 +598,8 @@ static public function table_tr( $a ) {
597598
elseif ( $key == 'textbox' )
598599
Util_Ui::textbox( $id, $e['name'], $e['value'],
599600
( isset( $e['disabled'] ) ? $e['disabled'] : false ),
600-
( !empty( $e['size'] ) ? $e['size'] : 20 ) );
601+
( !empty( $e['size'] ) ? $e['size'] : 20 ),
602+
( !empty( $e['type'] ) ? $e['type'] : 'text' ) );
601603
elseif ( $key == 'textarea' )
602604
Util_Ui::textarea( $id, $e['name'], $e['value'],
603605
( isset( $e['disabled'] ) ? $e['disabled'] : false ) );
@@ -691,6 +693,7 @@ static public function config_item( $a ) {
691693
'name' => Util_Ui::config_key_to_http_name( $a['key'] ),
692694
'value' => $a['value'],
693695
'disabled' => $disabled,
696+
'type' => isset( $a['textbox_type'] ) ? $a['textbox_type'] : null,
694697
'size' => isset( $a['textbox_size'] ) ? $a['textbox_size'] : null
695698
);
696699
} else if ( $a['control'] == 'textarea' ) {

0 commit comments

Comments
 (0)