From 5e36e4604956c63e19f983ac8c8adb96be1c7e7c Mon Sep 17 00:00:00 2001 From: Samuel Clay Date: Thu, 29 May 2014 13:30:41 -0700 Subject: [PATCH] Count feed_id starred stories when totals don't match. --- apps/oauth/views.py | 2 +- apps/reader/views.py | 6 ++++-- apps/rss_feeds/models.py | 38 +++++++++++++++++++++++++++----------- 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/apps/oauth/views.py b/apps/oauth/views.py index 2c883d039..efaa2cac5 100644 --- a/apps/oauth/views.py +++ b/apps/oauth/views.py @@ -732,7 +732,7 @@ def api_save_new_story(request): } story = MStarredStory.objects.create(**story_db) logging.user(request, "~FCStarring by ~SBIFTTT~SN: ~SB%s~SN in ~SB%s" % (story_db['story_title'][:50], original_feed and original_feed)) - MStarredStoryCounts.count_tags_for_user(user.pk) + MStarredStoryCounts.count_for_user(user.pk) except OperationError: logging.user(request, "~FCAlready starred by ~SBIFTTT~SN: ~SB%s" % (story_db['story_title'][:50])) pass diff --git a/apps/reader/views.py b/apps/reader/views.py index d859e198a..891a14138 100644 --- a/apps/reader/views.py +++ b/apps/reader/views.py @@ -3,6 +3,7 @@ import time import boto import redis import requests +import random import zlib from django.shortcuts import get_object_or_404 from django.shortcuts import render @@ -1850,8 +1851,9 @@ def _mark_story_as_starred(request): MStarredStoryCounts.adjust_count(request.user.pk, tag=tag, amount=1) for tag in removed_user_tags: MStarredStoryCounts.adjust_count(request.user.pk, tag=tag, amount=-1) - - MStarredStoryCounts.schedule_count_tags_for_user(request.user.pk) + + if random.random() < 0.01: + MStarredStoryCounts.schedule_count_tags_for_user(request.user.pk) MStarredStoryCounts.count_for_user(request.user.pk, total_only=True) starred_counts = MStarredStoryCounts.user_counts(request.user.pk) diff --git a/apps/rss_feeds/models.py b/apps/rss_feeds/models.py index 907e6e1a9..54be7caea 100644 --- a/apps/rss_feeds/models.py +++ b/apps/rss_feeds/models.py @@ -2215,17 +2215,24 @@ class MStarredStoryCounts(mongo.Document): for c in counts], key=lambda x: (x.get('tag', '') or '').lower()) - if counts == [] and try_counting: + total = 0 + feed_total = 0 + for c in counts: + if not c['tag'] and not c['feed_id']: + total = c['count'] + if c['feed_id']: + feed_total += c['count'] + + if try_counting and total != feed_total: + user = User.objects.get(pk=user_id) + logging.user(user, "~FC~SBCounting~SN saved stories (%s total vs. %s counted)..." % + (total, feed_total)) cls.count_for_user(user_id) return cls.user_counts(user_id, include_total=include_total, try_counting=False) if include_total: - for c in counts: - if not c['tag'] and not c['feed_id']: - return counts, c['count'] - return counts, 0 - + return counts, total return counts @classmethod @@ -2243,8 +2250,8 @@ class MStarredStoryCounts(mongo.Document): user_feeds = cls.count_feeds_for_user(user_id) total_stories_count = MStarredStory.objects(user_id=user_id).count() - cls.objects(user_id=user_id, tag="").update_one(set__count=total_stories_count, - upsert=True) + cls.objects(user_id=user_id, tag=None, feed_id=None).update_one(set__count=total_stories_count, + upsert=True) return dict(total=total_stories_count, tags=user_tags, feeds=user_feeds) @@ -2265,9 +2272,18 @@ class MStarredStoryCounts(mongo.Document): @classmethod def count_feeds_for_user(cls, user_id): all_feeds = MStarredStory.objects(user_id=user_id).item_frequencies('story_feed_id') - user_feeds = [(k, v) for k, v in all_feeds.items() if int(v) > 0 and k] - - for feed_id, count in dict(user_feeds).items(): + user_feeds = dict([(k, v) for k, v in all_feeds.items() if v]) + + # Clean up None'd and 0'd feed_ids, so they can be counted against the total + if user_feeds.get(None, False): + user_feeds[0] = user_feeds.get(0, 0) + user_feeds[0] += user_feeds.get(None) + del user_feeds[None] + if user_feeds.get(0, False): + user_feeds[-1] = user_feeds.get(0, 0) + del user_feeds[0] + + for feed_id, count in user_feeds.items(): cls.objects(user_id=user_id, feed_id=feed_id, slug="feed:%s" % feed_id).update_one(set__count=count,