#!/bin/perl -w
require "adtracklib.pl";
# a good array of pretty distinct colors,
# each associated with an advertiser
@old_color_assign = ("#000088","#0000FF","#0088FF","#8800FF","#8888FF","#FF0088",
"#FF00FF","#FF0088","#FF8888","#88FFFF","#FF88FF","#FFFF00",
"#444400","#BBFF00","#FF8844","#0044BB","#BB88FF","#FF4488",
"#FF00FF","#FF0088","#FF8888","#88FFFF","#FF88FF","#FFFF00",
"#444400","#BBFF00","#FF8844","#0044BB","#BB88FF","#FF4488",
"#BB00FF","#FFBB88","#FF4400","#884444","#4444FF","#FFFF44");
@color_assign = (
"#FFFF00","#CCFF00","#99FF00","#66FF00","#33FF00",
"#00FF00","#00FF33","#00FF66","#00FF99","#00FFCC",
"#00FFFF","#00CCFF","#0099FF","#0066FF","#0033FF",
"#0000FF","#3300FF","#6600FF","#9900FF","#CC00FF",
"#FF00FF","#FF00CC","#FF0099","#FF0066","#FF0033",
"#FF0000","#FF3300","#FF6600","#FF9900","#FFCC00");
$next_color = 0;
$background_color = "#FFFFFF";
$total_income = {};
$total_hits = {};
print "Content-type: text/html\n\n";
&ReadFormInput;
if (! $INPUT{'start_date'}) { print "Access this utility through get_income.cgi"; exit;}
$str_start_date = &ConvertTimeToString($INPUT{'start_date'});
$str_end_date = &ConvertTimeToString($INPUT{'end_date'});
# Temporarily hardwired, should be:
$site = $INPUT{'site'};
&InitSiteVars($site);
print "\n
View Income By Date From $str_start_date To $str_end_date
\n
Income By Date
From $str_start_date To $str_end_date
site: $site
";
print "| Date | Hits |   | Total | ";
$scale_width = 100;
&print_scale($scale_width);
print "
";
#figure out how many records are in the directory
@dates = &get_dates_from_report_file_list(); #$start_date, $end_date);
foreach $temp_date (@dates) {
&read_date_file($temp_date);
}
@adsort = sort keys(%keep);
$next_color = 0;
foreach $advertiser (@adsort) {
$color{$advertiser} = $color_assign[$next_color];
$next_color = $next_color + 1;
#wrap color assignment
if ($next_color > $#color_assign ) { $next_color = 0; }
}
@max_scale = reverse sort by_number values(%total_income);
$max_scale_income = int($max_scale[0]);
foreach $temp_date (@dates) {
&print_stacked_bar($temp_date,$max_scale_income,$scale_width);
}
print "
";
exit;
sub by_number {$a <=> $b; }
sub print_scale {
$scale = $_[0];
print "";
for ($n = 0; $n < $scale; $n++) {
# print " N | \n";
print " n | \n";
}
}
sub print_stacked_bar {
$my_date = $_[0];
$my_date_string = &ConvertTimeToString($my_date);
$my_graphmax = $_[1];
$scale = $_[2];
$str_income = sprintf('%3.2f',$total_income{$my_date});
$str_hits = sprintf('%5.0f',$total_hits{$my_date});
print "";
print "| $my_date_string | $str_hits | | $str_income | ";
# print bars such that colspan is a percentage of the maximum income for the set
# of dates we have chosen
# $colspan = $my_income/$my_graphmax * $scale;
foreach $advertiser (@adsort) {
#print "$advertiser
$my_date
";
$idx = "$advertiser $my_date";
if (! defined($income{$idx})){
#print "income{$idx} was undef
";
$income{$idx} = 0;
}
#print "get idx = $idx income = $income{$idx}
";
$colspan = int(( $income{$idx} / $my_graphmax ) * $scale);
if ($colspan > 0)
{
$advert_short_name = substr($advertiser,0,$colspan);
#$advert_short_name = $advertiser;
$sPrint = "";
$sPrint .= $advert_short_name;
$sPrint .= " | \n";
print $sPrint;
}
}
print "
\n";
}
sub read_date_file {
# takes a date
# sets up the "income" associative array
# sets up advertiser colors as necessary
$this_date = $_[0];
if (open INFILE, "$reports_dir/$this_date.db"){
while () {
$cents = 0; $adjustment = 0; $hits = 0;
$advertiser = 0; $sponsor = 0;
($advertiser,$cents,$sponsor,$adjustment,$hits) = split(/:/,$_,5);
# print "advertiser $advertiser cents $cents sponsor $sponsor adjustment $adjustment
";
$idx = "$advertiser $this_date";
$temp = $cents - $adjustment;
# print "$cents - $adjustment = $temp ";
$income{$idx} = (($cents - $adjustment) * $hits);
#print "set idx = $idx income{$idx} = $income{$idx}
";
$hits{$idx} = $hits;
$sponsor{$idx} = $sponsor;
$cents{$idx} = $cents;
$adjustment{$idx} = $adjustment;
if ( defined($income{$idx}) )
{ $total_income{$this_date} = $total_income{$this_date} + $income{$idx};}
if ( defined($hits{$idx}) )
{$total_hits{$this_date} = $total_hits{$this_date} + $hits{$idx}; }
$keep{$advertiser} = 1;
}
}
close INFILE;
}
sub get_dates_from_report_file_list {
# Get a list of reports in the reports directory.
# They are named by date, and we get a list of dates
#my $start_date = $_[0];
#my $end_date = $_[1];
opendir(DIR,"$reports_dir") || print "Could not open $reports_dir";
@files = grep(/\.db/, readdir(DIR));
closedir(DIR);
for (@files) {
($mydate, $spew) = split(/\.db/);
if (($mydate >= $INPUT{"start_date"}) && ($mydate <= $INPUT{"end_date"})) { push(@dates, $mydate);}
#push(@dates, $mydate);
#print "$mydate\n";
}
@dates = sort by_number @dates;
#exit;
}