Skip to content

Commit

Permalink
Merge utility for sanity-checking log files
Browse files Browse the repository at this point in the history
  • Loading branch information
dreeves committed Mar 19, 2013
1 parent 5fc394c commit 7fc847b
Show file tree
Hide file tree
Showing 9 changed files with 390 additions and 271 deletions.
22 changes: 6 additions & 16 deletions cntpings.pl
@@ -1,19 +1,12 @@
#!/usr/bin/env perl #!/usr/bin/env perl
# Count the number of a pings with given tags in the given time period. # Count the number of a pings with given tags in the given time period.
# Related scripts:
# aux/contest.pl -- for chrock contests
# aux/frask.pl -- a different kind of chrock contest
# aux/tot.pl -- customized for keeping track of yahoo job pings
# aux/showpie.pl -- this one is just wrong given how we currently use tagtime
# aux/showvenn.pl -- not sure about this one
# ../kibo/timepiekib.pl -- converts tagtime log to kib file


BEGIN { require "$ENV{HOME}/.tagtimerc"; } BEGIN { require "$ENV{HOME}/.tagtimerc"; }
use lib $path, "$path/lib"; use lib $path, "$path/lib";


require "util.pl"; require "util.pl";
use Getopt::Long qw(:config bundling); use Getopt::Long qw(:config bundling);
use TagTime qw(match); use TagTime qw(match); # should be in util.pl (or util.pl should all be here)


