Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #1540 from CPAN-API/leo/fastly_stats
Create a /about/stats page that queries Fastly for our stats
  • Loading branch information
oalders committed Jun 4, 2015
2 parents 997d593 + 7eb504b commit da204bb
Show file tree
Hide file tree
Showing 9 changed files with 276 additions and 6 deletions.
2 changes: 2 additions & 0 deletions cpanfile
Expand Up @@ -29,6 +29,7 @@ requires 'Digest::SHA1';
requires 'ElasticSearch';
requires 'Encode', '2.51';
requires 'Exporter';
requires 'Format::Human::Bytes';
requires 'File::Path';
requires 'Gravatar::URL';
requires 'HTML::Escape';
Expand Down Expand Up @@ -72,6 +73,7 @@ requires 'Regexp::Common';
requires 'Regexp::Common::time';
requires 'Starman', '>= 0.4008';
requires 'Template::Alloy';
requires 'Template::Plugin::Comma';
requires 'Template::Plugin::DateTime';
requires 'Template::Plugin::JSON';
requires 'Template::Plugin::Markdown';
Expand Down
22 changes: 16 additions & 6 deletions cpanfile.snapshot
Expand Up @@ -2495,6 +2495,13 @@ DISTRIBUTIONS
Filesys::Notify::Simple 0.12
requirements:
ExtUtils::MakeMaker 6.30
Format-Human-Bytes-0.06
pathname: S/SE/SEWI/Format-Human-Bytes-0.06.tar.gz
provides:
Format::Human::Bytes 0.06
requirements:
ExtUtils::MakeMaker 6.42
Test::More 0
Getopt-Long-Descriptive-0.097
pathname: R/RJ/RJBS/Getopt-Long-Descriptive-0.097.tar.gz
provides:
Expand Down Expand Up @@ -2955,6 +2962,7 @@ DISTRIBUTIONS
provides:
JSON::MaybeXS 1.002002
requirements:
Cpanel::JSON::XS 2.3310
ExtUtils::CBuilder 0.27
ExtUtils::MakeMaker 0
File::Spec 0
Expand Down Expand Up @@ -5507,6 +5515,14 @@ DISTRIBUTIONS
requirements:
Digest::MD5 1
ExtUtils::MakeMaker 0
Template-Plugin-Comma-0.04
pathname: M/MI/MIYAGAWA/Template-Plugin-Comma-0.04.tar.gz
provides:
Template::Plugin::Comma 0.04
requirements:
ExtUtils::MakeMaker 0
Template 2.07
Test::More 0.32
Template-Plugin-DateTime-0.06002
pathname: D/DM/DMAKI/Template-Plugin-DateTime-0.06002.tar.gz
provides:
Expand Down Expand Up @@ -6039,12 +6055,6 @@ DISTRIBUTIONS
Test::Builder::Tester 1.02
Test::More 0
perl 5.006
Test-Without-Module-0.17
pathname: C/CO/CORION/Test-Without-Module-0.17.tar.gz
provides:
Test::Without::Module 0.17
requirements:
ExtUtils::MakeMaker 0
Test-XPath-0.16
pathname: D/DW/DWHEELER/Test-XPath-0.16.tar.gz
provides:
Expand Down
53 changes: 53 additions & 0 deletions lib/MetaCPAN/Web/Controller/About.pm
@@ -1,6 +1,7 @@
package MetaCPAN::Web::Controller::About;

use Moose;
use Format::Human::Bytes;

BEGIN { extends 'MetaCPAN::Web::Controller' }

Expand Down Expand Up @@ -44,6 +45,58 @@ sub metadata : Local {
$c->stash( template => 'about/metadata.html' );
}

