#!perl

use strict;
use warnings;
use IO::Socket;
use Getopt::Long qw(:config posix_default no_ignore_case gnu_compat bundling);
use Pod::Usage qw(pod2usage);

use App::RedisTop::Perform;
use App::RedisTop::Component::CPU;
use App::RedisTop::Component::Memory;
use App::RedisTop::Component::Connection;
use App::RedisTop::Component::Save;
use App::RedisTop::Component::Command;
use App::RedisTop::Component::DB;

#################################
# main
#################################
package main;

my %groups = (
    cpu     => { class => App::RedisTop::Component::CPU->new(),         sort => 1, },
    memory  => { class => App::RedisTop::Component::Memory->new(),      sort => 2, },
    conn    => { class => App::RedisTop::Component::Connection->new(),  sort => 3, },
    save    => { class => App::RedisTop::Component::Save->new(),        sort => 4, },
    command => { class => App::RedisTop::Component::Command->new(),     sort => 5, },
    db      => { class => App::RedisTop::Component::DB->new(dbid => 0), sort => 6, },
);

sub main {
    my $opt = +{};

    GetOptions(
        'c|cpu'         => \$opt->{cpu},
        'M|memory'      => \$opt->{memory},
        'n|conn'        => \$opt->{conn},
        's|save'        => \$opt->{save},
        'C|command'     => \$opt->{command},
        'd|db'          => \$opt->{db},
        'i|instances=s' => \$opt->{instances},
        'sleep=i'       => \$opt->{sleep},
        'nocolor'       => \$opt->{nocolor},
        'h|help'        => \$opt->{help},
    );

    pod2usage 1 if $opt->{help};

    my $sleep     = $opt->{sleep} || 3;
    my @instances = ($opt->{instances}) ? split(/,/, $opt->{instances}) : ('127.0.0.1:6379');
    $ENV{ANSI_COLORS_DISABLED} = 1 if $opt->{nocolor};

    # group specified
    my @sp_groups;
    for my $key (sort{ $groups{$a}->{'sort'} <=> $groups{$b}->{'sort'}} keys %groups) {
        push @sp_groups, $groups{$key}->{class} if $opt->{$key};
    }

    # default
    if(scalar @sp_groups == 0) {
        push @sp_groups, $groups{$_}->{class} for qw/cpu memory conn save command db/;
    }

    my $perform = App::RedisTop::Perform->new(
        groups    => \@sp_groups,
        instances => \@instances,
    );
    while(1) {
        $perform->run;
        sleep($sleep);
    }
}

main();

__END__

=head1 NAME

redis-top - Redis resource statistics tool.

=head1 DESCRIPTION

Redis resource statistics tool.

=head1 USAGE

redis-top [options]

=head2 Example

=over 4

=item redis-top -i 127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381,127.0.0.1:6382

=item redis-top --sleep 1 --nocolor --cpu --memory --db

=item redis-top -cMnsCd  # full

=back

=head1 OPTIONS

=head2 GROUP OPTIONS

=over 4

=item -c,--cpu

enable cpu stats

=item -M,--memory

enable memory stats

=item -n,--conn

enable connection stats

=item -s,--save

enable save stats

=item -C,--command

enable command stats

=item -d,--db

enable db stats (default:db0 stats)

=back

=head2 GLOBAL OPTIONS

=over 4

=item --sleep

sleep time (default:3)

=item --nocolor

disable colors

=item -h --help

show help

=back

=cut

