#!/usr/bin/perl

use strict;
use warnings;
use open ':std', ':encoding(UTF-8)';

use JSON::XS;
use FileHandle;
use Text::CSV::Easy(qw/csv_build csv_parse/);

use lib '/app/tools/common/lib';
use Common::Session;

eval {
    my $sql = (
        qq{
SELECT
    t.table_schema,
    SUM(t.data_length),
    SUM(t.index_length),
    SUM(t.data_length + t.index_length)
FROM
    information_schema.tables t
WHERE 1
	AND t.table_schema <> 'sys'
	AND t.table_schema <> 'mysql'
	AND t.table_schema <> 'percona'
	AND t.table_schema <> 'information_schema'
	AND t.table_schema <> 'performance_schema'
	AND t.table_name NOT REGEXP '^agg_'
GROUP BY
    t.table_schema
}
    );

    my $clients = Common::Session::getclients;

    my $dbx = Common::Session::getdbx('admindb');
    my $host = $dbx->resultset('Report::DDbHost')->search_related( 'aws_host', { 'is_production' => 1 } );
    while ( my $row = $host->next ) {
        my $hid  = $row->db_host->d_db_host_id;
        my $dbh  = Common::Session::getdbh( $row->hostname );
        my $rows = $dbh->selectall_arrayref($sql);
        foreach my $r ( @{$rows} ) {
            ## get stats by host/db
            my ( $db, $databytes, $idxbytes, $ttlbytes ) = @{$r};

            ## search d_db_db dimension for db info
            my $d_db = $dbx->resultset('Report::DDb')->search( { 'd_db_host_id' => $hid, 'db_name' => $db } )->first;

            ## if db not defined it might be a test or something we don't care about
            ## else it's a new client and/or hasn't been created in the dimension yet
            if ( not defined $d_db ) {
                my $d_db = $dbx->resultset('Report::DDb')->search( { 'db_name' => $db } )->first;
                if ( defined $d_db ) {
                    if ( $d_db->db_status eq 'Active' ) {
                        logMessage( 'error', 'Another active DB has same name ' . $d_db->db_name );
                    }
                } else {
                    my $client = $clients->find($db);
                    if ( defined $client and not $client->is_test and $client->client_type ne 'Other' ) {
                        logMessage( 'info', $row->hostname . ":$db creating in admin db" );
                        $d_db = $dbx->resultset('Report::DDb')->create( {
                                'd_db_host_id' => $hid,
                                'db_name'      => $db,
                                'db_backup'    => 1,
                                'calc_stat'    => 1,
                                'is_test'      => 0,
                                'client_id'    => $client->clientId,
                                'client_type'  => $client->client_type,
                                'date_created' => Common::Session::mysql_now(),
                            }
                        );
                    } elsif ( not defined $client ) {
                        logMessage( 'error', $row->hostname . ":$db is not in the RSDB client config hash" );
                    }
                }
            }
            next if not defined $d_db;
        }
    }
};
if ($@) {
    logMessage( 'fatal', $@ );
}

