How to get credit usage by an application using ArcGIS Python API

605
6
10-06-2021 08:24 AM
Labels (1)
AnandAkmanchi
Occasional Contributor

I have an application created for the purposes of doing geocoding operations. The client-id of this registered application is used by an external application which does the geocoding. It is a server-side script and not used for logins, but only for geocoding. 

I am able to see the credit usage in ArcGIS Online for this application. However, I'd like to query and get the same through the ArcGIS API for Python.

I have tried gis.item.usage(date_range='7D'as_df=True), but it returns an empty dataframe, whereas I can see credit usage in ArcGIS Online for the same period. 

Is it supported in ArcGIS API for Python or does it currently support usage reporting only on views and logins?

thanks,

Anand

0 Kudos
6 Replies
jcarlson
MVP Notable Contributor

If you look at your org's credits dashboard, you'll see that credit usage is only broken down by application type and date.

If you use gis.admin.credits.credit_usage(), you can see that same breakdown:

{'intnotebks': 0.0,
 'schdnotebks': 0.67500002,
 'features': 13.303148600000002,
 'notebooks': 0.000133846385,
 'portal': 0.105026248,
 'applogin': 0.0,
 'basemaps': 0.0,
 'landscapeimagery': 0.0,
 'landscapemaps': 0.0,
 'premiummaps': 0.0}

I am able to see the credit usage in ArcGIS Online for this application. 

Is there a certain spot in AGOL where you see app-specific credit usage?

- Josh Carlson
Kendall County GIS
0 Kudos
AnandAkmanchi
Occasional Contributor

Hi Josh, 

 

Yes, in AGOL there is a certain spot where we can see credits consumed by the application. Here is a screenshot of the place where I typically see the credits for each application. But I am unable to locate how to get this using Python API.

 

AnandAkmanchi_1-1633589214864.png

 

0 Kudos
jcarlson
MVP Notable Contributor

That's very interesting! I don't see anything in the API docs for this, but I may be looking in the wrong spot.

Can you open your browser's developer tools (Ctrl + Shift + I on Firefox) and watch the Network tab when you click to request the credits usage for the app? You should be able to see what kind of URL request is being sent, which may help narrow down where to look for this.

- Josh Carlson
Kendall County GIS
0 Kudos
DrAnandAkmanchi
New Contributor

Yes! it is quite a useful feature. I did watch the Network tab and looked at the URL requests being sent, but could not find an equivalent in the API. 

here is the request that is sent when I click Application Usage

https://org.maps.arcgis.com/sharing/rest/portals/DlnsrgerasuvLGwepDczjeSgG/usage?startTime=1633528539315&endTime=1633611339315&period=1h&appId=4Yrginubsswrgfert7F9T3xw&vars=credits,num,bw&groupBy=etype,stype&f=json&token=jYNFS8FofRFkyYcr 

 

thanks

Anand

0 Kudos
jcarlson
MVP Notable Contributor

Oh! I missed this bit on item.usage():

Apps that allow access to subscriber content through the organization
subscription show usage by credits.

I do see that when I try item.usage() on a locator service, the dataframe is empty, regardless of whether or not it's actually been used in the timeframe specified. It feels like you may be in Bug Report territory on that point.

Beyond that, though, I find it curious that the URL request is being made using the appID and not the ItemID from your content. And that the button appears in the Application section of the settings rather than the Usage tab.

Prior to the usage URL request, it looks like the ItemID is used to get the registeredAppInfo...

So, looking at the URL again. The usage request is happening at the portal level, with the appID being given as one of several variables.

Unfortunately, the /portals/[PortalID]/usage item is missing from the documentation, so we can only really guess how it's supposed to work. Nothing similar in the Python API either.

jcarlson_0-1633614433170.png

A Workaround

For now, I think you can still accomplish this by utilizing the requests library.

 

import requests
from datetime import datetime, timedelta

# Define span of report
enddate = datetime.now()
span = timedelta(days=7)
startdate = enddate - span

# There's a programmatic way to get a token, since you won't be doing this via the ArcGIS Python API, but I forgot its specifics
the_token = 'some_token'
app_id = 'some_id'

# Construct URL for request
reportURL = f'https://[your-org].maps.arcgis.com/sharing/rest/portals/[portalID]/usage?startTime={round(enddate.timestamp()*1000)}&endTime={round(enddate.timestamp()*1000)}&period=1h&appId={app_id}&vars=credits,num,bw&groupBy=etype,stype&f=json&token={the_token}'

# Get request
response = requests.get(reportURL)

 

 

The output of response.json() will be something like this:

{'startTime': 1633615200000,
 'endTime': 1633618800000,
 'period': '1h',
 'data': []}

Which you can easily bring into something nicer like pandas.

pd.DataFrame(response.json()['data'])

Give this a try and let me know what it looks like when your app actually has credits to report. I get an empty dataframe, but in my case, that is accurate.

- Josh Carlson
Kendall County GIS
0 Kudos
AnandAkmanchi
Occasional Contributor

Josh, apologies as it took me a while to respond and thank you for your suggestion!

I was hoping that we should be able to do this by the ArcGIS Python API, but sadly it looks like it doesn't support it yet.

So yes, for the time being it looks like we will have go with writing our own custom code, and you example is very helpful. Thank you so much!

regards

Anand

0 Kudos