Skip to content

Commit

Permalink
Better trending carousel sorting on homepage (#7394)
Browse files Browse the repository at this point in the history
Co-authored-by: Christian Clauss <cclauss@me.com>
Co-authored-by: Drini Cami <cdrini@gmail.com>
  • Loading branch information
3 people authored Jan 12, 2023
1 parent faf27ac commit c041082
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
13 changes: 11 additions & 2 deletions openlibrary/core/bookshelves.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ def most_logged_books(
since: date | None = None,
page: int = 1,
fetch: bool = False,
sort_by_count: bool = True,
minimum: int = 0,
) -> list:
"""Returns a ranked list of work OLIDs (in the form of an integer --
i.e. OL123W would be 123) which have been most logged by
Expand All @@ -106,8 +108,15 @@ def most_logged_books(
where = 'WHERE bookshelf_id' + ('=$shelf_id' if shelf_id else ' IS NOT NULL ')
if since:
where += ' AND created >= $since'
query = f'select work_id, count(*) as cnt from bookshelves_books {where}'
query += ' group by work_id order by cnt desc limit $limit offset $offset'
group_by = 'group by work_id'
if minimum:
group_by += f" HAVING COUNT(*) > {minimum}"
order_by = 'order by cnt desc' if sort_by_count else ''
query = f"""
select work_id, count(*) as cnt
from bookshelves_books
{where} {group_by} {order_by}
limit $limit offset $offset"""
logger.info("Query: %s", query)
data = {'shelf_id': shelf_id, 'limit': limit, 'offset': offset, 'since': since}
logged_books = list(oldb.query(query, vars=data))
Expand Down
6 changes: 5 additions & 1 deletion openlibrary/plugins/openlibrary/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,18 @@ def GET(self, period="/daily"):
from openlibrary.views.loanstats import SINCE_DAYS

period = period[1:] # remove slash
i = web.input(page=1, limit=100, days=0, hours=0)
i = web.input(
page=1, limit=100, days=0, hours=0, sort_by_count=False, minimum=0
)
days = SINCE_DAYS.get(period, int(i.days))
works = get_trending_books(
since_days=days,
since_hours=int(i.hours),
limit=int(i.limit),
page=int(i.page),
books_only=True,
sort_by_count=not i.sort_by_count == 'false',
minimum=i.minimum,
)
result = {
'query': f"/trending/{period}",
Expand Down
2 changes: 1 addition & 1 deletion openlibrary/templates/home/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
$:render_template("home/welcome", test=test)

$if not test:
$:render_template("books/custom_carousel", books=get_trending_books(books_only=True), title=_('Trending Books'), url="/trending/daily", test=test, load_more={"url": "/trending/hours.json?hours=1", "mode": "page", "limit": 18})
$:render_template("books/custom_carousel", books=get_trending_books(books_only=True, since_days=0, since_hours=24, minimum=3, sort_by_count=False), title=_('Trending Books'), url="/trending/daily", test=test, load_more={"url": "/trending/hours.json?hours=24&minimum=3&sort_by_count=false", "mode": "page", "limit": 18})

$:macros.QueryCarousel(query="ddc:8* first_publish_year:[* TO 1950] publish_year:[2000 TO *] NOT public_scan_b:false", title=_('Classic Books'), key="public_domain", url="/read", sort='random.hourly')

Expand Down
12 changes: 11 additions & 1 deletion openlibrary/views/loanstats.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,15 @@ def reading_log_summary():


@public
def get_trending_books(since_days=1, since_hours=0, limit=18, page=1, books_only=False):
def get_trending_books(
since_days=1,
since_hours=0,
limit=18,
page=1,
books_only=False,
sort_by_count=True,
minimum=None,
):
logged_books = (
Bookshelves.fetch(get_activity_stream(limit=limit, page=page)) # i.e. "now"
if (since_days == 0 and since_hours == 0)
Expand All @@ -56,6 +64,8 @@ def get_trending_books(since_days=1, since_hours=0, limit=18, page=1, books_only
limit=limit,
page=page,
fetch=True,
sort_by_count=sort_by_count,
minimum=minimum,
)
)
return (
Expand Down

0 comments on commit c041082

Please sign in to comment.