script/es-aggregate.pl untested

filter: Statementstmt 0.0% 0 / 88 Branchbran 0.0% 0 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 12 Podpod n/a totaltotal 0.0% 0 / 120 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 #!/usr/local/bin/perl
2 #
3 # PODNAME: es-aggregate.pl
4 # ABSTRACT: Multi-level aggregations in Elasticsearch
5 #
6 use v5.10;
7 use strict;
8 use warnings;
9  
10 use App::ElasticSearch::Utilities qw(es_request);
11 use App::ElasticSearch::Utilities::QueryString;
12 use App::ElasticSearch::Utilities::Aggregations;
13 use CLI::Helpers qw(:output);
14 use Getopt::Long::Descriptive;
15 use JSON::MaybeXS;
16 use Pod::Usage;
17 use Storable qw(dclone);
18 use YAML::XS ();
19  
20 # Grab a copy of the args
21 my @args = @ARGV;
22 # Process args
23 my ($opt,$usage) = describe_options("%c %o",
24 ['aggregate|agg=s@', "Aggregate these fields, specified more than once to sub aggregate", { required => 1 }],
25 ['by=s@', "Sort by this aggregation" ],
26 ['asc', "Sort ascending, default is descnding" ],
27 [],
28 ["Display"],
29 ['json|jq', "Results as JSON"],
30 ['show-aggs', "Show computed aggregation block"],
31 ['show-raw', "Show raw results from Elasticsearch"],
32 [],
33 ['help', "Display this help", { shortcircuit => 1 } ],
34 ['manual', "Display complete options and documentation.", { shortcircuit => 1 }],
35 );
36 if( $opt->help ) {
37 print $usage->text;
38 exit 0;
39 }
40 pod2usage(-exitval => 0, -verbose => 2) if $opt->manual;
41  
42 my $json = JSON->new->utf8->canonical;
43 my $qs = App::ElasticSearch::Utilities::QueryString->new();
44 my $q = $qs->expand_query_string( @ARGV );
45 $q->set_size(0);
46  
47 # Figure out where the --by's are spatially
48 my $ORDER = $opt->asc ? 'asc' : 'desc';
49 my @agg_param = @{ $opt->aggregate };
50 my @by_param = $opt->by ? @{ $opt->by } : ();
51 my @by = ();
52  
53 foreach my $token ( reverse @args ) {
54 if( $token =~ /^--agg/ ) {
55 $q->wrap_aggs( %{ expand_aggregate_string( pop @agg_param ) } );
56 $q->aggs_by( $ORDER => [@by] ) if @by;
57 @by=();
58 }
59 elsif( $token eq '--by' ) {
60 push @by, pop @by_param;
61 }
62 }
63  
64 output({color=>'yellow'}, YAML::XS::Dump($q->aggregations)) if $opt->show_aggs;
65  
66 my $result = $q->execute();
67 my $aggs = $result->{aggregations};
68  
69 output({color=>'cyan'}, YAML::XS::Dump($aggs)) if $opt->show_raw;
70  
71 my $flat = es_flatten_aggs($aggs);
72 foreach my $row ( @{ $flat } ) {
73 if ( $opt->json ) {
74 output({data=>1}, $json->encode({ @{ $row } }));
75 }
76 else {
77 output({data=>1}, join("\t", grep { !/\.hits$/ } @{ $row }));
78 }
79 }
80  
81 __END__