Read percentages

This commit is contained in:
Samuel Clay 2016-10-06 19:33:09 -07:00
parent 88d0a58d40
commit 44e59c2b63

View file

@ -1469,6 +1469,7 @@ class Feed(models.Model):
feed_title_to_id[feed.feed_title] = feed_id
seen_feeds.add(feed.feed_title)
if feed_id not in popularity:
read_pct, story_count = feed.well_read_score()
popularity[feed_id] = {
'feed_title': feed.feed_title,
'feed_url': feed.feed_link,
@ -1476,6 +1477,8 @@ class Feed(models.Model):
'feed_id': feed.pk,
'story_ids': [],
'authors': {},
'read_pct': read_pct,
'story_count': story_count
}
popularity[feed_id]['story_ids'].append(story_hash)
@ -1496,7 +1499,6 @@ class Feed(models.Model):
'stories': [],
}
authors = feed['authors'][story['story_authors']]
authors['count'] += 1
seen = False
for seen_story in authors['stories']:
if seen_story['url'] == story['story_permalink']:
@ -1508,6 +1510,7 @@ class Feed(models.Model):
'url': story['story_permalink'],
'date': story['story_date'],
})
authors['count'] += 1
if seen: continue # Don't recount tags
for tag in story['story_tags']:
if tag not in authors['tags']:
@ -1516,12 +1519,28 @@ class Feed(models.Model):
sorted_authors = sorted(feed['authors'].values(), key=lambda x: x['count'])
feed['authors'] = sorted_authors
pprint(sorted_popularity)
# pprint(sorted_popularity)
return sorted_popularity
def well_read_score(self):
# Average percentage of stories read vs published across recently active subscribers
r = redis.Redis(connection_pool=settings.REDIS_STORY_HASH_POOL)
p = r.pipeline()
rs_keys = r.keys('RS:*:%s' % self.pk) # Expensive!
for user_rs in rs_keys:
p.scard(user_rs)
counts = p.execute()
story_count = MStory.objects(story_feed_id=self.pk,
story_date__gte=self.unread_cutoff).count()
if len(counts) and story_count:
average_pct = (sum(counts) / float(len(counts))) / float(story_count)
else:
average_pct = 0
return average_pct, story_count
@classmethod
def xls_query_popularity(cls, queries, limit):
@ -1529,6 +1548,7 @@ class Feed(models.Model):
workbook = xlsxwriter.Workbook('NewsBlurPopularity.xlsx')
bold = workbook.add_format({'bold': 1})
date_format = workbook.add_format({'num_format': 'mmm d yyyy'})
unread_format = workbook.add_format({'font_color': '#E0E0E0'})
if isinstance(queries, str):
queries = [q.strip() for q in queries.split(',')]
@ -1539,21 +1559,25 @@ class Feed(models.Model):
worksheet.write(0, col, 'Feed', bold)
worksheet.write(0, col+1, 'Feed URL', bold)
worksheet.write(0, col+2, 'Subscribers', bold)
worksheet.write(0, col+3, 'Author', bold)
worksheet.write(0, col+4, 'Story Title', bold)
worksheet.write(0, col+5, 'Story URL', bold)
worksheet.write(0, col+6, 'Story Date', bold)
worksheet.write(0, col+7, 'Tag', bold)
worksheet.write(0, col+8, 'Tag Count', bold)
worksheet.write(0, col+3, 'Read %', bold)
worksheet.write(0, col+4, '# Stories 30d', bold)
worksheet.write(0, col+5, 'Author', bold)
worksheet.write(0, col+6, 'Story Title', bold)
worksheet.write(0, col+7, 'Story URL', bold)
worksheet.write(0, col+8, 'Story Date', bold)
worksheet.write(0, col+9, 'Tag', bold)
worksheet.write(0, col+10, 'Tag Count', bold)
worksheet.set_column(col, col, 15)
worksheet.set_column(col+1, col+1, 20)
worksheet.set_column(col+2, col+2, 8)
worksheet.set_column(col+3, col+3, 15)
worksheet.set_column(col+4, col+4, 30)
worksheet.set_column(col+5, col+5, 20)
worksheet.set_column(col+6, col+6, 10)
worksheet.set_column(col+7, col+7, 15)
worksheet.set_column(col+8, col+8, 8)
worksheet.set_column(col+3, col+3, 8)
worksheet.set_column(col+4, col+4, 8)
worksheet.set_column(col+5, col+5, 15)
worksheet.set_column(col+6, col+6, 30)
worksheet.set_column(col+7, col+7, 20)
worksheet.set_column(col+8, col+8, 10)
worksheet.set_column(col+9, col+9, 15)
worksheet.set_column(col+10, col+10, 8)
popularity = cls.query_popularity(query, limit=limit)
worksheet.write(row, col, query)
@ -1561,16 +1585,22 @@ class Feed(models.Model):
worksheet.write(row, col+0, feed['feed_title'])
worksheet.write_url(row, col+1, feed['feed_url'])
worksheet.write(row, col+2, feed['num_subscribers'])
worksheet.write(row, col+3, feed['read_pct'])
worksheet.write(row, col+4, feed['story_count'])
worksheet.conditional_format(row, col+3, row, col+4, {'type': 'cell',
'criteria': '==',
'value': 0,
'format': unread_format})
for author in feed['authors']:
worksheet.write(row, col+3, author['name'])
worksheet.write(row, col+5, author['name'])
for story in author['stories']:
worksheet.write(row, col+4, story['title'])
worksheet.write_url(row, col+5, story['url'])
worksheet.write_datetime(row, col+6, story['date'], date_format)
worksheet.write(row, col+6, story['title'])
worksheet.write_url(row, col+7, story['url'])
worksheet.write_datetime(row, col+8, story['date'], date_format)
row += 1
for tag, count in author['tags'].items():
worksheet.write(row, col+7, tag)
worksheet.write(row, col+8, count)
worksheet.write(row, col+9, tag)
worksheet.write(row, col+10, count)
row += 1
workbook.close()