#!/usr/bin/perl -w

# Reports on various metrics

use Pod::Usage;
use Getopt::Long;
use SOAP::Lite;
use strict;

# Global options
our $opt_version;
our $opt_help;
our $opt_man;
our $opt_resource = 'http://www.osdl.org/WebService/TestSystem';
our $opt_server   = 'http://localhost:8081/';
our $opt_year     = (localtime)[5] + 1900;   
our $opt_month    = (localtime)[4] + 1;

# Handle commandline options
Getopt::Long::Configure ('bundling', 'no_ignore_case');
GetOptions(
           'version|V'    => \$opt_version,
           'help|h'       => \$opt_help,
           'man'          => \$opt_man,
           'server|s=s'   => \$opt_server,
           'resource|r=s' => \$opt_resource,
           'year|y=s'     => \$opt_year,
           'month|m=s'    => \$opt_month,
           );

# Handle -V or --version
if ($opt_version) {
    print '$0: $Revision: 1.11 $', "\n";
    exit 0;
}

# Usage
pod2usage(-verbose => 2, -exitstatus => 0) if ($opt_man);
pod2usage(-verbose => 1, -exitstatus => 0) if ($opt_help);

exit main();



sub main {
    # Connect to the server
    my $soap = create_soap_instance($opt_resource, $opt_server);

    # Create the test service object
    my $response = $soap->call(new => 1);
    soap_assert($response);
    my $testsys = $response->result;

    if (! $testsys) {
        die "Could not create testsys object\n";
    }

    print "Test Runtime for $opt_month/$opt_year\n";
    report_metrics($soap->metrics_test_run_time($testsys, $opt_year, $opt_month),
		   "%5s %15s %10s %15s %15s\n",
		   "%5d %15s %10d %15d %15d\n",
		   'id', 'test', 'count', 'total_run_time', 'ave_run_time')
        or return -1;

    print "\n\nTotal users in $opt_year\n";
    report_metrics($soap->metrics_requests_per_month($testsys, $opt_year, 'all'),
		   "%5s %5s %15s %15s\n",
		   "%5d %5d %15d %15d\n",
		   'month', 'year', 'total_requests', 'requestors')
        or return -1;

    print "\n\nExternal users in $opt_year\n";
    report_metrics($soap->metrics_requests_per_month($testsys, $opt_year, 'external'),
		   "%5s %5s %10s %10s\n",
		   "%5s %5d %10d %10d\n",
		   'month', 'year', 'total_requests', 'requestors')
        or return -1;
    
    print "\n\nDistros tested in $opt_year\n";
    report_metrics($soap->metrics_distros_tested_per_month($testsys, $opt_year),
		   "%5s %10s\n",
		   "%5s %10d\n",
		   'month', 'count')
        or return -1;

    return 0;
}

=head2 report_metrics($response, $header_format, $data_format, @fields)

Generates a printed report from the SOAP response.  Checks the
error return from SOAP and prints an error msg to STDERR if there
is an error, otherwise generates a data table for the given @fields.

=cut
sub report_metrics {
    my $response = shift;
    my $header_format = shift || return undef;
    my $data_format = shift || return undef;
    my @fields = @_;

    # Check for errors...
    return undef unless @fields;
    soap_assert($response);

    printf($header_format, @fields);

    foreach my $row (@{$response->result}) {
	my @values;
	foreach my $field (@fields) {
	    push @values, ($row->{$field}) || 0;
	}
	printf($data_format, @values);
    }

    return 1;
}

# Convenience function to create the soap instance
sub create_soap_instance {
    my $resource = shift || return undef;
    my $server = shift || return undef;

    my $soap = SOAP::Lite
	-> uri($resource)
	-> proxy($server,
		 options => {compress_threshold => 10000});
    return $soap;
};

# Convenience function to print out any errors encountered in a soap call
# and exit.
sub soap_assert {
    my $response = shift;
    if ($response->fault) {
        print join ', ',
	$response->faultcode,
	$response->faultstring;
	return undef;
    }
    return 1;
}

__END__

=head1 NAME

stp-metrics - returns metrics about the testing system

=head1 SYNOPSIS

stp-metrics

=head1 DESCRIPTION

This script generates a set of metrics about usage of the test system.  

=head1 OPTIONS

=over 8

=item B<-V>, B<--version>

Displays the version number of the script and exits.

=item B<-h>, B<--help>

Displays a brief usage message

=item B<--man>

Displays the man page

=item B<-s> I<server_url>, B<--server>=I<server_url>

The URL of the WebService::TestSystem server to connect to.  By default,
it uses 'http://localhost:8081'.

=item B<-r> I<resource_uri>, B<--resource>=I<resource_uri>

The URI of the service provided by the server.  By default, it uses
'http://www.osdl.org/WebService/TestSystem'.  Users should not typically
need to alter this setting.

=back

=head1 PREREQUISITES

B<SOAP::Lite>,
B<Pod::Usage>,
B<Getopt::Long>

=head1 AUTHOR

Bryce Harrington E<lt>bryce@osdl.orgE<gt>

=head1 COPYRIGHT

Copyright (C) 2004 Open Source Development Labs
All Rights Reserved.
    This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.

=head1 REVISION

Revision: $Revision: 1.11 $

=cut

