Refactoring event count api
This commit is contained in:
parent
5ee5240339
commit
4f27f3e393
@ -100,6 +100,44 @@ Uses the provided message_id's and http status codes to update image and instanc
|
||||
Read APIs
|
||||
*********
|
||||
|
||||
|
||||
|
||||
db/stats/events
|
||||
===============
|
||||
|
||||
.. http:get:: http://example.com/db/stats/events/
|
||||
|
||||
Returns a count of events stored in Stacktach's Rawdata tables from
|
||||
``when_min`` to ``when_max``
|
||||
|
||||
**Query Parameters**
|
||||
|
||||
* ``event``: event type to filter by
|
||||
* ``when_min``: datetime (yyyy-mm-dd hh:mm:ss)
|
||||
* ``when_max``: datetime (yyyy-mm-dd hh:mm:ss)
|
||||
* ``service``: ``nova`` or ``glance``. default="nova"
|
||||
|
||||
**Example request**:
|
||||
|
||||
.. sourcecode:: http
|
||||
|
||||
GET db/stats/events/ HTTP/1.1
|
||||
Host: example.com
|
||||
Accept: application/json
|
||||
|
||||
|
||||
**Example response**:
|
||||
|
||||
.. sourcecode:: http
|
||||
|
||||
HTTP/1.1 200 OK
|
||||
Vary: Accept
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
count: 10
|
||||
}
|
||||
|
||||
db/stats/nova/exists/
|
||||
=====================
|
||||
|
||||
@ -876,45 +914,10 @@ Returns a single instance exists matching provided id
|
||||
}
|
||||
}
|
||||
|
||||
db/count/verified/
|
||||
==================
|
||||
/db/repair
|
||||
==========
|
||||
|
||||
.. http:get:: http://example.com/count/verified/
|
||||
|
||||
Returns a count of .verified events stored in Stacktach's Rawdata table from
|
||||
``audit_period_beginning`` to ``audit_period_ending``
|
||||
|
||||
**Query Parameters**
|
||||
|
||||
* ``audit_period_beginning``: datetime (yyyy-mm-dd)
|
||||
* ``audit_period_ending``: datetime (yyyy-mm-dd)
|
||||
* ``service``: ``nova`` or ``glance``. default="nova"
|
||||
|
||||
**Example request**:
|
||||
|
||||
.. sourcecode:: http
|
||||
|
||||
GET db/count/verified/ HTTP/1.1
|
||||
Host: example.com
|
||||
Accept: application/json
|
||||
|
||||
|
||||
**Example response**:
|
||||
|
||||
.. sourcecode:: http
|
||||
|
||||
HTTP/1.1 200 OK
|
||||
Vary: Accept
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
count: 10
|
||||
}
|
||||
|
||||
repair
|
||||
======
|
||||
|
||||
.. http:post:: http://example.com/repair/
|
||||
.. http:post:: http://example.com/db/repair/
|
||||
|
||||
Changes the status of all the exists of message-ids sent with the request
|
||||
from 'pending' to 'sent_unverified' so that the verifier does not end up
|
||||
@ -926,7 +929,7 @@ repair
|
||||
|
||||
.. sourcecode::http
|
||||
|
||||
POST /repair/ HTTP/1.1
|
||||
POST /db/repair/ HTTP/1.1
|
||||
Host: example.com
|
||||
Accept: application/json
|
||||
|
||||
|
@ -459,23 +459,26 @@ def _rawdata_factory(service):
|
||||
|
||||
|
||||
@api_call
|
||||
def get_verified_count(request):
|
||||
def get_event_stats(request):
|
||||
try:
|
||||
audit_period_beginning = datetime.strptime(
|
||||
request.GET.get("audit_period_beginning"), "%Y-%m-%d")
|
||||
audit_period_ending = datetime.strptime(
|
||||
request.GET.get("audit_period_ending"), "%Y-%m-%d")
|
||||
filters = {}
|
||||
if 'when_min' in request.GET:
|
||||
when_min = utils.str_time_to_unix(request.GET['when_min'])
|
||||
filters['when__gte'] = when_min
|
||||
|
||||
if 'when_max' in request.GET:
|
||||
when_max = utils.str_time_to_unix(request.GET['when_max'])
|
||||
filters['when__lte'] = when_max
|
||||
|
||||
if 'event' in request.GET:
|
||||
filters['event'] = request.GET['event']
|
||||
|
||||
service = request.GET.get("service", "nova")
|
||||
rawdata = _rawdata_factory(service)
|
||||
filters = {
|
||||
'when__gte': dt.dt_to_decimal(audit_period_beginning),
|
||||
'when__lte': dt.dt_to_decimal(audit_period_ending),
|
||||
'event': "compute.instance.exists.verified"
|
||||
}
|
||||
return {'count': rawdata.filter(**filters).count()}
|
||||
except KeyError and TypeError:
|
||||
return {'stats': {'count': rawdata.filter(**filters).count()}}
|
||||
except (KeyError, TypeError):
|
||||
raise BadRequestException(message="Invalid/absent query parameter")
|
||||
except ValueError:
|
||||
except (ValueError, AttributeError):
|
||||
raise BadRequestException(message="Invalid format for date (Correct "
|
||||
"format should be %YYYY-%mm-%dd)")
|
||||
|
||||
|
@ -91,7 +91,7 @@ dbapi_urls = (
|
||||
'stacktach.dbapi.get_usage_exist_stats'),
|
||||
url(r'db/stats/glance/exists$',
|
||||
'stacktach.dbapi.get_usage_exist_stats_glance'),
|
||||
url(r'db/count/verified', 'stacktach.dbapi.get_verified_count'),
|
||||
url(r'db/stats/events', 'stacktach.dbapi.get_event_stats'),
|
||||
url(r'db/repair/', 'stacktach.dbapi.repair_stacktach_down'),
|
||||
)
|
||||
|
||||
|
@ -1106,9 +1106,10 @@ class DBAPITestCase(StacktachBaseTestCase):
|
||||
def test_get_verified_count(self):
|
||||
fake_request = self.mox.CreateMockAnything()
|
||||
fake_request.method = 'GET'
|
||||
fake_request.GET = {'audit_period_beginning': "2014-02-26",
|
||||
'audit_period_ending': "2014-02-27",
|
||||
'service': "nova"}
|
||||
fake_request.GET = {'when_min': "2014-02-26 00:00:00",
|
||||
'when_max': "2014-02-27 00:00:00",
|
||||
'service': "nova",
|
||||
'event': 'compute.instance.exists.verified'}
|
||||
mock_query = self.mox.CreateMockAnything()
|
||||
self.mox.StubOutWithMock(models.RawData.objects, "filter")
|
||||
models.RawData.objects.filter(event='compute.instance.exists.verified',
|
||||
@ -1118,21 +1119,22 @@ class DBAPITestCase(StacktachBaseTestCase):
|
||||
mock_query.count().AndReturn(100)
|
||||
self.mox.ReplayAll()
|
||||
|
||||
response = dbapi.get_verified_count(fake_request)
|
||||
response = dbapi.get_event_stats(fake_request)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(json.loads(response.content), {'count': 100})
|
||||
self.assertEqual(json.loads(response.content),
|
||||
{'stats': {'count': 100}})
|
||||
self.mox.VerifyAll()
|
||||
|
||||
def test_get_verified_count_wrong_date_format_returns_400(self):
|
||||
fake_request = self.mox.CreateMockAnything()
|
||||
fake_request.method = 'GET'
|
||||
fake_request.GET = {'audit_period_beginning': "2014-020-26",
|
||||
fake_request.GET = {'when_min': "2014-020-26",
|
||||
|
||||
'service': "nova"}
|
||||
|
||||
self.mox.ReplayAll()
|
||||
|
||||
response = dbapi.get_verified_count(fake_request)
|
||||
response = dbapi.get_event_stats(fake_request)
|
||||
self.assertEqual(response.status_code, 400)
|
||||
self.assertEqual(json.loads(response.content)['message'],
|
||||
"Invalid format for date"
|
||||
@ -1142,30 +1144,18 @@ class DBAPITestCase(StacktachBaseTestCase):
|
||||
def test_get_verified_count_wrong_service_returns_400(self):
|
||||
fake_request = self.mox.CreateMockAnything()
|
||||
fake_request.method = 'GET'
|
||||
fake_request.GET = {'audit_period_beginning': "2014-02-26",
|
||||
"audit_period_ending": "2014-02-27",
|
||||
fake_request.GET = {'when_min': "2014-02-26 00:00:00",
|
||||
"when_min": "2014-02-27 00:00:00",
|
||||
'service': "qonos"}
|
||||
|
||||
self.mox.ReplayAll()
|
||||
|
||||
response = dbapi.get_verified_count(fake_request)
|
||||
response = dbapi.get_event_stats(fake_request)
|
||||
self.assertEqual(response.status_code, 400)
|
||||
self.assertEqual(json.loads(response.content)['message'],
|
||||
"Invalid service")
|
||||
self.mox.VerifyAll()
|
||||
|
||||
def test_get_verified_count_invalid_query_parameter_returns_400(self):
|
||||
fake_request = self.mox.CreateMockAnything()
|
||||
fake_request.method = 'GET'
|
||||
fake_request.GET = {'audit_period': "2014-02-26",}
|
||||
|
||||
self.mox.ReplayAll()
|
||||
|
||||
response = dbapi.get_verified_count(fake_request)
|
||||
self.assertEqual(response.status_code, 400)
|
||||
self.assertEqual(json.loads(response.content)['message'],
|
||||
"Invalid/absent query parameter")
|
||||
self.mox.VerifyAll()
|
||||
|
||||
class StacktachRepairScenarioApi(StacktachBaseTestCase):
|
||||
def setUp(self):
|
||||
|
Loading…
x
Reference in New Issue
Block a user