#!/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,
);

## Khafka 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 $url  = "http://$HOST:$PORT/$PATH";
my $ht   = HTTP::Tiny->new;

my $time = time; # don't have a better marker
my $snap_hour = int($time / 3600) * 3600 - 1;     # the last second of the previous hour
my $snap_day = int(($snap_hour) / 86400) * 86400; # the first second of the day

sub format_time {
    my $t = shift;
    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime($t);
    sprintf("%04d-%02d-%02d %02d:%02d:%02d.000",$year+1900,$mon+1,$mday,$hour,$min,$sec);
}

## Elasticsearch setup
my $prefix = 'user.cmh';
$nodes //= 'elastic.beta.pro1.techops.tools:9200';
my $e = Search::Elasticsearch->new( nodes => $nodes );

my $results = $e->search(
    body =>{
        size => 0, # Don't return the records, just the aggregate
        query => {
            bool => {
                filter => {
                    range => {
                        timestamp => {
                            gte => format_time($snap_day),
                            lte => format_time($snap_hour),
                        }
                    }
                }
            }
        },
        aggs => {
            group_by_domain => {
                terms => {
                    size => 5000,# TODO, find a way that won't bust things
                    field => 'domain'
                },
                aggs => {
                    usernames => {
                        terms => {
                            size => 5000,# TODO, find a way that won't bust things
                            field => 'acctname'
                        },
                        aggs => {
                            visitor_count => {
                                cardinality => {
                                    field => 'clientip' ## how many unique IP
                                    # Do not use the "IP" field, since it includes both source and destination IPs
                                }
                            },
                        }
                    },
                }
            }
        }
    }
);

# capture some internal metrics off of the elastic-search results
my $internal_metrics = {};
$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});
$internal_metrics->{host_post_error} = 0;
$internal_metrics->{host_post_success} = 0;

my @mets = ();
if( $results->{'aggregations'} ) {
    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'};
        my $name = "$domain.visitors";

        foreach my $user_hits ( @{ $site_hits->{'usernames'}->{'buckets'} } ){
            my $value = $user_hits->{'visitor_count'}->{'value'} // 0;
            $internal_metrics->{total_site_count}++;
            $internal_metrics->{total_site_visitors} += $value;
            $internal_metrics->{total_doc_count} += $site_hits->{doc_count};
            next unless $value;
            my $username = $user_hits->{'key'} // '-';
            warn "\t$username total visitor: $value\n" if $ENV{'DEBUG_METRIC'} && $ENV{'DEBUG_METRIC'} < 2;
            my $metric = sprintf "%s.%s.%s.%s.%s %d %d\n", $prefix, 'mwp_techops_tools', 'cp', $username, $name, $value, $snap_hour;
            warn $metric if $ENV{'DEBUG_METRIC'} > 1;
            push( @mets, { value => encode_base64($metric, '') } );
            send_to_kafka(\@mets) if scalar @mets >= 1000; # avoid size limits by sending in chunks
        }
    }
}
send_to_kafka(\@mets);

sub send_to_kafka {
    my ($mets) = @_;

    return unless scalar @{$mets};
    my $json = encode_json({records => $mets });
    warn "Sending ", scalar(@{$mets}), " records to khafka\n" if $ENV{'DEBUG_METRIC'};
    @{$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";
    }
}


$internal_metrics->{runtime} = time() - $time;
my $local_hostname = hostname();
$local_hostname =~ s/[^a-z0-9-]/_/g;
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, '') } );
}
send_to_kafka(\@mets);
exit 1 if $internal_metrics->{host_post_error};
