def update_stats(config):
    today = datetime.date.today()
    yesterday_start = (today - datetime.timedelta(days=2))
    yesterday_end = (today - datetime.timedelta(days=1))
    last_month_end = (today.replace(day=1) - datetime.timedelta(days=1))
    last_month_start = last_month_end.replace(day=1)
    ranges = {
        'yesterday': (yesterday_start, yesterday_end),
        'last_month': (last_month_start, last_month_end),
    }

    stats = {"timestamp": int(time.time())}
    def run_stats(f):
        start_time = time.time()
        stats.update(f(config, ranges))
        end_time = time.time()
        print >> sys.stderr, '%s took %0.2f seconds.' % (f.__name__, end_time - start_time)

    print >> sys.stderr, 'recalculating reddit stats...'
    run_stats(subreddit_stats)
    run_stats(vote_stats)
    run_stats(traffic_stats)
    run_stats(ga_stats)
    print >> sys.stderr, 'finished:', stats
    NamedGlobals.set('about_reddit_stats', stats)
Example #2
0
def update_stats(config):
    today = datetime.date.today()
    yesterday_start = (today - datetime.timedelta(days=2))
    yesterday_end = (today - datetime.timedelta(days=1))
    last_month_end = (today.replace(day=1) - datetime.timedelta(days=1))
    last_month_start = last_month_end.replace(day=1)
    ranges = {
        'yesterday': (yesterday_start, yesterday_end),
        'last_month': (last_month_start, last_month_end),
    }

    stats = {"timestamp": int(time.time())}

    def run_stats(f):
        start_time = time.time()
        stats.update(f(config, ranges))
        end_time = time.time()
        print >> sys.stderr, '%s took %0.2f seconds.' % (f.__name__,
                                                         end_time - start_time)

    print >> sys.stderr, 'recalculating reddit stats...'
    run_stats(subreddit_stats)
    run_stats(vote_stats)
    run_stats(traffic_stats)
    run_stats(ga_stats)
    print >> sys.stderr, 'finished:', stats
    NamedGlobals.set('about_reddit_stats', stats)
Example #3
0
def update_trending_subreddits():
    try:
        trending_sr = Subreddit._by_name(g.config['trending_sr'])
    except NotFound:
        g.log.info("Unknown trending subreddit %r or trending_sr config "
                   "not set. Not updating.", g.config['trending_sr'])
        return

    iq = iter(trending_sr.get_links('new', 'all'))
    link = Thing._by_fullname(next(iq), data=True)
    subreddit_names = _SUBREDDIT_RE.findall(link.title)
    trending_data = {
        'subreddit_names': subreddit_names,
        'permalink': link.make_permalink(trending_sr),
        'link_id': link._id,
    }
    NamedGlobals.set(TRENDING_SUBREDDITS_KEY, trending_data)
    g.log.debug("Trending subreddit data set to %r", trending_data)
def has_timer_expired():
    # note: this only checks if the timer has been marked as expired, it doesn't
    # actually check its value (that's done in check_timer)
    key = _EXPIRED_KEY()
    val = g.thebuttoncache.get(key)
    if val is None:
        try:
            val = NamedGlobals.get(key)
        except NotFoundException:
            # has never been set, set the key
            val = False
            NamedGlobals.set(key, val)
        # update the cache
        g.thebuttoncache.set(key, val)

    if val:
        return _deserialize_datetime(val)

    return val
Example #5
0
def has_timer_expired():
    # note: this only checks if the timer has been marked as expired, it doesn't
    # actually check its value (that's done in check_timer)
    key = _EXPIRED_KEY()
    val = g.thebuttoncache.get(key)
    if val is None:
        try:
            val = NamedGlobals.get(key)
        except NotFoundException:
            # has never been set, set the key
            val = False
            NamedGlobals.set(key, val)
        # update the cache
        g.thebuttoncache.set(key, val)

    if val:
        return _deserialize_datetime(val)

    return val
Example #6
0
 def GET_index(self):
     quote = self._get_quote()
     images = self._get_images()
     stats = NamedGlobals.get("about_reddit_stats", None)
     content = About(
         quote=quote,
         images=images,
         stats=stats,
         events=g.plugins["about"].timeline_data,
         sites=g.plugins["about"].sites_data,
     )
     return AboutPage("about-main", _("we power awesome communities."), _("about reddit"), content).render()
Example #7
0
def update_trending_subreddits():
    try:
        trending_sr = Subreddit._by_name(g.config['trending_sr'])
    except NotFound:
        g.log.info("Unknown trending subreddit %r or trending_sr config "
                   "not set. Not updating.", g.config['trending_sr'])
        return

    link = _get_newest_link(trending_sr)
    if not link:
        g.log.info("Unable to find active link in subreddit %r. Not updating.",
                   g.config['trending_sr'])
        return

    subreddit_names = _SUBREDDIT_RE.findall(link.title)
    trending_data = {
        'subreddit_names': subreddit_names,
        'permalink': link.make_permalink(trending_sr),
        'link_id': link._id,
    }
    NamedGlobals.set(TRENDING_SUBREDDITS_KEY, trending_data)
    g.log.debug("Trending subreddit data set to %r", trending_data)
