def monthly_story_count(self, value: MonthlyStoryCount): if value is None: self.monthly_story_count_data = None self.dryness = None else: self.monthly_story_count_data = value.dump() self.dryness = value.dryness()
def _update_feed_monthly_story_count(feed, new_story_objects): monthly_story_count = MonthlyStoryCount.load(feed.monthly_story_count_data) for story in new_story_objects: if not story.dt_published: continue year, month = story.dt_published.year, story.dt_published.month if not MonthlyStoryCount.is_valid_year_month(year, month): continue count = monthly_story_count.get(year, month) monthly_story_count.put(year, month, count + 1) feed.monthly_story_count = monthly_story_count feed.save()
def refresh_feed_monthly_story_count(feed_id): count_sql = """ SELECT CAST(EXTRACT(YEAR FROM dt_published) AS INTEGER) AS year, CAST(EXTRACT(MONTH FROM dt_published) AS INTEGER) AS month, count(1) as count FROM rssant_api_story WHERE feed_id = %s AND dt_published IS NOT NULL GROUP BY CAST(EXTRACT(YEAR FROM dt_published) AS INTEGER), CAST(EXTRACT(MONTH FROM dt_published) AS INTEGER); """ with connection.cursor() as cursor: cursor.execute(count_sql, [feed_id]) rows = list(cursor.fetchall()) items = [] for row in rows: year, month, count = map(int, row) if 1970 <= year <= 9999: items.append((year, month, count)) monthly_story_count = MonthlyStoryCount(items) with transaction.atomic(): feed = Feed.objects.filter(pk=feed_id).get() feed.monthly_story_count = monthly_story_count feed.save()
def monthly_story_count(self): return MonthlyStoryCount.load(self.monthly_story_count_data)