Merge "add unit attribute to statistics"

This commit is contained in:
Jenkins 2013-08-12 07:54:47 +00:00 committed by Gerrit Code Review
commit 5d8a6e56e2
8 changed files with 28 additions and 7 deletions

View File

@ -364,6 +364,9 @@ class Statistics(_Base):
"""Computed statistics for a query. """Computed statistics for a query.
""" """
unit = wtypes.text
"The unit type of the data set"
min = float min = float
"The minimum volume seen in the data" "The minimum volume seen in the data"
@ -435,7 +438,8 @@ class Statistics(_Base):
@classmethod @classmethod
def sample(cls): def sample(cls):
return cls(min=1, return cls(unit='GiB',
min=1,
max=9, max=9,
avg=4.5, avg=4.5,
sum=45, sum=45,

View File

@ -472,6 +472,7 @@ class Connection(base.Connection):
""" """
vol = int(meter['f:counter_volume']) vol = int(meter['f:counter_volume'])
ts = timeutils.parse_strtime(meter['f:timestamp']) ts = timeutils.parse_strtime(meter['f:timestamp'])
stat.unit = meter['f:counter_unit']
stat.min = min(vol, stat.min or vol) stat.min = min(vol, stat.min or vol)
stat.max = max(vol, stat.max) stat.max = max(vol, stat.max)
stat.sum = vol + (stat.sum or 0) stat.sum = vol + (stat.sum or 0)
@ -541,7 +542,8 @@ class Connection(base.Connection):
period_end = period_start + datetime.timedelta( period_end = period_start + datetime.timedelta(
0, period) 0, period)
results.append( results.append(
models.Statistics(count=0, models.Statistics(unit='',
count=0,
min=0, min=0,
max=0, max=0,
avg=0, avg=0,

View File

@ -175,7 +175,8 @@ class Connection(base.Connection):
MAP_STATS = bson.code.Code(""" MAP_STATS = bson.code.Code("""
function () { function () {
emit('statistics', { min : this.counter_volume, emit('statistics', { unit: this.counter_unit,
min : this.counter_volume,
max : this.counter_volume, max : this.counter_volume,
sum : this.counter_volume, sum : this.counter_volume,
count : NumberInt(1), count : NumberInt(1),
@ -195,7 +196,8 @@ class Connection(base.Connection):
- period_first) / period) - period_first) / period)
* period); * period);
emit(period_start, emit(period_start,
{ min : this.counter_volume, { unit: this.counter_unit,
min : this.counter_volume,
max : this.counter_volume, max : this.counter_volume,
sum : this.counter_volume, sum : this.counter_volume,
count : NumberInt(1), count : NumberInt(1),
@ -208,7 +210,8 @@ class Connection(base.Connection):
REDUCE_STATS = bson.code.Code(""" REDUCE_STATS = bson.code.Code("""
function (key, values) { function (key, values) {
var res = { min: values[0].min, var res = { unit: values[0].unit,
min: values[0].min,
max: values[0].max, max: values[0].max,
count: values[0].count, count: values[0].count,
sum: values[0].sum, sum: values[0].sum,

View File

@ -434,6 +434,7 @@ class Connection(base.Connection):
def _make_stats_query(sample_filter): def _make_stats_query(sample_filter):
session = sqlalchemy_session.get_session() session = sqlalchemy_session.get_session()
query = session.query( query = session.query(
Meter.counter_unit.label('unit'),
func.min(Meter.timestamp).label('tsmin'), func.min(Meter.timestamp).label('tsmin'),
func.max(Meter.timestamp).label('tsmax'), func.max(Meter.timestamp).label('tsmax'),
func.avg(Meter.counter_volume).label('avg'), func.avg(Meter.counter_volume).label('avg'),
@ -450,6 +451,7 @@ class Connection(base.Connection):
if result.tsmin is not None and result.tsmax is not None if result.tsmin is not None and result.tsmax is not None
else None) else None)
return api_models.Statistics( return api_models.Statistics(
unit=result.unit,
count=int(result.count), count=int(result.count),
min=result.min, min=result.min,
max=result.max, max=result.max,

View File

@ -209,12 +209,13 @@ class Sample(Model):
class Statistics(Model): class Statistics(Model):
"""Computed statistics based on a set of sample data. """Computed statistics based on a set of sample data.
""" """
def __init__(self, def __init__(self, unit,
min, max, avg, sum, count, min, max, avg, sum, count,
period, period_start, period_end, period, period_start, period_end,
duration, duration_start, duration_end): duration, duration_start, duration_end):
"""Create a new statistics object. """Create a new statistics object.
:param unit: The unit type of the data set
:param min: The smallest volume found :param min: The smallest volume found
:param max: The largest volume found :param max: The largest volume found
:param avg: The average of all volumes found :param avg: The average of all volumes found
@ -227,7 +228,7 @@ class Statistics(Model):
:param duration_start: The earliest time for the matching samples :param duration_start: The earliest time for the matching samples
:param duration_end: The latest time for the matching samples :param duration_end: The latest time for the matching samples
""" """
Model.__init__(self, Model.__init__(self, unit=unit,
min=min, max=max, avg=avg, sum=sum, count=count, min=min, max=max, avg=avg, sum=sum, count=count,
period=period, period_start=period_start, period=period, period_start=period_start,
period_end=period_end, duration=duration, period_end=period_end, duration=duration,

View File

@ -57,6 +57,7 @@ class TestComputeDurationByResource(tests_api.TestBase,
def _set_stats(self, start, end): def _set_stats(self, start, end):
def get_meter_statistics(event_filter): def get_meter_statistics(event_filter):
return models.Statistics( return models.Statistics(
unit='',
min=0, max=0, avg=0, sum=0, count=0, min=0, max=0, avg=0, sum=0, count=0,
period=None, period=None,
period_start=None, period_start=None,

View File

@ -68,6 +68,7 @@ class TestComputeDurationByResource(FunctionalTest,
duration = timeutils.delta_seconds(duration_start, duration_end) duration = timeutils.delta_seconds(duration_start, duration_end)
return [ return [
models.Statistics( models.Statistics(
unit='',
min=0, min=0,
max=0, max=0,
avg=0, avg=0,
@ -142,6 +143,7 @@ class TestComputeDurationByResource(FunctionalTest,
def get_interval(ignore_self, event_filter, period): def get_interval(ignore_self, event_filter, period):
return [ return [
models.Statistics( models.Statistics(
unit=None,
count=0, count=0,
min=None, min=None,
max=None, max=None,
@ -171,6 +173,7 @@ class TestComputeDurationByResource(FunctionalTest,
def get_interval(ignore_self, event_filter, period): def get_interval(ignore_self, event_filter, period):
return [ return [
models.Statistics( models.Statistics(
unit=None,
count=0, count=0,
min=None, min=None,
max=None, max=None,

View File

@ -707,6 +707,7 @@ class StatisticsTest(DBTestBase):
(datetime.datetime(2012, 9, 25, 12, 32) (datetime.datetime(2012, 9, 25, 12, 32)
- datetime.datetime(2012, 9, 25, 10, 30)).seconds) - datetime.datetime(2012, 9, 25, 10, 30)).seconds)
assert results.count == 3 assert results.count == 3
assert results.unit == 'GiB'
assert results.min == 8 assert results.min == 8
assert results.max == 10 assert results.max == 10
assert results.sum == 27 assert results.sum == 27
@ -746,6 +747,7 @@ class StatisticsTest(DBTestBase):
self.assertEqual(r.period_start, self.assertEqual(r.period_start,
datetime.datetime(2012, 9, 25, 10, 28)) datetime.datetime(2012, 9, 25, 10, 28))
self.assertEqual(r.count, 2) self.assertEqual(r.count, 2)
self.assertEqual(r.unit, 'GiB')
self.assertEqual(r.avg, 8.5) self.assertEqual(r.avg, 8.5)
self.assertEqual(r.min, 8) self.assertEqual(r.min, 8)
self.assertEqual(r.max, 9) self.assertEqual(r.max, 9)
@ -814,6 +816,7 @@ class StatisticsTest(DBTestBase):
self.assertEqual(r.period_start, self.assertEqual(r.period_start,
datetime.datetime(2012, 9, 25, 10, 28)) datetime.datetime(2012, 9, 25, 10, 28))
self.assertEqual(r.count, 1) self.assertEqual(r.count, 1)
self.assertEqual(r.unit, 'GiB')
self.assertEqual(r.avg, 8) self.assertEqual(r.avg, 8)
self.assertEqual(r.min, 8) self.assertEqual(r.min, 8)
self.assertEqual(r.max, 8) self.assertEqual(r.max, 8)
@ -838,6 +841,7 @@ class StatisticsTest(DBTestBase):
results = list(self.conn.get_meter_statistics(f))[0] results = list(self.conn.get_meter_statistics(f))[0]
self.assertEqual(results.duration, 0) self.assertEqual(results.duration, 0)
assert results.count == 1 assert results.count == 1
assert results.unit == 'GiB'
assert results.min == 6 assert results.min == 6
assert results.max == 6 assert results.max == 6
assert results.sum == 6 assert results.sum == 6
@ -853,6 +857,7 @@ class StatisticsTest(DBTestBase):
(datetime.datetime(2012, 9, 25, 12, 32) (datetime.datetime(2012, 9, 25, 12, 32)
- datetime.datetime(2012, 9, 25, 10, 30)).seconds) - datetime.datetime(2012, 9, 25, 10, 30)).seconds)
assert results.count == 3 assert results.count == 3
assert results.unit == 'GiB'
assert results.min == 5 assert results.min == 5
assert results.max == 7 assert results.max == 7
assert results.sum == 18 assert results.sum == 18