Example #8
0
def update_trending_subreddits():
    try:
        trending_sr = Subreddit._by_name(g.config['trending_sr'])
    except NotFound:
        g.log.info(
            "Unknown trending subreddit %r or trending_sr config "
            "not set. Not updating.", g.config['trending_sr'])
        return

    link = _get_newest_link(trending_sr)
    if not link:
        g.log.info("Unable to find active link in subreddit %r. Not updating.",
                   g.config['trending_sr'])
        return

    subreddit_names = _SUBREDDIT_RE.findall(link.title)
    trending_data = {
        'subreddit_names': subreddit_names,
        'permalink': link.make_permalink(trending_sr),
        'link_id': link._id,
    }
    NamedGlobals.set(TRENDING_SUBREDDITS_KEY, trending_data)
    g.log.debug("Trending subreddit data set to %r", trending_data)
Example #9
0
def get_current_press():
    key = _CURRENT_PRESS_KEY()
    val = g.thebuttoncache.get(key)
    if val is None:
        try:
            val = NamedGlobals.get(key)
        except NotFoundException:
            val = NONE
        g.thebuttoncache.set(key, val)

    if val == NONE:
        return None
    elif val:
        return _deserialize_datetime(val)
def update_stats(config):
    today = datetime.date.today()
    yesterday_start = today - datetime.timedelta(days=2)
    yesterday_end = today - datetime.timedelta(days=1)
    last_month_end = today.replace(day=1) - datetime.timedelta(days=1)
    last_month_start = last_month_end.replace(day=1)
    ranges = {"yesterday": (yesterday_start, yesterday_end), "last_month": (last_month_start, last_month_end)}

    stats = {"timestamp": int(time.time())}

    def run_stats(f):
        start_time = time.time()
        stats.update(f(config, ranges))
        end_time = time.time()
        print >>sys.stderr, "%s took %0.2f seconds." % (f.__name__, end_time - start_time)

    print >>sys.stderr, "recalculating reddit stats..."
    run_stats(subreddit_stats)
    run_stats(vote_stats)
    run_stats(traffic_stats)
    run_stats(ga_stats)
    print >>sys.stderr, "finished:", stats
    NamedGlobals.set("about_reddit_stats", stats)
def get_current_press():
    key = _CURRENT_PRESS_KEY()
    val = g.thebuttoncache.get(key)
    if val is None:
        try:
            val = NamedGlobals.get(key)
        except NotFoundException:
            val = NONE
        g.thebuttoncache.set(key, val)

    if val == NONE:
        return None
    elif val:
        return _deserialize_datetime(val)
 def GET_index(self):
     quote = self._get_quote()
     images = self._get_images()
     stats = NamedGlobals.get('about_reddit_stats', None)
     c.js_preload.set('#images', images)
     content = About(
         quote=quote,
         images=images,
         stats=stats,
         events=g.plugins['about'].timeline_data,
         sites=g.plugins['about'].sites_data,
     )
     return AboutPage(
         content_id='about-main',
         title_msg=_('we power awesome communities.'),
         pagename=_('about reddit'),
         content=content,
     ).render()
Example #13
0
 def GET_index(self):
     quote = self._get_quote()
     images = self._get_images()
     stats = NamedGlobals.get('about_reddit_stats', None)
     c.js_preload.set('#images', images)
     content = About(
         quote=quote,
         images=images,
         stats=stats,
         events=g.plugins['about'].timeline_data,
         sites=g.plugins['about'].sites_data,
     )
     return AboutPage(
         content_id='about-main',
         title_msg=_('we power awesome communities.'),
         pagename=_('about reddit'),
         content=content,
     ).render()
Example #14
0
def get_trending_subreddits():
    return NamedGlobals.get(TRENDING_SUBREDDITS_KEY, None)
Example #15
0
def health_check():
    """Calculate the number of seconds since promotions were last updated"""
    return time.time() - int(NamedGlobals.get(PROMO_HEALTH_KEY, default=0))
Example #16
0
def _mark_promos_updated():
    NamedGlobals.set(PROMO_HEALTH_KEY, time.time())
Example #17
0
def set_current_press(press_time):
    key = _CURRENT_PRESS_KEY()
    serialized = _serialize_datetime(press_time)
    NamedGlobals.set(key, serialized)
    g.thebuttoncache.set(key, serialized)
    g.thebuttoncache.incr(_PARTICIPANTS_KEY())
Example #18
0
def mark_timer_expired(expiration_time):
    key = _EXPIRED_KEY()
    serialized = _serialize_datetime(expiration_time)
    NamedGlobals.set(key, serialized)
    g.thebuttoncache.set(key, serialized)
def mark_timer_expired(expiration_time):
    key = _EXPIRED_KEY()
    serialized = _serialize_datetime(expiration_time)
    NamedGlobals.set(key, serialized)
    g.thebuttoncache.set(key, serialized)
Example #20
0
def _mark_promos_updated():
    NamedGlobals.set(PROMO_HEALTH_KEY, time.time())
Example #21
0
def health_check():
    return NamedGlobals.get(PROMO_HEALTH_KEY, default=0)
def set_current_press(press_time):
    key = _CURRENT_PRESS_KEY()
    serialized = _serialize_datetime(press_time)
    NamedGlobals.set(key, serialized)
    g.thebuttoncache.set(key, serialized)
    g.thebuttoncache.incr(_PARTICIPANTS_KEY())
Example #23
0
def health_check():
    """Calculate the number of seconds since promotions were last updated"""
    return time.time() - int(NamedGlobals.get(PROMO_HEALTH_KEY, default=0))
Example #24
0
def get_trending_subreddits():
    return NamedGlobals.get(TRENDING_SUBREDDITS_KEY, None)