Fix broken statistics in sqlalchemy

When querying the samples for statistics calculations,
the filtering is done without joining the Sample table.
This patch fixes this.

Closes-Bug: #1289325
Change-Id: I45edc974a6c054d27ac33d5287f760bcff08cf91
This commit is contained in:
Nejc Saje 2014-03-07 13:47:43 +00:00 committed by Gordon Chung
parent 2e150c57db
commit b530bc4fb2
2 changed files with 35 additions and 1 deletions

View File

@ -700,7 +700,8 @@ class Connection(base.Connection):
group_attributes = [getattr(models.Sample, g) for g in groupby] group_attributes = [getattr(models.Sample, g) for g in groupby]
select.extend(group_attributes) select.extend(group_attributes)
query = session.query(*select) query = session.query(*select).filter(
models.Meter.id == models.Sample.meter_id)
if groupby: if groupby:
query = query.group_by(*group_attributes) query = query.group_by(*group_attributes)

View File

@ -1153,6 +1153,39 @@ class StatisticsTest(DBTestBase,
secret='not-so-secret', secret='not-so-secret',
) )
self.conn.record_metering_data(msg) self.conn.record_metering_data(msg)
for i in range(3):
c = sample.Sample(
'memory',
'gauge',
'MB',
8 + i,
'user-5',
'project2',
'resource-6',
timestamp=datetime.datetime(2012, 9, 25, 10 + i, 30 + i),
resource_metadata={},
source='test',
)
msg = utils.meter_message_from_counter(
c,
secret='not-so-secret',
)
self.conn.record_metering_data(msg)
def test_by_meter(self):
f = storage.SampleFilter(
meter='memory'
)
results = list(self.conn.get_meter_statistics(f))[0]
self.assertEqual(results.duration,
(datetime.datetime(2012, 9, 25, 12, 32)
- datetime.datetime(2012, 9, 25, 10, 30)).seconds)
self.assertEqual(results.count, 3)
self.assertEqual(results.unit, 'MB')
self.assertEqual(results.min, 8)
self.assertEqual(results.max, 10)
self.assertEqual(results.sum, 27)
self.assertEqual(results.avg, 9)
def test_by_user(self): def test_by_user(self):
f = storage.SampleFilter( f = storage.SampleFilter(