Skip to content

Commit

Permalink
Improve gam report
Browse files Browse the repository at this point in the history
  • Loading branch information
taers232c committed Mar 22, 2021
1 parent d77dff4 commit 859a094
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 26 deletions.
7 changes: 7 additions & 0 deletions src/GamUpdate.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ You'll have to do `gam update project` and `gam oauth create` to enable these co

As with any initial release, proceed with care and report any problems.

Updated `gam report customer|user` to continue processing subsequent dates when Google indicates that no data is available
for a particular date.

Improved `Got` messages so it is easier to monitor progress.

Removed extraneous `email` header in `gam report user ... aggregatebydate`.

5.35.08

Fixed bug in `gam calendar <CalendarEntity> create|import|update event` where `noreminders` was not disabling reminders.
Expand Down
75 changes: 49 additions & 26 deletions src/gam/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2203,10 +2203,12 @@ def printGettingEntityItemForWhom(entityItem, forWhom, i=0, count=0):
LAST_ITEM_MARKER = '%%last_item%%'
TOTAL_ITEMS_MARKER = '%%total_items%%'

def getPageMessage(showFirstLastItems=False):
def getPageMessage(showFirstLastItems=False, showDate=None):
if not GC.Values[GC.SHOW_GETTINGS]:
return None
pageMessage = f'{Msg.GOT} {TOTAL_ITEMS_MARKER} {{0}}'
if showDate:
pageMessage += f' on {showDate}'
if showFirstLastItems:
pageMessage += f': {FIRST_ITEM_MARKER} - {LAST_ITEM_MARKER}'
else:
Expand All @@ -2217,12 +2219,14 @@ def getPageMessage(showFirstLastItems=False):
GM.Globals[GM.LAST_GOT_MSG_LEN] = 0
return pageMessage

