#!/usr/bin/python2 # Copyright (c) 2011 The Chromium OS Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. """ Tool to check DUT usage by querying the Autotest DB. Sample usage: utils/site_check_dut_usage.py 11/1/2011 11/5/2011 netbook_LABEL """ import datetime import optparse import sys import common from autotest_lib.database import database_connection _DATE_FORMAT = '%m/%d/%Y' class CheckDutUsageRunner(object): """Checks DUT usage for given label or hostname during a time period.""" def __init__(self, start_time, end_time, label, hostname, list_hostnames): """ Instantiates a CheckDUTUsageRunner. @start_time: start date of time period we are interested in. @end_time: end date of time period we are interested in. Note the time period is (start_date, end_date]. @label: If not None, the platform label of the hostnames we are interested in. @hostname: If not None, the hostname we are intersted in. @list_hostnames: If set, print out the list of hostnames found that ran jobs during the given time period. """ self._start_time = start_time self._end_time = end_time self._list_hostnames = list_hostnames self._label = label self._hostname = hostname self._database_connection = None def find_all_durations(self): """ Returns all list of tuples containing durations. A duration is a 4-tuple containing |queued_time|, |started_time|, |finished_time|, |hostname|. """ query = ('select queued_time, started_time, finished_time, ' ' hostname ' 'from tko_jobs left join tko_machines on ' ' tko_jobs.machine_idx=tko_machines.machine_idx ' 'where tko_jobs.started_time>=DATE(%s) and ' ' tko_jobs.finished_time