From 8a96d29a3a3526023eef6ecc98e3f413f47af121 Mon Sep 17 00:00:00 2001 From: David Shrewsbury Date: Wed, 21 Nov 2012 10:33:40 -0500 Subject: [PATCH] Add a timestamp to LB stats object. Timestamp will be used to identify when the stats are recorded. Change-Id: I1d73f360055447b0e8cde91aadd622250881ab00 --- libra/common/lbstats.py | 14 ++++++++++++++ tests/test_lbstats.py | 14 ++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/libra/common/lbstats.py b/libra/common/lbstats.py index 5313905f..2c3d6b72 100644 --- a/libra/common/lbstats.py +++ b/libra/common/lbstats.py @@ -12,6 +12,8 @@ # License for the specific language governing permissions and limitations # under the License. +import datetime + class LBStatistics(object): """ Load balancer statistics class. """ @@ -20,6 +22,7 @@ class LBStatistics(object): self.stats = {} self.bytes_out = 0 self.bytes_in = 0 + self.utc_timestamp = datetime.datetime.utcnow() @property def bytes_out(self): @@ -40,3 +43,14 @@ class LBStatistics(object): if not isinstance(value, int): raise TypeError("Must be an integer: '%s'" % 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 diff --git a/tests/test_lbstats.py b/tests/test_lbstats.py index 4371ca68..aab676b3 100644 --- a/tests/test_lbstats.py +++ b/tests/test_lbstats.py @@ -1,3 +1,4 @@ +import datetime import unittest from libra.common.lbstats import LBStatistics @@ -10,8 +11,14 @@ class TestLBStatistics(unittest.TestCase): pass def testInitValues(self): + now = datetime.datetime.utcnow() self.assertEquals(self.stats.bytes_out, 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): self.stats.bytes_in = 99 @@ -24,3 +31,10 @@ class TestLBStatistics(unittest.TestCase): self.assertEquals(self.stats.bytes_out, 100) with self.assertRaises(TypeError): 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" \ No newline at end of file