def look_for_verdicts(): from r2.models import Trial, Jury print "checking all trials for verdicts..." for defendant in Trial.all_defendants(): print "Looking at reddit.com/comments/%s/x" % defendant._id36 v = Trial(defendant).check_verdict() print "Verdict: %r" % v Jury.delete_old(verbose=True, limit=1000)
def look_for_verdicts(): from r2.models import Trial, Jury print "checking all trials for verdicts..." for defendant in Trial.all_defendants(): print "Looking at populr.de/comments/%s/x" % defendant._id36 v = Trial(defendant).check_verdict() print "Verdict: %r" % v Jury.delete_old(verbose=True, limit=1000)
def assign_trial(account, ip, slash16): from r2.models import Jury, Subreddit, Trial from r2.lib.db import queries defendants_voted_upon = [] defendants_assigned_to = [] for jury in Jury.by_account(account): defendants_assigned_to.append(jury._thing2_id) if jury._name != '0': defendants_voted_upon.append(jury._thing2_id) subscribed_sr_ids = Subreddit.user_subreddits(account, ids=True, limit=None) # Pull defendants, except ones which already have lots of juryvotes defs = Trial.all_defendants(quench=True) # Filter out defendants outside this user's subscribed SRs defs = filter(lambda d: d.sr_id in subscribed_sr_ids, defs) # Dictionary of sr_id => SR for all defendants' SRs srs = Subreddit._byID(set([d.sr_id for d in defs])) # Dictionary of sr_id => eligibility bool submit_srs = {} for sr_id, sr in srs.iteritems(): submit_srs[sr_id] = sr.can_submit(account) and not sr._spam # Filter out defendants with ineligible SRs defs = filter(lambda d: submit_srs.get(d.sr_id), defs) likes = queries.get_likes(account, defs) if not g.debug: # Filter out things that the user has upvoted or downvoted defs = filter(lambda d: likes.get((account, d)) is None, defs) # Prefer oldest trials defs.sort(key=lambda x: x._date) for defendant in defs: sr = srs[defendant.sr_id] if voir_dire(account, ip, slash16, defendants_voted_upon, defendant, sr): if defendant._id not in defendants_assigned_to: j = Jury._new(account, defendant) return defendant return None
def assign_trial(account, ip, slash16): from r2.models import Jury, Subreddit, Trial from r2.lib.db import queries defendants_voted_upon = [] defendants_assigned_to = [] for jury in Jury.by_account(account): defendants_assigned_to.append(jury._thing2_id) if jury._name != '0': defendants_voted_upon.append(jury._thing2_id) subscribed_sr_ids = Subreddit.user_subreddits(account, ids=True, limit=None) # Pull defendants, except ones which already have lots of juryvotes defs = Trial.all_defendants(quench=True) # Filter out defendants outside this user's subscribed SRs defs = filter (lambda d: d.sr_id in subscribed_sr_ids, defs) # Dictionary of sr_id => SR for all defendants' SRs srs = Subreddit._byID(set([ d.sr_id for d in defs ])) # Dictionary of sr_id => eligibility bool submit_srs = {} for sr_id, sr in srs.iteritems(): submit_srs[sr_id] = sr.can_submit(account) and not sr._spam # Filter out defendants with ineligible SRs defs = filter (lambda d: submit_srs.get(d.sr_id), defs) likes = queries.get_likes(account, defs) if not g.debug: # Filter out things that the user has upvoted or downvoted defs = filter (lambda d: likes.get((account, d)) is None, defs) # Prefer oldest trials defs.sort(key=lambda x: x._date) for defendant in defs: sr = srs[defendant.sr_id] if voir_dire(account, ip, slash16, defendants_voted_upon, defendant, sr): if defendant._id not in defendants_assigned_to: j = Jury._new(account, defendant) return defendant return None
def populate_spotlight(): raise Exception("this function is broken (re: ip_and_slash16) and pending demolition") from r2.models import Jury from r2.lib.db.thing import NotFound if not (c.user_is_loggedin and c.user.jury_betatester()): g.log.debug("not eligible") return None try: juries_already_on = Jury.by_account(c.user) except NotFound: # This can happen if Jury.delete_old() just so happens to be cleaning # up this user's old Jury rels while they're visiting the front page. # In this unlucky case, just skip the 20% nagging below. juries_already_on = [] # If they're already on a jury, and haven't yet voted, re-show # it every five or so times. if rand.random() < 0.2: unvoted = filter(lambda j: j._name == '0', juries_already_on) defs = [u._thing2 for u in unvoted] active_trials = trial_info(defs) for d in defs: if active_trials.get(d._fullname, False): return d if not g.cache.add("global-jury-key", True, 5): g.log.debug("not yet time to add another juror") return None ip, slash16 = ip_and_slash16(request) jcd = jury_cache_dict(c.user, ip, slash16) if jcd is None: g.cache.delete("global-jury-key") return None if g.cache.get_multi(jcd.keys()) and not g.debug: g.log.debug("recent juror") g.cache.delete("global-jury-key") return None trial = assign_trial(c.user, juries_already_on, ip, slash16) if trial is None: g.log.debug("nothing available") g.cache.delete("global-jury-key") return None for k, v in jcd.iteritems(): g.cache.set(k, True, v) log_text("juryassignment", "%s was just assigned to the jury for %s" % (c.user.name, trial._id36), level="info") return trial
def verdict(self): from r2.models import Jury from r2.lib.utils.trial_utils import update_voting koshers = 0 spams = 0 nones = 0 now = datetime.now(g.tz) defendant_age = now - self.defendant._date if defendant_age.days > 0: return ("jury timeout", None, None) latest_juryvote = None for j in Jury.by_defendant(self.defendant): if j._name == "0": nones += 1 continue # For non-zero votes, update latest_juryvote if latest_juryvote is None: latest_juryvote = j._date else: latest_juryvote = max(latest_juryvote, j._date) if j._name == "1": koshers += 1 elif j._name == "-1": spams += 1 else: raise ValueError("weird jury vote: [%s]" % j._name) # The following trace is temporary; it'll be removed once this # is done via cron job as opposed to manually print "%d koshers, %d spams, %d haven't voted yet" % (koshers, spams, nones) update_voting(self.defendant, koshers, spams) total_votes = koshers + spams if total_votes < 7: g.log.debug("not enough votes yet") return (None, koshers, spams) # Stop showing this in the spotlight box once it has 20 votes if total_votes >= 20: g.cache.set("quench_jurors-" + self.defendant._fullname, True) quenching = True else: quenching = False # If a trial is less than an hour old, and votes are still trickling # in (i.e., there was one in the past five minutes), we're going to # require a nearly unanimous opinion to end the trial without # waiting for more votes. if defendant_age.seconds < 3600 and (now - latest_juryvote).seconds < 300: trickling = True else: trickling = False kosher_pct = float(koshers) / float(total_votes) if kosher_pct < 0.13: return ("guilty", koshers, spams) elif kosher_pct > 0.86: return ("innocent", koshers, spams) elif trickling: g.log.debug("votes still trickling in") return (None, koshers, spams) elif kosher_pct < 0.34: return ("guilty", koshers, spams) elif kosher_pct > 0.66: return ("innocent", koshers, spams) elif not quenching: g.log.debug("not yet quenching") return (None, koshers, spams) # At this point, we're not showing the link to any new jurors, and # the existing jurors haven't changed or submitted votes for several # minutes, so we're not really expecting to get many more votes. # Thus, lower our standards for consensus. elif kosher_pct < 0.3999: return ("guilty", koshers, spams) elif kosher_pct > 0.6001: return ("innocent", koshers, spams) elif total_votes >= 100: # This should never really happen; quenching should kick in # after 20 votes, so new jurors won't be assigned to the # trial. Just in case something goes wrong, close any trials # with more than 100 votes. return ("hung jury", koshers, spams) else: g.log.debug("hung jury, so far") return (None, koshers, spams) # no decision yet; wait for more voters
def verdict(self): from r2.models import Jury from r2.lib.utils.trial_utils import update_voting koshers = 0 spams = 0 nones = 0 now = datetime.now(g.tz) defendant_age = now - self.defendant._date if defendant_age.days > 0: return ("jury timeout", None, None) latest_juryvote = None for j in Jury.by_defendant(self.defendant): if j._name == "0": nones += 1 continue # For non-zero votes, update latest_juryvote if latest_juryvote is None: latest_juryvote = j._date else: latest_juryvote = max(latest_juryvote, j._date) if j._name == "1": koshers += 1 elif j._name == "-1": spams += 1 else: raise ValueError("weird jury vote: [%s]" % j._name) # The following trace is temporary; it'll be removed once this # is done via cron job as opposed to manually print "%d koshers, %d spams, %d haven't voted yet" % (koshers, spams, nones) update_voting(self.defendant, koshers, spams) total_votes = koshers + spams if total_votes < 7: g.log.debug("not enough votes yet") return (None, koshers, spams) # Stop showing this in the spotlight box once it has 20 votes if total_votes >= 20: g.cache.set("quench_jurors-" + self.defendant._fullname, True) quenching = True else: quenching = False # If a trial is less than an hour old, and votes are still trickling # in (i.e., there was one in the past five minutes), we're going to # require a nearly unanimous opinion to end the trial without # waiting for more votes. if defendant_age.seconds < 3600 and (now - latest_juryvote).seconds < 300: trickling = True else: trickling = False kosher_pct = float(koshers) / float(total_votes) if kosher_pct < 0.13: return ("guilty", koshers, spams) elif kosher_pct > 0.86: return ("innocent", koshers, spams) elif trickling: g.log.debug("votes still trickling in") return (None, koshers, spams) elif kosher_pct < 0.34: return ("guilty", koshers, spams) elif kosher_pct > 0.66: return ("innocent", koshers, spams) elif not quenching: g.log.debug("not yet quenching") return (None, koshers, spams) # At this point, we're not showing the link to any new jurors, and # the existing jurors haven't changed or submitted votes for several # minutes, so we're not really expecting to get many more votes. # Thus, lower our standards for consensus. elif kosher_pct < 0.3999: return ("guilty", koshers, spams) elif kosher_pct > 0.6001: return ("innocent", koshers, spams) elif total_votes >= 100: # This should never really happen; quenching should kick in # after 20 votes, so new jurors won't be assigned to the # trial. Just in case something goes wrong, close any trials # with more than 100 votes. return ("hung jury", koshers, spams) else: g.log.debug("hung jury, so far") return (None, koshers, spams ) # no decision yet; wait for more voters