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
|
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/
|
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/
|
.. http:post:: http://example.com/db/repair/
|
||||||
|
|
||||||
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/
|
|
||||||
|
|
||||||
Changes the status of all the exists of message-ids sent with the request
|
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
|
from 'pending' to 'sent_unverified' so that the verifier does not end up
|
||||||
@ -926,7 +929,7 @@ repair
|
|||||||
|
|
||||||
.. sourcecode::http
|
.. sourcecode::http
|
||||||
|
|
||||||
POST /repair/ HTTP/1.1
|
POST /db/repair/ HTTP/1.1
|
||||||
Host: example.com
|
Host: example.com
|
||||||
Accept: application/json
|
Accept: application/json
|
||||||
|
|
||||||
|
@ -459,23 +459,26 @@ def _rawdata_factory(service):
|
|||||||
|
|
||||||
|
|
||||||
@api_call
|
@api_call
|
||||||
def get_verified_count(request):
|
def get_event_stats(request):
|
||||||
try:
|
try:
|
||||||
audit_period_beginning = datetime.strptime(
|
filters = {}
|
||||||
request.GET.get("audit_period_beginning"), "%Y-%m-%d")
|
if 'when_min' in request.GET:
|
||||||
audit_period_ending = datetime.strptime(
|
when_min = utils.str_time_to_unix(request.GET['when_min'])
|
||||||
request.GET.get("audit_period_ending"), "%Y-%m-%d")
|
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")
|
service = request.GET.get("service", "nova")
|
||||||
rawdata = _rawdata_factory(service)
|
rawdata = _rawdata_factory(service)
|
||||||
filters = {
|
return {'stats': {'count': rawdata.filter(**filters).count()}}
|
||||||
'when__gte': dt.dt_to_decimal(audit_period_beginning),
|
except (KeyError, TypeError):
|
||||||
'when__lte': dt.dt_to_decimal(audit_period_ending),
|
|
||||||
'event': "compute.instance.exists.verified"
|
|
||||||
}
|
|
||||||
return {'count': rawdata.filter(**filters).count()}
|
|
||||||
except KeyError and TypeError:
|
|
||||||
raise BadRequestException(message="Invalid/absent query parameter")
|
raise BadRequestException(message="Invalid/absent query parameter")
|
||||||
except ValueError:
|
except (ValueError, AttributeError):
|
||||||
raise BadRequestException(message="Invalid format for date (Correct "
|
raise BadRequestException(message="Invalid format for date (Correct "
|
||||||
"format should be %YYYY-%mm-%dd)")
|
"format should be %YYYY-%mm-%dd)")
|
||||||
|
|
||||||
|
@ -91,7 +91,7 @@ dbapi_urls = (
|
|||||||
'stacktach.dbapi.get_usage_exist_stats'),
|
'stacktach.dbapi.get_usage_exist_stats'),
|
||||||
url(r'db/stats/glance/exists$',
|
url(r'db/stats/glance/exists$',
|
||||||
'stacktach.dbapi.get_usage_exist_stats_glance'),
|
'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'),
|
url(r'db/repair/', 'stacktach.dbapi.repair_stacktach_down'),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -1106,9 +1106,10 @@ class DBAPITestCase(StacktachBaseTestCase):
|
|||||||
def test_get_verified_count(self):
|
def test_get_verified_count(self):
|
||||||
fake_request = self.mox.CreateMockAnything()
|
fake_request = self.mox.CreateMockAnything()
|
||||||
fake_request.method = 'GET'
|
fake_request.method = 'GET'
|
||||||
fake_request.GET = {'audit_period_beginning': "2014-02-26",
|
fake_request.GET = {'when_min': "2014-02-26 00:00:00",
|
||||||
'audit_period_ending': "2014-02-27",
|
'when_max': "2014-02-27 00:00:00",
|
||||||
'service': "nova"}
|
'service': "nova",
|
||||||
|
'event': 'compute.instance.exists.verified'}
|
||||||
mock_query = self.mox.CreateMockAnything()
|
mock_query = self.mox.CreateMockAnything()
|
||||||
self.mox.StubOutWithMock(models.RawData.objects, "filter")
|
self.mox.StubOutWithMock(models.RawData.objects, "filter")
|
||||||
models.RawData.objects.filter(event='compute.instance.exists.verified',
|
models.RawData.objects.filter(event='compute.instance.exists.verified',
|
||||||
@ -1118,21 +1119,22 @@ class DBAPITestCase(StacktachBaseTestCase):
|
|||||||
mock_query.count().AndReturn(100)
|
mock_query.count().AndReturn(100)
|
||||||
self.mox.ReplayAll()
|
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(response.status_code, 200)
|
||||||
self.assertEqual(json.loads(response.content), {'count': 100})
|
self.assertEqual(json.loads(response.content),
|
||||||
|
{'stats': {'count': 100}})
|
||||||
self.mox.VerifyAll()
|
self.mox.VerifyAll()
|
||||||
|
|
||||||
def test_get_verified_count_wrong_date_format_returns_400(self):
|
def test_get_verified_count_wrong_date_format_returns_400(self):
|
||||||
fake_request = self.mox.CreateMockAnything()
|
fake_request = self.mox.CreateMockAnything()
|
||||||
fake_request.method = 'GET'
|
fake_request.method = 'GET'
|
||||||
fake_request.GET = {'audit_period_beginning': "2014-020-26",
|
fake_request.GET = {'when_min': "2014-020-26",
|
||||||
|
|
||||||
'service': "nova"}
|
'service': "nova"}
|
||||||
|
|
||||||
self.mox.ReplayAll()
|
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(response.status_code, 400)
|
||||||
self.assertEqual(json.loads(response.content)['message'],
|
self.assertEqual(json.loads(response.content)['message'],
|
||||||
"Invalid format for date"
|
"Invalid format for date"
|
||||||
@ -1142,30 +1144,18 @@ class DBAPITestCase(StacktachBaseTestCase):
|
|||||||
def test_get_verified_count_wrong_service_returns_400(self):
|
def test_get_verified_count_wrong_service_returns_400(self):
|
||||||
fake_request = self.mox.CreateMockAnything()
|
fake_request = self.mox.CreateMockAnything()
|
||||||
fake_request.method = 'GET'
|
fake_request.method = 'GET'
|
||||||
fake_request.GET = {'audit_period_beginning': "2014-02-26",
|
fake_request.GET = {'when_min': "2014-02-26 00:00:00",
|
||||||
"audit_period_ending": "2014-02-27",
|
"when_min": "2014-02-27 00:00:00",
|
||||||
'service': "qonos"}
|
'service': "qonos"}
|
||||||
|
|
||||||
self.mox.ReplayAll()
|
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(response.status_code, 400)
|
||||||
self.assertEqual(json.loads(response.content)['message'],
|
self.assertEqual(json.loads(response.content)['message'],
|
||||||
"Invalid service")
|
"Invalid service")
|
||||||
self.mox.VerifyAll()
|
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):
|
class StacktachRepairScenarioApi(StacktachBaseTestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user