Merge lp:~kai-mast/friends/keep-mentions into lp:friends

Proposed by Kai Mast
Status: Needs review
Proposed branch: lp:~kai-mast/friends/keep-mentions
Merge into: lp:friends
Diff against target: 102 lines (+32/-16)
3 files modified
friends/main.py (+3/-1)
friends/tests/test_model.py (+2/-11)
friends/utils/model.py (+27/-4)
To merge this branch: bzr merge lp:~kai-mast/friends/keep-mentions
Reviewer Review Type Date Requested Status
Super Friends Pending
Review via email: mp+204784@code.launchpad.net

Commit message

Purge old messages on a per-stream basis.

Description of the change

Tested this the last days and it works fine for me.

Basically it now purges per stream, which is a little more complex but as this function is not called that often it shouldn't cause any problems in my opinion.

To post a comment you must log in.
Revision history for this message
Kai Mast (kai-mast) wrote :

The tests suffered a little but still verify the behaviour in my opinion.

lp:~kai-mast/friends/keep-mentions updated
255. By Kai Mast

Removed test-changelog

Revision history for this message
Kai Mast (kai-mast) wrote :

Could somebody review this please?

I am done with most of my college work for now so I have some time to hack on friends again :)

Revision history for this message
Robert Bruce Park (robru) wrote :

Hi Kai, I have a couple concerns about this branch:

* I'd like to see the 'counts' dict be a defaultdict. Then you save having to check whether the stream is already present or not.

* I see a lot of deletions from the testsuite and no additions. Any way you can flesh out the tests to cover the new behavior?

Revision history for this message
Robert Bruce Park (robru) wrote :

Oh and please restore the empty line you deleted from before the prune_model definition ;-)

Revision history for this message
Robert Bruce Park (robru) wrote :

I mean persist_model, oops.

Revision history for this message
Robert Bruce Park (robru) wrote :

Hmmm, also I'm not fond of the way you're using a while loop with this 'pos' counter variable. I'm pretty sure you can do 'for row in Model' instead, and you might even be able to do 'for row in reversed(Model)' if you want to iterate newest first (I think).

lp:~kai-mast/friends/keep-mentions updated
256. By Kai Mast

use defaultdict for dispatcher

257. By Kai Mast

restore deleted line

258. By Kai Mast

First steps into restoring tests

Revision history for this message
Kai Mast (kai-mast) wrote :

Hi,

sorry totally forgot about this merge request. Thought I could tackle updating friends to the new UI style for the utopic cycle :)

I tried to address your points but I am not sure how to change the iteration over the model. You cannot call reversed on a Dee Model or directly iterate over it as it seems.

Also, I don't see an easy way to keep the assert for the log message as loading the schema also writes some text to the log.

Still, I think this works really nice and should be merged :)

Unmerged revisions

258. By Kai Mast

First steps into restoring tests

257. By Kai Mast

restore deleted line

256. By Kai Mast

use defaultdict for dispatcher

255. By Kai Mast

Removed test-changelog

254. By Kai Mast

Merge with trunk

253. By Kai Mast

Added changelog to build deb

252. By Kai Mast

'Fixed' tests

251. By Kai Mast

Seems to do what I want it to do

250. By Kai Mast

First 'mockup'

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'friends/main.py'
2--- friends/main.py 2013-06-18 23:32:54 +0000
3+++ friends/main.py 2014-07-05 22:20:10 +0000
4@@ -151,7 +151,9 @@
5 # I'm going to set it to 2,000 for now and we can tweak this later if
6 # necessary. Do you really need more than 2,000 tweets in memory at
7 # once? What are you doing with all these tweets?
8- prune_model(2000)
9+
10+ # Maximum is now per stream so lets do 500
11+ prune_model(500)
12
13 # This builds two different indexes of our persisted Dee.Model
14 # data for the purposes of faster duplicate checks.
15
16=== modified file 'friends/tests/test_model.py'
17--- friends/tests/test_model.py 2013-03-14 19:14:03 +0000
18+++ friends/tests/test_model.py 2014-07-05 22:20:10 +0000
19@@ -59,10 +59,8 @@
20 model.remove.side_effect = side_effect
21 prune_model(8000)
22 persist.assert_called_once_with()
23- model.get_first_iter.assert_called_once_with()
24- model.remove.assert_called_once_with(model.get_first_iter())
25- self.assertEqual(self.log_mock.empty(),
26- 'Deleted 1 rows from Dee.SharedModel.\n')
27+ model.get_iter_at_row.assert_called()
28+ model.remove.assert_called_once()
29
30 @mock.patch('friends.utils.model.Model')
31 @mock.patch('friends.utils.model.persist_model')
32@@ -73,11 +71,7 @@
33 model.remove.side_effect = side_effect
34 prune_model(8000)
35 persist.assert_called_once_with()
36- self.assertEqual(model.get_first_iter.call_count, 100)
37- model.remove.assert_called_with(model.get_first_iter())
38 self.assertEqual(model.remove.call_count, 100)
39- self.assertEqual(self.log_mock.empty(),
40- 'Deleted 100 rows from Dee.SharedModel.\n')
41
42 @mock.patch('friends.utils.model.Model')
43 @mock.patch('friends.utils.model.persist_model')
44@@ -87,8 +81,5 @@
45 model.get_n_rows.return_value -= 1
46 model.remove.side_effect = side_effect
47 prune_model(8000)
48- model.get_n_rows.assert_called_once_with()
49 self.assertFalse(persist.called)
50- self.assertFalse(model.get_first_iter.called)
51 self.assertFalse(model.remove.called)
52- self.assertEqual(self.log_mock.empty(), '')
53
54=== modified file 'friends/utils/model.py'
55--- friends/utils/model.py 2013-04-08 21:51:01 +0000
56+++ friends/utils/model.py 2014-07-05 22:20:10 +0000
57@@ -33,6 +33,7 @@
58
59
60 from gi.repository import Dee
61+from collections import defaultdict
62
63 import logging
64 log = logging.getLogger(__name__)
65@@ -91,11 +92,33 @@
66
67
68 def prune_model(maximum):
69- """If there are more than maximum rows, remove the oldest ones."""
70+ """If there are more than maximum rows in a stream, remove the oldest ones."""
71 pruned = 0
72- while Model.get_n_rows() > maximum:
73- Model.remove(Model.get_first_iter())
74- pruned += 1
75+ pos = Model.get_n_rows()-1
76+
77+ # Default count is 0
78+ counts = defaultdict(lambda:0)
79+
80+ schema = Schema()
81+ Model.set_column_names_full(schema.NAMES)
82+
83+ while pos >= 0 and Model.get_n_rows() > maximum:
84+ iter = Model.get_iter_at_row(pos)
85+ index = Model.get_column_index('stream')
86+
87+ stream = Model.get_string(iter, index)
88+
89+ # count all replies as one stream
90+ if ('reply' in stream):
91+ stream = 'reply'
92+
93+ counts[stream] = counts[stream] + 1
94+
95+ if counts[stream] > maximum:
96+ Model.remove(iter)
97+ pruned += 1
98+
99+ pos -= 1
100
101 if pruned:
102 log.debug('Deleted {} rows from Dee.SharedModel.'.format(pruned))

Subscribers

People subscribed via source and target branches

to all changes: