feat: show analytics across periods in tables
This commit is contained in:
1 parent
e4b1b1e510
commit
1ff4012e2f
2 files changed
+26
-19
No files matched your search
+25
-18
@@ -134,23 +134,30 @@ def _html_table(headers: list[str], rows: list[list[object]]) -> str:
|
||||
|
||||
|
||||
def render_report(period: str = "all", db_path: str | None = None) -> str:
|
||||
metrics = all_metrics(period, db_path)
|
||||
types = metrics["message_types"]
|
||||
generation_types = {k[1]: v for k, v in types.items() if k[0] == "generation"}
|
||||
training_types = {k[1]: v for k, v in types.items() if k[0] == "training"}
|
||||
users = metrics["users"]
|
||||
active = metrics["active_users"]
|
||||
user_rows = [["Всего", users], ["Активные", active]]
|
||||
activity_rows = [["Генерации постов", metrics["post_generations"]], ["Идеи", metrics["idea_generations"]], ["Правки постов", metrics["post_revisions"]], ["Сообщения", metrics["generation_messages"]]]
|
||||
generation_type_rows = [[key, value] for key, value in sorted(generation_types.items())] or [["Нет данных", 0]]
|
||||
training_type_rows = [[key, value] for key, value in sorted(training_types.items())] or [["Нет данных", 0]]
|
||||
training_rows = [["Сообщения", metrics["training_messages"]]]
|
||||
labels = {"day": "Сегодня", "week": "7 дней", "month": "30 дней", "all": "Всё время"}
|
||||
metrics_by_period = {key: all_metrics(key, db_path) for key in labels}
|
||||
titles = ["Сегодня", "7 дней", "30 дней", "Всё время"]
|
||||
|
||||
def table(title: str, metric_rows: list[tuple[str, str]]) -> str:
|
||||
rows = [[label, *(metrics_by_period[key][metric] for key in labels)] for label, metric in metric_rows]
|
||||
return "<h3>" + title + "</h3>" + _html_table(["Показатель", *titles], rows)
|
||||
|
||||
def table(title: str, metric_rows: list[tuple[str, str]]) -> str:
|
||||
rows = [[label, *(metrics_by_period[key][metric] for key in labels)] for label, metric in metric_rows]
|
||||
return "<h3>" + title + "</h3>" + _html_table(["Показатель", *titles], rows)
|
||||
|
||||
type_names = sorted({key[1] for metrics in metrics_by_period.values() for key in metrics["message_types"]})
|
||||
generation_rows = [[kind, *(metrics_by_period[p]["message_types"].get(("generation", kind), 0) for p in labels)] for kind in type_names]
|
||||
training_type_rows = [[kind, *(metrics_by_period[p]["message_types"].get(("training", kind), 0) for p in labels)] for kind in type_names]
|
||||
if not generation_rows:
|
||||
generation_rows = [["Нет данных", 0, 0, 0, 0]]
|
||||
training_type_rows = [["Нет данных", 0, 0, 0, 0]]
|
||||
|
||||
return (
|
||||
"<h2>📊 Полная статистика бота</h2>"
|
||||
f"<p>Период: {period}</p>"
|
||||
"<h3>Пользователи</h3>" + _html_table(["Показатель", "Количество"], user_rows)
|
||||
+ "<h3>Генерация</h3>" + _html_table(["Показатель", "Количество"], activity_rows)
|
||||
+ "<h3>Типы сообщений в генерации</h3>" + _html_table(["Тип", "Количество"], generation_type_rows)
|
||||
+ "<h3>Обучение</h3>" + _html_table(["Показатель", "Количество"], training_rows)
|
||||
+ "<h3>Типы сообщений в обучении</h3>" + _html_table(["Тип", "Количество"], training_type_rows)
|
||||
"<h2>📊 Статистика бота</h2>"
|
||||
+ table("Пользователи", [("Активные", "active_users")])
|
||||
+ table("Генерация", [("Посты", "post_generations"), ("Идеи", "idea_generations"), ("Правки", "post_revisions"), ("Сообщения", "generation_messages")])
|
||||
+ "<h3>Типы сообщений в генерации</h3>" + _html_table(["Тип", *titles], generation_rows)
|
||||
+ table("Обучение", [("Сообщения", "training_messages")])
|
||||
+ "<h3>Типы сообщений в обучении</h3>" + _html_table(["Тип", *titles], training_type_rows)
|
||||
)
|
||||
Reference in new issue
Block a user