Added a popular tags feature, greatly cleaned up code, commented spots
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
from collections import defaultdict
|
||||
from dataclasses import dataclass, field
|
||||
from datetime import datetime
|
||||
from operator import itemgetter
|
||||
from typing import Any, Dict, List
|
||||
|
||||
|
||||
@@ -34,11 +36,15 @@ class StatsModel:
|
||||
total_original_post_notes_by_month_and_year: Dict[str, int] = field(
|
||||
init=False)
|
||||
|
||||
# Tags ranked from most popular to least popular by notes.
|
||||
most_popular_tags: List[Dict[str, Any]] = field(init=False)
|
||||
|
||||
def __post_init__(self):
|
||||
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.most_popular_tags = self.determine_most_popular_tags()
|
||||
|
||||
def calculate_total_posts(self) -> int:
|
||||
return len(self.original_post_map) + len(self.unoriginal_post_map)
|
||||
@@ -65,3 +71,27 @@ class StatsModel:
|
||||
else:
|
||||
date_map[post_date_key] = post['note_count']
|
||||
return date_map
|
||||
|
||||
def determine_most_popular_tags(self) -> List[Dict[str, Any]]:
|
||||
tag_dict: Dict[str, Any] = {}
|
||||
tag_dict = defaultdict(lambda : {'note_count': 0,
|
||||
'post_count': 0},
|
||||
tag_dict)
|
||||
for post_key in self.original_post_map:
|
||||
post = self.original_post_map[post_key]
|
||||
tags = post['tags']
|
||||
for tag in tags:
|
||||
sts = tag_dict[tag]
|
||||
sts['tag'] = tag
|
||||
sts['post_count'] += 1
|
||||
sts['note_count'] += post['note_count']
|
||||
|
||||
for tag in tag_dict:
|
||||
sts = tag_dict[tag]
|
||||
post_count = sts['post_count']
|
||||
note_count = sts['note_count']
|
||||
sts['notes_to_posts_ratio'] = note_count / post_count
|
||||
|
||||
tag_list = sorted(list(tag_dict.values()), key=itemgetter('note_count'),
|
||||
reverse=True)
|
||||
return tag_list
|
||||
|
||||
Reference in New Issue
Block a user