#!/bin/perl -w require "/var/www/htdocs/romana/glamazon/cgi/adtrack/adtracklib.pl"; $site = $ARGV[0]; $debug = $ARGV[1]; if ($debug) {print "Debugging is on!";} $newOnly = "true"; &Reap($site); &InitSiteVars($site); opendir(DIR,"$logs_dir"); @files = sort grep(/(\w)\.log/, readdir(DIR)); closedir(DIR); #Need to handle case where there are no previous files in logs_dir # 1 day should be enough. if (@files) { ($earliest, $spew) = split(/\.log/,$files[$#files - 1]); } else { $earliest = time() - 86400; } $latest = time(); #Print them out to check them if ($debug){ print "Start time: $earliest ". &ConvertTimeToString($earliest). "\n"; print "End time: $latest ". &ConvertTimeToString($latest). "\n"; } #Read display variables in #Should be handled by InitializeAdArrays &InitializeAdArrays; foreach $type (@types) { foreach $advertiser (keys %{$show_banners{$type}}) { print "$advertiser $show_banners{$type}{$advertiser}\n" if ($debug); &ReadDisplayVars($advertiser); $display_vars{$advertiser} = "$cents:$show_banners{$type}{$advertiser}:$adjustment"; } } #Process that data! $nextPeriod = $earliest; $nextMidnight = roundUpMidnight($latest); while ($nextPeriod < $nextMidnight) { &getStats($nextPeriod); &printReport($nextPeriod); $nextPeriod = $nextPeriod + 86400; } exit; sub roundUpMidnight { $midnightTime = (int($_[0]/86400) + 1) * 86400; return $midnightTime; } sub roundDownMidnight { $midnightTime = int($_[0]/86400) * 86400; return $midnightTime; } sub countUniques { # make this take a starting and ending time param # "count uniques for this advertiser and sponsor from start to finish" $startTime = $_[0]; $logname = "$logs_dir/$startTime.log"; $advertiser = $_[1]; $unique_count = 0; if (-e $logname) { open (UNIQUELOG, "<$logname") || die("Could not open file $logs_dir/$logname.log"); while () { chop; $count = ($_ =~ tr/ //); if ($count == 3) { ($ip_addr, $ip_time_logged, $referer, $gotclick) = split(/ /,$_,4); if ($gotclick eq $advertiser){ $unique_count++; } } } close UNIQUELOG; } print "Counted $unique_count hits on " . &ConvertTimeToString($startTime) ." for .$advertiser.\n" if ($debug); return $unique_count; } sub getStats { $now = $_[0]; foreach $type (@types) { foreach $advertiser (keys %{$show_banners{$type}}) { $uniques{$advertiser} = &countUniques($now, $advertiser); } } } sub printReport { $reportname = "$reports_dir/$_[0].db"; $timestring = &ConvertTimeToString($_[0]); print "\nprinting report for $timestring\n"; if (open (OUTFILE, ">$reportname")) { foreach $type (@types) { foreach $advertiser (keys %{$show_banners{$type}}) { print "$advertiser:$display_vars{$advertiser}:$uniques{$advertiser}\n" if ($debug);; print OUTFILE "$advertiser:$display_vars{$advertiser}:$uniques{$advertiser}\n"; } } close OUTFILE; } } sub Reap() { local ($site) = $_[0]; local ($my_directory) = "$gsInstallDir/sites/$site/logs"; $numdate = time(); $cutoff = $numdate - ($advertHours * 3600); print "Adverts expire in $advertHours hours. Cutoff is " . &ConvertTimeToString($cutoff) . "\n" if ($debug); opendir(DIR,"$my_directory"); @files = sort grep(/\.reap/, readdir(DIR)); closedir(DIR); chdir($my_directory); foreach $hitfile (@files) { ($ip_addr, $spew) = split(/\.reap/,$hitfile); if ($debug) {print "Reading $hitfile\n"; } open (HITFILE, $hitfile); #Open the hit file @hits = ; close HITFILE; unlink $hitfile; if ($debug) {print "Removed $hitfile\n";} $prev_hit = ""; @hitArray = ""; undef %seen; foreach $hit (@hits) { ($my_time, $my_referer, $my_ad, $my_sponsor) = split(/ /,$hit); if ($my_time > $cutoff) { # Is this hit expired? push (@hitArray, $hit); } else { $log_time = &roundDownMidnight($my_time); push @{$logArray{$log_time}}, "$ip_addr $my_time $my_referer $my_ad\n" unless $seen{$my_sponsor}++; if ($debug) {print "To $log_time.log: $ip_addr $my_time $my_referer $my_ad\n";} } } if ($#hitArray != 0) { if ($debug) {print "Writing $#hitArray unexpired hits back to $hitfile\n";} open (HITFILE, ">>$hitfile") || die("Could not open $hitfile for writing"); foreach $i ( 0 .. $#hitArray ) { print HITFILE "$hitArray[$i]"; } close HITFILE; @hitArray = ""; } } foreach $log_time (sort(keys %logArray)) { open (LOGFILE, ">>$log_time.log"); foreach $i ( 0 .. $#{ $logArray{$log_time} } ) { if ($debug) {print "To $log_time.log: $logArray{$log_time}[$i]";} print LOGFILE "$logArray{$log_time}[$i]"; } close LOGFILE; } }