#!/usr/bin/env python
# Copyright 2016 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import argparse
import json
import os
import sys

sys.path.insert(1, os.path.join(os.path.dirname(__file__), '..'))
from tracing.metrics import metric_runner

def Main(argv):
  parser = argparse.ArgumentParser(
      description='Runs metrics on local traces')
  parser.add_argument('metric', nargs='+',
                      help='A list of metrics from tracing/metrics/')
  parser.add_argument('trace_file_or_dir',
                      help='A trace file, or a dir containing trace files')

  args = parser.parse_args(argv[1:])
  metrics = [metric + ('' if metric.endswith('.html') else '.html') for metric
             in args.metric]

  if os.path.isdir(args.trace_file_or_dir):
    trace_dir = args.trace_file_or_dir
    traces = [os.path.join(trace_dir, trace) for trace in os.listdir(trace_dir)]
  else:
    traces = [args.trace_file_or_dir]

  results = {}
  for trace in traces:
    results[trace] = metric_runner.RunMetrics(trace, metrics).AsDict()

  print json.dumps(results, indent=2, sort_keys=True, separators=(',', ': '))


if __name__ == '__main__':
  sys.exit(Main(sys.argv))
