]>
Commit | Line | Data |
---|---|---|
a832cd59 MG |
1 | package Gruntmaster::Opener; |
2 | use 5.014; | |
3 | use warnings; | |
4 | ||
5 | use parent qw/Exporter/; | |
6 | use re '/s'; | |
7 | ||
8 | our @EXPORT = qw/handle_line/; | |
9 | our @EXPORT_OK = @EXPORT; | |
add75bf5 | 10 | our $VERSION = '5999.000_015'; |
a832cd59 MG |
11 | |
12 | use Date::Parse qw/str2time/; | |
13 | use Gruntmaster::Data; | |
14 | ||
15 | sub _analyze_request { | |
16 | s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg for @_; # From URI::Escape POD | |
17 | my ($req, $parms) = @_; | |
18 | return unless $parms =~ /contest=(\w+)/; | |
19 | my $ct = $1; | |
20 | return $req =~ m,/pb/(\w+), ? ($1, $ct) : (); | |
21 | } | |
22 | ||
23 | sub handle_line { | |
24 | my ($owner, $datetime, $request, $parms) = $_[0] =~ | |
25 | /(\w+)\s # user | |
26 | \[([^]]+)\]\s # date | |
27 | "\w+\s # request method | |
28 | ([^" ?]+) # URL (without query string) | |
29 | [?] | |
30 | ([^" ]+)\s # query string | |
31 | [^"]+"\s # HTTP version | |
32 | 2 # response code starts with 2 | |
33 | /x or return; | |
34 | my ($pb, $ct) = _analyze_request $request, $parms or return; | |
35 | my $time = str2time $datetime; | |
36 | open_problem $ct, $pb, $owner, $time; | |
37 | } | |
38 | ||
39 | 1; | |
40 | __END__ | |
41 | ||
42 | =encoding utf-8 | |
43 | ||
44 | =head1 NAME | |
45 | ||
46 | Gruntmaster::Opener - Populate opens table from NCSA access logs | |
47 | ||
48 | =head1 SYNOPSIS | |
49 | ||
50 | use Gruntmaster::Opener; | |
51 | ||
52 | open my $fh, '<', '/var/log/apache2/access.log'; | |
53 | handle_line $_ while <$fh>; | |
54 | ||
55 | =head1 DESCRIPTION | |
56 | ||
57 | Gruntmaster::Opener is the backend of the L<gruntmaster-opener> script | |
58 | that reads NCSA-style access logs, finds lines that represent | |
59 | successful requests to problems during contests, extracts data from | |
60 | them and inserts it into the database. | |
61 | ||
62 | B<handle_line>($line) | |
63 | ||
64 | The only function in this module. Exported by default. Takes a single | |
65 | parameter, a line from a logfile in NCSA common/combined format. | |
66 | ||
67 | If the request described in the given line: | |
68 | ||
69 | =over | |
70 | ||
71 | =item * | |
72 | ||
73 | Is successful (response code is 2xx) | |
74 | ||
75 | =item * | |
76 | ||
77 | Targets a problem (C</pb/something>) | |
78 | ||
79 | =item * | |
80 | ||
81 | Has a query parameter named C<contest> | |
82 | ||
83 | =item * | |
84 | ||
85 | Happened during the contest named in the C<contest> query parameter | |
86 | (this restriction is enforced by the B<open_problem> function). | |
87 | ||
88 | =back | |
89 | ||
90 | an entry is added to the C<opens> table, using the B<open_problem> | |
91 | function from L<Gruntmaster::Data>. | |
92 | ||
93 | =head1 SEE ALSO | |
94 | ||
95 | L<gruntmaster-opener> | |
96 | ||
97 | =head1 AUTHOR | |
98 | ||
99 | Marius Gavrilescu E<lt>marius@ieval.roE<gt> | |
100 | ||
101 | =head1 COPYRIGHT AND LICENSE | |
102 | ||
e1b9f3dd | 103 | Copyright (C) 2014-2015 by Marius Gavrilescu |
a832cd59 MG |
104 | |
105 | This library is free software; you can redistribute it and/or modify | |
e1b9f3dd | 106 | it under the same terms as Perl itself, either Perl version 5.20.1 or, |
a832cd59 MG |
107 | at your option, any later version of Perl 5 you may have available. |
108 | ||
109 | ||
110 | =cut |