#!/usr/bin/perl

use strict;
use warnings;
use utf8;
use JSON;
use Yandex::Trace qw/$INDEX $PROFILE_INDEX/;
use Yandex::ListUtils qw/xsort/;
use Getopt::Long;
use Pod::Usage;

GetOptions(\my %opt,
    "help|h",
);

if ($opt{help}) {
    pod2usage();
}

=head1 USAGE

    trace_log_pp trace.log.201504*
    tail trace.log.20150401 | trace_log_pp 

Скрипт для вывода трейс лога в человекочитаемом виде.
Раскрывает значения внутри profile и сортирует их по all_ela desc

=cut

my $json = JSON->new()->utf8(1)->pretty(1)->canonical(1);

while (<>) {
    my $data = eval { $json->decode($_) };
    unless ($data) {
        my $line_part = substr($_,0,80);
        print STDERR "invalid json at line $. : '$line_part'...\n";
        next;
    }
    $data->[$INDEX->{DATA}]->{profile} = Yandex::Trace::profile_readable($data->[$INDEX->{DATA}]->{profile});
    print $json->encode($data);
}

