1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
11 from crunchstat_summary import logger, summarizer
14 class ArgumentParser(argparse.ArgumentParser):
16 super(ArgumentParser, self).__init__(
17 description='Summarize resource usage of an Arvados Crunch job')
18 src = self.add_mutually_exclusive_group()
20 '--job', '--container', '--container-request',
21 type=str, metavar='UUID',
22 help='Look up the specified job, container, or container request '
23 'and read its log data from Keep (or from the Arvados event log, '
24 'if the job is still running)')
26 '--pipeline-instance', type=str, metavar='UUID',
27 help='Summarize each component of the given pipeline instance')
29 '--log-file', type=str,
30 help='Read log data from a regular file')
32 '--skip-child-jobs', action='store_true',
33 help='Do not include stats from child jobs/containers')
35 '--format', type=str, choices=('html', 'text'), default='text',
38 '--threads', type=int, default=8,
39 help='Maximum worker threads to run')
41 '--verbose', '-v', action='count', default=0,
42 help='Log more information (once for progress, twice for debug)')
45 class UTF8Decode(object):
46 '''Wrap a file-like iterable to decode UTF-8 bytes into a strings
48 def __init__(self, fh):
54 def __exit__(self, exc_type, exc_val, exc_tb):
61 return next(self.fh).decode('utf-8')
66 # mimic Gzip behavior and don't close underlying object
70 class Command(object):
71 def __init__(self, args):
73 logger.setLevel(logging.WARNING - 10 * args.verbose)
77 'skip_child_jobs': self.args.skip_child_jobs,
78 'threads': self.args.threads,
80 if self.args.pipeline_instance:
81 self.summer = summarizer.NewSummarizer(self.args.pipeline_instance, **kwargs)
83 self.summer = summarizer.NewSummarizer(self.args.job, **kwargs)
84 elif self.args.log_file:
85 if self.args.log_file.endswith('.gz'):
86 fh = UTF8Decode(gzip.open(self.args.log_file))
88 fh = open(self.args.log_file, mode = 'r', encoding = 'utf-8')
89 self.summer = summarizer.Summarizer(fh, **kwargs)
91 self.summer = summarizer.Summarizer(sys.stdin, **kwargs)
92 return self.summer.run()
95 if self.args.format == 'html':
96 return self.summer.html_report()
97 elif self.args.format == 'text':
98 return self.summer.text_report()