#!/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_stop  = int($time / 300) * 300 - 1;     # the last second of the 5 minutes
my $snap_start = int(($snap_stop) / 3600) * 3600 - 3600; # the first second of 2 hours ago

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_start),
                            lte => format_time($snap_stop),
                        }
                    }
                }
            }
        },
        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 => {
                            group_by_source => {
                                terms => {
                                    size => 5000,# TODO, find a way that won't bust things
                                    field => 'source'
                                },
                                aggs => {
                                    bandwidth_over_time => {
                                        date_histogram => {
                                            field => 'timestamp',
                                            interval => '5m',
                                        },
                                        aggs => {
                                            bandwidth_breakout => {
                                                extended_stats => {
                                                    field => 'bytes'
                                                }
                                            },
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
);

# 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.bandwidth";
        $internal_metrics->{total_site_count}++;

        foreach my $user_hits ( @{ $site_hits->{'usernames'}->{'buckets'} } ){
            my $username = $user_hits->{'key'} // '-';
            foreach my $server ( @{ $user_hits->{'group_by_source'}->{'buckets'} } ) {
                $internal_metrics->{total_site_server_count}++;
                my $server_name = $server->{'key'};
                my ($short_host, $long_host) = split /\./, $server_name, 2;
                $long_host =~ s/[^a-z0-9-]/_/g;
                $short_host =~ s/[^a-z0-9-]/_/g;
                foreach my $five_min ( @{ $server->{'bandwidth_over_time'}->{'buckets'} } ) {
                    next unless $five_min->{'doc_count'};
                    my $bucket_time = $five_min->{'key'} * .001; ## or do we want key_as_string ?
                    my $value = $five_min->{'bandwidth_breakout'}->{'sum'} // 0; ##error page hits that have 0 bandwidth value

                    $internal_metrics->{total_site_bandwidth} += $value;
                    $internal_metrics->{total_bucket_count}++;

                    warn "\t\t\t", join(',', $five_min->{'key_as_string'}, $five_min->{'doc_count'}, $value), "\n" if $ENV{'DEBUG_METRIC'};
                    my $metric = sprintf "%s.%s.%s.%s.%s %d %d\n", $prefix, $long_host, $short_host, $username, $name, $value, $bucket_time;
                    warn $metric if defined($ENV{'DEBUG_METRIC'}) and $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_bandwidth",$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};
