import datetime import json from typing import Any, Callable, Literal from .schedule import getFacCourseByGroup from .user import User def I(condition: bool): return 1 if condition else 0 FEATURE_LIST: dict[str, Callable[[User], int]] = { "accounts": lambda _: 1, "today_accounts": lambda u: I(u.usage["date"] == datetime.datetime.now().strftime("%Y.%m.%d")), "today_interactions": lambda u: ( max(u.usage["count"], 0) if u.usage["date"] == datetime.datetime.now().strftime("%Y.%m.%d") else 0 ), "feature_groups": lambda u: I(len(u.saved_groups) > 1), "feature_notifications": lambda u: I(len(u.notifications) > 0), "feature_extended_schedule": lambda u: I('extended_schedule' in u.flags), "fetaure_v2_ui": lambda u: I('v2_ui' in u.flags) } class Analytics: def __init__(self): self.issued_at: datetime.datetime = datetime.datetime.now() self.custom: dict[str, Any] = { "group_list": [], } self.total: dict[str, int] = {} self.data: dict[ str, # faculty dict[ str, # course dict[str, int], # features ], ] = {} def add(self, u: User): if u.state == "broken": return faculty, course = getFacCourseByGroup(u.group) if not faculty: return faculty: str course: str # custom [self.custom["group_list"].append(groupname) for groupname in u.saved_groups] self.custom["group_list"] = list(set(self.custom["group_list"])) # features for feature_name, func in FEATURE_LIST.items(): result = func(u) if feature_name not in self.total: self.total[feature_name] = 0 self.total[feature_name] += result if faculty not in self.data: self.data[faculty] = {} if course not in self.data[faculty]: self.data[faculty][course] = {} if feature_name not in self.data[faculty][course]: self.data[faculty][course][feature_name] = 0 self.data[faculty][course][feature_name] += result def toDict(self): return { "version": 2, "issued_at": self.issued_at.isoformat(), "total": self.total, "data": self.data, "custom": self.custom, } def append(self): try: w: list[dict[str, Any]] = json.load( open("./web/analytics/data-v2.json", "r", encoding="utf-8") ) except: w = [] w.append(self.toDict()) w = w[-30:] json.dump( w, open("./web/analytics/data-v2.json", "w", encoding="utf-8"), ensure_ascii=False, # indent=0, ) def text(self) -> str: return ( f""" Аналитика за {self.issued_at.strftime("%m.%d")} 📊""" + "\n".join( [ f"{feature.capitalize()}: {number}" for feature, number in self.total.items() ] ) + """ Сайт """ )