1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
12 from crunchstat_summary import logger, summarizer
13 from crunchstat_summary._version import __version__
16 class ArgumentParser(argparse.ArgumentParser):
18 super(ArgumentParser, self).__init__(
19 description='Summarize resource usage of an Arvados Crunch job')
20 src = self.add_mutually_exclusive_group()
22 '--job', '--container-request',
23 type=str, metavar='UUID',
24 help='Look up the specified job or container request '
25 'and read its log data from Keep (or from the Arvados event log, '
26 'if the job is still running)')
29 type=str, metavar='UUID',
30 help='[Deprecated] Look up the specified container find its container request '
31 'and read its log data from Keep (or from the Arvados event log, '
32 'if the job is still running)')
34 '--pipeline-instance', type=str, metavar='UUID',
35 help='[Deprecated] Summarize each component of the given pipeline instance (historical pre-1.4)')
37 '--log-file', type=str,
38 help='Read log data from a regular file')
40 '--skip-child-jobs', action='store_true',
41 help='Do not include stats from child jobs/containers')
43 '--format', type=str, choices=('html', 'text'), default='text',
46 '--threads', type=int, default=8,
47 help='Maximum worker threads to run')
49 '--verbose', '-v', action='count', default=0,
50 help='Log more information (once for progress, twice for debug)')
51 self.add_argument('--version', action='version',
52 version="%s %s" % (sys.argv[0], __version__),
53 help='Print version and exit.')
56 class UTF8Decode(object):
57 '''Wrap a file-like iterable to decode UTF-8 bytes into a strings
59 def __init__(self, fh):
65 def __exit__(self, exc_type, exc_val, exc_tb):
72 return next(self.fh).decode('utf-8')
77 # mimic Gzip behavior and don't close underlying object
81 class Command(object):
82 def __init__(self, args):
84 logger.setLevel(logging.WARNING - 10 * args.verbose)
88 'skip_child_jobs': self.args.skip_child_jobs,
89 'threads': self.args.threads,
90 'arv': arvados.api('v1')
92 if self.args.pipeline_instance:
93 self.summer = summarizer.NewSummarizer(self.args.pipeline_instance, **kwargs)
95 self.summer = summarizer.NewSummarizer(self.args.job, **kwargs)
96 elif self.args.container:
97 self.summer = summarizer.NewSummarizer(self.args.container, **kwargs)
98 elif self.args.log_file:
99 if self.args.log_file.endswith('.gz'):
100 fh = UTF8Decode(gzip.open(self.args.log_file))
102 fh = open(self.args.log_file, mode = 'r', encoding = 'utf-8')
103 self.summer = summarizer.Summarizer(fh, **kwargs)
105 self.summer = summarizer.Summarizer(sys.stdin, **kwargs)
106 return self.summer.run()
109 if self.args.format == 'html':
110 return self.summer.html_report()
111 elif self.args.format == 'text':
112 return self.summer.text_report()