Skip to content

Commit

Permalink
log requests to get a simple history, UB-Mannheim#21
Browse files Browse the repository at this point in the history
js: make cgiUrl configurable
request logging: expose via action=history
  • Loading branch information
kba committed May 4, 2016
1 parent e9d34fa commit b359fdf
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
Binary file not shown.
7 changes: 5 additions & 2 deletions dist/js/ocr-gt-tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
var UISettings = {
zoomInFactor: 1.4,
zoomOutFactor: 0.8,
cgiUrl: 'ocr-gt-tools.cgi'
};
console.log(UISettings.cgiUrl);
console.log(UISettings.cgiUrl + '?action=save');

var Utils = {};

Expand Down Expand Up @@ -87,7 +90,7 @@ function loadGtEditLocation(url) {

$.ajax({
type: 'POST',
url: 'ocr-gt-tools.cgi?action=create',
url: UISettings.cgiUrl + '?action=create',
data: {'data_url': url},
beforeSend: function(xhr) {
// to instantly see when a new document has been retrieved
Expand Down Expand Up @@ -171,7 +174,7 @@ function saveGtEditLocation() {

$.ajax({
type: 'post',
url: 'ocr-gt-tools.cgi?action=save',
url: UISettings.cgiUrl + '?action=save',
data: window.ocrGtLocation,
success: function() {
// after #file_correction is saved
Expand Down
24 changes: 20 additions & 4 deletions ocr-gt-tools.cgi
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use warnings;
my $OCR_GT_BASEDIR;
my $ERRORLOG;
my $REQUESTLOG;
# my $DATE_FORMAT = "%Y-%m-%d %H:%M:%S";
my $DATE_FORMAT = "%H:%M:%S";
my $DATE_FORMAT = "%Y-%m-%d";
my $TIME_FORMAT = "%H:%M:%S";

use Data::Dumper;
use JSON;
Expand Down Expand Up @@ -46,7 +46,7 @@ sub debug
{
my $msg = sprintf(shift(), @_);
my $t = time;
my $timestamp = strftime $DATE_FORMAT, localtime $t;
my $timestamp = strftime $TIME_FORMAT, localtime $t;
$timestamp .= sprintf ".%03d", ($t-int($t))*1000; # without rounding
printf $ERRORLOG "%s: %s\n", $timestamp, $msg;
}
Expand All @@ -64,8 +64,12 @@ sub logRequest
if (!$url) {
$url = $cgi->param('imageUrl');
}
if (!$url) {
debug("No URL to log for this request");
return;
}
my $t = time;
my $timestamp = strftime $DATE_FORMAT, localtime $t;
my $timestamp = strftime "$DATE_FORMAT $TIME_FORMAT", localtime $t;
printf $REQUESTLOG "%s;%s;%s\n", $timestamp, $ENV{REMOTE_ADDR}, $url;
}

Expand Down Expand Up @@ -436,6 +440,8 @@ sub processRequest
processCreateRequest($cgi, $config);
} elsif ($action eq 'save') {
processSaveRequest($cgi, $config);
} elsif ($action eq 'history') {
processHistoryRequest($cgi, $config);
} else {
http400($cgi, "URL parameter 'action' must be 'create' or 'save', not %s", $action);
}
Expand Down Expand Up @@ -480,6 +486,16 @@ sub processSaveRequest
saveComments($cgi, $config, $location->{commentsTxt}, $pageComment, $lineComments);
return httpJSON($cgi, { result => 1 });
}
sub processHistoryRequest
{
my ($cgi, $config) = @_;
open my $RL, "<", "$OCR_GT_BASEDIR/log/request.log";
my @lines;
while (<$RL>) {
push @lines, [split(/;/, $_)];
}
return httpJSON($cgi, \@lines);
}

debugStandout('START REQUEST');
my $cgi = CGI->new;
Expand Down

0 comments on commit b359fdf

Please sign in to comment.