import argparse
import xcatperf
from xcatperf import analysis
from xcatperf.common import utils


class PerfShell(object):
    def get_base_parser(self):
        parser = argparse.ArgumentParser(
            prog='xcat-perf',
            # description=__doc__.strip(),
            epilog='See "xcat-perf help COMMAND" '
                   'for help on a specific command.',
            add_help=False,
            formatter_class=HelpFormatter,
        )
        parser.add_argument('-h', '--help',
                            action='store_true',
                            help=argparse.SUPPRESS,
        )
        parser.add_argument('-v', '--version',
                            action='version',
                            version=xcatperf.__version__)
        parser.add_argument('--nytprof-dir',
                            help="The directory where the data files "
                                 "generated by ntyprof live. "
                                 "Default /opt/nytprof/nytprof .",
                            default='/opt/nytprof/nytprof',
                            type=str)
        parser.add_argument('-p', '--pool',
                            help="The amount of concurrency to connected to "
                                 "xcat daemon",
                            type=int,
                            default=1)
        parser.add_argument('-c', '--count',
                            help="The amount of nodes or records to registed "
                                 "in xcat as test data",
                            type=int,
                            default=1)
        parser.add_argument('--is-nytprof',
                            help="Enable nytprof in xcatd or not",
                            type=bool,
                            default=False)
        parser.add_argument('--http-url',
                            help="http url of the perf html",
                            type=str,
                            default='http://10.4.40.25:8086/')
        return parser

    def do_help(self, args):
        self.parser.print_help()

    def main(self, argv):
        self.parser = self.get_base_parser()
        (options, args) = self.parser.parse_known_args(argv)

        if options.help:
            self.do_help(options)
            return 0

        if options.is_nytprof:
            prof = analysis.Nytprof()

        args = args or ['defls.DeflsCase']
        for arg in args:
            test_case = utils.import_object('xcatperf.scenario.%s' % arg,
                                            count=options.count,
                                            is_nytprof=options.is_nytprof,
                                            nytprof_dir=options.nytprof_dir)
            test_case.run(concurrency=options.pool)
            test_case.stop()

        if options.is_nytprof:
            prof.build(options.nytprof_dir, options.http_url)


class HelpFormatter(argparse.HelpFormatter):
    def start_section(self, heading):
        # Title-case the headings
        heading = '%s%s' % (heading[0].upper(), heading[1:])
        super(HelpFormatter, self).start_section(heading)
