Add a timestamp to LB stats object.

Timestamp will be used to identify when the stats are recorded.

Change-Id: I1d73f360055447b0e8cde91aadd622250881ab00
This commit is contained in:
David Shrewsbury 2012-11-21 10:33:40 -05:00
parent 7e3ac2cd2e
commit 8a96d29a3a
2 changed files with 28 additions and 0 deletions

View File

@ -12,6 +12,8 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import datetime
class LBStatistics(object): class LBStatistics(object):
""" Load balancer statistics class. """ """ Load balancer statistics class. """
@ -20,6 +22,7 @@ class LBStatistics(object):
self.stats = {} self.stats = {}
self.bytes_out = 0 self.bytes_out = 0
self.bytes_in = 0 self.bytes_in = 0
self.utc_timestamp = datetime.datetime.utcnow()
@property @property
def bytes_out(self): def bytes_out(self):
@ -40,3 +43,14 @@ class LBStatistics(object):
if not isinstance(value, int): if not isinstance(value, int):
raise TypeError("Must be an integer: '%s'" % value) raise TypeError("Must be an integer: '%s'" % value)
self.stats['bytes_in'] = value self.stats['bytes_in'] = value
@property
def utc_timestamp(self):
""" UTC timestamp for when these statistics are generated. """
return self._utc_ts
@utc_timestamp.setter
def utc_timestamp(self, value):
if not isinstance(value, datetime.datetime):
raise TypeError("Must be a datetime.datetime: '%s'" % value)
self._utc_ts = value

View File

@ -1,3 +1,4 @@
import datetime
import unittest import unittest
from libra.common.lbstats import LBStatistics from libra.common.lbstats import LBStatistics
@ -10,8 +11,14 @@ class TestLBStatistics(unittest.TestCase):
pass pass
def testInitValues(self): def testInitValues(self):
now = datetime.datetime.utcnow()
self.assertEquals(self.stats.bytes_out, 0) self.assertEquals(self.stats.bytes_out, 0)
self.assertEquals(self.stats.bytes_in, 0) self.assertEquals(self.stats.bytes_in, 0)
ts = self.stats.utc_timestamp
self.assertEquals(ts.year, now.year)
self.assertEquals(ts.month, now.month)
self.assertEquals(ts.day, now.day)
self.assertEquals(ts.hour, now.hour)
def testSetBytesIn(self): def testSetBytesIn(self):
self.stats.bytes_in = 99 self.stats.bytes_in = 99
@ -24,3 +31,10 @@ class TestLBStatistics(unittest.TestCase):
self.assertEquals(self.stats.bytes_out, 100) self.assertEquals(self.stats.bytes_out, 100)
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
self.stats.bytes_out = "NaN" self.stats.bytes_out = "NaN"
def testSetUTCTimestamp(self):
ts = datetime.datetime.utcnow()
self.stats.utc_timestamp = ts
self.assertEquals(self.stats.utc_timestamp, ts)
with self.assertRaises(TypeError):
self.stats.utc_timestamp = "2012-01-01 12:00:00"