#!/usr/bin/perl
#
# This shows the total number of rows in the public and history (where applicable) schemas of all available 
# databases. It is meant as a diagnostic tool
#

use strict;
use warnings;
use Anvil::Tools;
use Data::Dumper;

$| = 1;

my $THIS_FILE           =  ($0 =~ /^.*\/(.*)$/)[0];
my $running_directory   =  ($0 =~ /^(.*?)\/$THIS_FILE$/)[0];
if (($running_directory =~ /^\./) && ($ENV{PWD}))
{
	$running_directory =~ s/^\./$ENV{PWD}/;
}

my $anvil = Anvil::Tools->new();

$anvil->Database->connect({debug => 3});
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2, secure => 0, key => "log_0132"});
if (not $anvil->data->{sys}{database}{connections})
{
	# No databases, exit.
	$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, 'print' => 1, priority => "err", key => "error_0003"});
	$anvil->nice_exit({exit_code => 1});
}
$anvil->Get->switches();

my $table_length = 0;
my $count_length = 0;
my $db_length    = 0; 
my $tables       = $anvil->Database->get_tables_from_schema({schema_file => "all"});
foreach my $table (@{$tables})
{
	if (length($table) > $table_length)
	{
		$table_length = length($table);
	}
	
	foreach my $uuid (keys %{$anvil->data->{cache}{database_handle}})
	{
		$anvil->data->{counts}{$table}{$uuid}{public_count}  = 0;
		$anvil->data->{counts}{$table}{$uuid}{history_count} = 0;
		if ($anvil->data->{sys}{database}{history_table}{$table})
		{
			my $query = "SELECT COUNT(*) FROM history.".$table.";";
			$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { query => $query }});
			
			$anvil->data->{counts}{$table}{$uuid}{history_count} = $anvil->Database->query({uuid => $uuid, query => $query, source => $THIS_FILE, line => __LINE__})->[0]->[0];
			
			my $say_count = $anvil->Convert->add_commas({number => $anvil->data->{counts}{$table}{$uuid}{history_count}});
			$anvil->data->{counts}{$table}{$uuid}{history_comma} = $say_count;
			$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { 
				"counts::${table}::${uuid}::history_count" => $anvil->data->{counts}{$table}{$uuid}{history_count},
				"counts::${table}::${uuid}::history_comma" => $anvil->data->{counts}{$table}{$uuid}{history_comma},
			}});
			if (length($say_count) > $count_length)
			{
				$count_length = length($say_count);
			}
		}
		else
		{
			$anvil->data->{counts}{$table}{$uuid}{history_count} = -1;
			$anvil->data->{counts}{$table}{$uuid}{history_comma} = "--";
			$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { 
				"counts::${table}::${uuid}::history_count" => $anvil->data->{counts}{$table}{$uuid}{history_count},
				"counts::${table}::${uuid}::history_comma" => $anvil->data->{counts}{$table}{$uuid}{history_comma},
			}});
		}
		my $query = "SELECT COUNT(*) FROM ".$table.";";
		$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { query => $query }});
		
		$anvil->data->{counts}{$table}{$uuid}{public_count} = $anvil->Database->query({uuid => $uuid, query => $query, source => $THIS_FILE, line => __LINE__})->[0]->[0];
			
		my $say_count = $anvil->Convert->add_commas({number => $anvil->data->{counts}{$table}{$uuid}{public_count}});
		$anvil->data->{counts}{$table}{$uuid}{public_comma} = $say_count;
		$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { 
			"counts::${table}::${uuid}::public_count" => $anvil->data->{counts}{$table}{$uuid}{public_count},
			"counts::${table}::${uuid}::public_comma" => $anvil->data->{counts}{$table}{$uuid}{public_comma},
		}});
		if (length($say_count) > $count_length)
		{
			$count_length = length($say_count);
		}
	}
}

$db_length = (($count_length * 2) + 3);
foreach my $uuid (keys %{$anvil->data->{cache}{database_handle}})
{
	my $host_name =  $anvil->Get->host_name_from_uuid({host_uuid => $uuid});
	   $host_name =~ s/\..*$//;
	if (length($host_name) > $db_length) 
	{
		$db_length = length($host_name);
	}
	
	$anvil->data->{host_uuid}{$uuid}{host_name}    = $host_name;
	$anvil->data->{db_host_name}{$host_name}{uuid} = $uuid;
}

if ($db_length > (($count_length * 2) - 3))
{
	$count_length = (($db_length - 3) / 2);
	
	if ($count_length =~ /\./)
	{
		$count_length = (int($count_length) + 1);
		$db_length++;
	}
}

# header, line 1, and build break line
my $break_line = "-"; for (1..$table_length) { $break_line .= "-"; };
print " "; for (1..$table_length) { print " "; };
foreach my $host_name (sort {$a cmp $b} keys %{$anvil->data->{db_host_name}})
{
	print " | ".sprintf("%-${db_length}s", $host_name);
	$break_line .= "-+-"; for (1..$count_length) { $break_line .= "-"; };
	$break_line .= "-+-"; for (1..$count_length) { $break_line .= "-"; };
}
$break_line .= "-";
print " \n";

# header, line 2
my $say_table      = $anvil->Words->string({key => "header_0062"});
my $say_public     = $anvil->Words->string({key => "header_0063"});
my $say_history    = $anvil->Words->string({key => "header_0064"});
my $center_table   = $anvil->Words->center_text({string => $say_table,   width => $table_length});
my $center_public  = $anvil->Words->center_text({string => $say_public,  width => $count_length});
my $center_history = $anvil->Words->center_text({string => $say_history, width => $count_length});
print " ".$center_table;
foreach my $host_name (sort {$a cmp $b} keys %{$anvil->data->{db_host_name}})
{
	print " | ".$center_public." | ".$center_history;
}
print " \n";
print $break_line."\n";

foreach my $table (sort {$a cmp $b} keys %{$anvil->data->{counts}})
{
	print " ".sprintf("%-${table_length}s", $table);
	foreach my $host_name (sort {$a cmp $b} keys %{$anvil->data->{db_host_name}})
	{
		my $uuid         = $anvil->data->{db_host_name}{$host_name}{uuid};
		my $public_rows  = $anvil->data->{counts}{$table}{$uuid}{public_comma};
		my $history_rows = $anvil->data->{counts}{$table}{$uuid}{history_comma};
		#print " | ".$public_rows." | ".$history_rows;
		print " | ".sprintf("%${count_length}s", $public_rows)." | ".sprintf("%${count_length}s", $history_rows);
	}
	print " \n";
}
print $break_line."\n";

$anvil->nice_exit({exit_code => 0});
