Skip to content

Instantly share code, notes, and snippets.

@gourneau
Created February 1, 2013 21:55
Show Gist options
  • Select an option

  • Save gourneau/4694424 to your computer and use it in GitHub Desktop.

Select an option

Save gourneau/4694424 to your computer and use it in GitHub Desktop.
from __future__ import division
import requests
import math
import sys
def human_size(size_bytes):
"""
format a size in bytes into a 'human' file size, e.g. bytes, KB, MB, GB, TB, PB
Note that bytes/KB will be reported in whole numbers but MB and above will have greater precision
e.g. 1 byte, 43 bytes, 443 KB, 4.3 MB, 4.43 GB, etc
"""
if size_bytes == 1:
# because I really hate unnecessary plurals
return "1 byte"
suffixes_table = [('bytes',0),('KB',0),('MB',1),('GB',2),('TB',2), ('PB',2)]
num = float(size_bytes)
for suffix, precision in suffixes_table:
if num < 1024.0:
break
num /= 1024.0
if precision == 0:
formatted_size = "%d" % num
else:
formatted_size = str(round(num, ndigits=precision))
return "%s %s" % (formatted_size, suffix)
date = "2013-01-30"
limit = 10
server = "http://ionwest.itw"
plugin = sys.argv[1]
url = "{server}/rundb/api/v1/pluginresult/?format=json&endtime__gte={date}&limit={limit}&plugin__name={plugin}".format(
date=date,
server=server,
limit=limit,
plugin=plugin)
r = requests.get(url)
ALLOWED_STATES = (
('Completed', 'Completed'),
('Error', 'Error'),
('Started', 'Started'),
('Declined', 'Declined'),
('Unknown', 'Unknown'),
('Queued', 'Queued'), # In SGE queue
('Pending', 'Pending'), # Prior to submitting to SGE
('Resource', 'Exceeded Resource Limits'), ## SGE Errors
)
data = {}
count = {}
paths = []
#just look at the plugins that have are not queued or pending
plugins = [o for o in r.json()["objects"] if o["state"] != "Queued" or o["state"] != "Pending"]
success = 0
error = 0
for p in plugins:
data[p["plugin"]["name"]] = data.get(p["plugin"]["name"],0) + int(p["size"])
count[p["plugin"]["name"]] = count.get(p["plugin"]["name"],0) + 1
if p["state"] == "Error":
error += 1
elif p["state"] == "Completed":
success += 1
result = server + p["result"] + "?format=json&noplugin=True"
results = requests.get(result)
print results.json()["reportLink"] , human_size(int(p["size"]))
for key, value in sorted(data.iteritems(), key=lambda (k,v): (v,k), reverse=True):
print "{} ({} occurances) : {} ".format(key, count[key], human_size(value))
"""
print "Plugins that ran " , len(plugins)
print "Plugins that failed ", error
print math.ceil((error/len(plugins)) * 100), "% failure rate"
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment