rsp() changes and PEP8 formatting
This commit is contained in:
parent
bc4a3f6942
commit
417df627ea
@ -4,8 +4,6 @@ import sys
|
||||
import time
|
||||
import os
|
||||
|
||||
import prettytable
|
||||
|
||||
sys.path.append(os.environ.get('STACKTACH_INSTALL_DIR', '/stacktach'))
|
||||
from stacktach import datetime_to_decimal as dt
|
||||
from stacktach import image_type
|
||||
@ -28,12 +26,13 @@ if len(sys.argv) == 2:
|
||||
hours = 0
|
||||
length = 24
|
||||
|
||||
start = datetime.datetime(year=yesterday.year, month=yesterday.month, day=yesterday.day)
|
||||
start = datetime.datetime(year=yesterday.year, month=yesterday.month,
|
||||
day=yesterday.day)
|
||||
end = start + datetime.timedelta(hours=length-1, minutes=59, seconds=59)
|
||||
|
||||
instance_map = {} # { uuid : [request_id, request_id, ...] }
|
||||
metadata = {'format': 'json', 'instances': instance_map}
|
||||
report = [metadata]
|
||||
metadata = {'report_format': 'json', 'instances': instance_map}
|
||||
report = [metadata] # Tell Stacky to format as JSON
|
||||
|
||||
dstart = dt.dt_to_decimal(start)
|
||||
dend = dt.dt_to_decimal(end)
|
||||
@ -64,7 +63,7 @@ for uuid_dict in updates:
|
||||
|
||||
# All the unique Request ID's for this instance during that timespan.
|
||||
reqs = models.RawData.objects.filter(instance=uuid,
|
||||
when__gt=dstart, when__lte=dend) \
|
||||
when__gt=dstart, when__lte=dend)\
|
||||
.values('request_id').distinct()
|
||||
|
||||
req_list = []
|
||||
@ -141,7 +140,7 @@ for uuid_dict in updates:
|
||||
_end = _when
|
||||
diff = _end - _start
|
||||
|
||||
if diff > 3600 and failure_type == None:
|
||||
if diff > 3600 and failure_type is None:
|
||||
failure_type = ">60"
|
||||
|
||||
key = (operation, image_type_num, cell)
|
||||
@ -189,7 +188,8 @@ for uuid_dict in updates:
|
||||
failed_request['exception'] = exc
|
||||
|
||||
exc_str = str(exc)
|
||||
error_messages[exc_str] = error_messages.get(exc_str, 0) + 1
|
||||
error_messages[exc_str] = \
|
||||
error_messages.get(exc_str, 0) + 1
|
||||
|
||||
# extract the code, if any ...
|
||||
code = exc.get('kwargs', {}).get('code')
|
||||
|
@ -29,7 +29,7 @@ def get_host_names():
|
||||
def routing_key_type(key):
|
||||
if key.endswith('error'):
|
||||
return 'E'
|
||||
return ' '
|
||||
return ' '
|
||||
|
||||
|
||||
def get_deployments():
|
||||
@ -71,15 +71,13 @@ def sec_to_time(diff):
|
||||
return "%dd %02d:%02d:%02d%s" % (days, hours, minutes, seconds, usec)
|
||||
|
||||
|
||||
def rsp(data, status=200):
|
||||
if isinstance(data, list) or isinstance(data, dict):
|
||||
return HttpResponse(json.dumps(data), content_type="application/json", status=status)
|
||||
return HttpResponse(data, content_type="application/json", status=status)
|
||||
def rsp(data, content_type="application/json", status=200):
|
||||
return HttpResponse(data, content_type=content_type, status=status)
|
||||
|
||||
|
||||
def error_response(status, type, message):
|
||||
results = [["Error", "Message"], [type, message]]
|
||||
return rsp(results, status)
|
||||
return rsp(json.dumps(results), status)
|
||||
|
||||
|
||||
def do_deployments(request):
|
||||
@ -87,7 +85,7 @@ def do_deployments(request):
|
||||
results = [["#", "Name"]]
|
||||
for deployment in deployments:
|
||||
results.append([deployment.id, deployment.name])
|
||||
return rsp(results)
|
||||
return rsp(json.dumps(results))
|
||||
|
||||
|
||||
def do_events(request):
|
||||
@ -95,7 +93,7 @@ def do_events(request):
|
||||
results = [["Event Name"]]
|
||||
for event in events:
|
||||
results.append([event['event']])
|
||||
return rsp(results)
|
||||
return rsp(json.dumps(results))
|
||||
|
||||
|
||||
def do_hosts(request):
|
||||
@ -103,7 +101,7 @@ def do_hosts(request):
|
||||
results = [["Host Name"]]
|
||||
for host in hosts:
|
||||
results.append([host['host']])
|
||||
return rsp(results)
|
||||
return rsp(json.dumps(results))
|
||||
|
||||
|
||||
def do_uuid(request):
|
||||
@ -121,7 +119,7 @@ def do_uuid(request):
|
||||
results.append([e.id, routing_key_type(e.routing_key), str(when),
|
||||
e.deployment.name, e.event, e.host, e.state,
|
||||
e.old_state, e.old_task])
|
||||
return rsp(results)
|
||||
return rsp(json.dumps(results))
|
||||
|
||||
|
||||
def do_timings_uuid(request):
|
||||
@ -129,8 +127,8 @@ def do_timings_uuid(request):
|
||||
if not utils.is_uuid_like(uuid):
|
||||
msg = "%s is not uuid-like" % uuid
|
||||
return error_response(400, 'Bad Request', msg)
|
||||
|
||||
return rsp(get_timings_for_uuid(uuid))
|
||||
results = get_timings_for_uuid(uuid)
|
||||
return rsp(json.dumps(results))
|
||||
|
||||
|
||||
def do_timings(request):
|
||||
@ -142,7 +140,7 @@ def do_timings(request):
|
||||
|
||||
for t in timings:
|
||||
results.append([t.lifecycle.instance, sec_to_time(t.diff)])
|
||||
return rsp(results)
|
||||
return rsp(json.dumps(results))
|
||||
|
||||
|
||||
def do_summary(request):
|
||||
@ -176,7 +174,7 @@ def do_summary(request):
|
||||
|
||||
results.append([name, int(num), sec_to_time(_min),
|
||||
sec_to_time(_max), sec_to_time(int(total / num))])
|
||||
return rsp(results)
|
||||
return rsp(json.dumps(results))
|
||||
|
||||
|
||||
def do_request(request):
|
||||
@ -194,7 +192,7 @@ def do_request(request):
|
||||
results.append([e.id, routing_key_type(e.routing_key), str(when),
|
||||
e.deployment.name, e.event, e.host, e.state,
|
||||
e.old_state, e.old_task])
|
||||
return rsp(results)
|
||||
return rsp(json.dumps(results))
|
||||
|
||||
|
||||
def do_show(request, event_id):
|
||||
@ -225,7 +223,7 @@ def do_show(request, event_id):
|
||||
final.append(json.dumps(j, indent=2))
|
||||
final.append(event.instance)
|
||||
|
||||
return rsp(final)
|
||||
return rsp(json.dumps(final))
|
||||
|
||||
|
||||
def do_watch(request, deployment_id):
|
||||
@ -281,8 +279,9 @@ def do_watch(request, deployment_id):
|
||||
deployment_map[raw.deployment.id].name,
|
||||
raw.event,
|
||||
uuid])
|
||||
results_json = json.dumps([c, results, str(dec_now)])
|
||||
|
||||
return rsp([c, results, str(dec_now)])
|
||||
return rsp(results_json)
|
||||
|
||||
|
||||
def do_kpi(request, tenant_id=None):
|
||||
@ -306,7 +305,7 @@ def do_kpi(request, tenant_id=None):
|
||||
if tenant_id is None or (tenant_id == end_event.tenant):
|
||||
results.append([event, sec_to_time(track.duration),
|
||||
uuid, end_event.deployment.name])
|
||||
return rsp(results)
|
||||
return rsp(json.dumps(results))
|
||||
|
||||
|
||||
def do_list_usage_launches(request):
|
||||
@ -332,7 +331,7 @@ def do_list_usage_launches(request):
|
||||
launched = str(dt.dt_from_decimal(launch.launched_at))
|
||||
results.append([launch.instance, launched, launch.instance_type_id])
|
||||
|
||||
return rsp(results)
|
||||
return rsp(json.dumps(results))
|
||||
|
||||
|
||||
def do_list_usage_deletes(request):
|
||||
@ -361,7 +360,7 @@ def do_list_usage_deletes(request):
|
||||
deleted = str(dt.dt_from_decimal(delete.deleted_at))
|
||||
results.append([delete.instance, launched, deleted])
|
||||
|
||||
return rsp(results)
|
||||
return rsp(json.dumps(results))
|
||||
|
||||
|
||||
def do_list_usage_exists(request):
|
||||
@ -393,7 +392,7 @@ def do_list_usage_exists(request):
|
||||
exist.instance_type_id, exist.message_id,
|
||||
exist.status])
|
||||
|
||||
return rsp(results)
|
||||
return rsp(json.dumps(results), content_type="application/json")
|
||||
|
||||
|
||||
def do_jsonreports(request):
|
||||
@ -413,7 +412,7 @@ def do_jsonreports(request):
|
||||
float(report.created),
|
||||
report.name,
|
||||
report.version])
|
||||
return rsp(results)
|
||||
return rsp(json.dumps(results))
|
||||
|
||||
|
||||
def do_jsonreport(request, report_id):
|
||||
|
Loading…
x
Reference in New Issue
Block a user