Merge branch '8178-keepstore-trash-interface' of git.curoverse.com:arvados into 8178...
[arvados.git] / tools / crunchstat-summary / crunchstat_summary / command.py
1 import argparse
2 import gzip
3 import logging
4 import sys
5
6 from crunchstat_summary import logger, summarizer
7
8
9 class ArgumentParser(argparse.ArgumentParser):
10     def __init__(self):
11         super(ArgumentParser, self).__init__(
12             description='Summarize resource usage of an Arvados Crunch job')
13         src = self.add_mutually_exclusive_group()
14         src.add_argument(
15             '--job', type=str, metavar='UUID',
16             help='Look up the specified job and read its log data from Keep')
17         src.add_argument(
18             '--pipeline-instance', type=str, metavar='UUID',
19             help='Summarize each component of the given pipeline instance')
20         src.add_argument(
21             '--log-file', type=str,
22             help='Read log data from a regular file')
23         self.add_argument(
24             '--skip-child-jobs', action='store_true',
25             help='Do not include stats from child jobs')
26         self.add_argument(
27             '--format', type=str, choices=('html', 'text'), default='text',
28             help='Report format')
29         self.add_argument(
30             '--verbose', '-v', action='count', default=0,
31             help='Log more information (once for progress, twice for debug)')
32
33
34 class Command(object):
35     def __init__(self, args):
36         self.args = args
37         logger.setLevel(logging.WARNING - 10 * args.verbose)
38
39     def run(self):
40         kwargs = {
41             'skip_child_jobs': self.args.skip_child_jobs,
42         }
43         if self.args.pipeline_instance:
44             self.summer = summarizer.PipelineSummarizer(self.args.pipeline_instance, **kwargs)
45         elif self.args.job:
46             self.summer = summarizer.JobSummarizer(self.args.job, **kwargs)
47         elif self.args.log_file:
48             if self.args.log_file.endswith('.gz'):
49                 fh = gzip.open(self.args.log_file)
50             else:
51                 fh = open(self.args.log_file)
52             self.summer = summarizer.Summarizer(fh, **kwargs)
53         else:
54             self.summer = summarizer.Summarizer(sys.stdin, **kwargs)
55         return self.summer.run()
56
57     def report(self):
58         if self.args.format == 'html':
59             return self.summer.html_report()
60         elif self.args.format == 'text':
61             return self.summer.text_report()