sub stats : Local {
my ( $self, $c ) = @_;

$c->add_surrogate_key('html');
$c->add_surrogate_key('stats');
$c->cdn_cache_ttl(86_400); # 1 day

$c->stash( template => 'about/stats.html' );

# See if user has the fastly credentials
if ( my $fastly = $c->_net_fastly() ) {

my @interested_fields = qw(hit_ratio bandwidth requests);

my $fastly_services = $c->config->{fastly}->{service};

my %all_stats;
foreach my $service ( @{$fastly_services} ) {

next unless $service->{display_on_stats_page};

my $stats_from_fastly = $fastly->stats(
service => $service->{id},
by => 'day',
);

my %site_stats;

# build [ { time => $time, y => $value }, {} ]
map {
my $data = $_;
my $time = $data->{start_time};
map {
push @{ $site_stats{$_} },
{ time => $time, y => $data->{$_} };
$site_stats{totals}->{$_} += $data->{$_};
} @interested_fields;

} @{$stats_from_fastly};

$site_stats{totals}->{bandwidth} = Format::Human::Bytes->base10(
$site_stats{totals}->{bandwidth} );

$all_stats{ $service->{name} } = \%site_stats;
}

$c->stash->{fastly_stats} = \%all_stats;

}

}

__PACKAGE__->meta->make_immutable;

1;
80 changes: 80 additions & 0 deletions root/about/stats.html
@@ -0,0 +1,80 @@
<%
title = "MetaCPAN Network stats";

# tell wrapper to load js/css required
stats_page = 1;

USE Comma;
%>
<% PROCESS inc/about-bar.html %>

<div class="content about epoch-theme-light">

<script>
var monthNames = [
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
"Aug", "Sep", "Oct", "Nov", "Dec"
];
</script>

<h2>Networks stats for the last 30 days</h2>

<% UNLESS fastly_stats.keys %>
This must be run on a box that has access to
the fastly_api_key, production usually
<% END %>

<%
# Define styles to use
css_category_lookup = {
requests => 'category10',
bandwidth => 'category20b',
hit_ratio => 'category20c',
};

# Now loop the sites we have stats on
FOREACH site IN fastly_stats.keys.sort;

# Data for this site
site_data = fastly_stats.$site;

# A key for the div/data
sitekey = site.replace(/\./,'_');
%>
<h2><em><% site %></em></h2>

<% FOREACH type IN [ 'requests', 'bandwidth' ] %>

<h4><% type | ucfirst %>: <% site_data.totals.$type | comma %></h4>

<div class="epoch <% css_category_lookup.$type %>" id="chart-<% sitekey %>-<% type %>" style="width: 100%; height: 200px"></div>

<script>
var chartData<% sitekey %><% type %> = [
{
label: "<% type %>",
values: [
<% FOREACH data_point = site_data.$type %>
{x: <% data_point.time %>, y: <% data_point.y %>}<%- ',' UNLESS loop.last -%>
<% END %>
]
}<% ', ' UNLESS loop.last %>
];
$('#chart-<% sitekey %>-<% type %>').epoch({
type: 'area',
data: chartData<% sitekey %><% type %>,
margins: { right: 10 },
ticks: { bottom: 5 },
tickFormats: { bottom: function(d) {
var dt = new Date(0); // The 0 there is the key, which sets the date to the epoch
dt.setUTCSeconds(d);
return monthNames[dt.getMonth()] + ' ' + dt.getDate();
}}
});
</script>
<% END %>

<% END %>

</div>
3 changes: 3 additions & 0 deletions root/inc/about-bar.html
Expand Up @@ -28,5 +28,8 @@
<li<% IF req.action == 'about/metadata' %> class="active"<% END %>>
<a href="/about/metadata">Distribution Metadata</a>
</li>
<li<% IF req.action == 'about/stats' %> class="active"<% END %>>
<a href="/about/stats">Network Stats</a>
</li>
</ul>
</div>
1 change: 1 addition & 0 deletions root/static/stats/epoch.min.css

Large diffs are not rendered by default.

0 comments on commit da204bb

Please sign in to comment.