App-ElasticSearch-Utilities

 view release on metacpan or  search on metacpan

scripts/es-storage-overview.pl  view on Meta::CPAN

#!perl
# PODNAME: es-storage-overview.pl
# ABSTRACT: Index Storage Overview by Index Name without Dates
use strict;
use warnings;
use feature qw(state);

use App::ElasticSearch::Utilities qw(es_request es_index_strip_date es_index_bases);
use CHI;
use CLI::Helpers qw(:output);
use Getopt::Long::Descriptive;
use Pod::Usage;

#------------------------------------------------------------------------#
# Argument Collection
my ($opt,$usage) = describe_options('%c %o',
    ['sort=s',  "sort by name or size, default: name",
            { default => 'name', callbacks => { 'must be name or size' => sub { $_[0] =~ /^name|size$/ } } }
    ],
    ['asc',     "Sort ascending  (default by name)"],
    ['desc',    "Sort descending (default by size)"],
    ['limit=i', "Limit to showing only this many, ie top N", { default => 0 }],
    ['raw',     "Display numeric data without rollups"],
    [],
    ['clear-cache', "Clear the _cat/indices cache"],
    [],
    ['help|h',  "Display this help", { shortcircuit => 1 }],
    ['manual|m',"Display the full manual", { shortcircuit => 1 }],
);

#------------------------------------------------------------------------#
# Documentations!
if( $opt->help ) {
    print $usage->text;
    exit;
}
pod2usage(-exitstatus => 0, -verbose => 2) if $opt->manual;

my $cache = CHI->new(
    driver    => 'File',
    root_dir  => "$ENV{HOME}/.es-utils/cache",
    namespace => 'storage',
    expires_in => '10min',
);

$cache->clear if $opt->clear_cache;

my %indices = ();
my %bases = ();
my %overview = (
    shards  => 0,
    indices => 0,
    docs    => 0,
    size    => 0,
    memory  => 0,
);

my %fields = qw(
    index index
    pri   primary
    rep   replica
    docs.count docs
    store.size size
    memory.total memory
);
my $result = $cache->get('_cat/indices');
if( !defined $result ) {
    output({color=>'cyan'}, "Fetching Index Meta-Data");
    $result  ||= es_request('_cat/indices',
            {
                uri_param => {
                    h      => join(',', sort keys %fields),
                    bytes  => 'b',
                    format => 'json'
                }
            }
    );
    $cache->set('_cat/indices', $result);



( run in 2.099 seconds using v1.01-cache-2.11-cpan-cdf2f3d4e48 )