######################################################################
#
#  EPrints Staff Status Page
#
######################################################################
#
#  This file is part of GNU EPrints 2.
#  
#  Copyright (c) 2000-2004 University of Southampton, UK. SO17 1BJ.
#  
#  EPrints 2 is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#  
#  EPrints 2 is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  
#  You should have received a copy of the GNU General Public License
#  along with EPrints 2; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
######################################################################

use EPrints::Session;
use EPrints::Database;
use EPrints::EPrint;
use EPrints::User;

use strict;

my $session = new EPrints::Session;

# Check we have privs
if( !$session->auth_check( "view-status" ) )
{
	$session->terminate();
	exit( 0 );
}

my $rows;

# Number of users in each group
my $total_users = $session->get_archive()->get_dataset( "user" )->count( $session );

my( %num_users, $usertypes, $usertype, $userds, $subds );
$userds = $session->get_archive()->get_dataset( "user" );
$subds = $session->get_archive()->get_dataset( "subscription" );
$usertypes = $userds->get_types();
foreach $usertype ( @{$usertypes} )
{
	my $searchexp = new EPrints::SearchExpression(
		session => $session,
		dataset => $userds );

	$searchexp->add_field(
		$userds->get_field( "usertype" ),
		$usertype );

	$searchexp->perform_search();
	$num_users{ $usertype } = $searchexp->count();
	$searchexp->dispose();
}

my %num_eprints = ();
my @esets = ( "archive", "buffer", "inbox", "deletion" );

foreach( @esets )
{
	# Number of submissions in dataset
	$num_eprints{$_} = $session->get_archive()->get_dataset( $_ )->count( $session );
}

my $db_status = ( $total_users > 0 ? "OK" : "DOWN" );

my( $html , $table , $p , $span );

# Write the results to a table

$html = $session->make_doc_fragment;

$table = $session->make_element( "table", border=>"0" );
$html->appendChild( $table );

$table->appendChild( render_row( 
		$session,
		$session->html_phrase( "cgi/users/status:release" ),
		$session->make_text( 
			EPrints::Config::get( "version" ) ) ) );

$table->appendChild(
	render_row( 
		$session,
		$session->html_phrase( "cgi/users/status:database" ),
		$session->make_text( $db_status ) ) );

$table = $session->make_element( "table", border=>"0" );
$html->appendChild( $session->html_phrase( "cgi/users/status:usertitle" ) );
$html->appendChild( $table );

foreach $usertype ( keys %num_users )
{
	$table->appendChild(
		render_row( 
			$session,
			$session->make_text(
				$userds->get_type_name( $session, $usertype ).
					":"), 
			$session->make_text( $num_users{$usertype} ) ) );
}
$table->appendChild(
	render_row( 
		$session,
		$session->html_phrase( "cgi/users/status:users" ),
		$session->make_text( $total_users ) ) );

$table = $session->make_element( "table", border=>"0" );
$html->appendChild( $session->html_phrase( "cgi/users/status:articles" ) );
$html->appendChild( $table );

foreach( @esets )
{
	$table->appendChild(
		render_row( 
			$session,
			$session->html_phrase( "cgi/users/status:set_".$_ ),
			$session->make_text( $num_eprints{$_} ) ) );
}


unless( $EPrints::SystemSettings::conf->{disable_df} )
{
	$table = $session->make_element( "table", border=>"0" );
	$html->appendChild( $session->html_phrase( "cgi/users/status:diskspace" ) );
	$html->appendChild( $table );

	my $best_size = 0;

	my @dirs = $session->get_archive()->get_store_dirs();
	my $dir;
	foreach $dir ( @dirs )
	{
		my $size = $session->get_archive()->get_store_dir_size( $dir );
		$table->appendChild(
			render_row( 
				$session,
				$session->html_phrase( 
					"cgi/users/status:diskfree",
					dir=>$session->make_text( $dir ) ),
				$session->html_phrase( 
					"cgi/users/status:mbfree",
					mb=>$session->make_text( 
						int($size/1024) ) ) ) );
	
		$best_size = $size if( $size > $best_size );
	}
	
	if( $best_size < $session->get_archive()->get_conf( 
					"diskspace_error_threshold" ) )
	{
		$p = $session->make_element( "p" );
		$html->appendChild( $p );
		$p->appendChild( 
			$session->html_phrase( 
				"cgi/users/status:out_of_space" ) );
	}
	elsif( $best_size < $session->get_archive()->get_conf( 
						"diskspace_warn_threshold" ) )
	{
		$p = $session->make_element( "p" );
		$html->appendChild( $p );
		$p->appendChild( 
			$session->html_phrase( 
				"cgi/users/status:nearly_out_of_space" ) );
	}
}


$table = $session->make_element( "table", border=>"0" );
$html->appendChild( $session->html_phrase( "cgi/users/status:subscriptions" ) );
$html->appendChild( $table );

$table->appendChild(
	render_row( 
		$session,
		undef,
		$session->html_phrase( "cgi/users/status:subcount" ),
		$session->html_phrase( "cgi/users/status:subsent" ) ) );
foreach my $freq ( "never", "daily", "weekly", "monthly" )
{
	my $sent;
	if( $freq ne "never" )
	{
		$sent = EPrints::Subscription::get_last_timestamp( 
			$session, 
			$freq );
	}
	if( !defined $sent )
	{
		$sent = "?";
	}
	my $searchexp = new EPrints::SearchExpression(
		session => $session,
		dataset => $subds );

	$searchexp->add_field(
		$userds->get_field( "frequency" ),
		$freq );

	$searchexp->perform_search();
	my $n = $searchexp->count;
	$searchexp->dispose;

	my $k = $session->make_doc_fragment;
	$k->appendChild( $session->html_phrase( "subscription_fieldopt_frequency_".$freq ) );
	$k->appendChild( $session->make_text( ":" ) );
	$table->appendChild(
		render_row( 
			$session,
			$k,
			$session->make_text( $n ),
			$session->make_text( $sent ) ) );
}

$table = $session->make_element( "table", border=>"0" );
$html->appendChild( $session->html_phrase( "cgi/users/status:index_updates" ) );
$html->appendChild( $table );

$table->appendChild(
	render_row( 
		$session,
		undef,
		$session->html_phrase( "cgi/users/status:indexupdate" ) ) );
my @ds_ids = &EPrints::DataSet::get_sql_dataset_ids;
foreach my $ds_id ( sort @ds_ids )
{
	my $index = new EPrints::Index( 
			$session, 
			$session->get_archive->get_dataset( $ds_id ) );
	$table->appendChild(
		render_row( 
			$session,
			$session->make_text( $ds_id.":" ),
			$session->make_text( $index->get_last_timestamp ) ) );
}

$session->build_page( 
	$session->html_phrase( "cgi/users/status:title" ),
	$html,
	"status" );

$session->send_page(); 

$session->terminate();




	
# this cjg should probably by styled.
sub render_row
{
	my( $session, $key, @vals ) = @_;
	
	my( $tr, $td );
	$tr = $session->make_element( "tr" );

	if( !defined $key )
	{
		$td = $session->make_element( "td" );
		$tr->appendChild( $td );
	}
	else
	{
		$td = $session->make_element( "td", class=>"status_row_heading" );
		$tr->appendChild( $td );
		$td->appendChild( $key );
	}

	foreach( @vals )
	{
		$td = $session->make_element( "td", align=>"center", class=>"status_cell"  );
		$tr->appendChild( $td );
		$td->appendChild( $_ );
	}
	return $tr;
}
