42 lines
1.6 KiB
Python
42 lines
1.6 KiB
Python
from dataclasses import dataclass, field
|
|
from operator import itemgetter
|
|
from typing import Any, Dict, List
|
|
|
|
from stats_model import StatsModel
|
|
|
|
|
|
@dataclass(kw_only=True)
|
|
class BuildTotalStatsModel(StatsModel):
|
|
"""Stats model built around calculating stats for posts containing one one or more specified tags."""
|
|
operation: str = 'build_total_stats'
|
|
|
|
# Top 100 posts, ranked from most popular to least popular by notes.
|
|
top_100_ranked_post_urls: List[str] = field(default_factory=list)
|
|
|
|
# Posts ranked from most popular to least popular by notes within each month and year.
|
|
top_post_urls_by_month_and_year: Dict[str, List[str]] = field(init=False)
|
|
|
|
# Tags ranked from most popular to least popular by notes.
|
|
most_popular_tags: List[Dict[str, Any]] = field(default_factory=list)
|
|
|
|
def __post_init__(self):
|
|
super().__post_init__()
|
|
self.most_popular_tags = self.determine_most_popular_tags()
|
|
|
|
def determine_most_popular_tags(self) -> List[Dict[str, Any]]:
|
|
tag_dict: Dict[str, Any] = {}
|
|
for post_key in self.original_post_map:
|
|
post = self.original_post_map[post_key]
|
|
tags = post['tags']
|
|
for tag in tags:
|
|
if tag in tag_dict:
|
|
tag_dict[tag] = {
|
|
'tag': tag, 'note_count': tag_dict[tag] + post['note_count']}
|
|
else:
|
|
tag_dict[tag] = {'tag': tag,
|
|
'note_count': post['note_count']}
|
|
|
|
tag_list = sorted(list(tag_dict.values()),
|
|
key=itemgetter('note_count'), reverse=True)
|
|
return tag_list
|