OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2006,2007 Mitch Garnaat http://garnaat.org/ |
| 3 # |
| 4 # Permission is hereby granted, free of charge, to any person obtaining a |
| 5 # copy of this software and associated documentation files (the |
| 6 # "Software"), to deal in the Software without restriction, including |
| 7 # without limitation the rights to use, copy, modify, merge, publish, dis- |
| 8 # tribute, sublicense, and/or sell copies of the Software, and to permit |
| 9 # persons to whom the Software is furnished to do so, subject to the fol- |
| 10 # lowing conditions: |
| 11 # |
| 12 # The above copyright notice and this permission notice shall be included |
| 13 # in all copies or substantial portions of the Software. |
| 14 # |
| 15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 16 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- |
| 17 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT |
| 18 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 19 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 21 # IN THE SOFTWARE. |
| 22 |
| 23 import os |
| 24 from datetime import datetime, timedelta |
| 25 from boto.utils import parse_ts |
| 26 import boto |
| 27 |
| 28 class ResultProcessor: |
| 29 |
| 30 LogFileName = 'log.csv' |
| 31 |
| 32 def __init__(self, batch_name, sd, mimetype_files=None): |
| 33 self.sd = sd |
| 34 self.batch = batch_name |
| 35 self.log_fp = None |
| 36 self.num_files = 0 |
| 37 self.total_time = 0 |
| 38 self.min_time = timedelta.max |
| 39 self.max_time = timedelta.min |
| 40 self.earliest_time = datetime.max |
| 41 self.latest_time = datetime.min |
| 42 self.queue = self.sd.get_obj('output_queue') |
| 43 self.domain = self.sd.get_obj('output_domain') |
| 44 |
| 45 def calculate_stats(self, msg): |
| 46 start_time = parse_ts(msg['Service-Read']) |
| 47 end_time = parse_ts(msg['Service-Write']) |
| 48 elapsed_time = end_time - start_time |
| 49 if elapsed_time > self.max_time: |
| 50 self.max_time = elapsed_time |
| 51 if elapsed_time < self.min_time: |
| 52 self.min_time = elapsed_time |
| 53 self.total_time += elapsed_time.seconds |
| 54 if start_time < self.earliest_time: |
| 55 self.earliest_time = start_time |
| 56 if end_time > self.latest_time: |
| 57 self.latest_time = end_time |
| 58 |
| 59 def log_message(self, msg, path): |
| 60 keys = sorted(msg.keys()) |
| 61 if not self.log_fp: |
| 62 self.log_fp = open(os.path.join(path, self.LogFileName), 'a') |
| 63 line = ','.join(keys) |
| 64 self.log_fp.write(line+'\n') |
| 65 values = [] |
| 66 for key in keys: |
| 67 value = msg[key] |
| 68 if value.find(',') > 0: |
| 69 value = '"%s"' % value |
| 70 values.append(value) |
| 71 line = ','.join(values) |
| 72 self.log_fp.write(line+'\n') |
| 73 |
| 74 def process_record(self, record, path, get_file=True): |
| 75 self.log_message(record, path) |
| 76 self.calculate_stats(record) |
| 77 outputs = record['OutputKey'].split(',') |
| 78 if 'OutputBucket' in record: |
| 79 bucket = boto.lookup('s3', record['OutputBucket']) |
| 80 else: |
| 81 bucket = boto.lookup('s3', record['Bucket']) |
| 82 for output in outputs: |
| 83 if get_file: |
| 84 key_name = output.split(';')[0] |
| 85 key = bucket.lookup(key_name) |
| 86 file_name = os.path.join(path, key_name) |
| 87 print 'retrieving file: %s to %s' % (key_name, file_name) |
| 88 key.get_contents_to_filename(file_name) |
| 89 self.num_files += 1 |
| 90 |
| 91 def get_results_from_queue(self, path, get_file=True, delete_msg=True): |
| 92 m = self.queue.read() |
| 93 while m: |
| 94 if 'Batch' in m and m['Batch'] == self.batch: |
| 95 self.process_record(m, path, get_file) |
| 96 if delete_msg: |
| 97 self.queue.delete_message(m) |
| 98 m = self.queue.read() |
| 99 |
| 100 def get_results_from_domain(self, path, get_file=True): |
| 101 rs = self.domain.query("['Batch'='%s']" % self.batch) |
| 102 for item in rs: |
| 103 self.process_record(item, path, get_file) |
| 104 |
| 105 def get_results_from_bucket(self, path): |
| 106 bucket = self.sd.get_obj('output_bucket') |
| 107 if bucket: |
| 108 print 'No output queue or domain, just retrieving files from output_
bucket' |
| 109 for key in bucket: |
| 110 file_name = os.path.join(path, key) |
| 111 print 'retrieving file: %s to %s' % (key, file_name) |
| 112 key.get_contents_to_filename(file_name) |
| 113 self.num_files + 1 |
| 114 |
| 115 def get_results(self, path, get_file=True, delete_msg=True): |
| 116 if not os.path.isdir(path): |
| 117 os.mkdir(path) |
| 118 if self.queue: |
| 119 self.get_results_from_queue(path, get_file) |
| 120 elif self.domain: |
| 121 self.get_results_from_domain(path, get_file) |
| 122 else: |
| 123 self.get_results_from_bucket(path) |
| 124 if self.log_fp: |
| 125 self.log_fp.close() |
| 126 print '%d results successfully retrieved.' % self.num_files |
| 127 if self.num_files > 0: |
| 128 self.avg_time = float(self.total_time)/self.num_files |
| 129 print 'Minimum Processing Time: %d' % self.min_time.seconds |
| 130 print 'Maximum Processing Time: %d' % self.max_time.seconds |
| 131 print 'Average Processing Time: %f' % self.avg_time |
| 132 self.elapsed_time = self.latest_time-self.earliest_time |
| 133 print 'Elapsed Time: %d' % self.elapsed_time.seconds |
| 134 tput = 1.0 / ((self.elapsed_time.seconds/60.0) / self.num_files) |
| 135 print 'Throughput: %f transactions / minute' % tput |
| 136 |
OLD | NEW |