def getPageMessageForWhom(forWhom=None, showFirstLastItems=False):
def getPageMessageForWhom(forWhom=None, showFirstLastItems=False, showDate=None):
if not GC.Values[GC.SHOW_GETTINGS]:
return None
if forWhom:
Ent.SetGettingForWhom(forWhom)
pageMessage = f'{Msg.GOT} {TOTAL_ITEMS_MARKER} {{0}}{Ent.GettingPostQualifier()} {Msg.FOR} {Ent.GettingForWhom()}'
if showDate:
pageMessage += f' on {showDate}'
if showFirstLastItems:
pageMessage += f': {FIRST_ITEM_MARKER} - {LAST_ITEM_MARKER}'
else:
Expand Down Expand Up @@ -4370,7 +4374,7 @@ def _processGAPIpagesResult(results, items, allResults, totalItems, pageMessage,
lastItem = str(lastItem)
showMessage = showMessage.replace(FIRST_ITEM_MARKER, firstItem)
showMessage = showMessage.replace(LAST_ITEM_MARKER, lastItem)
writeGotMessage(showMessage.replace('{0}', str(Ent.Choose(entityType, totalItems))))
writeGotMessage(showMessage.replace('{0}', str(Ent.Choose(entityType, totalItems))))
return (pageToken, totalItems)

def _finalizeGAPIpagesResult(pageMessage):
Expand Down Expand Up @@ -10095,25 +10099,24 @@ def validateYYYYMMDD(argstr):
endDateTime = startEndTime.endDateTime
endDate = endDateTime.strftime(YYYYMMDD_FORMAT)
startUseDate = endUseDate = None
showGetting = False
if not orgUnitId:
showOrgUnit = False
if userReports:
if select:
showGetting = True
Ent.SetGetting(Ent.REPORT)
kwargs = [{'userKey': normalizeEmailAddressOrUID(user)} for user in users]
elif userKey == 'all':
if orgUnitId:
kwargs[0]['orgUnitID'] = orgUnitId
userOrgUnits = getUserOrgUnits(cd, orgUnit, orgUnitId)
printGettingEntityItemForWhom(Ent.REPORT, f'users in orgUnit {orgUnit}')
forWhom = f'users in orgUnit {orgUnit}'
else:
printGettingEntityItemForWhom(Ent.REPORT, 'all users')
pageMessage = getPageMessage()
forWhom = 'all users'
printGettingEntityItemForWhom(Ent.REPORT, forWhom)
else:
Ent.SetGetting(Ent.REPORT)
kwargs = [{'userKey': normalizeEmailAddressOrUID(userKey)}]
printGettingEntityItemForWhom(Ent.REPORT, kwargs[0]['userKey'])
pageMessage = getPageMessage()
if showOrgUnit:
titles.append('orgUnitPath')
else:
Expand All @@ -10129,9 +10132,10 @@ def validateYYYYMMDD(argstr):
startDateTime += oneDay
try:
for kwarg in kwargs:
if showGetting:
printGettingEntityItemForWhom(Ent.REPORT, kwarg['userKey'])
pageMessage = getPageMessage()
if not select and userKey == 'all':
pageMessage = getPageMessageForWhom(forWhom, showDate=useDate)
else:
pageMessage = getPageMessageForWhom(kwarg['userKey'], showDate=useDate)
try:
usage = callGAPIpages(service, 'get', 'usageReports',
pageMessage=pageMessage,
Expand Down Expand Up @@ -10274,8 +10278,12 @@ def validateYYYYMMDD(argstr):
# [(fields|parameters <String>)|(services <CustomerServiceNameList>)] [noauthorizedapps]
def doReport():
def processUserUsage(usage, lastDate):
if not usage or lastDate == usage[0]['date']:
return (lastDate is None, lastDate)
# if not usage or lastDate == usage[0]['date']:
# return (lastDate is None, lastDate)
if not usage:
return (True, lastDate)
if lastDate == usage[0]['date']:
return (False, lastDate)
lastDate = usage[0]['date']
for user_report in usage:
if 'entity' not in user_report:
Expand Down Expand Up @@ -10313,8 +10321,12 @@ def processUserUsage(usage, lastDate):
return (True, lastDate)

def processAggregateUserUsage(usage, lastDate):
if not usage or lastDate == usage[0]['date']:
return (lastDate is None, lastDate)
# if not usage or lastDate == usage[0]['date']:
# return (lastDate is None, lastDate)
if not usage:
return (True, lastDate)
if lastDate == usage[0]['date']:
return (False, lastDate)
lastDate = usage[0]['date']
for user_report in usage:
if 'entity' not in user_report:
Expand All @@ -10334,8 +10346,12 @@ def processAggregateUserUsage(usage, lastDate):
return (True, lastDate)

def processCustomerUsageOneRow(usage, lastDate):
if not usage or lastDate == usage[0]['date']:
return (lastDate is None, lastDate)
# if not usage or lastDate == usage[0]['date']:
# return (lastDate is None, lastDate)
if not usage:
return (True, lastDate)
if lastDate == usage[0]['date']:
return (False, lastDate)
lastDate = usage[0]['date']
row = {'date': lastDate}
for item in usage[0].get('parameters', []):
Expand Down Expand Up @@ -10396,8 +10412,12 @@ def processCustomerUsageOneRow(usage, lastDate):
return (True, lastDate)

def processCustomerUsage(usage, lastDate):
if not usage or lastDate == usage[0]['date']:
return (lastDate is None, lastDate)
# if not usage or lastDate == usage[0]['date']:
# return (lastDate is None, lastDate)
if not usage:
return (True, lastDate)
if lastDate == usage[0]['date']:
return (False, lastDate)
lastDate = usage[0]['date']
for item in usage[0].get('parameters', []):
if 'name' not in item:
Expand Down Expand Up @@ -10577,25 +10597,24 @@ def processCustomerUsage(usage, lastDate):
if startEndTime.startDateTime is None:
startEndTime.startDateTime = startEndTime.endDateTime = todaysDate()
if select:
pageMessage = None
normalizeUsers = True
orgUnitId = None
Ent.SetGetting(Ent.REPORT)
elif userKey == 'all':
if orgUnitId:
userOrgUnits = getUserOrgUnits(cd, orgUnit, orgUnitId)
printGettingEntityItemForWhom(Ent.REPORT, f'users in orgUnit {orgUnit}')
forWhom = f'users in orgUnit {orgUnit}'
else:
printGettingEntityItemForWhom(Ent.REPORT, 'all users')
pageMessage = getPageMessage()
forWhom = 'all users'
printGettingEntityItemForWhom(Ent.REPORT, forWhom)
users = ['all']
else:
Ent.SetGetting(Ent.REPORT)
pageMessage = getPageMessage()
users = [normalizeEmailAddressOrUID(userKey)]
orgUnitId = None
if aggregateUserUsage:
titles = ['date']
if not showOrgUnit:
elif not showOrgUnit:
titles = ['email', 'date']
else:
titles = ['email', 'orgUnitPath', 'date']
Expand Down Expand Up @@ -10631,6 +10650,10 @@ def processCustomerUsage(usage, lastDate):
break
startDateTime = endDateTime = datetime.datetime.strptime(tryDate, YYYYMMDD_FORMAT)
continue
if not select and userKey == 'all':
pageMessage = getPageMessageForWhom(forWhom, showDate=tryDate)
else:
pageMessage = getPageMessageForWhom(user, showDate=tryDate)
usage = callGAPIpages(rep.userUsageReport(), 'get', 'usageReports',
pageMessage=pageMessage,
throwReasons=[GAPI.INVALID, GAPI.BAD_REQUEST, GAPI.FORBIDDEN],
Expand Down

0 comments on commit 859a094

Please sign in to comment.