Delete organic_pos.

This commit is contained in:
bsimpson63
2013-01-15 17:58:09 -05:00
parent a4e0aa022f
commit eb985d12a7
4 changed files with 33 additions and 106 deletions

View File

@@ -21,7 +21,7 @@
###############################################################################
from oauth2 import OAuth2ResourceController, require_oauth2_scope
from reddit_base import RedditController, base_listing, organic_pos
from reddit_base import RedditController, base_listing
from r2.models import *
from r2.models.query_cache import CachedQuery, MergedCachedQuery
@@ -274,82 +274,42 @@ class HotController(FixListing, ListingController):
or (c.user_is_loggedin and c.user.pref_organic))):
spotlight_links = organic.organic_links(c.user)
pos = organic_pos()
if not spotlight_links:
pos = 0
elif pos != 0:
pos = pos % len(spotlight_links)
num_links = organic.organic_length
random.shuffle(spotlight_links)
# If prefs allow it, mix in promoted links and sr discovery content
if c.user.pref_show_sponsors or not c.user.gold:
if g.live_config['sr_discovery_links']:
spotlight_links.extend(g.live_config['sr_discovery_links'])
random.shuffle(spotlight_links)
num_links = len(spotlight_links)
spotlight_links, pos, campaigns_by_link = promote.insert_promoted(spotlight_links,
pos)
# Need to do this again, because if there was a duplicate removed,
# pos might be pointing outside the list.
if not spotlight_links:
pos = 0
elif pos != 0:
pos = pos % len(spotlight_links)
spotlight_links, campaigns_by_link = promote.insert_promoted(spotlight_links)
if not spotlight_links:
return None
# get links in proximity to pos
num_tl = len(spotlight_links)
if num_tl <= 3:
disp_links = spotlight_links
else:
left_side = max(-1, min(num_tl - 3, 8))
disp_links = [spotlight_links[(i + pos) % num_tl]
for i in xrange(-2, left_side)]
disp_links = spotlight_links[-2:] + spotlight_links[:8]
b = IDBuilder(disp_links,
wrap = self.builder_wrapper,
num = num_links,
keep_fn = organic.keep_fresh_links,
skip = True)
try:
vislink = spotlight_links[pos]
except IndexError:
g.log.error("spotlight_links = %r" % spotlight_links)
g.log.error("pos = %d" % pos)
raise
vislink = spotlight_links[0]
s = SpotlightListing(b, spotlight_items = spotlight_links,
visible_item = vislink,
max_num = self.listing_obj.max_num,
max_score = self.listing_obj.max_score).listing()
if vislink not in s.lookup:
# FIXME: spotlight vislink is missing from the items returned
# by the builder.
# This may result in an empty spotlight box.
pass
else:
has_subscribed = c.user.has_subscribed
promo_visible = promote.is_promo(s.lookup[vislink])
if not promo_visible:
prob = g.live_config['spotlight_interest_sub_p'
if has_subscribed else
'spotlight_interest_nosub_p']
if random.random() < prob:
bar = InterestBar(has_subscribed)
s.spotlight_items.insert(pos, bar)
s.visible_item = bar
has_subscribed = c.user.has_subscribed
promo_visible = promote.is_promo(s.lookup[vislink])
if not promo_visible:
prob = g.live_config['spotlight_interest_sub_p'
if has_subscribed else
'spotlight_interest_nosub_p']
if random.random() < prob:
bar = InterestBar(has_subscribed)
s.spotlight_items.insert(0, bar)
s.visible_item = bar
if len(s.things) > 0:
# only pass through a listing if the links made it
# through our builder
organic.update_pos(pos+1)
# add campaign id to promoted links for tracking
for thing in s.things:
thing.campaign = campaigns_by_link.get(thing._fullname, None)

View File

