#!/usr/bin/perl -w

# an rdiff-backup statistics generating script
#
# supply backup-statstics with a pathname to an rdiff-backup mirror directory
# (i.e. a directory which contains an rdiff-backup subdir) and it will display
# the most recent backup statistics, the average of all backup statistics, and
# any directories which contain a total of more than 4000000 bytes of new files
# or increment files.
#
# Copyright (c) 2005 Dean Gaudet <dean@arctic.org>
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.

# $Id: backup-statistics,v 1.5 2005/05/10 04:51:07 dean Exp $

use strict;

my $dir = shift or die "usage: $0 mirror-directory [date_tag]\n";

$dir="$dir/rdiff-backup-data";
-d "$dir" or die "$0: $dir is not a directory\n";

my $tag = shift;
if (!defined($tag)) {
	my @current = sort <$dir/current_mirror.*.data>;
	$tag = $current[0];
	$tag =~ s#.*/current_mirror.(.*).data#$1#;
}
print "Statistics for $dir $tag:\n";

print "\nSession statistics:\n";
my $sfile = "$dir/session_statistics.$tag.data";
open(SFILE, "<$sfile") or die "$0: unable to read $sfile: $!\n";
print <SFILE>;
close(SFILE);


print "\nAverage statistics:\n";
system("rdiff-backup --calculate-average $dir/session_statistics.*");


my $limit = 4;
print "\nDirectory statistics (total of new or inc filesizes over ${limit}MB):\n";
my $fsfile = "$dir/file_statistics.$tag.data.gz";
-r $fsfile or die "$0: unable to read $fsfile: $!\n";
open(FS, "zcat '$fsfile' |") or die "unable to fork zcat $fsfile: $!\n";

my $line = <FS>;
chomp($line);
$line eq '# Format of each line in file statistics file:' or die "$0: unexpected first line\n";
$line = <FS>;
chomp($line);
$line eq '# Filename Changed SourceSize MirrorSize IncrementSize' or die "$0: unexpected second line\n";

my $warnings = 0;
my %newfile;
my %incfile;

while (<FS>) {
	chomp;
	my ($filename, $changed, $ssize, $msize, $isize) = m#^(.*) (\S+) (\S+) (\S+) (\S+)$#;
	if (!defined($isize)) {
		warn "unable to parse: $_\n";
		++$warnings;
		if ($warnings == 100) {
			die "too many warnings\n";
		}
		next;
	}

	my $newsize = ($ssize ne 'NA' and $msize eq 'NA') ? $ssize : 0;
	my $incsize = ($isize ne 'NA') ? $isize : 0;
	my $parent = $filename;
	while ($parent =~ s#(.*)/[^/]+#$1#) {
		if (!defined($newfile{$parent})) {
			$newfile{$parent} = 0;
			$incfile{$parent} = 0;
		}
		$newfile{$parent} += $newsize;
		$incfile{$parent} += $incsize;
	}
}

my $key;
foreach $key (sort { $newfile{$b} <=> $newfile{$a} } keys %newfile) {
	last if ($newfile{$key} < $limit*1000000);
	printf "new %12s %s\n", $newfile{$key}, $key;
}

foreach $key (sort { $incfile{$b} <=> $incfile{$a} } keys %incfile) {
	last if ($incfile{$key} < $limit*1000000);
	printf "inc %12s %s\n", $incfile{$key}, $key;
}
