Added build_deep_stats_model, made other small tweaks
This commit is contained in:
@@ -47,8 +47,8 @@ class StatsModel:
|
||||
self.total_posts = self.calculate_total_posts()
|
||||
self.total_original_posts = self.calculate_total_original_posts()
|
||||
self.total_original_post_notes = self.calculate_total_original_post_notes()
|
||||
self.total_original_post_notes_by_month_and_year = self.calculate_total_original_post_notes_by_month_and_year()
|
||||
self.total_original_post_notes_by_qtr_and_year = self.calculate_total_original_post_notes_by_qtr_and_year()
|
||||
self.total_original_post_notes_by_month_and_year = self.calculate_total_original_post_notes_by_month_and_year('note_count')
|
||||
self.total_original_post_notes_by_qtr_and_year = self.calculate_total_original_post_notes_by_qtr_and_year('note_count')
|
||||
self.most_popular_tags = self.determine_most_popular_tags('note_count')
|
||||
|
||||
def calculate_total_posts(self) -> int:
|
||||
@@ -63,11 +63,12 @@ class StatsModel:
|
||||
total += self.original_post_map[post_key]['note_count']
|
||||
return total
|
||||
|
||||
def calculate_total_original_post_notes_by_month_and_year(self) -> Dict[str, Any]:
|
||||
def calculate_total_original_post_notes_by_month_and_year(self, sort_key: str) -> Dict[str, Any]:
|
||||
# https://docs.python.org/3/library/collections.html#defaultdict-objects
|
||||
date_map: Dict[str, Any] = {}
|
||||
date_map = defaultdict(lambda: {'note_count': 0,
|
||||
'post_count': 0},
|
||||
'post_count': 0,
|
||||
'month_popular_tags': {}},
|
||||
date_map)
|
||||
|
||||
# Gathering the results.
|
||||
@@ -81,13 +82,34 @@ class StatsModel:
|
||||
sts['year_month'] = post_date_key
|
||||
sts['post_count'] += 1
|
||||
sts['note_count'] += post['note_count']
|
||||
for tag in post['tags']:
|
||||
if tag not in sts['month_popular_tags']:
|
||||
sts['month_popular_tags'][tag] = {}
|
||||
sts['month_popular_tags'][tag]['note_count'] = 0
|
||||
sts['month_popular_tags'][tag]['post_count'] = 0
|
||||
tgs = sts['month_popular_tags'][tag]
|
||||
tgs['post_count'] += 1
|
||||
tgs['note_count'] += post['note_count']
|
||||
|
||||
|
||||
# Results postprocessing.
|
||||
for date in date_map:
|
||||
# Overall notes to posts ratio.
|
||||
sts = date_map[date]
|
||||
post_count = sts['post_count']
|
||||
note_count = sts['note_count']
|
||||
sts['notes_to_posts_ratio'] = note_count / post_count
|
||||
# By month + tag notes to posts ratio.
|
||||
tgs = sts['month_popular_tags']
|
||||
for tag in tgs:
|
||||
stg = tgs[tag]
|
||||
stg['tag'] = tag
|
||||
t_post_count = stg['post_count']
|
||||
t_note_count = stg['note_count']
|
||||
stg['notes_to_posts_ratio'] = t_note_count / t_post_count
|
||||
tgs_list = sorted(list(tgs.values()), key=itemgetter(sort_key),
|
||||
reverse=True)
|
||||
sts['month_popular_tags'] = tgs_list
|
||||
|
||||
return date_map
|
||||
|
||||
@@ -119,10 +141,10 @@ class StatsModel:
|
||||
return sorted(list(tag_dict.values()), key=itemgetter(sort_key),
|
||||
reverse=True)
|
||||
|
||||
def calculate_total_original_post_notes_by_qtr_and_year(self) -> Dict[str, Any]:
|
||||
total_original_post_notes_by_month_and_year: Dict[str, int] = self.total_original_post_notes_by_month_and_year
|
||||
def calculate_total_original_post_notes_by_qtr_and_year(self, sort_key: str) -> Dict[str, Any]:
|
||||
total_original_post_notes_by_month_and_year: Dict[str, Any] = self.total_original_post_notes_by_month_and_year
|
||||
if not total_original_post_notes_by_month_and_year:
|
||||
total_original_post_notes_by_month_and_year = self.calculate_total_original_post_notes_by_month_and_year()
|
||||
total_original_post_notes_by_month_and_year = self.calculate_total_original_post_notes_by_month_and_year(sort_key)
|
||||
self.total_original_post_notes_by_month_and_year = total_original_post_notes_by_month_and_year.copy()
|
||||
|
||||
quarter_map: Dict[str, Any] = {}
|
||||
|
||||
Reference in New Issue
Block a user