Permalink
Browse files

Adding RStats, necessary to remove mongodb analytics.

  • Loading branch information...
1 parent b5a0e99 commit 79200d7bc1845cbe95dbc9d4022030260046b8cb @samuelclay committed Mar 25, 2013
Showing with 46 additions and 1 deletion.
  1. +2 −0 apps/rss_feeds/models.py
  2. +40 −0 apps/statistics/rstats.py
  3. +1 −0 settings.py
  4. +1 −1 utils/feed_functions.py
  5. +2 −0 utils/log.py
View
@@ -25,6 +25,7 @@
from apps.rss_feeds.tasks import UpdateFeeds, PushFeeds
from apps.rss_feeds.text_importer import TextImporter
from apps.search.models import SearchStarredStory, SearchFeed
+from apps.statistics.rstats import RStats
from utils import json_functions as json
from utils import feedfinder, feedparser
from utils import urlnorm
@@ -1679,6 +1680,7 @@ def save(self, *args, **kwargs):
if not isinstance(self.exception, basestring):
self.exception = unicode(self.exception)
super(MFeedFetchHistory, self).save(*args, **kwargs)
+ RStats.add('feed_fetch')
@classmethod
def feed_history(cls, feed_id, timezone=None):
View
@@ -0,0 +1,40 @@
+import redis
+import datetime
+from django.conf import settings
+
+
+class RStats:
+
+ STATS_TYPE = {
+ 'page_load': 'PLT',
+ 'feed_fetch': 'FFH',
+ }
+
+ @classmethod
+ def stats_type(cls, name):
+ return cls.STATS_TYPE[name]
+
+ @classmethod
+ def add(cls, name, duration=None):
+ r = redis.Redis(connection_pool=settings.REDIS_STATISTICS_POOL)
+ pipe = r.pipeline()
+ minute = round_time(round_to=60)
+ key = "%s:%s" % (cls.stats_type(name), minute.strftime('%s'))
+ pipe.incr("%s:s" % key)
+ if duration:
+ pipe.incrbyfloat("%s:a" % key, duration)
+ pipe.expireat("%s:a" % key, (minute + datetime.timedelta(days=2)).strftime("%s"))
+ pipe.expireat("%s:s" % key, (minute + datetime.timedelta(days=2)).strftime("%s"))
+ pipe.execute()
+
+def round_time(dt=None, round_to=60):
+ """Round a datetime object to any time laps in seconds
+ dt : datetime.datetime object, default now.
+ round_to : Closest number of seconds to round to, default 1 minute.
+ Author: Thierry Husson 2012 - Use it as you want but don't blame me.
+ """
+ if dt == None : dt = datetime.datetime.now()
+ seconds = (dt - dt.min).seconds
+ rounding = (seconds+round_to/2) // round_to * round_to
+ return dt + datetime.timedelta(0,rounding-seconds,-dt.microsecond)
+
View
@@ -546,6 +546,7 @@ def custom_show_toolbar(request):
REDIS_POOL = redis.ConnectionPool(host=REDIS['host'], port=6379, db=0)
REDIS_STORY_POOL = redis.ConnectionPool(host=REDIS['host'], port=6379, db=1)
REDIS_ANALYTICS_POOL = redis.ConnectionPool(host=REDIS['host'], port=6379, db=2)
+REDIS_STATISTICS_POOL = redis.ConnectionPool(host=REDIS['host'], port=6379, db=3)
REDIS_SESSION_POOL = redis.ConnectionPool(host=REDIS['host'], port=6379, db=5)
JAMMIT = jammit.JammitAssets(NEWSBLUR_DIR)
View
@@ -69,7 +69,7 @@ def append_query_string_to_url(url, **kwargs):
def cache_bust_url(url):
return append_query_string_to_url(url, _=random.randint(0, 10000))
-
+
# From: http://www.poromenos.org/node/87
def levenshtein_distance(first, second):
"""Find the Levenshtein distance between two strings."""
View
@@ -5,6 +5,7 @@
from django.core.handlers.wsgi import WSGIRequest
from django.conf import settings
from utils.user_functions import extract_user_agent
+from apps.statistics.rstats import RStats
class NullHandler(logging.Handler): #exists in python 3.1
def emit(self, record):
@@ -54,6 +55,7 @@ def user(u, msg, request=None):
if path in page_load_paths:
MAnalyticsPageLoad.add(user=u, is_premium=is_premium, platform=platform, path=path,
duration=seconds)
+ RStats.add('page_load', duration=seconds)
def cipher(msg):
shift = len(msg)

0 comments on commit 79200d7

Please sign in to comment.