Add
Browse files
main.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
import os
|
| 2 |
-
from datetime import datetime, timedelta
|
| 3 |
from fastapi import FastAPI
|
| 4 |
from pymongo import MongoClient
|
| 5 |
|
|
@@ -11,6 +11,10 @@ client = MongoClient(MONGO_URI)
|
|
| 11 |
db = client["myAppDB"]
|
| 12 |
collection = db["Training_Response"]
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
@app.get("/")
|
| 15 |
def home():
|
| 16 |
return {"status": "running"}
|
|
@@ -18,16 +22,23 @@ def home():
|
|
| 18 |
@app.get("/daily-count")
|
| 19 |
def daily_count():
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
pipeline = [
|
| 26 |
{
|
| 27 |
"$match": {
|
| 28 |
"createdAt": {
|
| 29 |
-
"$gte":
|
| 30 |
-
"$lt":
|
| 31 |
}
|
| 32 |
}
|
| 33 |
},
|
|
@@ -42,6 +53,6 @@ def daily_count():
|
|
| 42 |
results = list(collection.aggregate(pipeline))
|
| 43 |
|
| 44 |
return {
|
| 45 |
-
"date":
|
| 46 |
"counts": results
|
| 47 |
}
|
|
|
|
| 1 |
import os
|
| 2 |
+
from datetime import datetime, timedelta, timezone
|
| 3 |
from fastapi import FastAPI
|
| 4 |
from pymongo import MongoClient
|
| 5 |
|
|
|
|
| 11 |
db = client["myAppDB"]
|
| 12 |
collection = db["Training_Response"]
|
| 13 |
|
| 14 |
+
# ===== Timezone config =====
|
| 15 |
+
UTC = timezone.utc
|
| 16 |
+
VN_TZ = timezone(timedelta(hours=7))
|
| 17 |
+
|
| 18 |
@app.get("/")
|
| 19 |
def home():
|
| 20 |
return {"status": "running"}
|
|
|
|
| 22 |
@app.get("/daily-count")
|
| 23 |
def daily_count():
|
| 24 |
|
| 25 |
+
# Giờ Việt Nam
|
| 26 |
+
now_vn = datetime.now(VN_TZ)
|
| 27 |
+
|
| 28 |
+
# Đầu ngày & cuối ngày theo giờ VN
|
| 29 |
+
start_vn = datetime(now_vn.year, now_vn.month, now_vn.day, tzinfo=VN_TZ)
|
| 30 |
+
end_vn = start_vn + timedelta(days=1)
|
| 31 |
+
|
| 32 |
+
# Convert sang UTC để query MongoDB
|
| 33 |
+
start_utc = start_vn.astimezone(UTC)
|
| 34 |
+
end_utc = end_vn.astimezone(UTC)
|
| 35 |
|
| 36 |
pipeline = [
|
| 37 |
{
|
| 38 |
"$match": {
|
| 39 |
"createdAt": {
|
| 40 |
+
"$gte": start_utc,
|
| 41 |
+
"$lt": end_utc
|
| 42 |
}
|
| 43 |
}
|
| 44 |
},
|
|
|
|
| 53 |
results = list(collection.aggregate(pipeline))
|
| 54 |
|
| 55 |
return {
|
| 56 |
+
"date": now_vn.strftime("%d/%m/%Y"), # hiển thị đúng giờ VN
|
| 57 |
"counts": results
|
| 58 |
}
|