-
Notifications
You must be signed in to change notification settings - Fork 10
Discord: Automate Stat Messages #1012
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
e222617
db92ae6
53ba5ed
e228b43
034b501
bcc57ea
f322b37
ff8dd33
48f96b9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| import logging | ||
| from datetime import datetime | ||
| from typing import Any | ||
|
|
||
| from sqlalchemy import text | ||
| from sqlmodel import Session | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| _LLM_TOKEN_SUMMARY_SQL = text( | ||
| """ | ||
| SELECT | ||
| o.name AS organization_name, | ||
| l.model AS model, | ||
| COALESCE(SUM((l.usage->>'total_tokens')::INTEGER), 0) AS sum_total_tokens | ||
| FROM llm_call l | ||
| INNER JOIN organization o ON l.organization_id = o.id | ||
| WHERE l.inserted_at BETWEEN :start_at AND :end_at | ||
| AND l.deleted_at IS NULL | ||
| GROUP BY o.name, l.model | ||
| ORDER BY sum_total_tokens DESC | ||
| """ | ||
| ) | ||
|
|
||
| _LLM_CALL_COUNTS_SQL = text( | ||
| """ | ||
| SELECT | ||
| o.name AS organization_name, | ||
| COUNT(*) AS call_count | ||
| FROM llm_call l | ||
| INNER JOIN organization o ON l.organization_id = o.id | ||
| WHERE l.inserted_at BETWEEN :start_at AND :end_at | ||
| AND l.deleted_at IS NULL | ||
| GROUP BY o.name | ||
| ORDER BY call_count DESC | ||
| """ | ||
| ) | ||
|
|
||
| _LLM_MODALITY_COUNTS_SQL = text( | ||
| """ | ||
| SELECT | ||
| o.name AS organization_name, | ||
| CASE | ||
| WHEN l.input_type = 'text' AND l.output_type = 'text' THEN 'TEXT' | ||
| WHEN l.input_type = 'audio' AND l.output_type = 'text' THEN 'STT' | ||
| WHEN l.input_type = 'text' AND l.output_type = 'audio' THEN 'TTS' | ||
| ELSE 'OTHER' | ||
| END AS modality, | ||
| COUNT(*) AS call_count | ||
| FROM llm_call l | ||
| INNER JOIN organization o ON l.organization_id = o.id | ||
| WHERE l.inserted_at BETWEEN :start_at AND :end_at | ||
| AND l.deleted_at IS NULL | ||
| GROUP BY o.name, modality | ||
| ORDER BY o.name, modality | ||
| """ | ||
| ) | ||
|
|
||
| _JOB_COUNTS_SQL = text( | ||
| """ | ||
| SELECT | ||
| o.name AS organization_name, | ||
| j.job_type AS job_type, | ||
| COUNT(*) AS job_count | ||
| FROM job j | ||
| INNER JOIN project p ON j.project_id = p.id | ||
| INNER JOIN organization o ON p.organization_id = o.id | ||
| WHERE j.inserted_at BETWEEN :start_at AND :end_at | ||
| GROUP BY o.name, j.job_type | ||
| ORDER BY o.name, j.job_type | ||
| """ | ||
| ) | ||
|
|
||
|
|
||
| def _org_count_sql(table: str) -> Any: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win Narrow
♻️ Proposed fix-from sqlalchemy import text
+from sqlalchemy import TextClause, text
-def _org_count_sql(table: str) -> Any:
+def _org_count_sql(table: str) -> TextClause:
-def _rows(session: Session, stmt: Any, params: dict[str, Any]) -> list[dict[str, Any]]:
+def _rows(session: Session, stmt: TextClause, params: dict[str, Any]) -> list[dict[str, Any]]:Also applies to: 97-97 🤖 Prompt for AI AgentsSource: Coding guidelines
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can avoid the any type here. This is good suggestion given by coderabbit https://github.com/ProjectTech4DevAI/kaapi-backend/pull/1012/changes#r3558059640. Please handle it. |
||
| return text( | ||
| f""" | ||
| SELECT | ||
| o.name AS organization_name, | ||
| COUNT(*) AS row_count | ||
| FROM {table} t | ||
| INNER JOIN organization o ON t.organization_id = o.id | ||
| WHERE t.inserted_at BETWEEN :start_at AND :end_at | ||
| GROUP BY o.name | ||
| ORDER BY row_count DESC | ||
| """ | ||
| ) | ||
|
|
||
|
|
||
| _EVAL_RUN_COUNTS_SQL = _org_count_sql("evaluation_run") | ||
| _STT_RESULT_COUNTS_SQL = _org_count_sql("stt_result") | ||
| _TTS_RESULT_COUNTS_SQL = _org_count_sql("tts_result") | ||
| _ASSESSMENT_COUNTS_SQL = _org_count_sql("assessment") | ||
|
|
||
|
|
||
| def _rows(session: Session, stmt: Any, params: dict[str, Any]) -> list[dict[str, Any]]: | ||
| return [dict(row) for row in session.execute(stmt, params).mappings().all()] | ||
|
|
||
|
|
||
| def get_daily_stats( | ||
| *, session: Session, start_at: datetime, end_at: datetime | ||
| ) -> dict[str, list[dict[str, Any]]]: | ||
| params = {"start_at": start_at, "end_at": end_at} | ||
| logger.info( | ||
| f"[get_daily_stats] Collecting stats | start_at: {start_at.isoformat()}, " | ||
| f"end_at: {end_at.isoformat()}" | ||
| ) | ||
| return { | ||
| "llm_call_counts": _rows(session, _LLM_CALL_COUNTS_SQL, params), | ||
| "llm_call_token_summary": _rows(session, _LLM_TOKEN_SUMMARY_SQL, params), | ||
| "llm_call_modality_counts": _rows(session, _LLM_MODALITY_COUNTS_SQL, params), | ||
| "job_type_counts": _rows(session, _JOB_COUNTS_SQL, params), | ||
| "evaluation_run_counts": _rows(session, _EVAL_RUN_COUNTS_SQL, params), | ||
| "stt_result_counts": _rows(session, _STT_RESULT_COUNTS_SQL, params), | ||
| "tts_result_counts": _rows(session, _TTS_RESULT_COUNTS_SQL, params), | ||
| "assessment_counts": _rows(session, _ASSESSMENT_COUNTS_SQL, params), | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,147 @@ | ||||||||||||||||||
| import logging | ||||||||||||||||||
| from datetime import timedelta | ||||||||||||||||||
| from typing import Any | ||||||||||||||||||
|
|
||||||||||||||||||
| import requests | ||||||||||||||||||
| from sqlmodel import Session | ||||||||||||||||||
|
|
||||||||||||||||||
| from app.core.config import settings | ||||||||||||||||||
| from app.core.util import now | ||||||||||||||||||
| from app.crud.stats import get_daily_stats | ||||||||||||||||||
|
|
||||||||||||||||||
| logger = logging.getLogger(__name__) | ||||||||||||||||||
|
|
||||||||||||||||||
| DAILY_WINDOW = timedelta(hours=168) | ||||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this |
||||||||||||||||||
| _MAX_ROWS_PER_SECTION = 20 | ||||||||||||||||||
| _DISCORD_CHUNK_LIMIT = 1900 # Discord content cap is 2000; leave headroom | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
| def collect_daily_stats( | ||||||||||||||||||
| *, session: Session, window_hours: int | None = None | ||||||||||||||||||
| ) -> dict[str, Any]: | ||||||||||||||||||
| end_at = now() | ||||||||||||||||||
| start_at = end_at - ( | ||||||||||||||||||
| timedelta(hours=window_hours) if window_hours else DAILY_WINDOW | ||||||||||||||||||
| ) | ||||||||||||||||||
|
Comment on lines
+22
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🐛 Proposed fix- start_at = end_at - (
- timedelta(hours=window_hours) if window_hours else DAILY_WINDOW
- )
+ start_at = end_at - (
+ timedelta(hours=window_hours) if window_hours is not None else DAILY_WINDOW
+ )📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||
| stats = get_daily_stats(session=session, start_at=start_at, end_at=end_at) | ||||||||||||||||||
| return { | ||||||||||||||||||
| "window": { | ||||||||||||||||||
| "start_at": start_at.isoformat(), | ||||||||||||||||||
| "end_at": end_at.isoformat(), | ||||||||||||||||||
| }, | ||||||||||||||||||
| "stats": stats, | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
| def section_counts(result: dict[str, Any]) -> dict[str, int]: | ||||||||||||||||||
| return { | ||||||||||||||||||
| section: len(rows) | ||||||||||||||||||
| for section, rows in result["stats"].items() | ||||||||||||||||||
| if isinstance(rows, list) | ||||||||||||||||||
| } | ||||||||||||||||||
|
Comment on lines
+36
to
+41
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this function is not used anywhere, if this not needed please remove this dead code. |
||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
| _COL_ALIASES = { | ||||||||||||||||||
| "organization_name": "org", | ||||||||||||||||||
| "sum_total_tokens": "tokens", | ||||||||||||||||||
| "call_count": "calls", | ||||||||||||||||||
| "job_count": "jobs", | ||||||||||||||||||
| "row_count": "count", | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| _SECTION_TITLES = { | ||||||||||||||||||
| "llm_call_counts": "LLM Calls", | ||||||||||||||||||
| "llm_call_token_summary": "LLM Tokens", | ||||||||||||||||||
| "llm_call_modality_counts": "LLM Modality", | ||||||||||||||||||
| "job_type_counts": "Jobs by Type", | ||||||||||||||||||
| "evaluation_run_counts": "Evaluation Runs", | ||||||||||||||||||
| "stt_result_counts": "STT Results", | ||||||||||||||||||
| "tts_result_counts": "TTS Results", | ||||||||||||||||||
| "assessment_counts": "Assessments", | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
| def _short_ts(iso: str) -> str: | ||||||||||||||||||
| return iso.split(".", 1)[0].replace("T", " ") | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
| def _fmt_num(v: Any) -> str: | ||||||||||||||||||
| return f"{v:,}" if isinstance(v, int) else str(v) | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
| def _render_section(section: str, rows: list[dict[str, Any]]) -> str | None: | ||||||||||||||||||
| title = _SECTION_TITLES.get(section, section) | ||||||||||||||||||
| if not rows: | ||||||||||||||||||
| return None | ||||||||||||||||||
| cols = list(rows[0].keys()) | ||||||||||||||||||
| sample = rows[:_MAX_ROWS_PER_SECTION] | ||||||||||||||||||
| labels = {c: _COL_ALIASES.get(c, c) for c in cols} | ||||||||||||||||||
| is_num = {c: all(isinstance(r.get(c), (int, float)) for r in sample) for c in cols} | ||||||||||||||||||
| widths = { | ||||||||||||||||||
| c: max(len(labels[c]), *(len(_fmt_num(r.get(c, ""))) for r in sample)) | ||||||||||||||||||
| for c in cols | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| def fmt(val: Any, c: str) -> str: | ||||||||||||||||||
| s = _fmt_num(val) | ||||||||||||||||||
| return f"{s:>{widths[c]}}" if is_num[c] else f"{s:<{widths[c]}}" | ||||||||||||||||||
|
|
||||||||||||||||||
| header = " ".join(fmt(labels[c], c) for c in cols) | ||||||||||||||||||
| body = "\n".join(" ".join(fmt(r.get(c, ""), c) for c in cols) for r in sample) | ||||||||||||||||||
| truncated = ( | ||||||||||||||||||
| f"\n… +{len(rows) - _MAX_ROWS_PER_SECTION} more" | ||||||||||||||||||
| if len(rows) > _MAX_ROWS_PER_SECTION | ||||||||||||||||||
| else "" | ||||||||||||||||||
| ) | ||||||||||||||||||
| return f"\n{title}\n```\n{header}\n{body}{truncated}\n```" | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
| def format_daily_stats_message(result: dict[str, Any]) -> str: | ||||||||||||||||||
| window = result["window"] | ||||||||||||||||||
| start = _short_ts(window["start_at"]) | ||||||||||||||||||
| end = _short_ts(window["end_at"]) | ||||||||||||||||||
| blocks = [f"Daily Stats · {start} → {end} UTC"] | ||||||||||||||||||
|
|
||||||||||||||||||
| quiet: list[str] = [] | ||||||||||||||||||
| for section, rows in result["stats"].items(): | ||||||||||||||||||
| if not isinstance(rows, list): | ||||||||||||||||||
| continue | ||||||||||||||||||
| rendered = _render_section(section, rows) | ||||||||||||||||||
| if rendered is None: | ||||||||||||||||||
| quiet.append(_SECTION_TITLES.get(section, section)) | ||||||||||||||||||
| else: | ||||||||||||||||||
| blocks.append(rendered) | ||||||||||||||||||
|
|
||||||||||||||||||
| if quiet: | ||||||||||||||||||
| blocks.append("\nNo data: " + ", ".join(quiet)) | ||||||||||||||||||
| return "\n".join(blocks) | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
| def _chunk_message(message: str, limit: int) -> list[str]: | ||||||||||||||||||
| chunks: list[str] = [] | ||||||||||||||||||
| buf = "" | ||||||||||||||||||
| for block in message.split("\n\n"): | ||||||||||||||||||
| if len(buf) + len(block) + 2 > limit and buf: | ||||||||||||||||||
| chunks.append(buf) | ||||||||||||||||||
| buf = block | ||||||||||||||||||
| else: | ||||||||||||||||||
| buf = f"{buf}\n\n{block}" if buf else block | ||||||||||||||||||
| if buf: | ||||||||||||||||||
| chunks.append(buf) | ||||||||||||||||||
| return chunks | ||||||||||||||||||
|
Ayush8923 marked this conversation as resolved.
|
||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
| def post_daily_stats_to_discord(message: str) -> None: | ||||||||||||||||||
| """Fire-and-forget Discord post. No-op if webhook not configured. | ||||||||||||||||||
| Logs and stops on the first failed chunk — never raises.""" | ||||||||||||||||||
| url = settings.DISCORD_STATS_WEBHOOK_URL | ||||||||||||||||||
| if not url: | ||||||||||||||||||
| return | ||||||||||||||||||
| for chunk in _chunk_message(message, _DISCORD_CHUNK_LIMIT): | ||||||||||||||||||
| try: | ||||||||||||||||||
| requests.post( | ||||||||||||||||||
| str(url), json={"content": chunk}, timeout=5 | ||||||||||||||||||
| ).raise_for_status() | ||||||||||||||||||
| except requests.RequestException as e: | ||||||||||||||||||
| logger.warning(f"[post_daily_stats_to_discord] Webhook post failed: {e}") | ||||||||||||||||||
| return | ||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Use a parameterized return type.
-> dictis unparameterized (equivalent todict[Any, Any]). Per coding guidelines, type hints must be specific; usedict[str, Any]to match the return type ofcollect_daily_stats.As per coding guidelines: "Use type hints on every function parameter and return value;
-> Anyis not acceptable unless the type can be narrowed."🔧 Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents
Source: Coding guidelines