#!/usr/bin/perl

use strict;
use warnings;

use Search::Elasticsearch;
use HTTP::Tiny;
use JSON::MaybeXS qw(encode_json);
use MIME::Base64 qw(encode_base64);
use Getopt::Long qw(GetOptions);
use Sys::Hostname;


GetOptions(
    'nodes=s' => \my $nodes,
    'host=s'  => \my $host,
);

## SETUP
    my $HOST    = $host // 'kafkarest.beta.pro1.techops.tools'; # '10.102.98.11'; # TODO should have defaults for beta and prod as well
    my $PORT    = 80;                        # 8082;
    my $PATH    = 'topics/user-graphite';

my $internal_metrics = {};
my $time = time; ## don't have a better marker
my $url  = "http://$HOST:$PORT/$PATH";
my $prefix = 'user.cmh';
my $ht = HTTP::Tiny->new;


my $e = Search::Elasticsearch->new(
    nodes => $nodes // 'elastic.beta.pro1.techops.tools:9200',
);

my $results = $e->search(
    body =>{
## Don't return the records, just the aggregate
        size => 0,
        query => {
            bool => {
                filter => {
                    range => {
                        timestamp => {
                            gte => 'now/d',  #for today's count
                        }
                    }
                }
            }
        },
        aggs => {
            group_by_domain => {
                terms => {
                    field => 'domain'
                },
                aggs => {
                    usernames => {
                        terms => {
                            field => 'acctname'
                        },
                    },
## how many unique IP
                    visitor_count => {
                        cardinality => {
                            field => 'IP'
                        }
                    },
                    visitors_group => {
                        terms => {
                            field => 'IP'
                        },
                        aggs => {
                            visitors => {
                                cardinality => {
                                    field => 'IP'
                                }
                            }
                        }
                    }

                }
            }
        }
    }
);

# capture some internal metrics off of the elastic-search results
$internal_metrics->{es_took} = $results->{took};
$internal_metrics->{es_shards_failed} = $results->{_shards}->{failed};
$internal_metrics->{es_shards_skipped} = $results->{_shards}->{skipped};
$internal_metrics->{es_shards_successful} = $results->{_shards}->{successful};
$internal_metrics->{es_shards_total} = $results->{_shards}->{total};
$internal_metrics->{es_hits_total} = $results->{hits}->{total} if defined($results->{hits}->{total});

   # visitor_count => value => sum of unique IP's
   # visitors_group => buckets => array of IP objects (breakout of hits per visitor)

if( $results->{'aggregations'} ) {
    my @mets = ();
    foreach my $site_hits ( @{ $results->{'aggregations'}->{'group_by_domain'}->{'buckets'} } ){
        my $domain = $site_hits->{'key'};
        $domain =~ s/[^a-z0-9-]/_/g;
        warn "Processing: ", $domain, "\n" if $ENV{'DEBUG_METRIC'};
        warn "\tTotal Visitor: ", $site_hits->{'visitor_count'}->{'value'}, "\n" if $ENV{'DEBUG_METRIC'};
        my $name = "$domain.visitors";
        my $value = $site_hits->{'visitor_count'}->{'value'} // 0;
        my $username = $site_hits->{'usernames'}->{'buckets'}->[0]->{'key'} // '-';
        $internal_metrics->{total_site_count}++;
        $internal_metrics->{total_site_visitors} += $value;
        $internal_metrics->{total_doc_count} += $site_hits->{doc_count};
        my $metric = sprintf "%s.%s.%s.%s.%s %d %d\n", $prefix, 'mwp_techops_tools', 'cp', $username, $name, $value, $time; 
        warn $metric if defined($ENV{'DEBUG_METRIC'}) and $ENV{'DEBUG_METRIC'} > 1;;
            # using domain instead of user for now
        push( @mets, { value => encode_base64($metric, '') } );
        foreach my $visitor_hits ( @{ $site_hits->{'visitors_group'}->{'buckets'} } ) {
            warn "\t\t", $visitor_hits->{'key'}, " : ", $visitor_hits->{'doc_count'}, "\n" if $ENV{'DEBUG_METRIC'};
        }
    }
    my $json = encode_json({records => \@mets });
    my $resp = $ht->request( POST => $url, {
        content => $json,
        headers => {
            'Content-Type' => 'application/vnd.kafka.binary.v2+json',
            Accept         => 'application/vnd.kafka.v2+json',
        },
    });

    if (! $resp->{'success'}) {
        warn "Failed sending data to $url (".($resp->{'code'} // 'undef').")" if $ENV{'DEBUG_METRIC'};
        print "ERROR\n";
        $internal_metrics->{host_post_error}++; 
    }else{
        $internal_metrics->{host_post_success}++;
    }
    print "SUCCESS\n";
}


my $time_end = time();
$internal_metrics->{runtime} = $time_end - $time;
my $local_hostname = hostname();
$local_hostname =~ s/\./_/g;
my @mets = ();
foreach my $metric_key (keys %$internal_metrics){
    my $metric = sprintf("%s.%s.%s %d %d\n","mwp_metrics_visitors",$local_hostname,$metric_key,$internal_metrics->{$metric_key},$time);
    warn $metric if $ENV{'DEBUG_METRIC'};
    push( @mets, { value => encode_base64($metric, '') } );
}
my $json = encode_json({records => \@mets });
my $resp = $ht->request( POST => $url, {
    content => $json,
    headers => {
        'Content-Type' => 'application/vnd.kafka.binary.v2+json',
        Accept         => 'application/vnd.kafka.v2+json',
    },
});
if (! $resp->{'success'}) {
     warn "Failed sending data to $url (".($resp->{'code'} // 'undef').")" if $ENV{'DEBUG_METRIC'};
     print "ERROR\n";
}

