Spaces:
Running
Running
Monteeq CI commited on
Commit ·
8ed5529
1
Parent(s): af296c7
🚀 Deploy from GitHub @ e079ce322d53f2d7614c9bec0ae4f98f18f26d43
Browse filesTriggered by: Smasduq
Message: Merge pull request #15 from Monteeq/fix/landing-real-stats
fix: drive landing copy from live platform stats
app/api/v1/endpoints/metrics.py
CHANGED
|
@@ -1,8 +1,13 @@
|
|
| 1 |
-
from fastapi import APIRouter, Request, BackgroundTasks
|
| 2 |
-
from typing import List, Dict, Any
|
| 3 |
from pydantic import BaseModel
|
|
|
|
|
|
|
| 4 |
import logging
|
| 5 |
|
|
|
|
|
|
|
|
|
|
| 6 |
router = APIRouter()
|
| 7 |
logger = logging.getLogger(__name__)
|
| 8 |
|
|
@@ -15,6 +20,63 @@ class MetricEvent(BaseModel):
|
|
| 15 |
class MetricsBatchRequest(BaseModel):
|
| 16 |
events: List[MetricEvent]
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
@router.post("/batch")
|
| 19 |
async def batch_metrics(request: MetricsBatchRequest, background_tasks: BackgroundTasks):
|
| 20 |
"""
|
|
|
|
| 1 |
+
from fastapi import APIRouter, Request, BackgroundTasks, Depends
|
| 2 |
+
from typing import List, Dict, Any, Optional
|
| 3 |
from pydantic import BaseModel
|
| 4 |
+
from sqlalchemy.orm import Session
|
| 5 |
+
from sqlalchemy import func
|
| 6 |
import logging
|
| 7 |
|
| 8 |
+
from app.db.session import get_db
|
| 9 |
+
from app.models.models import User, Video, Like, Comment, Challenge
|
| 10 |
+
|
| 11 |
router = APIRouter()
|
| 12 |
logger = logging.getLogger(__name__)
|
| 13 |
|
|
|
|
| 20 |
class MetricsBatchRequest(BaseModel):
|
| 21 |
events: List[MetricEvent]
|
| 22 |
|
| 23 |
+
@router.get("/public")
|
| 24 |
+
def public_platform_stats(db: Session = Depends(get_db)):
|
| 25 |
+
"""
|
| 26 |
+
Lightweight public counters for the marketing landing page.
|
| 27 |
+
No auth. Safe aggregates only.
|
| 28 |
+
"""
|
| 29 |
+
creators = (
|
| 30 |
+
db.query(func.count(User.id))
|
| 31 |
+
.filter(User.role != "admin")
|
| 32 |
+
.filter((User.is_active.is_(True)) | (User.is_active.is_(None)))
|
| 33 |
+
.scalar()
|
| 34 |
+
or 0
|
| 35 |
+
)
|
| 36 |
+
videos = db.query(func.count(Video.id)).scalar() or 0
|
| 37 |
+
views = db.query(func.coalesce(func.sum(Video.views), 0)).scalar() or 0
|
| 38 |
+
likes = db.query(func.count(Like.id)).scalar() or 0
|
| 39 |
+
comments = db.query(func.count(Comment.id)).scalar() or 0
|
| 40 |
+
open_challenges = (
|
| 41 |
+
db.query(func.count(Challenge.id)).filter(Challenge.is_open.is_(True)).scalar() or 0
|
| 42 |
+
)
|
| 43 |
+
featured_challenge: Optional[Challenge] = (
|
| 44 |
+
db.query(Challenge)
|
| 45 |
+
.filter(Challenge.is_open.is_(True))
|
| 46 |
+
.order_by(Challenge.created_at.desc())
|
| 47 |
+
.first()
|
| 48 |
+
)
|
| 49 |
+
countries = (
|
| 50 |
+
db.query(func.count(func.distinct(User.country)))
|
| 51 |
+
.filter(
|
| 52 |
+
User.country.isnot(None),
|
| 53 |
+
User.country != "",
|
| 54 |
+
User.country != "Unknown",
|
| 55 |
+
User.role != "admin",
|
| 56 |
+
)
|
| 57 |
+
.scalar()
|
| 58 |
+
or 0
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
return {
|
| 62 |
+
"creators": int(creators),
|
| 63 |
+
"videos": int(videos),
|
| 64 |
+
"views": int(views),
|
| 65 |
+
"likes": int(likes),
|
| 66 |
+
"comments": int(comments),
|
| 67 |
+
"open_challenges": int(open_challenges),
|
| 68 |
+
"countries": int(countries),
|
| 69 |
+
"featured_challenge": (
|
| 70 |
+
{
|
| 71 |
+
"title": featured_challenge.title,
|
| 72 |
+
"prize": featured_challenge.prize,
|
| 73 |
+
"entry_count": featured_challenge.entry_count or 0,
|
| 74 |
+
}
|
| 75 |
+
if featured_challenge
|
| 76 |
+
else None
|
| 77 |
+
),
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
@router.post("/batch")
|
| 81 |
async def batch_metrics(request: MetricsBatchRequest, background_tasks: BackgroundTasks):
|
| 82 |
"""
|