-
-
Save mjgardner/aefb8d7f7817f0d32111530b3e4efddd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use Syntax::Construct qw< | |
operator-double-diamond | |
regex-named-capture-group | |
>; | |
use Regexp::Log::Common; | |
use Date::WeekNumber 'iso_week_number'; | |
use List::Util 1.33 'any'; | |
use Number::Format 'format_number'; | |
my $parser = Regexp::Log::Common->new( | |
format => ':extended', | |
capture => [qw<req ts status>], | |
); | |
my @fields = $parser->capture; | |
my $compiled_re = $parser->regexp; | |
my @skip_uri_patterns = qw< | |
^/+robots\.txt | |
[-\w]*sitemap[-\w]*\.xml | |
^/+wp- | |
/feed/?$ | |
^/+\?rest_route= | |
>; | |
my %month = ( | |
Jan => '01', Feb => '02', Mar => '03', Apr => '04', | |
May => '05', Jun => '06', Jul => '07', Aug => '08', | |
Sep => '09', Oct => '10', Nov => '11', Dec => '12', | |
); | |
my ( %count, %week_of ); | |
while (<<>>) { | |
my %log; | |
@log{@fields} = /$compiled_re/; | |
# only interested in successful or cached requests | |
next unless $log{status} =~ /^2/ or $log{status} == 304; | |
my ( $method, $uri, $protocol ) = split ' ', $log{req}; | |
next unless $method eq 'GET'; | |
next if any { $uri =~ $_ } @skip_uri_patterns; | |
# convert log timestamp to YYYY-MM-DD for Date::WeekNumber | |
$log{ts} =~ m!^ (?<day>\d\d) / (?<month>...) / (?<year>\d{4}) : !x; | |
my $date = "$+{year}-$month{ $+{month} }-$+{day}"; | |
my $week = iso_week_number($date); | |
$week_of{$week} ||= $date; | |
$count{$week}++; | |
} | |
printf "Week of %s: % 10s\n", $week_of{$_}, format_number( $count{$_} ) | |
for sort keys %count; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment