[tor-commits] [nyx/master] Change days_since() to day_count()

atagar at torproject.org atagar at torproject.org
Tue May 5 16:18:22 UTC 2015


commit f58e1c2788bfc42890bd8eb5dbe8cbde0b452dda
Author: Damian Johnson <atagar at torproject.org>
Date:   Tue May 5 09:06:59 2015 -0700

    Change days_since() to day_count()
    
    Earlier I changed the behavior of days_since() to work as advertised, but
    actually having an absolute count as it previously did is a lot less error
    prone. For instance, the change introduced a narrow timing bug with
    deduplication.
    
    Reverting to our previous behavior, but with a more accruate name.
---
 nyx/log_panel.py |   20 +++++++++-----------
 nyx/util/log.py  |   30 +++++++++++++-----------------
 2 files changed, 22 insertions(+), 28 deletions(-)

diff --git a/nyx/log_panel.py b/nyx/log_panel.py
index 626a42f..a76340d 100644
--- a/nyx/log_panel.py
+++ b/nyx/log_panel.py
@@ -43,8 +43,6 @@ CONFIG = conf.config_dict('nyx', {
   'attr.log_color': {},
 }, conf_handler)
 
-TIMEZONE_OFFSET = time.altzone if time.localtime()[8] else time.timezone
-
 # The height of the drawn content is estimated based on the last time we redrew
 # the panel. It's chiefly used for scrolling and the bar indicating its
 # position. Letting the estimate be too inaccurate results in a display bug, so
@@ -294,7 +292,7 @@ class LogPanel(panel.Panel, threading.Thread):
 
       # group entries by date, filtering out those that aren't visible
 
-      days_ago_to_entries = {}
+      day_to_entries, today = {}, nyx.util.log.day_count(time.time())
 
       for entry in event_log:
         if entry.is_duplicate and not CONFIG['features.log.showDuplicateEntries']:
@@ -302,20 +300,20 @@ class LogPanel(panel.Panel, threading.Thread):
         elif not self._filter.match(entry.display_message):
           continue  # filter doesn't match log message
 
-        days_ago_to_entries.setdefault(entry.days_since(), []).append(entry)
+        day_to_entries.setdefault(entry.day_count(), []).append(entry)
 
-      for days_ago in sorted(days_ago_to_entries.keys()):
-        if days_ago == 0:
-          for entry in days_ago_to_entries[days_ago]:
+      for day in sorted(day_to_entries.keys(), reverse = True):
+        if day == today:
+          for entry in day_to_entries[day]:
             y = self._draw_entry(x, y, width, entry)
         else:
           original_y, y = y, y + 1
 
-          for entry in days_ago_to_entries[days_ago]:
+          for entry in day_to_entries[day]:
             y = self._draw_entry(x, y, width, entry)
 
           ui_tools.draw_box(self, original_y, x - 1, width - x + 1, y - original_y + 1, curses.A_BOLD, 'yellow')
-          time_label = time.strftime(' %B %d, %Y ', time.localtime(days_ago_to_entries[days_ago][0].timestamp))
+          time_label = time.strftime(' %B %d, %Y ', time.localtime(day_to_entries[day][0].timestamp))
           self.addstr(original_y, x + 1, time_label, curses.A_BOLD, curses.A_BOLD, 'yellow')
 
           y += 1
@@ -414,10 +412,10 @@ class LogPanel(panel.Panel, threading.Thread):
     responsive if additions are less frequent.
     """
 
-    last_ran, last_day = -1, int((time.time() - TIMEZONE_OFFSET) / 86400)
+    last_ran, last_day = -1, nyx.util.log.day_count(time.time())
 
     while not self._halt:
-      current_day = int((time.time() - TIMEZONE_OFFSET) / 86400)
+      current_day = nyx.util.log.day_count(time.time())
       time_since_reset = time.time() - last_ran
       max_log_update_rate = CONFIG['features.log.maxRefreshRate'] / 1000.0
 
diff --git a/nyx/util/log.py b/nyx/util/log.py
index f224789..ca2b520 100644
--- a/nyx/util/log.py
+++ b/nyx/util/log.py
@@ -24,25 +24,21 @@ except ImportError:
   from stem.util.lru_cache import lru_cache
 
 TOR_RUNLEVELS = ['DEBUG', 'INFO', 'NOTICE', 'WARN', 'ERR']
+TIMEZONE_OFFSET = time.altzone if time.localtime()[8] else time.timezone
 CONFIG = stem.util.conf.config_dict('nyx', {'tor.chroot': ''})
 
 
-def days_since(timestamp):
+def day_count(timestamp):
   """
-  Provides the number of days since a given unix timestamp, by local time.
-  Daybreaks are rolled over at midnight. For instance, 5pm today would report
-  zero, and 5pm yesterday would report one.
+  Provoides a unique number for the day a given timestamp falls on, by local
+  time. Daybreaks are rolled over at midnight.
 
-  :param int timestamp: unix timestamp to provide time since
+  :param int timestamp: unix timestamp to provide a count for
 
-  :reutrns: **int** with the number of days since this event
+  :reutrns: **int** for the day it falls on
   """
 
-  # unix timestamp for today at midnight
-
-  midnight = time.mktime(datetime.datetime.today().date().timetuple())
-
-  return int((midnight - timestamp) / 86400)
+  return int((timestamp - TIMEZONE_OFFSET) / 86400)
 
 
 def log_file_path(controller):
@@ -202,10 +198,10 @@ class LogGroup(object):
   def add(self, entry):
     with self._lock:
       duplicate = None
-      our_day = entry.days_since()
+      our_day = entry.day_count()
 
       for existing_entry in self._entries:
-        if self._group_by_day and our_day != existing_entry.days_since():
+        if self._group_by_day and our_day != existing_entry.day_count():
           break
         elif entry.is_duplicate_of(existing_entry):
           duplicate = existing_entry
@@ -298,14 +294,14 @@ class LogEntry(object):
 
     return False
 
-  def days_since(self):
+  def day_count(self):
     """
-    Provides the number of days since this event, by local time.
+    Provides the day this event occured on by local time.
 
-    :reutrns: **int** with the number of days since this event
+    :reutrns: **int** with the day this occured on
     """
 
-    return days_since(self.timestamp)
+    return day_count(self.timestamp)
 
   def __eq__(self, other):
     if isinstance(other, LogEntry):



More information about the tor-commits mailing list