Commits

Virgil Dupras committed f297435

Visits are sorted according to hits before selected time instead of their respective last hit.

Comments (0)

Files changed (2)

core/model/visit.py

     def __init__(self):
         self._visits = []
         self._ip2visit = {}
-        self._visits_by_lasthit = None
         self.first_hit = None
         self.last_hit = None
     
     #--- Public
     def active_visits_at_time(self, time):
         # XXX do something faster
-        return [v for v in self.visits_by_lasthit if v.is_active_at_time(time)]
+        key = lambda v: v.hit_at_time(time).time
+        visits = (v for v in self._visits if v.is_active_at_time(time))
+        return sorted(visits, key=key, reverse=True)
     
     def add_hit(self, hit):
         visit = self._ip2visit.get(hit.ip, None)
         if self.first_hit is None:
             self.first_hit = hit
         self.last_hit = hit
-        self._visits_by_lasthit = None
     
-    #--- Properties
-    @property
-    def visits_by_lasthit(self):
-        if self._visits_by_lasthit is None:
-            key = lambda v: v.hits[-1].time
-            self._visits_by_lasthit = sorted(self._visits, key=key, reverse=True)
-        return self._visits_by_lasthit
-    

tests/main_test.py

     eq_(app.vtable[0].hitcount, 3)
 
 @with_app(TestApp)
-def test_visit_are_ordered_according_to_last_hit(app):
-    # Visits are ordered according to their last hit's time, not their first
-    lines = [
-        '1.1.1.1 - - [15/Nov/2010:04:20:00 +0000] "-" 200 0 "-" "-"',
-        '1.1.1.2 - - [15/Nov/2010:04:21:00 +0000] "-" 200 0 "-" "-"',
-        '1.1.1.1 - - [15/Nov/2010:04:22:00 +0000] "-" 200 0 "-" "-"',
-    ]
-    app.load_lines(lines)
-    eq_(app.vtable[0].ip, '1.1.1.1')
-
-@with_app(TestApp)
 def test_visits_are_shown_according_to_whether_theyre_active(app):
     # Visits in the vtable are shown based on whether they're active or not.
     lines = [
     # After loading logs, we select the first visit in the table.
     eq_(len(app.htable), 1)
     eq_(app.htable[0].url, '/updates/dupeguru.aiu')
+
+#---
+def app_two_interleaved_visits():
+    app = TestApp()
+    lines = [
+        '1.1.1.1 - - [15/Nov/2010:04:20:00 +0000] "-" 200 0 "-" "-"',
+        '1.1.1.2 - - [15/Nov/2010:04:21:00 +0000] "-" 200 0 "-" "-"',
+        '1.1.1.1 - - [15/Nov/2010:04:22:00 +0000] "-" 200 0 "-" "-"',
+    ]
+    app.load_lines(lines)
+    return app
+
+@with_app(app_two_interleaved_visits)
+def test_visit_are_ordered_according_to_last_hit(app):
+    # Visits are ordered according to their last hit's time, not their first
+    eq_(app.vtable[0].ip, '1.1.1.1')
+
+@with_app(app_two_interleaved_visits)
+def test_visits_are_order_according_to_hits_prior_to_selected_time(app):
+    # Hits happening after selected time are not considered when sorting visits
+    app.tslider.value = app.tslider.max - 1
+    eq_(app.vtable[0].ip, '1.1.1.2')