Fix customer/user report

This commit is contained in:
Jay Lee
2020-05-12 11:55:18 -04:00
parent 6a421d3b78
commit 8eb72ae6e7
2 changed files with 48 additions and 40 deletions

View File

@@ -5,7 +5,7 @@ default_language_version:
repos: repos:
- repo: https://github.com/pre-commit/pre-commit-hooks - repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.4.0 rev: v2.5.0
hooks: hooks:
- id: trailing-whitespace - id: trailing-whitespace
- id: end-of-file-fixer - id: end-of-file-fixer
@@ -17,13 +17,13 @@ repos:
- id: check-merge-conflict - id: check-merge-conflict
- repo: https://github.com/pre-commit/mirrors-yapf - repo: https://github.com/pre-commit/mirrors-yapf
rev: v0.29.0 rev: v0.30.0
hooks: hooks:
- id: yapf - id: yapf
args: [--style=google, --in-place] args: [--style=google, --in-place]
- repo: https://github.com/PyCQA/pylint - repo: https://github.com/PyCQA/pylint
rev: pylint-2.4.4 rev: pylint-2.5.0
hooks: hooks:
- id: pylint - id: pylint
args: [--output-format=colorized] args: [--output-format=colorized]

View File

@@ -306,7 +306,9 @@ def showReport():
elif myarg == 'fulldatarequired': elif myarg == 'fulldatarequired':
fullDataRequired = [] fullDataRequired = []
fdr = sys.argv[i + 1].lower() fdr = sys.argv[i + 1].lower()
if fdr and fdr != 'all': if fdr and fdr == 'all':
fullDataRequired = 'all'
else:
fullDataRequired = fdr.replace(',', ' ').split() fullDataRequired = fdr.replace(',', ' ').split()
i += 2 i += 2
elif myarg == 'start': elif myarg == 'start':
@@ -338,18 +340,19 @@ def showReport():
if report == 'user': if report == 'user':
while True: while True:
try: try:
if fullDataRequired is not None: one_page = gapi.call(rep.userUsageReport(),
warnings = gapi.get_items(rep.userUsageReport(),
'get', 'get',
'warnings',
throw_reasons=throw_reasons, throw_reasons=throw_reasons,
date=tryDate, date=tryDate,
userKey=userKey, userKey=userKey,
customerId=customerId, customerId=customerId,
orgUnitID=orgUnitId, orgUnitID=orgUnitId,
fields='warnings') fields='warnings,usageReports',
maxResults=1)
warnings = one_page.get('warnings', [])
has_reports = bool(one_page.get('usageReports'))
fullData, tryDate = _check_full_data_available( fullData, tryDate = _check_full_data_available(
warnings, tryDate, fullDataRequired) warnings, tryDate, fullDataRequired, has_reports)
if fullData < 0: if fullData < 0:
print('No user report available.') print('No user report available.')
sys.exit(1) sys.exit(1)
@@ -397,16 +400,16 @@ def showReport():
elif report == 'customer': elif report == 'customer':
while True: while True:
try: try:
if fullDataRequired is not None: first_page = gapi.call(rep.customerUsageReports(),
warnings = gapi.get_items(rep.customerUsageReports(),
'get', 'get',
'warnings',
throw_reasons=throw_reasons, throw_reasons=throw_reasons,
customerId=customerId, customerId=customerId,
date=tryDate, date=tryDate,
fields='warnings') fields='warnings,usageReports')
warnings = first_page.get('warnings', [])
has_reports = bool(first_page.get('usageReports'))
fullData, tryDate = _check_full_data_available( fullData, tryDate = _check_full_data_available(
warnings, tryDate, fullDataRequired) warnings, tryDate, fullDataRequired, has_reports)
if fullData < 0: if fullData < 0:
print('No customer report available.') print('No customer report available.')
sys.exit(1) sys.exit(1)
@@ -558,16 +561,21 @@ def _adjust_date(errMsg):
return str(match_date.group(1)) return str(match_date.group(1))
def _check_full_data_available(warnings, tryDate, fullDataRequired): def _check_full_data_available(warnings, tryDate, fullDataRequired,
has_reports):
one_day = datetime.timedelta(days=1) one_day = datetime.timedelta(days=1)
tryDateTime = datetime.datetime.strptime(tryDate, YYYYMMDD_FORMAT)
# move to day before if we don't have at least one usageReport
if not has_reports:
tryDateTime -= one_day
return (0, tryDateTime.strftime(YYYYMMDD_FORMAT))
for warning in warnings: for warning in warnings:
if warning['code'] == 'PARTIAL_DATA_AVAILABLE': if warning['code'] == 'PARTIAL_DATA_AVAILABLE':
for app in warning['data']: for app in warning['data']:
if app['key'] == 'application' and \ if app['key'] == 'application' and \
app['value'] != 'docs' and \ app['value'] != 'docs' and \
(not fullDataRequired or app['value'] in fullDataRequired): fullDataRequired is not None and \
tryDateTime = datetime.datetime.strptime( (fullDataRequired == 'all' or app['value'] in fullDataRequired):
tryDate, YYYYMMDD_FORMAT)
tryDateTime -= one_day tryDateTime -= one_day
return (0, tryDateTime.strftime(YYYYMMDD_FORMAT)) return (0, tryDateTime.strftime(YYYYMMDD_FORMAT))
elif warning['code'] == 'DATA_NOT_AVAILABLE': elif warning['code'] == 'DATA_NOT_AVAILABLE':