@@ -325,20 +325,6 @@ def set_redditfirst(key, val):
c.cookies['reddit_first'] = Cookie(simplejson.dumps(cookie),
expires=NEVER)
# this cookie is also accessed by organic.js, so changes to the format
# will have to be made there as well
organic_pos_key = 'organic_pos'
def organic_pos():
"organic_pos() -> (calc_date = str(), pos = int())"
pos = get_redditfirst(organic_pos_key, 0)
if not isinstance(pos, int):
pos = 0
return pos
def set_organic_pos(pos):
"set_organic_pos(str(), int()) -> None"
set_redditfirst(organic_pos_key, pos)
def over18():
if c.user.pref_over_18 or c.user_is_admin:

View File

@@ -75,8 +75,6 @@ def cached_organic_links(*sr_ids):
return link_names
def organic_links(user):
from r2.controllers.reddit_base import organic_pos
sr_ids = Subreddit.user_subreddits(user)
# make sure that these are sorted so the cache keys are constant
sr_ids.sort()
@@ -90,8 +88,3 @@ def organic_links(user):
sr_ids.sort()
return cached_organic_links(*sr_ids)[:organic_max_length]
def update_pos(pos):
"Update the user's current position within the cached organic list."
from r2.controllers import reddit_base
reddit_base.set_organic_pos(pos)

View File

@@ -836,7 +836,7 @@ def randomized_promotion_list(user, site):
return [(l, cid) for l, w, cid in promos]
def insert_promoted(link_names, pos, promoted_every_n=5):
def insert_promoted(link_names, promoted_every_n=5):
"""
Inserts promoted links into an existing organic list. Destructive
on `link_names'
@@ -845,55 +845,43 @@ def insert_promoted(link_names, pos, promoted_every_n=5):
promoted_link_names, campaign_ids = zip(*promo_tuples) if promo_tuples else ([], [])
if not promoted_link_names:
return link_names, pos, {}
return link_names, {}
campaigns_by_link = dict(promo_tuples)
# no point in running the builder over more promoted links than
# we'll even use
max_promoted = max(1, len(link_names) / promoted_every_n)
builder = IDBuilder(promoted_link_names, keep_fn=keep_fresh_links,
skip=True)
num=max_promoted, skip=True)
promoted_items = builder.get_items()[0]
focus = None
if promoted_items:
focus = promoted_items[0]._fullname
# insert one promoted item for every N items
for i, item in enumerate(promoted_items):
p = i * (promoted_every_n + 1)
if p > len(link_names):
break
p += pos
if p > len(link_names):
p = p % len(link_names)
link_names.insert(p, item._fullname)
link_names = filter(None, link_names)
if focus:
try:
pos = link_names.index(focus)
except ValueError:
pass
# don't insert one at the head of the list 50% of the time for
# logged in users, and 50% of the time for logged-off users when
# the pool of promoted links is less than 3 (to avoid showing the
# same promoted link to the same person too often)
if ((c.user_is_loggedin or len(promoted_items) < 3) and
random.choice((True, False))):
pos = (pos + 1) % len(link_names)
pos = 1
else:
pos = 0
return list(UniqueIterator(link_names)), pos, campaigns_by_link
if promoted_items:
for i, item in enumerate(promoted_items):
# Note: this is actually promoted every 6th link, not 5th
p = i * (promoted_every_n + 1)
p += pos
link_names.insert(p, item._fullname)
def benchmark_promoted(user, site, pos=0, link_sample=50, attempts=100):
return link_names, campaigns_by_link
def benchmark_promoted(user, site, link_sample=50, attempts=100):
c.user = user
c.site = site
link_names = ["blah%s" % i for i in xrange(link_sample)]
res = {}
for i in xrange(attempts):
names, p, campaigns_by_link = insert_promoted(link_names[::], pos)
name = names[p]
names, campaigns_by_link = insert_promoted(link_names[::])
name = names[0]
res[name] = res.get(name, 0) + 1
res = list(res.iteritems())
res.sort(key=lambda x: x[1], reverse=True)