Merge "api: add timestamp interval support in _list_events()"
This commit is contained in:
commit
48d8f574e8
@ -248,11 +248,14 @@ def _list_events(meter,
|
||||
user=None):
|
||||
"""Return a list of raw metering events.
|
||||
"""
|
||||
q_ts = _get_query_timestamps(flask.request.args)
|
||||
f = storage.EventFilter(user=user,
|
||||
project=project,
|
||||
source=source,
|
||||
meter=meter,
|
||||
resource=resource,
|
||||
start=q_ts['start_timestamp'],
|
||||
end=q_ts['end_timestamp'],
|
||||
)
|
||||
events = list(flask.request.storage_conn.get_raw_events(f))
|
||||
jsonified = flask.jsonify(events=events)
|
||||
|
@ -34,45 +34,48 @@ class TestListEvents(tests_api.TestBase):
|
||||
|
||||
def setUp(self):
|
||||
super(TestListEvents, self).setUp()
|
||||
self.counter1 = counter.Counter(
|
||||
'instance',
|
||||
'cumulative',
|
||||
1,
|
||||
'user-id',
|
||||
'project1',
|
||||
'resource-id',
|
||||
timestamp=datetime.datetime(2012, 7, 2, 10, 40),
|
||||
resource_metadata={'display_name': 'test-server',
|
||||
'tag': 'self.counter',
|
||||
}
|
||||
)
|
||||
msg = meter.meter_message_from_counter(self.counter1,
|
||||
cfg.CONF.metering_secret,
|
||||
'source1',
|
||||
)
|
||||
self.conn.record_metering_data(msg)
|
||||
|
||||
self.counter2 = counter.Counter(
|
||||
'instance',
|
||||
'cumulative',
|
||||
1,
|
||||
'user-id2',
|
||||
'project2',
|
||||
'resource-id-alternate',
|
||||
timestamp=datetime.datetime(2012, 7, 2, 10, 41),
|
||||
resource_metadata={'display_name': 'test-server',
|
||||
'tag': 'self.counter2',
|
||||
}
|
||||
)
|
||||
msg2 = meter.meter_message_from_counter(self.counter2,
|
||||
cfg.CONF.metering_secret,
|
||||
'source2',
|
||||
)
|
||||
self.conn.record_metering_data(msg2)
|
||||
|
||||
def test_all(self):
|
||||
data = self.get('/resources')
|
||||
self.assertEquals(2, len(data['resources']))
|
||||
for cnt in [
|
||||
counter.Counter(
|
||||
'instance',
|
||||
'cumulative',
|
||||
1,
|
||||
'user-id',
|
||||
'project1',
|
||||
'resource-id',
|
||||
timestamp=datetime.datetime(2012, 7, 2, 10, 40),
|
||||
resource_metadata={'display_name': 'test-server',
|
||||
'tag': 'self.counter',
|
||||
}
|
||||
),
|
||||
counter.Counter(
|
||||
'instance',
|
||||
'cumulative',
|
||||
2,
|
||||
'user-id',
|
||||
'project1',
|
||||
'resource-id',
|
||||
timestamp=datetime.datetime(2012, 7, 2, 10, 41),
|
||||
resource_metadata={'display_name': 'test-server',
|
||||
'tag': 'self.counter',
|
||||
}
|
||||
),
|
||||
counter.Counter(
|
||||
'instance',
|
||||
'cumulative',
|
||||
1,
|
||||
'user-id2',
|
||||
'project2',
|
||||
'resource-id-alternate',
|
||||
timestamp=datetime.datetime(2012, 7, 2, 10, 42),
|
||||
resource_metadata={'display_name': 'test-server',
|
||||
'tag': 'self.counter2',
|
||||
}
|
||||
),
|
||||
]:
|
||||
msg = meter.meter_message_from_counter(cnt,
|
||||
cfg.CONF.metering_secret,
|
||||
'source1')
|
||||
self.conn.record_metering_data(msg)
|
||||
|
||||
def test_empty_project(self):
|
||||
data = self.get('/projects/no-such-project/meters/instance')
|
||||
@ -80,7 +83,12 @@ class TestListEvents(tests_api.TestBase):
|
||||
|
||||
def test_by_project(self):
|
||||
data = self.get('/projects/project1/meters/instance')
|
||||
self.assertEquals(1, len(data['events']))
|
||||
self.assertEquals(2, len(data['events']))
|
||||
|
||||
def test_by_project_with_timestamps(self):
|
||||
data = self.get('/projects/project1/meters/instance',
|
||||
start_timestamp=datetime.datetime(2012, 7, 2, 10, 42))
|
||||
self.assertEquals(0, len(data['events']))
|
||||
|
||||
def test_empty_resource(self):
|
||||
data = self.get('/resources/no-such-resource/meters/instance')
|
||||
@ -88,7 +96,7 @@ class TestListEvents(tests_api.TestBase):
|
||||
|
||||
def test_by_resource(self):
|
||||
data = self.get('/resources/resource-id/meters/instance')
|
||||
self.assertEquals(1, len(data['events']))
|
||||
self.assertEquals(2, len(data['events']))
|
||||
|
||||
def test_empty_source(self):
|
||||
data = self.get('/sources/no-such-source/meters/instance')
|
||||
@ -96,7 +104,12 @@ class TestListEvents(tests_api.TestBase):
|
||||
|
||||
def test_by_source(self):
|
||||
data = self.get('/sources/source1/meters/instance')
|
||||
self.assertEquals(1, len(data['events']))
|
||||
self.assertEquals(3, len(data['events']))
|
||||
|
||||
def test_by_source_with_timestamps(self):
|
||||
data = self.get('/sources/source1/meters/instance',
|
||||
end_timestamp=datetime.datetime(2012, 7, 2, 10, 42))
|
||||
self.assertEquals(2, len(data['events']))
|
||||
|
||||
def test_empty_user(self):
|
||||
data = self.get('/users/no-such-user/meters/instance')
|
||||
@ -104,4 +117,10 @@ class TestListEvents(tests_api.TestBase):
|
||||
|
||||
def test_by_user(self):
|
||||
data = self.get('/users/user-id/meters/instance')
|
||||
self.assertEquals(2, len(data['events']))
|
||||
|
||||
def test_by_user_with_timestamps(self):
|
||||
data = self.get('/users/user-id/meters/instance',
|
||||
start_timestamp=datetime.datetime(2012, 7, 2, 10, 41),
|
||||
end_timestamp=datetime.datetime(2012, 7, 2, 10, 42))
|
||||
self.assertEquals(1, len(data['events']))
|
||||
|
Loading…
Reference in New Issue
Block a user