rendered paste body#!/app/gmd/bin/perl
use strict;
use Data::Dumper;
use Getopt::Std;
my %opts;
getopts('htdusr:', \%opts );
if ( $opts{h} ) {
usage();
exit;
}
# There's some reason I didn't use the time as a key ... I don't remember why :-/ this data structure sucks to work with
# Format of the data structure is :
# Per sample
# 0 : Time
# 1 : time ranking
# 2 : time converted to seconds since the beginning of the day
# 3 : updates / s for specific time sample
# 4 : delta ( change from the previous sample to the current )
# 5 : delta ranking
# 6 : slope
# 7 : slope ranking
my @file = <>;
my ($time,$seconds, $score,$trank);
my @pairs = map {
( $time , $score) = split;
$time =~ /(\d\d)\:(\d\d):(\d\d)/;
$seconds = ( $1*3600 + $2*60 + $3 );
[ $time,$trank++, $seconds, $score ]; # last column is for score ranking
} @file;
# populate the delta fields and the slope fields
for ( my $i; $i <= $#pairs; $i++ ) {
$pairs[$i][5] = ($i != 0) ? $pairs[$i][3] - $pairs[$i-1][3] : 0;
# print $pairs[$i][0],"\t",$pairs[$i][3],"\t",$pairs[$i][4],"\n"; #= $pairs[$i][2] - $pairs[$i-1][2]\n";
# print "$pairs[$i][7] = ($i != 0) ? ( $pairs[$i][5] / ($pairs[$i][2] - $pairs[$i-1][2]) ): 0\n**\n";
if ( $pairs[$i][2] eq $pairs[$i-1][2] ) {
print "Double timestamped for : $pairs[$i-1][0] and $pairs[$i][0] , assuming 5 second interval !\n";
$pairs[$i][7] = ($i != 0) ? ( $pairs[$i][5] / (5) ): 0;
}
else {
$pairs[$i][7] = ($i != 0) ? ( $pairs[$i][5] / ($pairs[$i][2] - $pairs[$i-1][2]) ): 0;
}
}
my $set = \@pairs;
# print "set before : ",join (",",@{$set}),"\n";
if ( $opts{r} ) {
if ( $opts{r} =~ /^(?:(09|1[0123456]))\:([012345]\d)to(?:(09|1[0123456])\:([012345]\d))/ ) {
print "begin_time : $1*60*60+$2*60 : ",$1*60*60+$2*60," end time: $3*60*60+$4*60 : ",$3*60*60+$4*60,"\n";
my $start = $1*60*60+$2*60;
my $end = $3*60*60+$4*60;
$set = trim_time($start, $end, $set );
}
else {
print "Option R must be formatted : '-r 09:50to10:00'\n";
exit;
}
}
#print "set after : ",join (",",@{$set}),"\n";
$set = trim_time( 9*60*60+55*60, 10*60*60, $set);
rank(3,$set); # update rate
rank(5,$set); # delta
rank(7,$set); # slope
if ( $opts{t} ) {
printme( "*** Time Sorted***",sort { $a->[2] <=> $b->[2] } @{$set});
}
if ( $opts{d} ) {
printme( "*** Deriv. Sorted***",sort { $b->[5] <=> $a->[5] } @{$set});
}
if ( $opts{u} ) {
printme( "*** Update rate Sorted ***",sort { $b->[3] <=> $a->[3] } @{$set}) ;
}
if ( $opts{s} ) {
printme ( "*** Slope Sorted ***",sort { $b->[7] <=> $a->[7] } @{$set});
}
#print $$set[0][0],"\n";
#print $$set[0][1],"\n";
#print $$set[0][2],"\n";
#print $$set[0][3],"\n";
#print $$set[0][4],"\n";
#print "sleeping 20\n";
#$set = trim_time( 8*60*60, 17*60*60, $set);
#print $$set[0][0],"\n";
#print $$set[0][1],"\n";
#print $$set[0][2],"\n";
#print $$set[0][3],"\n";
#print $$set[0][4],"\n";
#printme ( "** Trimmed **", sort { $b->[3] <=> $a->[3] } @{trim_time( 9*60*60+55*60, 10*60*60, $set)} );
#printme ( "** Trimmed **", sort mangle_updaterate @{$set} );
print "Done\n";
sub rank {
my $pos = shift;
my $pairs = shift;
# figure out the rank for the update rate
my $rank = 1;
my $last = 0;
foreach my $element ( sort { $b->[$pos] <=> $a->[$pos] } @{$pairs} ) {
if ( @{$element}[$pos] < $last ) {
$rank++;
}
# notice that [1] is used as our placeholder, it's the only unique item we have in the array between the 2 !
$$pairs[ @{$element}[1] ][$pos+1] = $rank; # stuff the ranking into the original array
$last = @{$element}[$pos];
}
}
sub trim_time { # expects 2 times in seconds and the data, returns a new array of the proper length
my $start = shift;
my $end = shift;
my $pairs = shift;
return [ grep $_->[2] > $start &&
$_->[2] < $end , @$pairs ];
}
sub printme {
my $header = shift;
my @array = @_;
print $header."\n";
printf "%9s %7s (%6s) %7s (%5s) %6s (%5s)\n", "Time","upd./s","rank","delta","rank","slope","rank";
map {
# printf "%9s %7s (%6s) %7s (%5s) %6.1f (%5s)\n",$_->[0],$_->[3],$_->[4],$_->[5],$_->[6],$_->[7],$_->[8]; # http://sysarch.com/Perl/hash_slice.txt
printf "%9s %7s (%6s) %7s (%5s) %6.1f (%5s)\n",@{$_}[0,3..8]; # http://sysarch.com/Perl/hash_slice.txt , this is a special slice for array positions 0,3-8!
} @array;
}
sub mangle_time {
$a->[2] <=> $b->[2];
}
sub usage {
print <<HERE
usage: $0 [-t] [-u] [-d] [datafile]
-d : delta -- compare the current update rate to the previous sample update rate and sort by the absolute value of the difference and print
-h : this help screen
-s : slope -- sort by the slope of the line between the previous and current samples
-t : time -- sort by the time within the file and print
-u : sort by the update rate and print
examples :
$0 -t 20070328.log
$0 -t -u 20070328.log
$0 -u 20070328.log
$0 -d 20070328.log
$0 -t -d 20070328.log
HERE
}