1#!/usr/bin/env python 2 3# Copyright 2016 The Chromium OS Authors. All rights reserved. 4# Use of this source code is governed by a BSD-style license that can be 5# found in the LICENSE file. 6 7"""This script is used to calculate the daily summary of the total size of 8the test results uploaded to Google Storage per day. It can also output the 9test results with the largest size. 10""" 11 12import argparse 13import time 14 15import common 16from autotest_lib.client.common_lib import time_utils 17from autotest_lib.client.common_lib.cros.graphite import autotest_es 18 19 20def get_summary(start_time, end_time, top=None, report_stat=False): 21 """Get the summary of total size of test results for given period. 22 23 @param start_time: Start time of the test results to search for. 24 @param end_time: End time of the test results to search for. 25 @param top: Number of top hits with the largest size of test results. 26 @param report_stat: True to report the total test results size to statsd. 27 """ 28 fields_returned = ['size_KB', 'time_recorded'] 29 if top > 0: 30 fields_returned.append('result_dir') 31 records = autotest_es.query( 32 fields_returned=fields_returned, 33 equality_constraints=[('_type', 'result_dir_size'),], 34 range_constraints=[('time_recorded', start_time, end_time)], 35 sort_specs=[{'time_recorded': 'asc'}]) 36 37 total_GB = sum([e['size_KB'] for e in records.hits]) / 1000000.0 38 print 'Number of test result entries found: %d' % len(records.hits) 39 print 'Total size of test results uploaded: %.2f GB' % total_GB 40 if top: 41 hits = sorted(records.hits, key=lambda hit:hit['size_KB'], reverse=True) 42 for hit in hits[:top]: 43 print ('%s: \t%.2f MB' % 44 (hit['result_dir'], hit['size_KB']/1000.0)) 45 46 47def main(): 48 """main script. """ 49 t_now = time.time() 50 t_now_minus_one_day = t_now - 3600*24 51 parser = argparse.ArgumentParser() 52 parser.add_argument('-l', type=int, dest='last', 53 help='last days to summary test results across', 54 default=None) 55 parser.add_argument('--start', type=str, dest='start', 56 help=('Enter start time as: yyyy-mm-dd hh:mm:ss,' 57 'defualts to 24h ago.'), 58 default=time_utils.epoch_time_to_date_string( 59 t_now_minus_one_day)) 60 parser.add_argument('--end', type=str, dest='end', 61 help=('Enter end time in as: yyyy-mm-dd hh:mm:ss,' 62 'defualts to current time.'), 63 default=time_utils.epoch_time_to_date_string(t_now)) 64 parser.add_argument('-t', type=int, dest='top', 65 help='Print the top x of large result folders.', 66 default=0) 67 parser.add_argument('-r', action='store_true', dest='report_stat', 68 default=False, 69 help='True to report total size to statsd.') 70 options = parser.parse_args() 71 72 if options.last: 73 start_time = t_now - 3600*24*options.last 74 end_time = t_now 75 else: 76 start_time = time_utils.to_epoch_time(options.start) 77 end_time = time_utils.to_epoch_time(options.end) 78 79 get_summary(start_time=start_time, end_time=end_time, 80 top=options.top, report_stat=options.report_stat) 81 82 83if __name__ == '__main__': 84 main() 85