######################################################################
#
#  EPrints Staff Status Page
#
######################################################################
#
#  This file is part of EPrints 2.
#  
#  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
#
######################################################################

#cjg NOT LANG

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

use strict;

my $session = new EPrints::Session;

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 );
$userds = $session->get_archive()->get_dataset( "user" );
$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 );
}

# Number of subscriptions
# my $num_subs = $session->get_archive()->get_dataset( "subscription" )->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->appendChild( render_empty_row( $session ) );

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->appendChild( render_empty_row( $session ) );

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

$table->appendChild( render_empty_row( $session ) );

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" ) );
}

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

$session->send_page(); 

$session->terminate();




sub render_empty_row
{
	my( $session ) = @_;

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

	return $tr;
}
