Starting usage api impl
This commit is contained in:
65
stacktach/dbapi.py
Normal file
65
stacktach/dbapi.py
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
import decimal
|
||||||
|
import json
|
||||||
|
|
||||||
|
from django.forms.models import model_to_dict
|
||||||
|
from django.http import HttpResponse
|
||||||
|
|
||||||
|
import datetime_to_decimal
|
||||||
|
import models
|
||||||
|
|
||||||
|
|
||||||
|
def rsp(data):
|
||||||
|
return HttpResponse(json.dumps(data))
|
||||||
|
|
||||||
|
|
||||||
|
def list_usage_launches(request):
|
||||||
|
filter_args = {}
|
||||||
|
if 'instance' in request.GET:
|
||||||
|
filter_args['instance'] = request.GET['instance']
|
||||||
|
|
||||||
|
if len(filter_args) > 0:
|
||||||
|
objects = models.InstanceUsage.objects.filter(**filter_args)
|
||||||
|
else:
|
||||||
|
objects = models.InstanceUsage.objects.all()
|
||||||
|
|
||||||
|
dicts = _convert_model_list(objects.order_by("launched_at"))
|
||||||
|
return rsp({'launches': dicts})
|
||||||
|
|
||||||
|
|
||||||
|
def list_usage_deletes(request):
|
||||||
|
filter_args = {}
|
||||||
|
if 'instance' in request.GET:
|
||||||
|
filter_args['instance'] = request.GET['instance']
|
||||||
|
|
||||||
|
if len(filter_args) > 0:
|
||||||
|
objects = models.InstanceDeletes.objects.filter(**filter_args)
|
||||||
|
else:
|
||||||
|
objects = models.InstanceDeletes.objects.all()
|
||||||
|
|
||||||
|
dicts = _convert_model_list(objects.order_by("launched_at"))
|
||||||
|
return rsp({'deletes': dicts})
|
||||||
|
|
||||||
|
|
||||||
|
def list_usage_exists(request):
|
||||||
|
filter_args = {}
|
||||||
|
if 'instance' in request.GET:
|
||||||
|
filter_args['instance'] = request.GET['instance']
|
||||||
|
|
||||||
|
if len(filter_args) > 0:
|
||||||
|
objects = models.InstanceExists.objects.filter(**filter_args)
|
||||||
|
else:
|
||||||
|
objects = models.InstanceExists.objects.all()
|
||||||
|
|
||||||
|
dicts = _convert_model_list(objects.order_by("id"))
|
||||||
|
return rsp({'exists': dicts})
|
||||||
|
|
||||||
|
|
||||||
|
def _convert_model_list(list):
|
||||||
|
converted = []
|
||||||
|
for item in list:
|
||||||
|
dict = model_to_dict(item)
|
||||||
|
for key in dict:
|
||||||
|
if isinstance(dict[key], decimal.Decimal):
|
||||||
|
dict[key] = str(datetime_to_decimal.dt_from_decimal(dict[key]))
|
||||||
|
converted.append(dict)
|
||||||
|
return converted
|
@@ -19,6 +19,12 @@ urlpatterns = patterns('',
|
|||||||
url(r'stacky/kpi/$', 'stacktach.stacky_server.do_kpi'),
|
url(r'stacky/kpi/$', 'stacktach.stacky_server.do_kpi'),
|
||||||
url(r'stacky/kpi/(?P<tenant_id>\d+)/$', 'stacktach.stacky_server.do_kpi'),
|
url(r'stacky/kpi/(?P<tenant_id>\d+)/$', 'stacktach.stacky_server.do_kpi'),
|
||||||
|
|
||||||
|
url(r'db/usage/launches/$',
|
||||||
|
'stacktach.dbapi.list_usage_launches'),
|
||||||
|
url(r'db/usage/deletes/$',
|
||||||
|
'stacktach.dbapi.list_usage_deletes'),
|
||||||
|
url(r'db/usage/exists/$', 'stacktach.dbapi.list_usage_exists'),
|
||||||
|
|
||||||
url(r'^(?P<deployment_id>\d+)/$', 'stacktach.views.home', name='home'),
|
url(r'^(?P<deployment_id>\d+)/$', 'stacktach.views.home', name='home'),
|
||||||
url(r'^(?P<deployment_id>\d+)/details/(?P<column>\w+)/(?P<row_id>\d+)/$',
|
url(r'^(?P<deployment_id>\d+)/details/(?P<column>\w+)/(?P<row_id>\d+)/$',
|
||||||
'stacktach.views.details', name='details'),
|
'stacktach.views.details', name='details'),
|
||||||
|
Reference in New Issue
Block a user