Skip to content

Commit

Permalink
Add EntryCounts.unread/unimportant. #283 / #342
Browse files Browse the repository at this point in the history
  • Loading branch information
chenthur authored Jun 22, 2024
1 parent 4cd624c commit c867823
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 9 deletions.
3 changes: 1 addition & 2 deletions src/reader/_app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ def add_entry():
FEED_SORT_NATIVE = {'title', 'added'}
FEED_SORT_FANCY = {
'important': lambda counts: counts.important,
# TODO: an unread/unimportant property would be nice
'unimportant':lambda counts: counts.unimportant,
'unread': lambda counts: counts.total - counts.read,
# TODO: if we keep these average intervals, properties for them might be nice too
'avg1m': lambda counts: counts.averages[0],
Expand All @@ -435,7 +435,6 @@ def feeds():

updates_enabled = request.args.get('updates-enabled')
updates_enabled = {None: None, 'no': False, 'yes': True}[updates_enabled]

sort = request.args.get('sort', 'title')
assert sort in FEED_SORT_ALL

Expand Down
5 changes: 2 additions & 3 deletions src/reader/_storage/_entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ def get_entry_counts(
context.update(new_context)

row = exactly_one(self.get_db().execute(str(query), context))

return EntryCounts(*row[:4], row[4:7]) # type: ignore[call-arg]
return EntryCounts(*row[:5], row[5:8]) # type: ignore[call-arg]

@wrap_exceptions()
def set_entry_read(
Expand Down Expand Up @@ -593,6 +592,7 @@ def get_entry_counts_query(
'count(*)',
'coalesce(sum(read == 1), 0)',
'coalesce(sum(important == 1), 0)',
'coalesce(sum(important == 0), 0)',
"""
coalesce(
sum(
Expand All @@ -606,7 +606,6 @@ def get_entry_counts_query(
.FROM("entries_filtered")
.JOIN("entries USING (id, feed)")
)

# one CTE / period + HAVING in the CTE is a tiny bit faster than
# one CTE + WHERE in the SELECT

Expand Down
2 changes: 1 addition & 1 deletion src/reader/_storage/_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ def search_entry_counts(

context = dict(query=query, **query_context)
row = exactly_one(self.get_db().execute(str(sql_query), context))
return EntrySearchCounts(*row[:4], row[4:7]) # type: ignore[call-arg]
return EntrySearchCounts(*row[:5], row[5:8]) # type: ignore[call-arg]


def make_search_entries_query(
Expand Down
6 changes: 6 additions & 0 deletions src/reader/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,9 @@ class EntryCounts(_namedtuple_compat):
#: Number of important entries.
important: int | None = None

#: Number of unimportant entries.
unimportant: int | None = None

#: Number of entries that have enclosures.
has_enclosures: int | None = None

Expand Down Expand Up @@ -935,6 +938,9 @@ class EntrySearchCounts(_namedtuple_compat):
#: Number of important entries.
important: int | None = None

#: Number of unimportant entries.
unimportant: int | None = None

#: Number of entries that have enclosures.
has_enclosures: int | None = None

Expand Down
14 changes: 14 additions & 0 deletions tests/test_reader_counts.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def entries_per_day(month, quarter, year):
9,
read=2,
important=4,
unimportant=0,
has_enclosures=8,
averages=entries_per_day(2, 3, 7),
),
Expand All @@ -76,6 +77,7 @@ def entries_per_day(month, quarter, year):
1,
read=0,
important=0,
unimportant=0,
has_enclosures=0,
averages=entries_per_day(0, 0, 1),
),
Expand All @@ -86,6 +88,7 @@ def entries_per_day(month, quarter, year):
8,
read=2,
important=4,
unimportant=0,
has_enclosures=8,
averages=entries_per_day(2, 3, 6),
),
Expand All @@ -96,6 +99,7 @@ def entries_per_day(month, quarter, year):
1,
read=0,
important=0,
unimportant=0,
has_enclosures=0,
averages=entries_per_day(0, 0, 1),
),
Expand All @@ -106,6 +110,7 @@ def entries_per_day(month, quarter, year):
1,
read=1,
important=1,
unimportant=0,
has_enclosures=1,
averages=entries_per_day(0, 0, 0),
),
Expand All @@ -116,6 +121,7 @@ def entries_per_day(month, quarter, year):
1,
read=0,
important=1,
unimportant=0,
has_enclosures=1,
averages=entries_per_day(0, 0, 1),
),
Expand All @@ -126,6 +132,7 @@ def entries_per_day(month, quarter, year):
1,
read=0,
important=0,
unimportant=0,
has_enclosures=1,
averages=entries_per_day(0, 0, 1),
),
Expand All @@ -136,6 +143,7 @@ def entries_per_day(month, quarter, year):
2,
read=2,
important=2,
unimportant=0,
has_enclosures=2,
averages=entries_per_day(1, 1, 1),
),
Expand All @@ -146,6 +154,7 @@ def entries_per_day(month, quarter, year):
7,
read=0,
important=2,
unimportant=0,
has_enclosures=6,
averages=entries_per_day(1, 2, 6),
),
Expand All @@ -156,6 +165,7 @@ def entries_per_day(month, quarter, year):
4,
read=2,
important=4,
unimportant=0,
has_enclosures=4,
averages=entries_per_day(1, 1, 2),
),
Expand All @@ -166,6 +176,7 @@ def entries_per_day(month, quarter, year):
5,
read=0,
important=0,
unimportant=0,
has_enclosures=4,
averages=entries_per_day(1, 2, 5),
),
Expand All @@ -176,6 +187,7 @@ def entries_per_day(month, quarter, year):
8,
read=2,
important=4,
unimportant=0,
has_enclosures=8,
averages=entries_per_day(2, 3, 6),
),
Expand All @@ -186,6 +198,7 @@ def entries_per_day(month, quarter, year):
1,
read=0,
important=0,
unimportant=0,
has_enclosures=0,
averages=entries_per_day(0, 0, 1),
),
Expand All @@ -196,6 +209,7 @@ def entries_per_day(month, quarter, year):
1,
read=0,
important=0,
unimportant=0,
has_enclosures=0,
averages=entries_per_day(0, 0, 1),
),
Expand Down
6 changes: 3 additions & 3 deletions tests/test_reader_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ def test_search_entries_basic(reader, sort):
# TODO: the asserts below look parametrizable

assert list(search('zero')) == []
assert search_counts('zero') == EntrySearchCounts(0, 0, 0, 0, (0, 0, 0))
assert search_counts('zero') == EntrySearchCounts(0, 0, 0, 0, 0, (0, 0, 0))
assert list(search('one')) == [
EntrySearchResult(
feed.url,
Expand All @@ -611,7 +611,7 @@ def test_search_entries_basic(reader, sort):
},
)
]
assert search_counts('one') == EntrySearchCounts(1, 0, 0, 0, (0, 0, 0))
assert search_counts('one') == EntrySearchCounts(1, 0, 0, 0,0,(0, 0, 0))
assert list(search('two')) == [
EntrySearchResult(
feed.url,
Expand Down Expand Up @@ -684,7 +684,7 @@ def test_search_entries_basic(reader, sort):
),
]
}
assert search_counts('summary') == EntrySearchCounts(3, 0, 0, 0, (0, 0, 0))
assert search_counts('summary') == EntrySearchCounts(3, 0, 0, 0, 0,(0, 0, 0))


# search_entries() filtering is tested in test_reader.py::test_entries_filtering{,_error}
Expand Down

0 comments on commit c867823

Please sign in to comment.