From 45b83c8da4a250fa0659aacf83ef98e38f1b5412 Mon Sep 17 00:00:00 2001 From: Razvan Deaconescu Date: Sun, 28 Aug 2011 07:46:28 +0300 Subject: [PATCH] ppf/new: Fix date/timestamp usage where required. Status messages require the start time to be known. Status message timestamp is computed by adding the number of status messages (equal to the number of second) to the start time. --- ppf/new/parsing.py | 39 ++++++++++---------- ppf/new/tests/test_parsing.py | 68 ++++++++++++++++++++++------------- 2 files changed, 63 insertions(+), 44 deletions(-) diff --git a/ppf/new/parsing.py b/ppf/new/parsing.py index 18fefb9..824d728 100644 --- a/ppf/new/parsing.py +++ b/ppf/new/parsing.py @@ -39,6 +39,10 @@ ch.setFormatter(formatter) # Add console handler to logger. logger.addHandler(ch) +# Define ONE_SECOND as a datatime.timedelta object. +ONE_SECOND = datetime.timedelta(seconds=1) + + class SessionLogParser(object): """ Top-level class for parsing log file(s) for a given BitTorrent session. @@ -67,12 +71,14 @@ class LibtorrentLogParser(SessionLogParser): libtorrent-rasterbar log folder parser. """ - def __init__(self, path, priority=None): + def __init__(self, path, start_time_string, priority=None): """ If priority == "verbose" parse verbose log files first. Else, parse status file first." """ super(LibtorrentLogParser, self).__init__(path) + self.start_time = datetime.datetime.strptime(start_time_string, + "%Y-%m-%d %H:%M:%S") # to_parse: list of files to be parsed # have_parsed: list of files that have been parsed @@ -80,9 +86,11 @@ class LibtorrentLogParser(SessionLogParser): self.have_parsed = [] self.f = None # handler to file being parsed + + # IP address and port are parsed from verbose log file name. self.verbose_log_ip = None self.verbose_log_port = None - self.log_year = "2009" + self.statmsg_count = 0 for entry in os.listdir(self.path): entry_path = os.path.join(self.path, entry) @@ -258,8 +266,12 @@ class LibtorrentLogParser(SessionLogParser): ps: 1, dht: 8 <> dl: 119.51kb/s, ul: 3.63kb/s <> dld: 1mb, uld: 0mb, size: 698mb <> eta: 1h 39m 37s """ msg = storage.StatusMessage() - # TODO: Fix timestamp. - msg.timestamp = None + + # Timestamp is swarm start_time plus a number of seconds equal to + # the number of messages. It is presumed that each message is + # delivered periodically each second. + msg.timestamp = self.start_time + self.statmsg_count * ONE_SECOND + self.statmsg_count = self.statmsg_count + 1 string_array = re.split("\ *[,<>]+\ *", line) for string in string_array: @@ -324,20 +336,6 @@ class LibtorrentLogParser(SessionLogParser): def parse_peer_status_log_line(self, line): pass - def string_to_timestamp(self, date_string): - """Convert string 'Mon DD HH:MM:SS' to datetime.""" - try: - my_time = time.strptime(date_string + " %s" - % (self.log_year), - "%b %d %H:%M:%S %Y") - my_date = datetime.datetime(my_time[0], my_time[1], - my_time[2], my_time[3], my_time[4], - my_time[5], my_time[6]) - except ValueError: - return None - - return my_date - def parse_verbose_log_line(self, line, message_type): if self.verbose_log_ip == None or self.verbose_log_port == None: # TODO: Initiate verbose_log_ip and verbose_log_port. @@ -361,8 +359,11 @@ class LibtorrentLogParser(SessionLogParser): else: transfer_direction = None + # Convert string 'Mon DD HH:MM:SS' to datetime. parts = re.split(r'[<=>]+', line) - timestamp = self.string_to_timestamp(parts[0].strip()) + date_string = parts[0].strip() + " %s" %(self.start_time.year) + timestamp = datetime.datetime.strptime(date_string, + "%b %d %H:%M:%S %Y") return (timestamp, transfer_direction) diff --git a/ppf/new/tests/test_parsing.py b/ppf/new/tests/test_parsing.py index 371b3ce..7e98e39 100644 --- a/ppf/new/tests/test_parsing.py +++ b/ppf/new/tests/test_parsing.py @@ -136,7 +136,8 @@ class LibtorrentLogParserTest(unittest.TestCase): """ exception_raised = False try: - p = parsing.LibtorrentLogParser(self.no_dir) + p = parsing.LibtorrentLogParser(self.no_dir, + "2009-01-01 12:13:14") except OSError, e: exception_raised = True @@ -154,7 +155,8 @@ class LibtorrentLogParserTest(unittest.TestCase): exception_raised = False try: - p = parsing.LibtorrentLogParser(self.ok_dir) + p = parsing.LibtorrentLogParser(self.ok_dir, + "2009-01-01 12:13:14") except (IOError, OSError, IndexError), e: exception_raised = True @@ -167,7 +169,7 @@ class LibtorrentLogParserTest(unittest.TestCase): # Remove status log file. os.remove(self.ok_status_file_path) - p = parsing.LibtorrentLogParser(self.ok_dir) + p = parsing.LibtorrentLogParser(self.ok_dir, "2009-01-01 12:13:14") current = p.get_current_parsing_filename() value = (current == self.ok_verbose_file_path1 or @@ -184,7 +186,7 @@ class LibtorrentLogParserTest(unittest.TestCase): # to_parse list should consists of 2 entries: the verbose log # files minus the one that is currently being parsed. - p = parsing.LibtorrentLogParser(self.ok_dir) + p = parsing.LibtorrentLogParser(self.ok_dir, "2009-01-01 12:13:14") count = len(p.get_to_parse_list()) self.assertEqual(count, 2) @@ -198,7 +200,7 @@ class LibtorrentLogParserTest(unittest.TestCase): os.remove(self.ok_verbose_file_path2) os.remove(self.ok_verbose_file_path3) - p = parsing.LibtorrentLogParser(self.ok_dir) + p = parsing.LibtorrentLogParser(self.ok_dir, "2009-01-01 12:13:14") current = p.get_current_parsing_filename() value = (current == self.ok_status_file_path) @@ -215,7 +217,7 @@ class LibtorrentLogParserTest(unittest.TestCase): # to_parse list should be empty: no verbose files and the # status file is currently being parsed. - p = parsing.LibtorrentLogParser(self.ok_dir) + p = parsing.LibtorrentLogParser(self.ok_dir, "2009-01-01 12:13:14") count = len(p.get_to_parse_list()) self.assertEqual(count, 0) @@ -224,7 +226,8 @@ class LibtorrentLogParserTest(unittest.TestCase): """Set priority as "status". get_current_parsing_filename() should return status log file. """ - p = parsing.LibtorrentLogParser(self.ok_dir, priority="status") + p = parsing.LibtorrentLogParser(self.ok_dir, + "2009-01-01 12:13:14", priority="status") current = p.get_current_parsing_filename() value = (current == self.ok_status_file_path) @@ -234,7 +237,8 @@ class LibtorrentLogParserTest(unittest.TestCase): """Set priority as "status". get_current_parsing_filename() should return a verbose log file. """ - p = parsing.LibtorrentLogParser(self.ok_dir, priority="verbose") + p = parsing.LibtorrentLogParser(self.ok_dir, + "2009-01-01 12:13:14", priority="verbose") current = p.get_current_parsing_filename() value = (current == self.ok_verbose_file_path1 or @@ -249,7 +253,7 @@ class LibtorrentLogParserTest(unittest.TestCase): # Remove status log file. os.remove(self.ok_status_file_path) - p = parsing.LibtorrentLogParser(self.ok_dir) + p = parsing.LibtorrentLogParser(self.ok_dir, "2009-01-01 12:13:14") msg = p.get_next_message() self.assertEqual(msg, None) @@ -263,7 +267,7 @@ class LibtorrentLogParserTest(unittest.TestCase): os.remove(self.ok_verbose_file_path2) os.remove(self.ok_verbose_file_path3) - p = parsing.LibtorrentLogParser(self.ok_dir) + p = parsing.LibtorrentLogParser(self.ok_dir, "2009-01-01 12:13:14") msg = p.get_next_message() self.assertEqual(msg, None) @@ -272,7 +276,7 @@ class LibtorrentLogParserTest(unittest.TestCase): """Log files are empty. Returned message should be None. """ - p = parsing.LibtorrentLogParser(self.ok_dir) + p = parsing.LibtorrentLogParser(self.ok_dir, "2009-01-01 12:13:14") msg = p.get_next_message() self.assertEqual(msg, None) @@ -285,7 +289,8 @@ class LibtorrentLogParserTest(unittest.TestCase): self.add_bogus_line(self.ok_status_file_path) self.add_verbose_line(self.ok_verbose_file_path1) - p = parsing.LibtorrentLogParser(self.ok_dir, priority="status") + p = parsing.LibtorrentLogParser(self.ok_dir, + "2009-01-01 12:13:14", priority="status") msg = p.get_next_message() self.add_message_to_count(msg) @@ -302,7 +307,8 @@ class LibtorrentLogParserTest(unittest.TestCase): self.add_bogus_line(self.ok_verbose_file_path3) self.add_status_line(self.ok_status_file_path) - p = parsing.LibtorrentLogParser(self.ok_dir, priority="verbose") + p = parsing.LibtorrentLogParser(self.ok_dir, + "2009-01-01 12:13:14", priority="verbose") msg = p.get_next_message() self.add_message_to_count(msg) @@ -313,7 +319,8 @@ class LibtorrentLogParserTest(unittest.TestCase): """Return a status message from status log file.""" self.add_status_line(self.ok_status_file_path) - p = parsing.LibtorrentLogParser(self.ok_dir, priority="verbose") + p = parsing.LibtorrentLogParser(self.ok_dir, + "2009-01-01 12:13:14", priority="verbose") msg = p.get_next_message() self.add_message_to_count(msg) @@ -325,7 +332,8 @@ class LibtorrentLogParserTest(unittest.TestCase): self.add_status_line(self.ok_status_file_path) self.add_status_line(self.ok_status_file_path) - p = parsing.LibtorrentLogParser(self.ok_dir, priority="verbose") + p = parsing.LibtorrentLogParser(self.ok_dir, + "2009-01-01 12:13:14", priority="verbose") msg1 = p.get_next_message() msg2 = p.get_next_message() @@ -339,7 +347,8 @@ class LibtorrentLogParserTest(unittest.TestCase): self.add_status_line(self.ok_status_file_path) self.add_status_line(self.ok_verbose_file_path1) - p = parsing.LibtorrentLogParser(self.ok_dir, priority="verbose") + p = parsing.LibtorrentLogParser(self.ok_dir, + "2009-01-01 12:13:14", priority="verbose") msg1 = p.get_next_message() msg2 = p.get_next_message() @@ -352,7 +361,8 @@ class LibtorrentLogParserTest(unittest.TestCase): """Return a peer status message from status log file.""" self.add_peer_status_line(self.ok_status_file_path) - p = parsing.LibtorrentLogParser(self.ok_dir, priority="verbose") + p = parsing.LibtorrentLogParser(self.ok_dir, + "2009-01-01 12:13:14", priority="verbose") msg = p.get_next_message() self.add_message_to_count(msg) @@ -364,7 +374,8 @@ class LibtorrentLogParserTest(unittest.TestCase): self.add_peer_status_line(self.ok_status_file_path) self.add_peer_status_line(self.ok_status_file_path) - p = parsing.LibtorrentLogParser(self.ok_dir, priority="verbose") + p = parsing.LibtorrentLogParser(self.ok_dir, + "2009-01-01 12:13:14", priority="verbose") msg1 = p.get_next_message() msg2 = p.get_next_message() @@ -378,7 +389,8 @@ class LibtorrentLogParserTest(unittest.TestCase): self.add_peer_status_line(self.ok_status_file_path) self.add_peer_status_line(self.ok_verbose_file_path1) - p = parsing.LibtorrentLogParser(self.ok_dir, priority="verbose") + p = parsing.LibtorrentLogParser(self.ok_dir, + "2009-01-01 12:13:14", priority="verbose") msg1 = p.get_next_message() msg2 = p.get_next_message() @@ -391,7 +403,8 @@ class LibtorrentLogParserTest(unittest.TestCase): """Return a verbose message from verbose log file.""" self.add_verbose_line(self.ok_verbose_file_path1) - p = parsing.LibtorrentLogParser(self.ok_dir, priority="status") + p = parsing.LibtorrentLogParser(self.ok_dir, + "2009-01-01 12:13:14", priority="status") msg = p.get_next_message() self.add_message_to_count(msg) @@ -403,7 +416,8 @@ class LibtorrentLogParserTest(unittest.TestCase): self.add_verbose_line(self.ok_verbose_file_path1) self.add_verbose_line(self.ok_verbose_file_path1) - p = parsing.LibtorrentLogParser(self.ok_dir, priority="verbose") + p = parsing.LibtorrentLogParser(self.ok_dir, + "2009-01-01 12:13:14", priority="verbose") msg1 = p.get_next_message() msg2 = p.get_next_message() @@ -417,7 +431,8 @@ class LibtorrentLogParserTest(unittest.TestCase): self.add_verbose_line(self.ok_verbose_file_path1) self.add_verbose_line(self.ok_verbose_file_path2) - p = parsing.LibtorrentLogParser(self.ok_dir, priority="verbose") + p = parsing.LibtorrentLogParser(self.ok_dir, + "2009-01-01 12:13:14", priority="verbose") msg1 = p.get_next_message() msg2 = p.get_next_message() @@ -432,7 +447,8 @@ class LibtorrentLogParserTest(unittest.TestCase): self.add_peer_status_line(self.ok_status_file_path) self.add_verbose_line(self.ok_status_file_path) - p = parsing.LibtorrentLogParser(self.ok_dir, priority="verbose") + p = parsing.LibtorrentLogParser(self.ok_dir, + "2009-01-01 12:13:14", priority="verbose") msg1 = p.get_next_message() msg2 = p.get_next_message() msg3 = p.get_next_message() @@ -454,7 +470,8 @@ class LibtorrentLogParserTest(unittest.TestCase): self.add_verbose_line(self.ok_verbose_file_path2) self.add_verbose_line(self.ok_verbose_file_path2) - p = parsing.LibtorrentLogParser(self.ok_dir, priority="verbose") + p = parsing.LibtorrentLogParser(self.ok_dir, + "2009-01-01 12:13:14", priority="verbose") msg1 = p.get_next_message() msg2 = p.get_next_message() msg3 = p.get_next_message() @@ -482,7 +499,8 @@ class LibtorrentLogParserTest(unittest.TestCase): self.add_verbose_line(self.ok_verbose_file_path2) self.add_verbose_line(self.ok_verbose_file_path2) - p = parsing.LibtorrentLogParser(self.ok_dir, priority="verbose") + p = parsing.LibtorrentLogParser(self.ok_dir, + "2009-01-01 12:13:14", priority="verbose") msg1 = p.get_next_message() msg2 = p.get_next_message() msg3 = p.get_next_message() -- 2.20.1