count / main.py
yuu1234's picture
Update
430d6b1
import os
from datetime import datetime, timedelta, timezone
from fastapi import FastAPI
from pymongo import MongoClient
app = FastAPI()
MONGO_URI = os.environ.get("MONGO_URI")
client = MongoClient(MONGO_URI)
db = client["myAppDB"]
collection = db["Training_Response"]
# ===== Timezone config =====
UTC = timezone.utc
VN_TZ = timezone(timedelta(hours=7))
@app.get("/")
def home():
return {"status": "running"}
@app.get("/daily-count")
def daily_count():
# Giờ Việt Nam
now_vn = datetime.now(VN_TZ)
# Đầu ngày & cuối ngày theo giờ VN
start_vn = datetime(now_vn.year, now_vn.month, now_vn.day, tzinfo=VN_TZ)
end_vn = start_vn + timedelta(days=1)
# Convert sang UTC để query MongoDB
start_utc = start_vn.astimezone(UTC)
end_utc = end_vn.astimezone(UTC)
pipeline = [
{
"$match": {
"createdAt": {
"$gte": start_utc,
"$lt": end_utc
}
}
},
{
"$group": {
"_id": "$reviewerId",
"count": {"$sum": 1}
}
}
]
results = list(collection.aggregate(pipeline))
return {
"date": now_vn.strftime("%d/%m/%Y"), # hiển thị đúng giờ VN
"counts": results
}
@app.get("/progress")
def progress():
total_items = collection.count_documents({})
total_required = 4682 * 6
progress_percent = (total_items / total_required) * 100
return {
"total_items": total_items,
"target": total_required,
"progress_percent": round(progress_percent, 2)
}