host_history.py revision 9e0be9d048fecdf06a525bd4c31d272715494653
15952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang#!/usr/bin/env python
25952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang
35952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang# Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
45952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang# Use of this source code is governed by a BSD-style license that can be
55952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang# found in the LICENSE file.
65952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang
75952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang# This file defines script for getting host_history for DUTs in Autotest.
85952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang
95952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang"""Script for checking host history for a selected group of hosts.
105952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang
115952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael LiangCurrently only supports aggregating stats for each host.
125952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang
135952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael LiangExample usage:
145952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang    python host_history.py --index=cautotest -n 10000 \
155952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang                           -l 24 --board=daisy
165952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang
175952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael LiangOutput:
185952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang
195952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang    trying to get all duts...
205952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang    making the query...
215952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang    found all duts. Time to get host_history.
225952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang    usage stats for host: chromeos2-row5-rack1-host6
235952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang      2014-07-24 10:24:07 - 2014-07-25 10:24:07
245952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang        Verifying: 0.00 %
255952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang        Running: 0.00 %
265952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang        Ready: 100.00 %
275952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang        Repairing: 0.00 %
285952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang        Repair Failed: 0.00 %
295952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang        Cleaning: 0.00 %
305952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang        Pending: 0.00 %
315952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang        Resetting: 0.00 %
325952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang        Provisioning: 0.00 %
335952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang        Locked: 0.00 %
345952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang    - -- --- ---- ----- ---- --- -- -
355952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang
365952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael LiangExample usage2: more than one host:
375952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang    python host_history.py --index=cautotest  -n 1000 -l 2 \
385952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang    --hosts chromeos2-row5-rack4-host6 chromeos4-row12-rack11-host2
395952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang
405952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang    ['chromeos2-row5-rack4-host6', 'chromeos4-row12-rack11-host2']
415952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang    found all duts. Time to get host_history.
425952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang    usage stats for host: chromeos2-row5-rack4-host6
435952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang     2014-07-25 13:02:22 - 2014-07-25 15:02:22
445952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang     Num entries found in this interval: 0
455952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang        Verifying:        0.00 %
465952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang        Running:          0.00 %
475952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang        Ready:            100.00 %
485952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang        Repairing:        0.00 %
495952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang        Repair Failed:    0.00 %
505952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang        Cleaning:         0.00 %
515952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang        Pending:          0.00 %
525952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang        Resetting:        0.00 %
535952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang        Provisioning:     0.00 %
545952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang        Locked:           0.00 %
555952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang    - -- --- ---- ----- ---- --- -- -
565952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang
575952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang    usage stats for host: chromeos4-row12-rack11-host2
585952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang     2014-07-25 13:02:22 - 2014-07-25 15:02:22
595952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang     Num entries found in this interval: 138
605952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang        Verifying:        0.00 %
615952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang        Running:          70.45 %
625952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang        Ready:            17.79 %
635952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang        Repairing:        0.00 %
645952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang        Repair Failed:    0.00 %
655952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang        Cleaning:         0.00 %
665952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang        Pending:          1.24 %
675952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang        Resetting:        10.78 %
685952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang        Provisioning:     0.00 %
695952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang        Locked:           0.00 %
705952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang    - -- --- ---- ----- ---- --- -- -
715952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang"""
725952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang
7393b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liangimport argparse
7493b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liangimport datetime
755952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liangimport multiprocessing
765952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liangimport multiprocessing.pool
775952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liangimport time
7893b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liangimport traceback
795952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang
805952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liangimport common
8193b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liangfrom autotest_lib.client.common_lib.cros.graphite import es_utils
825952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liangfrom autotest_lib.server import frontend
839e0be9d048fecdf06a525bd4c31d272715494653Michael Liangfrom autotest_lib.site_utils import host_history_utils
845952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang
855952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang
865952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liangdef should_care(board, pool, dut):
875952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang    """Whether we should care to print stats for this dut out
885952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang
895952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang    @param board: board we want, i.e. 'daisy'
905952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang    @param pool: pool we want, i.e. 'bvt'
915952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang    @param dut: Host object representing DUT.
925952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang    @returns: True if the dut's stats should be counted.
935952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang    """
945952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang    if not board and not pool:
955952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang        return True
965952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang    found_board = False if board else True
975952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang    found_pool = False if pool else True
985952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang    for label in dut.labels:
995952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang        if label.startswith('pool:%s' % (pool)):
1005952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang            found_pool = True
1015952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang        if label.startswith('board:%s' % (board)):
1025952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang            found_board = True
1035952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang    return found_board and found_pool
1045952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang
1055952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang
10693b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liangdef print_all_stats(results, labels, t_start, t_end):
10793b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang    """Prints overall stats followed by stats for each host.
10893b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang
10993b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang    @param results: A list of tuples of two elements.
11093b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang        1st element: String representing report for individual host.
11193b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang        2nd element: An ordered dictionary with
11293b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang            key being (ti, tf) and value being (status, dbg_str)
11393b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang            status = status of the host. e.g. 'Repair Failed'
11493b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang            ti is the beginning of the interval where the DUT's has that status
11593b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang            tf is the end of the interval where the DUT has that status
11693b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang            dbg_str is the self.dbg_str from the host. An example would be:
11793b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang                'Special Task 18858263 (host 172.22.169.106,
11893b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang                                        task Repair,
11993b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang                                        time 2014-07-27 20:01:15)'
12093b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang    @param labels: A list of labels useful for describing the group
12193b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang                   of hosts these overall stats represent.
12293b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang    @param t_start: beginning of time period we are interested in.
12393b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang    @param t_end: end of time period we are interested in.
12493b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang    """
12593b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang    result_strs, stat_intervals_lst = zip(*results)
12693b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang    overall_report_str = host_history_utils.get_overall_report(
12793b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang            labels, t_start, t_end, stat_intervals_lst)
12893b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang    # Print the overall stats
12993b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang    print overall_report_str
13093b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang    # Print the stats for each individual host.
13193b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang    for result_str in result_strs:
13293b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang        print result_str
13393b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang
13493b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang
13593b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liangdef get_host_history(input):
13693b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang    """Gets the host history.
13793b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang
13893b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang    @param input: A dictionary of input arguments to
13993b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang                  host_history_utils.host_history_stats.
14093b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang                  Must contain these keys:
14193b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang                    't_start',
14293b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang                    't_end',
14393b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang                    'hostname',
14493b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang                    'size,'
14593b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang                    'print_each_interval'
14693b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang    @returns:
14793b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang        result_str: String reporting history for specific host.
14893b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang        stat_intervals: A ordered dictionary with
14993b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang            key being (ti, tf) and value being (status, dbg_str)
15093b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang            status = status of the host. e.g. 'Repair Failed'
15193b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang            ti is the beginning of the interval where the DUT's has that status
15293b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang            tf is the end of the interval where the DUT has that status
15393b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang            dbg_str is the self.dbg_str from the host. An example would be:
15493b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang                'Special Task 18858263 (host 172.22.169.106,
15593b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang                                        task Repair,
15693b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang                                        time 2014-07-27 20:01:15)'
15793b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang    """
15893b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang    try:
15993b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang        result_str, stat_intervals = host_history_utils.get_report_for_host(
16093b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang                t_start=input['t_start'],
16193b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang                t_end=input['t_end'],
16293b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang                hostname=input['hostname'],
16393b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang                size=input['size'],
1649e0be9d048fecdf06a525bd4c31d272715494653Michael Liang                print_each_interval=input['print_each_interval'],
1659e0be9d048fecdf06a525bd4c31d272715494653Michael Liang                index=input['index'])
16693b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang        return result_str, stat_intervals
16793b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang    except Exception as e:
16893b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang        # Incase any process throws an Exception, we want to see it.
16993b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang        print traceback.print_exc()
17093b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang        return None, None
17193b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang
17293b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang
1735952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liangdef main():
1745952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang    """main script. """
17593b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang    t_now = time.time()
17693b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang    t_now_minus_one_day = t_now - 3600 * 24
1775952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang    parser = argparse.ArgumentParser()
1789e0be9d048fecdf06a525bd4c31d272715494653Michael Liang    parser.add_argument('--index', type=str, dest='index',
1799e0be9d048fecdf06a525bd4c31d272715494653Michael Liang                        help='Enter ES index name, such as "cautotest"')
1805952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang    parser.add_argument('-v', action='store_true', dest='verbose',
1815952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang                        default=False,
1829e0be9d048fecdf06a525bd4c31d272715494653Michael Liang                        help='-v to print out ALL entries.')
1835952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang    parser.add_argument('-n', type=int, dest='size',
1845952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang                        help='Maximum number of entries to return.',
1855952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang                        default=10000)
1865952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang    parser.add_argument('-l', type=float, dest='last',
1875952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang                        help='last hours to search results across',
18893b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang                        default=None)
1895952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang    parser.add_argument('--board', type=str, dest='board',
1905952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang                        help='restrict query by board, not implemented yet',
1915952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang                        default=None)
1925952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang    parser.add_argument('--pool', type=str, dest='pool',
1935952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang                        help='restrict query by pool, not implemented yet',
1945952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang                        default=None)
1955952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang    parser.add_argument('--hosts', nargs='+', dest='hosts',
1965952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang                        help='Enter space deliminated hostnames',
1975952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang                        default=[])
19893b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang    parser.add_argument('--start', type=str, dest='start',
19993b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang                        help=('Enter start time as: yyyy-mm-dd hh-mm-ss,'
20093b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang                              'defualts to 24h ago.'),
201329153d2117beabee16a29f9f02c2f374fc288aeMichael Liang                        default=host_history_utils.unix_time_to_readable_date(
202329153d2117beabee16a29f9f02c2f374fc288aeMichael Liang                                t_now_minus_one_day))
20393b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang    parser.add_argument('--end', type=str, dest='end',
20493b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang                        help=('Enter end time in as: yyyy-mm-dd hh-mm-ss,'
20593b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang                              'defualts to current time.'),
206329153d2117beabee16a29f9f02c2f374fc288aeMichael Liang                        default=host_history_utils.unix_time_to_readable_date(
207329153d2117beabee16a29f9f02c2f374fc288aeMichael Liang                                t_now))
2085952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang    options = parser.parse_args()
2095952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang
21093b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang    if options.last:
211329153d2117beabee16a29f9f02c2f374fc288aeMichael Liang        t_start = t_now - 3600 * options.last
212329153d2117beabee16a29f9f02c2f374fc288aeMichael Liang        t_end = t_now
21393b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang    else:
21493b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang        t_start = es_utils._to_epoch_time(datetime.datetime.strptime(
21593b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang                options.start, '%Y-%m-%d %H:%M:%S'))
21693b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang        t_end = es_utils._to_epoch_time(datetime.datetime.strptime(
21793b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang                options.end, '%Y-%m-%d %H:%M:%S'))
2185952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang
2195952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang    if options.hosts:
2205952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang        hosts = options.hosts
2215952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang    else:
2225952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang        hosts = []
2235952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang        print 'trying to get all duts...'
2245952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang        afe = frontend.AFE()
2255952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang        print 'making the query...'
2265952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang        duts = afe.get_hosts()
2275952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang        for dut in duts:
2285952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang            if should_care(options.board, options.pool, dut):
2295952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang                hosts.append(dut.hostname)
2305952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang    print 'found all duts. Time to get host_history.'
2315952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang
2325952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang    args = []
2335952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang    for hostname in hosts:
23493b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang        args.append({'t_start': t_start,
23593b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang                     't_end': t_end,
23693b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang                     'hostname': hostname,
23793b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang                     'size': options.size,
2389e0be9d048fecdf06a525bd4c31d272715494653Michael Liang                     'print_each_interval': options.verbose,
2399e0be9d048fecdf06a525bd4c31d272715494653Michael Liang                     'index': options.index})
2405952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang
2415952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang    # Parallizing this process.
2425952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang    pool = multiprocessing.pool.ThreadPool()
2435952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang    results = pool.imap_unordered(get_host_history, args)
24493b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang    time.sleep(3)
24593b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang    labels = []
24693b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang    if options.board:
24793b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang        labels.append('board:%s' % (options.board))
24893b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang    if options.pool:
24993b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang        labels.append('pool:%s' % (options.pool))
25093b4ba492c4af35fa025e3f995904fdeb8dbf7c4Michael Liang    print_all_stats(results, labels, t_start, t_end)
2515952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang
2525952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liang
2535952fbe77ee7f33a642d19aa1d13ffeae0b92117Michael Liangif __name__ == '__main__':
2549e0be9d048fecdf06a525bd4c31d272715494653Michael Liang    main()