Important note

The EdgeRules feature is available only on Enterprise plans.

This example shows the quick way of sorting the most requested files for your account, grouped into daily statistics from an API response.

In order to sort your top 50 files by bandwidth or cache hit (that are not able to be sorted by different keys during the request), you will have to get information in JSON format first. Then you will have to sort data based on your business needs.

First, define your API key/secret parameters, including the MaxCDN library:

from maxcdn import MaxCDN
api = MaxCDN("alias", "key", "secret")

Then you can choose to get data for a single zone or for all zones, a particular period of time (start and end date), and sorting method:

zoneid = raw_input("\n Zone Id (Hit [enter] for all zones): ")
ztype = raw_input(" Zone type (Hit [enter] for all types): ")
if zoneid != "":
	zoneid = "/" + zoneid
if ztype != "":
	ztype = "/" + ztype
datefrom = raw_input("\n Date from (YYYY-MM-DD): ")
dateto = raw_input(" Date to (YYYY-MM-DD): ")
sort = raw_input("\n Sort by (hit/bandwidth): ")
params = {"date_from":datefrom,"date_to":dateto}

To get your top 50 files in JSON format, use the GET method with proper parameters on /popularfiles.json:

data = api.get('/reports'+ ztype + zoneid +'/popularfiles.json', params)

Finally, sort the JSON response by bandwidth or hit, preview the results, and save the output in the appropriate file:

data = data['data']['popularfiles']
if sort == "bandwidth":
	sorted = sorted(data, key=lambda k: int(k['size']), reverse = True)
	print "\n----- Top 50 files sorted by Bandwidth -----"
	for i in range(0, len(data)):
		print "\n ", i+1
		print " Bandwidth: ",(sorted[i]['size']), "bytes"
		print " URI: ",(sorted[i]['uri'])
		print " Time: ",(sorted[i]['timestamp'])
		print " Hit: ", (sorted[i]['hit'])
	text_file = open("/reports/top50_by_bandwidth.json", "w")
	text_file.write(str(sorted))
	text_file.close()
	print "\n Report saved in /reports/top50_by_bandwidth.json\n"

elif sort == "hit":
	sorted = sorted(data, key=lambda k: int(k['hit']), reverse = True)
	print "\n----- Top 50 files sorted by Hit -----"
	for i in range(0, len(data)):
		print "\n ", i+1
		print " Hit: ", (sorted[i]['hit'])
		print " URI: ",(sorted[i]['uri'])
		print " Time: ",(sorted[i]['timestamp'])
		print " Bandwidth: ",(sorted[i]['size']), "bytes"
	text_file = open("/reports/top50_by_hit.json", "w")
	text_file.write(str(sorted))
	text_file.close()
	print "\n Report saved in /reports/top50_by_hit.json\n"