Util_UsageStatistics.php
2.97 KB
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
<?php
namespace W3TC;
class Util_UsageStatistics {
static public function bytes_to_size( $v ) {
if ( is_null( $v ) )
return 'n/a';
if ( $v > 500000000 )
return sprintf( '%.1f GB', $v / 1024 /*KB*/ / 1024 /*MB*/ / 1024/*GB*/ );
if ( $v > 500000 )
return sprintf( '%.1f MB', $v / 1024 /*KB*/ / 1024 /*MB*/ );
else
return sprintf( '%.1f KB', $v / 1024 /*KB*/ );
}
static public function bytes_to_size2( $a, $p1, $p2 = null, $p3 = null ) {
$v = self::v( $a, $p1, $p2, $p3 );
if ( is_null( $v ) )
return 'n/a';
return self::bytes_to_size( $v );
}
static public function percent( $v1, $v2 ) {
if ( $v2 == 0 )
return '0 %';
else
return sprintf( '%d', $v1 / $v2 * 100 ) . ' %';
}
static public function percent2( $a, $property1, $property2 ) {
if ( !isset( $a[$property1] ) || !isset( $a[$property2] ) )
return 'n/a';
else if ( $a[$property2] == 0 )
return '0 %';
else
return sprintf( '%d', $a[$property1] / $a[$property2] * 100 ) . ' %';
}
static public function sum( $history, $property ) {
$v = 0;
foreach ( $history as $i ) {
if ( !empty( $i[$property] ) )
$v += $i[$property];
}
return $v;
}
static public function time_mins( $timestamp ) {
return date( 'm/d/Y H:i', $timestamp );
}
static public function integer( $v ) {
return number_format( $v );
}
static public function integer2( $a, $p1, $p2 = null, $p3 = null ) {
$v = self::v( $a, $p1, $p2, $p3 );
if ( is_null( $v ) )
return 'n/a';
else
return number_format( $v );
}
static public function v( $a, $p1, $p2 = null, $p3 = null ) {
if ( !isset( $a[$p1] ) )
return null;
$v = $a[$p1];
if ( is_null( $p2 ) )
return $v;
if ( !isset( $v[$p2] ) )
return null;
$v = $v[$p2];
if ( is_null( $p3 ) )
return $v;
if ( !isset( $v[$p3] ) )
return null;
return $v[$p3];
}
static public function value_per_second( $a, $property1, $property2 ) {
if ( !isset( $a[$property1] ) || !isset( $a[$property2] ) )
return 'n/a';
else if ( $a[$property2] == 0 )
return '0';
else
return sprintf( '%.1f', $a[$property1] / $a[$property2] * 100 );
}
static public function value_per_period_seconds( $total, $summary ) {
if ( empty( $summary['period']['seconds'] ) )
return 'n/a';
$period_seconds = $summary['period']['seconds'];
return sprintf( '%.1f', $total / $period_seconds );
}
/**
* Special shared code for cache size counting
*/
static public function get_or_init_size_transient( $transient, $summary ) {
$should_count = false;
$v = get_transient( $transient );
if ( is_array( $v ) && isset( $v['timestamp_end'] ) &&
$v['timestamp_end'] == $summary['period']['timestamp_end'] ) {
return array( $v, false );
}
// limit number of processing counting it at the same time
$v = array(
'timestamp_end' => $summary['period']['timestamp_end'],
'size_used' => '...counting',
'items' => '...counting'
);
set_transient( $transient, $v, 120 );
return array( $v, true );
}
}