#!/usr/bin/env ruby
# SPDX-License-Identifier: MulanPSL-2.0+
# Copyright (c) 2020 Huawei Technologies Co., Ltd. All rights reserved.
# frozen_string_literal: true

# Usage:
#       compare "conditions_1" "conditions_2" ... -c "common_conditions"
#       compare "conditions" -d "dimensions"
#       compare -t template.yaml
# Eg:
#       compare "id=6000,6001" "id=7000,7001"
#       compare "commit=a12d232e" "commit=b3bacc31"
#       compare "os=debian" "os=centos" -c "suite=iperf"
#       compare os=centos -d "os_version os_arch"
#       compare os=centos suite=iperf -d "os_version os_arch" --theme="classic"

require 'optparse'
require_relative '../lib/compare.rb'

common_conditions = ''
is_group = false
dimensions = nil
options = {}
options[:min_samples] = 1
template = nil

opt_parser = OptionParser.new do |opts|
  opts.banner = 'Usage: compare "conditions" ... [option]'

  opts.separator ''
  opts.separator 'a conditions can be "id=100, ..." or "suite=iperf os=debian ..."'
  opts.separator ''
  opts.separator 'options:'

  opts.on('-c', '--common common_conditions', 'common conditions are same with conditions',
          'and will merge with each conditions') do |c|
    common_conditions = c
  end

  opts.on('-d', '--dimension dimensions', 'this option activate group compare mode, require filter and dimenssion',
          'filter like: os=openeuler,centos suite=iperf', 'dimensions like: "tbox_group os_arch os ..."') do |d|
    dimensions = d
    is_group = true
  end

  opts.on('-f', '--field field', 'field to filter stats_metrics of result',
          'use like: -f "iperf.tcp.sender.bps iperf.tcp.receiver.bps ..."') do |f|
    options[:fields] = f.split(',')
  end

  opts.on('--theme theme', 'turn on colorful display with theme: classic|focus_good|focus_bad',
          '|striking|light|json|html') do |theme|
    options[:theme] = theme
  end

  opts.on('--min_samples min_samples', 'give a number to litmit the min_samples of matrix which used to compare') do |min_samples|
    options[:min_samples] = min_samples.to_i
  end

  opts.on('--transposed', 'transposed dimensions and metric in result') do
    options[:transposed] = true
  end

  opts.on('-t', '--template template', 'compare with user-defined template') do |t|
    template = t
  end

  opts.on_tail('-h', '--help', 'show this message') do
    puts opts
    exit
  end
end

argv = if ARGV == []
         ['-h']
       else
         ARGV
       end
opt_parser.parse!(argv)

if template
  compare_by_template(template, options)
elsif is_group
  compare_group(argv, dimensions, options)
else
  compare_matrices_list(argv, common_conditions, options)
end
