Skip to content
This repository has been archived by the owner on Nov 13, 2018. It is now read-only.

Commit

Permalink
update graphs to combine 3 api calls into one and to use continuous q…
Browse files Browse the repository at this point in the history
…ueries. This allows us to display more consistent data to the user
  • Loading branch information
dneuman64 committed Jan 25, 2016
1 parent f4c04e1 commit 03e3b5f
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 84 deletions.
84 changes: 60 additions & 24 deletions traffic_ops/app/lib/Extensions/TrafficStats/API/CacheStats.pm
Original file line number Diff line number Diff line change
Expand Up @@ -111,42 +111,78 @@ sub get_stat {
return $summary_content->{results}[0]{series}[0]{values}[0][1];
}

return "err";
return "";
}

sub current_bandwidth {
sub current_stats {
my $self = shift;
my $cdn = $self->param('cdnName');
my $query = "SELECT sum(value)/6 FROM \"bandwidth\" WHERE time < now() - 60s and time > now() - 120s";
if ($cdn) {
$query = "SELECT sum(value)/6 FROM \"bandwidth\" WHERE time < now() - 60s and time > now() - 120s and cdn = \'$cdn\'";
my @stats;
my $current_bw = $self->get_current_bandwidth();
my $conns = $self->get_current_connections();
my $capacity = $self->get_current_capacity();
my $rs = $self->db->resultset('Cdn');
while ( my $cdn = $rs->next ) {
my $cdn_name = $cdn->name;
my $bw = $current_bw->{$cdn_name};
my $conn = $conns->{$cdn_name};
my $cap = $capacity->{$cdn_name};
push(@stats, ({cdn => $cdn_name, bandwidth => $bw, connections => $conn, capacity => $cap}));
}
my $bandwidth = $self->get_stat("cache_stats", $query);
return $self->success({"bandwidth" => $bandwidth/1000000});
push(@stats, ({cdn => "total", bandwidth => $current_bw->{"total"}, connections => $conns->{"total"}}));
return $self->success({"currentStats" => \@stats});
}

sub current_connections {
sub get_current_bandwidth {
my $self = shift;
my $cdn = $self->param('cdnName');
my $query = "select sum(value)/6 from \"ats.proxy.process.http.current_client_connections\" where time > now() - 120s and time < now() - 60s";
if ($cdn) {
$query = "select sum(value)/6 from \"ats.proxy.process.http.current_client_connections\" where time > now() - 120s and time < now() - 60s and cdn = \'$cdn\'";
my $bw;
my $total_bw = 0;
my $rs = $self->db->resultset('Cdn');
while ( my $cdn = $rs->next ) {
my $cdn_name = $cdn->name;
my $query = "SELECT last(value) FROM \"monthly\".\"bandwidth.cdn.1min\" WHERE cdn = \'$cdn_name\'";
my $bandwidth = $self->get_stat("cache_stats", $query);
if ($bandwidth) {
$bw->{$cdn_name} = $bandwidth/1000000;
$total_bw += $bandwidth;
}
}
my $connections = $self->get_stat("cache_stats", $query);
return $self->success({"connections" => $connections});
$bw->{"total"} = $total_bw/1000000;
return $bw;
}

sub current_capacity {
sub get_current_connections {
my $self = shift;
my $cdn = $self->param('cdnName');
my $query = "select sum(value)/6 from \"maxKbps\" where time > now() - 120s and time < now() - 60s";
if ($cdn) {
$query = "select sum(value)/6 from \"maxKbps\" where time > now() - 120s and time < now() - 60s and cdn = \'$cdn\'";
my $conn;
my $total_conn = 0;
my $rs = $self->db->resultset('Cdn');
while ( my $cdn = $rs->next ) {
my $cdn_name = $cdn->name;
my $query = "select last(value) from \"monthly\".\"connections.cdn.1min\" where cdn = \'$cdn_name\'";
my $connections = $self->get_stat("cache_stats", $query);
if ($connections) {
$conn->{$cdn_name} = $connections;
$total_conn += $connections;
}
}
$conn->{"total"} = $total_conn;
return $conn;
}

sub get_current_capacity {
my $self = shift;
my $cap;
my $rs = $self->db->resultset('Cdn');
while ( my $cdn = $rs->next ) {
my $cdn_name = $cdn->name;
my $query = "select sum(value)/6 from \"maxKbps\" where time > now() - 120s and time < now() - 60s and cdn = \'$cdn_name\'";
my $capacity = $self->get_stat("cache_stats", $query);
if ($capacity) {
$capacity = $capacity/1000000; #convert to Gbps
$capacity = $capacity * 0.85; # need a better way to figure out percentage of max besides hard-coding
$cap->{$cdn_name} = $capacity;
}
}
my $capacity = $self->get_stat("cache_stats", $query);
$capacity = $capacity/1000000; #convert to Gbps
$capacity = $capacity * 0.85; # need a better way to figure out percentage of max besides hard-coding
return $self->success({"capacity" => $capacity});
return $cap;
}

sub daily_summary {
Expand Down
4 changes: 1 addition & 3 deletions traffic_ops/app/lib/TrafficOpsRoutes.pm
Original file line number Diff line number Diff line change
Expand Up @@ -778,10 +778,8 @@ sub traffic_stats_routes {
$r->get( "/api/$version/deliveryservice_stats" => [ format => [qw(json)] ] )->over( authenticated => 1 )
->to( 'DeliveryServiceStats#index', namespace => $namespace );
$r->get( "/api/$version/cache_stats" => [ format => [qw(json)] ] )->over( authenticated => 1 )->to( 'CacheStats#index', namespace => $namespace );
$r->get( "internal/api/$version/current_bandwidth" => [ format => [qw(json)] ] )->to( 'CacheStats#current_bandwidth', namespace => $namespace );
$r->get( "internal/api/$version/current_connections" => [ format => [qw(json)] ] )->to( 'CacheStats#current_connections', namespace => $namespace );
$r->get( "internal/api/$version/current_capacity" => [ format => [qw(json)] ] )->to( 'CacheStats#current_capacity', namespace => $namespace );
$r->get( "internal/api/$version/daily_summary" => [ format => [qw(json)] ] )->to( 'CacheStats#daily_summary', namespace => $namespace );
$r->get( "internal/api/$version/current_stats" => [ format => [qw(json)] ] )->to( 'CacheStats#current_stats', namespace => $namespace );
}

sub catch_all {
Expand Down
83 changes: 26 additions & 57 deletions traffic_ops/app/templates/visual_status/graphs.html.ep
Original file line number Diff line number Diff line change
Expand Up @@ -28,67 +28,36 @@ $(function () {
</script>
<script>

function refreshStats()
{
% foreach my $cdn_name (sort @{ $cdn_names } ) {
getCurrentBandwidth("<%= $cdn_name %>");
getCurrentConnections("<%= $cdn_name %>");
% }
getCurrentBandwidth();
getCurrentConnections();

}

function getCurrentBandwidth(cdn_name) {
var bwUrl = "/internal/api/1.2/current_bandwidth.json";
if (cdn_name != null) {
var bandwidth = 0;
// get bandwidth
$.getJSON( bwUrl + "?cdnName=" + cdn_name, function() {})
.done(function(data) {
bandwidth = data.response.bandwidth.toFixed(2);
$('#' + cdn_name + '_gbps_val').text(bandwidth);
// get capacity
var capacityUrl = "/internal/api/1.2/current_capacity.json";
$.getJSON( capacityUrl + "?cdnName=" + cdn_name, function() {})
.done(function(data) {
var capacity = Math.round(data.response.capacity);
var percentage = Math.round(bandwidth/capacity * 100);
setProgress(cdn_name + "_percent_indicator", percentage);
setText(cdn_name + "_percent_indicator", "avail: " + capacity + ", used: " + percentage);
$('#' + cdn_name + '_gbps_val').text(bandwidth);
});
});

}
else {
$.getJSON( bwUrl, function() {})
.done(function(data) {
$('#total_gbps_val').text(data.response.bandwidth.toFixed(2));
});
}
}

function getCurrentConnections(cdn_name) {
var url = "/internal/api/1.2/current_connections.json";
if (cdn_name != null) {
$.getJSON( url + "?cdnName=" + cdn_name, function() {})
.done(function(data) {
var connections = Math.round(data.response.connections).toString();
$('#' + cdn_name + '_conn_val').text(connections.replace(/\B(?=(\d{3})+(?!\d))/g, ",")); //toLocaleString doesnt work across browsers, this regex does.
});
}
else {
$.getJSON( url, function() {})
.done(function(data) {
var connections = Math.round(data.response.connections).toString();
function getCurrentStats() {
var bwURL = "/internal/api/1.2/current_stats.json";
$.getJSON( bwURL, function() {})
.done(function(data) {
$.each( data.response.currentStats, function( i, item ) {
% foreach my $cdn_name (sort @{ $cdn_names } ) {
var cdn_name = "<%= $cdn_name %>";
if (item.cdn == cdn_name) {
var bandwidth = item.bandwidth.toFixed(2);
$('#' + cdn_name + '_gbps_val').text(bandwidth);
var connections = Math.round(item.connections).toString();
$('#' + cdn_name + '_conn_val').text(connections.replace(/\B(?=(\d{3})+(?!\d))/g, ",")); //toLocaleString doesnt work across browsers, this regex does.
var capacity = Math.round(item.capacity);
var percentage = Math.round(bandwidth/capacity * 100);
setProgress(cdn_name + "_percent_indicator", percentage);
setText(cdn_name + "_percent_indicator", "avail: " + capacity + ", used: " + percentage);
}
if (item.cdn == "total") {
var bandwidth = item.bandwidth.toFixed(2);
$('#total_gbps_val').text(item.bandwidth.toFixed(2));
var connections = Math.round(item.connections).toString();
$('#total_conn_val').text(connections.replace(/\B(?=(\d{3})+(?!\d))/g, ",")); //toLocaleString doesnt work across browsers, this regex does.
});
}
}
% }
});
});
}

//refresh every 30 seconds
setInterval('refreshStats()', 30000);
setInterval('getCurrentStats()', 30000);

</script>
<script type="text/javascript" src="/js/percentagebar.js"></script>
Expand Down

0 comments on commit 03e3b5f

Please sign in to comment.