All pastes #222834 Raw Edit

Stuff

public text v1 · immutable
#222834 ·published 2006-10-26 15:52 UTC
rendered paste body
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;

my $avgBytes;
my $bytesUrl;
my $bytesNum;
my $bytesDate;
my @reqLines;
my @cleanReqLines;
my %hits;
my %bytes;


#function to strip whitespace, which seperate fields, and replace it with commas. After substr the trailing comma
#the result is a comma seperated list
sub stripWS
{
	my $line=shift;
	$line =~ s/\s+/,/g;
	return $line;
}

#returns a url version of the file name. substr off the .txt 
sub getUrl
{
	my $file=shift;
	$file =~ s/^.*?-//;
	if($file !~ /2001/)
	{
		$file =~ s/-/\//g;
	}
	else
	{
		substr($file, index($file, '-'), 1)='/';
	}
	$file=substr($file, 0, -4);
	return $file	
}

sub formatDate
{
	my @months=qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
	my $full = shift;
	my @pieces=split("/", $full);
	my $month=$pieces[0]; 
	my $mindex = int($month) - 1; 
	my $day=$pieces[1];
	my $year=$pieces[2];
	my $monthAbr=$months[$mindex];
	my $pretty=$day."/".$monthAbr."/20".$year;
	return $pretty;
}

sub getBytes
{
	my $bytesDir='Bytes/';
	opendir(my $bytesDirH, $bytesDir) or die $!;
	while(my $file=readdir($bytesDirH))
	{
		if($file eq ".DS_Store" || $file eq "." || $file eq "..")
		{
			next;
		}
		my $bytesUrl=&getUrl($file);
#		print "$bytesDir$file\n";
		open(my $bytesFh, "<", "$bytesDir/$file") or die $!;
		chomp(my @byteLines=<$bytesFh>);
		my $year=3;
		
		foreach my $byteLine(@byteLines)
		{
			if($byteLine =~ /#/)
			{
				next;
			}
			if(length($byteLine) == 0)
			{
				next;
			}
			if(!defined($byteLine))
			{
				next;
			}
			if ($byteLine !~ /\d/)
			{
				next;
			}
			
			$byteLine=&stripWS($byteLine);
			my @byteParts=split(',',$byteLine);
			
			if($byteParts[1] eq "01/01")
			{
				$year++;
			}			
						
			$byteLine = join(",",@byteParts);

			(substr($byteLine, index($byteLine, '/') + 3,0)) = "/0$year";
			@byteParts=split(',',$byteLine);
			if(length($byteParts[1]) == 0)
			{
				next;
			}
			my $cleanBytesDate=&formatDate($byteParts[1]);
			push(@{$bytes{$bytesUrl}}, $cleanBytesDate . "," . $byteParts[2]);
		}
	}
	close $bytesDirH;
}

sub formatRow
{
	my $data=shift;
	my @pieces=split(",",$data);
	my $formatedRow="127.0.0.1 - - [$pieces[0]:12:00:00 -0400] \"GET /$pieces[2] HTTP/1.1\" 200  $pieces[3] \"http://www.gulfofmaine.org/wgb.php\" \"Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/417.9 (KHTML, like Gecko) Safari/417.8\"";
	return $formatedRow;
}

sub printLines
{
	my %hash = @_;
	my @keys = sort(keys(%hash));

	
	foreach my $key(@keys)
	{
		foreach my $array($hash{$key})
		{
			foreach my $item(@{$array})
			{
				print "$item\n\n";
			}
		}
	}
}

sub round 
{
    my($number) = shift;
    return int($number + .5);
}

&getBytes;

my $dir='RequestedPages/';

opendir(my $required, $dir) or die $!;

while(my $file=readdir($required))
{
	next unless (-f "$dir/$file");
	my $url = &getUrl($file);
	open(my $reqfile, "<", "$dir/$file") or die $!;
	chomp(@reqLines=<$reqfile>);
	my $year=3;

	foreach my $tmpLine(@reqLines)
	{
		
		if($tmpLine =~ /#/)
		{
			next;
		}
		if(length($tmpLine) == 0)
		{
			next;
		}
		if(!defined($tmpLine))
		{
			next;
		}
		
		$tmpLine=&stripWS($tmpLine);
		$tmpLine.=",$url";
		my @parts=split(',',$tmpLine);
		if($parts[1] eq "01/01")
		{
			$year++;
		}

		$tmpLine = join(",",@parts);
		(substr($tmpLine, index($tmpLine, '/') + 3,0)) = "/0$year";		
		@parts=split(',',$tmpLine);
		if(length($parts[1]) == 0)
		{
			next;
		}
		my $cleanDate=&formatDate($parts[1]);
		delete $parts[0];

		my $cleanLine=$cleanDate . "," . $parts[2] . "," . $parts[3] . ",";

		foreach my $byteRow(@{$bytes{$url}})
		{			
			my @bytesTmp=split(',',$byteRow);
			
			if($bytesTmp[0] eq $cleanDate)
			{
				$avgBytes=0;
						
				#If number of hits does not equal zero, then do division
				if($parts[2] != 0)
				{
					$avgBytes=$bytesTmp[1]/$parts[2];
					$avgBytes=round($avgBytes);
				}
				#otherwise, 0hits=0bytes
				else
				{
					$avgBytes=0;
				}
				
				$cleanLine.=$avgBytes;
				
				my $row=&formatRow($cleanLine);
		
				for(my $i=0; $i<$parts[2]; $i++)
				{
					push(@{$hits{$cleanDate}}, $row);
				}
			
				

			}
		
		}			

	}
}

#print Dumper(%hits);

&printLines(%hits);