-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
102 lines (84 loc) · 2.98 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
from fastapi import FastAPI, Query
from fastapi.middleware.cors import CORSMiddleware
import uvicorn
from datetime import datetime, time, timedelta
import pytz
import asyncio
from collections import defaultdict
app = FastAPI()
# not needed technically but keep for dev purposes
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
BERLIN_TZ = pytz.timezone('Europe/Berlin')
today = datetime.now(BERLIN_TZ).date()
start_time = BERLIN_TZ.localize(datetime.combine(today, time(8, 0)))
end_time = BERLIN_TZ.localize(datetime.combine(today, time(15, 0)))
TIME_SLOTS = []
current_time = start_time
while current_time < end_time:
next_time = current_time + timedelta(minutes=15)
TIME_SLOTS.append((current_time, next_time))
current_time = next_time
slot_assignments = {slot: 0 for slot in TIME_SLOTS}
user_assignments = defaultdict(int)
clear_data_task = None
@app.on_event("startup")
async def startup_event():
global clear_data_task
clear_data_task = asyncio.create_task(clear_user_assignments_every_60_minutes())
@app.on_event("shutdown")
async def shutdown_event():
global clear_data_task
if clear_data_task:
clear_data_task.cancel()
try:
await clear_data_task
except asyncio.CancelledError:
pass
print("Data clearing cancelled")
@app.get("/")
async def root():
return {"success": True, "message": "Service is Active"}
@app.get("/getTimeSlot")
def get_time_slot(user_name: str = Query(..., alias="q")):
if user_assignments[user_name] >= 2:
return {"success": False, "message": f"Please do not spam this service, {user_name}."}
now = datetime.now(BERLIN_TZ)
next_slot = None
for start, end in TIME_SLOTS:
if start <= now < end and slot_assignments.get((start, end), 0) < 10:
next_slot = (start, end)
break
if not next_slot:
for start, end in TIME_SLOTS:
if now < start and slot_assignments.get((start, end), 0) < 10:
next_slot = (start, end)
break
if next_slot:
slot_assignments[next_slot] += 1
user_assignments[user_name] += 1
start_str = next_slot[0].strftime("%I:%M %p")
end_str = next_slot[1].strftime("%I:%M %p")
return {
"success": True,
"message": f"The closest available time slot for {user_name} is from {start_str} to {end_str}",
"slotStart": start_str,
"slotEnd": end_str
}
else:
return {"success": False, "message": "No more time slots available for today"}
async def clear_user_assignments():
global user_assignments
user_assignments = defaultdict(int)
print("Cleared user assignments.")
async def clear_user_assignments_every_60_minutes():
while True:
await asyncio.sleep(3600)
await clear_user_assignments()
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=5000)