Skip to content

Instantly share code, notes, and snippets.

@mjgardner
Last active September 15, 2021 17:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjgardner/aefb8d7f7817f0d32111530b3e4efddd to your computer and use it in GitHub Desktop.
Save mjgardner/aefb8d7f7817f0d32111530b3e4efddd to your computer and use it in GitHub Desktop.
#!/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