#!/usr/bin/perl

=encoding UTF-8
=cut

=head1 DESCRIPTION

=cut

# common modules
use strict;
use warnings FATAL => 'all';
use feature 'say';
use utf8;
use open qw(:std :utf8);

use Carp;
use Moment;
use File::Slurp;
use JSON::XS;
use JSON::PP qw();

# global vars

# subs
sub to_pretty_json {
    my ($data) = @_;

    my $json_coder = JSON::PP
        ->new
        ->pretty
        ->canonical
        ->indent_length(4)
        ;

    my $pretty_json = $json_coder->encode($data);

    return $pretty_json;
}

sub get_create_text {
    my ($date) = @_;

    my $text = '';

    my @lines = read_file sprintf '/local/creator/log/create.%s.jsonl', $date;

    my %beta_create_by_login;

    foreach my $line (@lines) {
        my $data;

        eval {
            $data = decode_json $line;
        };

        next if $@;

        $beta_create_by_login{$data->{login} // 'unknown'}++;
    }

    $text .= "## Beta create by login\n";
    $text .= to_pretty_json \%beta_create_by_login;
    $text .= "\n";

    return $text;
}

sub get_api_text {
    my ($date) = @_;

    my $text = '';

    my @lines = read_file sprintf '/local/creator/log/api.%s.jsonl', $date;

    my %error_lines;

    my %api_requests_by_login;
    my %api_requests_by_code;
    my %api_requests_by_url;

#$VAR1 = {
#          'url' => '/api/3/betas/',
#          'elapsed' => '0.107972',
#          'code' => 200,
#          'user-agent' => 'HTTP-Tiny/0.025',
#          'has_login' => bless( do{\(my $o = 0)}, 'JSON::XS::Boolean' ),
#          'timestamp' => '2017-09-14T00:00:01Z',
#          'ip' => undef,
#          'method' => 'GET'
#        };

    foreach my $line (@lines) {
        my $data;

        eval {
            $data = decode_json $line;
        };

        next if $@;

        $api_requests_by_login{$data->{login} // 'unknown'}++;
        $api_requests_by_code{$data->{code}}++;
        $api_requests_by_url{$data->{url}}++;

        if ($data->{code} ne '200') {
            push @{$error_lines{ $data->{code} }}, $line;
        }
    }

    $text .= "## API requests by login\n";
    $text .= to_pretty_json \%api_requests_by_login;
    $text .= "\n";

    $text .= "## API requests by code\n";
    $text .= to_pretty_json \%api_requests_by_code;
    $text .= "\n";

    $text .= "## API requests by url\n";
    $text .= to_pretty_json \%api_requests_by_url;
    $text .= "\n";

    if (%error_lines) {
        foreach my $code (sort keys %error_lines) {
            $text .= "## $code\n";
            foreach my $line (@{$error_lines{$code}}) {
                $text .= $line;
            }

            $text .= "\n";
        }
    }

    return $text;
}

# main
sub main {

    my $date = Moment->now()->minus(day => 1)->get_d();

    my $hostname = `hostname -f`;
    chomp($hostname);

    my $text = sprintf "# %s %s\n\n",
        $hostname,
        $date,
        ;

    $text .= get_create_text($date);
    $text .= get_api_text($date);

    write_file('/tmp/creator_stat', $text);
    `cat /tmp/creator_stat|mail partner-creator\@yandex-team.ru --subject "$hostname $date"`;

}
main();
__END__
