This repository has been archived by the owner on Feb 23, 2019. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
Cdn_Highwinds_Widget.php
105 lines (81 loc) · 2.65 KB
/
Cdn_Highwinds_Widget.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
<?php
namespace W3TC;
class Cdn_Highwinds_Widget {
static public function admin_init_w3tc_dashboard() {
$o = new Cdn_Highwinds_Widget();
add_action( 'admin_print_styles',
array( $o, 'admin_print_styles' ) );
add_action( 'admin_print_scripts',
array( $o, 'admin_print_scripts' ) );
add_action( 'w3tc_widget_setup',
array( $o, 'w3tc_widget_setup' ) );
}
public function w3tc_widget_setup() {
Util_Widget::add( 'w3tc_highwinds',
'<div class="w3tc-widget-highwinds-logo"></div>',
array( $this, 'widget_form' ),
Util_Ui::admin_url( 'admin.php?page=w3tc_cdn' ),
'normal' );
}
public function widget_form() {
$c = Dispatcher::config();
$account_hash = $c->get_string( 'cdn.highwinds.account_hash' );
if ( empty( $account_hash ) ) {
include W3TC_DIR . '/Cdn_Highwinds_Widget_View_NotConfigured.php';
return;
}
$url_manage = 'https://striketracker3.highwinds.com/accounts/' .
$account_hash . '/configure/hosts';
$url_analyze = 'https://striketracker3.highwinds.com/accounts/' .
$account_hash . '/analyze/overview';
$url_purge = Util_Ui::url( array(
'page' => 'w3tc_cdn',
'w3tc_cdn_purge' => 'y'
) );
include W3TC_DIR . '/Cdn_Highwinds_Widget_View.php';
}
public function admin_print_styles() {
wp_enqueue_style( 'w3tc-widget' );
wp_enqueue_style( 'w3tc-highwinds-widget',
plugins_url( 'Cdn_Highwinds_Widget_View.css', W3TC_FILE ),
array(), W3TC_VERSION );
}
public function admin_print_scripts() {
wp_enqueue_script( 'google-jsapi', 'https://www.google.com/jsapi' );
wp_enqueue_script( 'w3tc-highwinds-widget',
plugins_url( 'Cdn_Highwinds_Widget_View.js', W3TC_FILE ),
array(), W3TC_VERSION );
}
static public function w3tc_ajax_cdn_highwinds_widgetdata() {
try {
$core = Dispatcher::component( 'Cdn_Core' );
$cdn = $core->get_cdn();
$analytics = $cdn->service_analytics_transfer();
$sum_mbytes = 0;
$sum_mbps = 0;
$sum_rps = 0;
$graph = array( array( 'Date', 'Requests' ) );
$count = count( $analytics );
foreach ( $analytics as $item ) {
$sum_mbytes += $item['xferUsedTotalMB'];
$sum_mbps += $item['xferRateMeanMbps'];
$sum_rps += $item['rpsMean'];
$graph[] = array(
gmdate( 'd M', $item['usageTime'] / 1000 ),
$item['requestsCountTotal']
);
}
$response = array(
'transferred_size' => Util_Ui::format_mbytes( $sum_mbytes / $count ),
'average_mbps' => sprintf( '%.2f', $sum_mbps / $count ),
'average_rps' => sprintf( '%.2f', $sum_rps / $count ),
'graph' => $graph
);
echo json_encode( $response );
} catch ( \Exception $e ) {
echo json_encode( array(
'error' => $e->getMessage()
) );
}
}
}