This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Use a normalized sort for rising on front page and multis.

  • Loading branch information...
alienth committed Feb 20, 2014
1 parent 060de64 commit d4933303465b29f3fb28922aa9e47e13583c1181
Showing with 40 additions and 1 deletion.
  1. +7 −1 r2/r2/controllers/listingcontroller.py
  2. +33 −0 r2/r2/lib/rising.py
@@ -30,7 +30,7 @@
from r2.lib.pages.things import wrap_links
from r2.lib.menus import TimeMenu, SortMenu, RecSortMenu, ProfileSortMenu
from r2.lib.menus import ControversyTimeMenu, menu
-from r2.lib.rising import get_rising
+from r2.lib.rising import get_rising, normalized_rising
from r2.lib.wrapped import Wrapped
from r2.lib.normalized_hot import normalized_hot
from r2.lib.db.thing import Query, Merge, Relations
@@ -393,6 +393,12 @@ class RisingController(NewController):
extra_page_classes = ListingController.extra_page_classes + ['rising-page']
def query(self):
+ if isinstance(c.site, DefaultSR):
+ sr_ids = Subreddit.user_subreddits(c.user)
+ return normalized_rising(sr_ids)
+ elif isinstance(c.site, MultiReddit):
+ return normalized_rising(c.site.kept_sr_ids)
+
return get_rising(c.site)
class BrowseController(ListingWithPromos):
View
@@ -21,10 +21,12 @@
###############################################################################
from datetime import datetime
+import heapq
from pylons import g
from r2.lib import count
+from r2.lib.cache import sgm
from r2.models.link import Link
@@ -61,3 +63,34 @@ def get_all_rising():
def get_rising(sr):
rising = get_all_rising()
return [link for link, score, sr_id in rising if sr.keep_for_rising(sr_id)]
+
+
+def get_rising_tuples(sr_ids):
+ rising = get_all_rising()
+
+ tuples_by_srid = {sr_id: [] for sr_id in sr_ids}
+ top_rising = {}
+
+ for link, score, sr_id in rising:
+ if sr_id not in sr_ids:
+ continue
+
+ if sr_id not in top_rising:
+ top_rising[sr_id] = score
+
+ norm_score = score / top_rising[sr_id]
+ tuples_by_srid[sr_id].append((-norm_score, -score, link))
+
+ return tuples_by_srid
+
+
+def normalized_rising(sr_ids):
+ if not sr_ids:
+ return []
+
+ tuples_by_srid = sgm(g.cache, sr_ids, miss_fn=get_rising_tuples,
+ prefix='normalized_rising', time=g.page_cache_time)
+
+ merged = heapq.merge(*tuples_by_srid.values())
+
+ return [link_name for norm_score, score, link_name in merged]

0 comments on commit d493330

Please sign in to comment.