my $start = -1; my $start = -1;
my $end = ts(time()); my $end = ts(time());
Expand Down Expand Up @@ -57,21 +50,18 @@
my $expr = '( ' . join(' )|( ', @ARGV) . ' )'; my $expr = '( ' . join(' )|( ', @ARGV) . ' )';
open(LOG, $logfile) or die qq{Cannot open logfile "$logfile" - $!\n}; open(LOG, $logfile) or die qq{Cannot open logfile "$logfile" - $!\n};
while(<LOG>) { while(<LOG>) {
my $line = strip($_); if(!parsable($_)) {

my $orig = $_;
$_ = strip($_);
if(!(s/^\d+\s+//) || /(\(|\)|\[|\])/) { # should be a function, 'parseable'
$e++; $e++;
$errstr .= $orig; $errstr .= $_;
next; next;
} }
my $line = strip($_);


my @tags = split(/\s+/, $line); my @tags = split(/\s+/, $line);
my $ts = shift(@tags); my $ts = shift(@tags);
if($first == -1 || $ts < $first) { $first = $ts; } if($first == -1 || $ts < $first) { $first = $ts; }
if($ts < $start) { $toosoon++; } if ($ts < $start) { $toosoon++; }
elsif($ts > $end) { $toolate++; } elsif($ts > $end) { $toolate++; }
elsif(match($expr, $line)) { elsif(match($expr, $line)) {
$m++; $m++;
for(@tags) { $tc{$_}++; } for(@tags) { $tc{$_}++; }
Expand Down
3 changes: 1 addition & 2 deletions lib/TagTime.pm
Expand Up @@ -21,8 +21,7 @@ sub match {
for(split(/\s+/, $line)) { $h{$_} = 1; } for(split(/\s+/, $line)) { $h{$_} = 1; }


# TODO: Refactor this so we're not using eval() at the end. # TODO: Refactor this so we're not using eval() at the end.
# Based upon what the user has entered, almost anything # Based upon what the user has entered, almost anything could happen!
# could happen!


$expr =~ s/([^\|])\|([^\|])/$1\|\|$2/g; $expr =~ s/([^\|])\|([^\|])/$1\|\|$2/g;
$expr =~ s/([^\&])\&([^\&])/$1\&\&$2/g; $expr =~ s/([^\&])\&([^\&])/$1\&\&$2/g;
Expand Down
103 changes: 103 additions & 0 deletions merge.pl
@@ -0,0 +1,103 @@
#!/usr/bin/env perl
# Merge the tagtime logs given on the command line; output to stdout.
# If only one log file is given this just fills in any missing pings autotagged
# with MISSING and autotags pings that shouldn't be there with UNSCHED.
# NB: The rest of this is not fully implemented yet!
# Currently just concatenates the tags from each log file.
# Eventual spec follows, and in the meantime you can use it with a single
# log file to just sanity check it...
# If multiple log files are given this will properly merge them, like if you
# use tagtime on multiple computers.
# Any ping that, according to the ping schedule, is missing from all the given
# logs will be added with the autotag MISSING and any pings present in any of
# the logs that shouldn't be there (again, according to the ping schedule) will
# have the autotag UNSCHED appended.
# For each outputted ping with timestamp t, include the union of the tags with
# timestamp t in all the given log files, ignoring log files that are tagged
# only with autotags at time t. Unless *all* the log files are tagged only with
# autotags at time t, in wich case go ahead and do the union like normal.
# Autotags are {MISSING, UNSCHED, RETRO, afk, off, err}.
# The earliest timestamp outputted is the earliest timestamp in all the logs.
# The latest timestamp outputted is the max of now and the latest in the logs.

# Notes: Collect every timestamp in every given log file, plus all the scheduled
# ping timestamps from the earliest one in the logs up to the max of now and the
# latest. Sort that whole collection and walk through it. For each t:
# missflag = true
# let @p = {}, a list of ping responses for each log file
# for each log file l:
# if tags{l+t} not empty: missflag = false
# push(@p, tags{l+t})
# if sch{t} and missflag: push(@p, "MISSING")
# if not sch{t}: push(@p, "UNSCHED")
# print t, join('+', @p)

BEGIN { require "$ENV{HOME}/.tagtimerc"; }
require "util.pl";

die "USAGE: $0 logfile+\n" if @ARGV < 1;

my $e = 0; # number of lines with parse errors
my $errstr = ""; # concatenation of bad lines from log files
my $earliest = -1; # earliest timestamp in all the log files
my $latest = 0; # latest timestamp in all the log files
my %th; # maps logfile+timestamp to tags for that log for that ping
my %alltimes; # maps all timestamps to 1
for my $logfile (@ARGV) {
open(LOG, $logfile) or die;
$prevts = 0; # remember the previous timestamp
while($line = <LOG>) {
if(!parsable($line)) {
$e++;
$errstr .= $line;
next;
}
my @tags = split(/\s+/, $line);
my $ts = shift(@tags);
if($ts <= $prevts) {
$e++;
$errstr .= "NON-MONOTONE:\n$line";
next;
}
$prevts = $ts;
if($ts < $earliest || $earliest == -1) { $earliest = $ts; }
if($ts > $latest) { $latest = $ts; }
$line =~ s/^\d+\s+//;
chomp($line);
$th{$logfile.$ts} = $line;
$alltimes{$ts} = 1;
}
close(LOG);
}

if($e>0) {
print "Errors in log file(s): $e. ",
"They have to be fixed before this script can run:\n";
print "\n$errstr";
exit(1);
}

my $now = time();
if($now > $latest) { $latest = $now; }
my %sch; # maps timestamps to whether they are a scheduled pings
my $i = prevping($earliest);
$i = nextping($i);
while($i <= $latest) {
$sch{$i} = 1;
$alltimes{$i} = 1;
$i = nextping($i);
}

for my $t (sort(keys(%alltimes))) {
my $missflag = 1;
my @p = ();
for my $l (@ARGV) {
if(defined($th{$l.$t})) {
$missflag = 0;
push(@p, $th{$l.$t});
}
}
if($sch{$t} && $missflag) { push(@p, annotime('MISSING', $t, 33)); }
if(!$sch{$t}) { push(@p, 'UNSCHED'); }
print $t, " ", join(' + ', @p), "\n";
}
8 changes: 3 additions & 5 deletions script/bitch.sh
@@ -1,7 +1,5 @@
#/bin/sh #/bin/sh
# Whoever has fewer pings is the other's bitch. # Whoever has fewer pings is the other's bitch (has to accept tasks from other)


#echo -n "B: "; ../cntpings.pl bsoule.log bsl hou -s2010.07.08 echo -n "B: "; ./cntpings.pl ./bsoule.log bsl hou -s2010.07.08
#echo -n "D: "; ../cntpings.pl dreeves.log bsl hou -s2010.07.08 echo -n "D: "; ./cntpings.pl ./dreeves.log bsl hou -s2010.07.08
echo -n "B: "; ./cntpings.pl ./bsoule.log yc -s2010.07.08
echo -n "D: "; ./cntpings.pl ./dreeves.log yc -s2010.07.08
1 change: 1 addition & 0 deletions script/contest.pl
@@ -1,4 +1,5 @@
#!/usr/bin/perl -w #!/usr/bin/perl -w
# For pomodoro contests...
# smack frac = fraction of in-front-of-the-computer time (ie, not "afk") that # smack frac = fraction of in-front-of-the-computer time (ie, not "afk") that
# was spent checking email, chatting, surfing, etc. (Even if some of that is # was spent checking email, chatting, surfing, etc. (Even if some of that is
# legitimate work.) Tag "smk" means "*smack* get back to real work". # legitimate work.) Tag "smk" means "*smack* get back to real work".
Expand Down

0 comments on commit 7fc847b

Please sign in to comment.