Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert ids/timestamps to int + add utils/tests
- Loading branch information
1 parent
aebc929
commit a89fa46
Showing
4 changed files
with
65 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import unittest | ||
from utils.this_queue import OurQueue | ||
|
||
# From JJ's ktm repo: https://github.com/jilljenn/ktm | ||
|
||
class TestOurQueue(unittest.TestCase): | ||
|
||
def test_simple(self): | ||
q = OurQueue() | ||
q.push(0) | ||
q.push(0.8 * 3600 * 24) | ||
q.push(5 * 3600 * 24) | ||
q.push(40 * 3600 * 24) | ||
self.assertEqual(q.get_counters(40 * 3600 * 24), [4, 1, 1, 1, 1]) | ||
|
||
def test_complex(self): | ||
q = OurQueue() | ||
q.push(0) | ||
q.push(10) | ||
q.push(3599) | ||
q.push(3600) | ||
q.push(3601) | ||
q.push(3600 * 24) | ||
q.push(3600 * 24 + 1) | ||
q.push(3600 * 24 * 7) | ||
q.push(3600 * 24 * 7 + 1) | ||
q.push(3600 * 24 * 7 * 30) | ||
q.push(3600 * 24 * 7 * 30 + 1) | ||
self.assertEqual(q.get_counters(3600 * 24 * 7 * 30 + 1), [11, 2, 2, 2, 2]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
class OurQueue: | ||
""" | ||
A queue for counting efficiently the number of events within time windows. | ||
Complexity: | ||
All operators in amortized O(W) time where W is the number of windows. | ||
From JJ's KTM repository: https://github.com/jilljenn/ktm. | ||
""" | ||
def __init__(self): | ||
#self.now = None | ||
self.queue = [] | ||
self.window_lengths = [3600 * 24 * 30, 3600 * 24 * 7, 3600 * 24, 3600] | ||
self.cursors = [0] * len(self.window_lengths) | ||
|
||
def __len__(self): | ||
return len(self.queue) | ||
|
||
def get_counters(self, t): | ||
self.update_cursors(t) | ||
return [len(self.queue)] + [len(self.queue) - cursor | ||
for cursor in self.cursors] | ||
|
||
def push(self, time): | ||
self.queue.append(time) | ||
|
||
def update_cursors(self, t): | ||
for pos, length in enumerate(self.window_lengths): | ||
while (self.cursors[pos] < len(self.queue) and | ||
t - self.queue[self.cursors[pos]] >= length): | ||
self.cursors[pos] += 1 |