| 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"] |
|
|
| |
| UTC = timezone.utc |
| VN_TZ = timezone(timedelta(hours=7)) |
|
|
| @app.get("/") |
| def home(): |
| return {"status": "running"} |
|
|
| @app.get("/daily-count") |
| def daily_count(): |
|
|
| |
| now_vn = datetime.now(VN_TZ) |
|
|
| |
| start_vn = datetime(now_vn.year, now_vn.month, now_vn.day, tzinfo=VN_TZ) |
| end_vn = start_vn + timedelta(days=1) |
|
|
| |
| 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"), |
| "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) |
| } |