From 97eb9117103b2a3fe052b545f157a35cf7032d70 Mon Sep 17 00:00:00 2001 From: Jason Harvey Date: Tue, 21 Aug 2012 18:47:23 -0700 Subject: [PATCH] Add a random jitter to accounts_activity metric. --- r2/r2/models/subreddit.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/r2/r2/models/subreddit.py b/r2/r2/models/subreddit.py index 59c6d0452..c637ccae7 100644 --- a/r2/r2/models/subreddit.py +++ b/r2/r2/models/subreddit.py @@ -39,6 +39,8 @@ from r2.lib.filters import _force_unicode from r2.lib.db import tdb_cassandra from r2.lib.cache import CL_ONE +import math + import os.path import random @@ -217,7 +219,26 @@ class Subreddit(Thing, Printable): @property def accounts_active(self): - return AccountsActiveBySR.get_count(self) + return self.get_accounts_active()[0] + + def get_accounts_active(self): + fuzzed = False + count = AccountsActiveBySR.get_count(self) + key = 'get_accounts_active-' + self._id36 + + # Fuzz counts having low values, for privacy reasons + if count < 100 and not c.user_is_admin: + fuzzed = True + cached_count = g.cache.get(key) + if not cached_count: + # decay constant is e**(-x / 60) + decay = math.exp(float(-count) / 60) + jitter = round(5 * decay) + count = count + random.randint(0, jitter) + g.cache.set(key, count, time=5*60) + else: + count = cached_count + return count, fuzzed def spammy(self): return self._spam