def GetUnitDetails(self, start_date='%Y%m01', end_date='%Y%m%d'): """Retrieve details of the API units usage for given date range. Defaults to current month. Args: [optional] start_date: str beginning of the date range, inclusive. end_date: str end of the date range, inclusive. Returns: tuple detailed API usage. """ glob_sanity_check.ValidateTypes( ((start_date, (str, unicode)), (end_date, (str, unicode)))) date = start_date start_date = datetime.datetime.now().strftime(start_date) end_date = datetime.datetime.now().strftime(end_date) ops_rates = Utils.GetOpsRates() usage = [] for parts in ops_rates: (method, operator) = (parts[2], '') if len(parts[2].split('.')) > 1: (method, operator) = parts[2].split('.') selector = { 'serviceName': parts[1], 'methodName': method, 'dateRange': { 'min': start_date, 'max': end_date }, 'apiUsageType': 'UNIT_COUNT' } if operator: selector['operator'] = operator unit_count = self.Get(selector)[0]['cost'] if long(unit_count) > 0: usage.append({ 'date': date, 'service': parts[1], 'method': method, 'operator': operator, 'units': unit_count }) return (usage, )
def testDataFileOpsRates(self): """Test whether csv data file with ops rates is valid.""" cols = 5 for item in Utils.GetOpsRates(): self.assertEqual(len(item), cols)