made account stats log processing more robust and added tests

This commit is contained in:
John Dickinson 2011-08-04 14:19:24 -05:00
parent c9e43e0510
commit 4cb4f57f7c
2 changed files with 31 additions and 9 deletions

View File

@ -34,15 +34,15 @@ class StatsLogProcessor(object):
(account,
container_count,
object_count,
bytes_used) = line.split(',')
except (IndexError, ValueError):
# bad line data
self.logger.debug(_('Bad line data: %s') % repr(line))
continue
bytes_used) = line.split(',')[:4]
account = account.strip('"')
container_count = int(container_count.strip('"'))
object_count = int(object_count.strip('"'))
bytes_used = int(bytes_used.strip('"'))
except (IndexError, ValueError):
# bad line data
self.logger.debug(_('Bad line data: %s') % repr(line))
continue
aggr_key = (account, year, month, day, hour)
d = account_totals.get(aggr_key, {})
d['replica_count'] = d.setdefault('replica_count', 0) + 1

View File

@ -13,16 +13,38 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# TODO: Tests
import unittest
from slogging import stats_processor
class TestStatsProcessor(unittest.TestCase):
def test_placeholder(self):
pass
def test_process(self):
p = stats_processor.StatsLogProcessor({})
test_obj_stream = ['a, 1, 1, 1', 'a, 1, 1, 1']
res = p.process(test_obj_stream, 'foo', 'bar', '2011/03/14/12/baz')
expected = {('a', '2011', '03', '14', '12'):
{'object_count': 2, 'container_count': 2,
'replica_count': 2, 'bytes_used': 2}}
self.assertEquals(res, expected)
def test_process_extra_columns(self):
p = stats_processor.StatsLogProcessor({})
test_obj_stream = ['a, 1, 1, 1, extra']
res = p.process(test_obj_stream, 'foo', 'bar', '2011/03/14/12/baz')
expected = {('a', '2011', '03', '14', '12'):
{'object_count': 1, 'container_count': 1,
'replica_count': 1, 'bytes_used': 1}}
self.assertEquals(res, expected)
def test_process_bad_line(self):
p = stats_processor.StatsLogProcessor({})
test_obj_stream = ['a, 1, 1, 1, extra', '', 'some bad line']
res = p.process(test_obj_stream, 'foo', 'bar', '2011/03/14/12/baz')
expected = {('a', '2011', '03', '14', '12'):
{'object_count': 1, 'container_count': 1,
'replica_count': 1, 'bytes_used': 1}}
self.assertEquals(res, expected)
if __name__ == '__main__':