Spaces:
Running on Zero
Running on Zero
Download app/db/repos/analytics.py from cipher-attacks/Dbu-central-bot: direct link, hf CLI and curl.
- Browser
- Download file 4.24 kB
-
https://huggingface.co/spaces/cipher-attacks/Dbu-central-bot/resolve/main/app/db/repos/analytics.py
- Command line
-
hf download hf://spaces/cipher-attacks/Dbu-central-bot/app/db/repos/analytics.py
-
curl -L -o analytics.py https://huggingface.co/spaces/cipher-attacks/Dbu-central-bot/resolve/main/app/db/repos/analytics.py
4.24 kB
| """Loads the plain data the analyzer needs (simple selects; all the maths is in services/analytics.py).""" | |
| from datetime import date, datetime, timedelta, timezone | |
| from typing import Optional | |
| from sqlalchemy import select | |
| from sqlalchemy.ext.asyncio import AsyncSession | |
| from app.db.models import ActivityEvent, ChannelPost, Course, Material, Submission, User, XpEvent | |
| from app.services import analytics as A | |
| def _utc(dt: Optional[datetime]) -> Optional[datetime]: | |
| if dt is None: | |
| return None | |
| return dt if dt.tzinfo else dt.replace(tzinfo=timezone.utc) | |
| def _day(dt: datetime) -> date: | |
| return _utc(dt).astimezone(A.ET).date() | |
| async def load(session: AsyncSession, now: Optional[datetime] = None) -> A.Snapshot: | |
| now = now or datetime.now(timezone.utc) | |
| users = [ | |
| A.UserRow( | |
| id=uid, created=_utc(created), joined=_day(created), source=source or "before tracking", | |
| verified=bool(passed), onboarded=year is not None, active_days=active or 0, stream=stream, missing=missing, | |
| ) | |
| for uid, created, source, passed, year, active, stream, missing in ( | |
| await session.execute( | |
| select(User.id, User.created_at, User.source, User.gate_passed, User.year, User.active_days, User.stream, User.gate_missing) | |
| ) | |
| ).all() | |
| ] | |
| checkins: dict[int, set[date]] = {} | |
| for uid, day in (await session.execute(select(XpEvent.user_id, XpEvent.day).where(XpEvent.kind == "checkin"))).all(): | |
| checkins.setdefault(uid, set()).add(date.fromisoformat(day)) | |
| since = now - timedelta(days=30) | |
| events = [ | |
| A.EventRow(kind=kind, ref=ref or "", n=n or 0, day=_day(created), user_id=uid) | |
| for kind, ref, n, created, uid in ( | |
| await session.execute( | |
| select(ActivityEvent.kind, ActivityEvent.ref, ActivityEvent.n, ActivityEvent.created_at, ActivityEvent.user_id) | |
| .where(ActivityEvent.created_at >= since) | |
| ) | |
| ).all() | |
| ] | |
| materials = [ | |
| A.MaterialRow(id=i, course_id=cid, title=title or "", downloads=dl or 0, status=status, kind=kind) | |
| for i, cid, title, dl, status, kind in ( | |
| await session.execute(select(Material.id, Material.course_id, Material.title, Material.downloads, Material.status, Material.kind)) | |
| ).all() | |
| ] | |
| courses = {cid: name for cid, name in (await session.execute(select(Course.id, Course.name))).all()} | |
| subs = [ | |
| A.SubRow(status=status, user_id=uid, created=_utc(created), reviewed=_utc(reviewed)) | |
| for status, uid, created, reviewed in ( | |
| await session.execute(select(Submission.status, Submission.user_id, Submission.created_at, Submission.reviewed_at)) | |
| ).all() | |
| ] | |
| posts = [ | |
| A.PostRow( | |
| message_id=mid, posted=_utc(posted), preview=preview or "", | |
| tags=[t for t in (tags or "").split(",") if t], reactions=reactions, | |
| ) | |
| for mid, posted, preview, tags, reactions in ( | |
| await session.execute( | |
| select(ChannelPost.message_id, ChannelPost.posted_at, ChannelPost.preview, ChannelPost.tags, ChannelPost.reactions) | |
| .order_by(ChannelPost.posted_at.desc()).limit(30) | |
| ) | |
| ).all() | |
| ] | |
| return A.Snapshot(now=now, users=users, checkins=checkins, events=events, materials=materials, courses=courses, subs=subs, posts=posts) | |
| async def save_post( | |
| session: AsyncSession, *, chat_id: int, message_id: int, posted_at: datetime, text: str, reactions: Optional[int] = None | |
| ) -> None: | |
| post = await session.get(ChannelPost, (chat_id, message_id)) | |
| preview = " ".join((text or "").split())[:300] or "(media post)" | |
| tags = ",".join(A.extract_tags(text or ""))[:120] | |
| if post: | |
| post.preview, post.tags = preview, tags | |
| else: | |
| session.add(ChannelPost(chat_id=chat_id, message_id=message_id, posted_at=posted_at, preview=preview, tags=tags, reactions=reactions)) | |
| await session.flush() | |
| async def set_reactions(session: AsyncSession, chat_id: int, message_id: int, total: int) -> None: | |
| post = await session.get(ChannelPost, (chat_id, message_id)) | |
| if post: | |
| post.reactions = total | |
| await session.flush() | |