Skip to content

Instantly share code, notes, and snippets.

@RamaRoberts
Created July 3, 2013 19:52
Show Gist options
  • Save RamaRoberts/5922160 to your computer and use it in GitHub Desktop.
Save RamaRoberts/5922160 to your computer and use it in GitHub Desktop.
Perl magic.
#!/usr/bin/perl
use strict;
use warnings;
my $file = $ARGV[0];
open FILE, "< $file" or die "Can't open $file : $!";
# save the total impressions across all creatives for this time period for use in reporting (Munin)
my $impressions_count_file = "/tmp/impressions-count";
open IMPRESSIONS_FILE, ">$impressions_count_file" or die "Can't open $impressions_count_file";
my $impressions_count = 0;
my $clicks_count_file = "/tmp/clicks-count";
open CLICKS_FILE, ">$clicks_count_file" or die "Can't open $clicks_count_file";
my $clicks_count = 0;
# the time frame the MongoDB query ran against
my $start_time = $ARGV[1] . " " . $ARGV[2];
my $end_time = $ARGV[3] . " " . $ARGV[4];
my %campaign;
while (my $l = <FILE>) {
if ($l =~ /^{\W+(\w+)\W+(\w+)\W+(\w+)\W+(\w+)\W+(\w+)\W+(\w+)/) { #only grab lines beginning with {, ignoring any headers
#print "1=$1 2=$2 3=$3 4=$4 5=$5 6=$6\n";
$campaign{$2}{$4} = $6;
}
}
close FILE;
#print "start_time\tend_time\tAID\tREQ\tIMP\tCLK\tTRK\n";
foreach my $key1 (sort keys %campaign) {
print "$start_time\t$end_time\t$key1\t" . ($campaign{$key1}{REQ} ? $campaign{$key1}{REQ} : 0) . "\t" . ($campaign{$key1}{IMP} ? $campaign{$key1}{IMP} : 0) . "\t" . ($campaign{$key1}{CLK} ? $campaign{$key1}{CLK} : 0) . "\t" . ($campaign{$key1}{TRK} ? $campaign{$key1}{TRK} : 0) . "\n";
$impressions_count += ($campaign{$key1}{IMP} ? $campaign{$key1}{IMP} : 0);
$clicks_count += ($campaign{$key1}{CLK} ? $campaign{$key1}{CLK} : 0);
}
#foreach my $key1 (sort keys %campaign) {
# foreach my $key2 (sort keys %{$campaign{$key1}}) {
# print "$key1, $key2, $campaign{$key1}{$key2}\n";
# }
#}
print IMPRESSIONS_FILE "$impressions_count\n";
close IMPRESSIONS_FILE;
print CLICKS_FILE "$clicks_count\n";
close CLICKS_FILE;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment