Viewing nightly.pl

#!/usr/local/bin/perl5

use strict;
use File::stat;
use lib '/www/lib', '/usr/local/www/lib';
require 'lockfile.pl';
my $blib = $0;
$blib =~ s{[^/]*$}{browserlib.pl};
require $blib;

sub addto ($$);

my $datafile = '/www/world/webinfo/case/gather/gather.data';

my (%browsers, %platforms) = ();
foreach my $file (glob "/gather/*/www/webinfo_case_gather") {
    next unless lockfile ($file);
    my $stamp = "$file.READ";
    if (-f $stamp) {
	my $fst = stat $file;
	my $sst = stat $stamp;
	next if $fst->mtime < $sst->mtime;
    }
    if (open F, "<$file") {
	while (<F>) {
	    chomp;
	    my ($b, $p) = whatbrowser ($_);
	    ++$browsers{$b};
	    ++$platforms{$p};
	}
	close F;
	#
	# Signal the file has been read
	#
	open F, ">$stamp";
	close F;
    }
    unlockfile ($file);
}

if (open F, "<$datafile") {
    my $ab = <F>;
    chomp $ab;
    my $ap = <F>;
    chomp $ap;
    my @wba = ();
    $_ = <F>;	# We don't need these weekly summaries;
    $_ = <F>;
    while (<F>) {
	chomp;
	push @wba, $_;
    }
    close F;
    my $len = int 0.5 * scalar @wba;
    my @wpa = splice @wba, $len;
    if ($len > 6) {
	splice @wba, 6;
	splice @wpa, 6;
    }
    $ab = addto ($ab, \%browsers);
    $ap = addto ($ap, \%platforms);
    unshift @wba, addto (undef, \%browsers);
    unshift @wpa, addto (undef, \%platforms);
    %browsers = ();
    my $wb;
    foreach my $item (@wba) {
	$wb = addto ($item, \%browsers);
	%browsers = split /\|/, $wb;
    }
    %platforms = ();
    my $wp;
    foreach my $item (@wpa) {
	$wp = addto ($item, \%platforms);
	%platforms = split /\|/, $wp;
    }
    if (open F, ">$datafile") {
	print F join "\n", $ab, $ap, $wb, $wp, @wba, @wpa;
	close F;
    }
}

sub addto ($$) {
    my ($vals, $new) = @_;
    my %valarr = split /\|/, $vals;
    foreach my $key (keys %$new) {
	$valarr{$key} += $new->{$key};
    }
    my @newvals = ();
    foreach my $key (keys %valarr) {
	push @newvals, $key, $valarr{$key};
    }
    join '|', @newvals;
}