#!/usr/bin/perl
## -*- mode: perl; indent-tabs-mode: t; perl-indent-level: 4 -*-
## vim: autoindent tabstop=4 shiftwidth=4 expandtab softtabstop=4 filetype=perl

# Usage: pbench-get-metrics <elasticsearch-hostname-and-port> <iteration-id>
#
# You can get the iteraton-id with pbench-get-iterations.
#
# This script will output all of the metrics (benchmark and tools)
# for an iteration-id

use strict;
use warnings;
use File::Basename;
use File::Find;
use REST::Client;
use Time::HiRes qw(gettimeofday);

use lib $ENV{'pbench_lib_dir'};
use JSON::MaybeXS;
use Data::Dumper;
use PbenchCDM qw(get_cdm_ver get_cdm_rel);
use PbenchBase qw(get_json_file put_json_file remove_element);
use PbenchES qw(get_name_format get_metric_data);
use Getopt::Long 'HelpMessage';


sub usage {
    print "--es-host=<string>       The hostname and port for Elasticsearch.  You can also set ES_HOST\n";
    print "--period-id=<UUID>       The UUID (period.id) for a time-period from a benchmark-iteration-sample.\n";
    print "                         Running 'pbench-get-iteration-metrics' will provide a period.id for the\n";
    print "                         primary period (the measurement phase) of the benchmark-iteration-samples\n";
    print "--metric-source=<string> The name of the metric source, like 'fio' or 'iostat'\n";
    print "--metric-type=<string>   The name of the metric type, like 'iops' for fio, or 'BusyCpuUtil' for mpstat\n";
    print "--breakout=<string>      If this option is not used, a single data-series will be returned which aggregates\n";
    print "                         all metrics which match the metric-source and metric-type.  However, a query can\n";
    print "                         return multiple data-series, broken-out by any of the field names found in the\n";
    print "                         name_format.  To get these available fields, either (a) inspect the returned\n";
    print "                         data from a call to this script, specifically, {{'breakouts'}:[<list of fields>]},\n";
    print "                         or, (b) run the get-iteraton-metrics script to see the available break-outs\n";
    print "                         for each of the metric source/tyops.\n";
    print "--begin=<int>            The epochtime in milliseconds for the beginning of the time domain.\n";
    print "                         This must be within the same time period for period.id and less than\n";
    print "                         'end' value provided\n";
    print "--end=<int>              The epochtime in milliseconds for the ending of the time domain.\n";
    print "                         This must be within the same time period for period.id and greater than\n";
    print "                         'begin' value provided\n";
    exit;
}

GetOptions(
    'es-host=s'       => \(my $es_host = $ENV{'ES_HOST'}),
    'period-id=s'     => \ my $period_id,
    'metric-source=s' => \ my $metric_source,
    'metric-type=s'   => \ my $metric_type,
    'breakout=s'      => \(my $breakout = ""),
    'begin=i'         => \ my $begin,
    'end=i'           => \ my $end,
    'resolution=i'    => \(my $resolution = 1)
) or usage;

$es_host and $period_id and $metric_source and $metric_type and $begin and $end or usage;

my $index_basename = "cdmv" . get_cdm_ver . get_cdm_rel;
my $script_name = basename($0);
my %data;
my $coder = JSON::MaybeXS->new->ascii->canonical->pretty;
$data{'name_format'} = get_name_format($es_host, $period_id, $metric_source, $metric_type);
$data{'metric_source'} = $metric_source;
$data{'metric_type'} = $metric_type;
get_metric_data($es_host, \%data, $begin, $end, $period_id, $metric_source, $metric_type, $resolution, split(/,/, $breakout));
my $data_json = $coder->encode(\%data);
print $data_json;
