Skip to content
Snippets Groups Projects

improve data collection

Files

+ 133
42
@@ -18,7 +18,7 @@ from db import models, schemas
def get_waiting_time(place: str, db: Session):
""" Get the last estimated waiting time for the given place """
current_date = datetime.now(tz=pytz.timezone("Europe/Paris"))
weekday, current_time = current_date.weekday(), current_date.time()
date, weekday, current_time = current_date.date(), current_date.weekday(), current_date.time()
opening_hours = db.query(
models.OpeningHours.open_time,
models.OpeningHours.close_time).filter(
@@ -26,17 +26,27 @@ def get_waiting_time(place: str, db: Session):
models.OpeningHours.day == weekday).order_by(
models.OpeningHours.open_time).all()
for time_slot in opening_hours:
if current_time < time_slot.open_time:
return schemas.WaitingTime(next_timetable=time_slot.open_time.strftime('%Hh%M'))
elif current_time <= time_slot.close_time:
limit = datetime.combine(date.today(), time_slot.open_time)
last_record = db.query(models.Records.waiting_time).filter(models.Records.place == place).filter(
models.Records.date >= limit).order_by(models.Records.date.desc()).first()
waiting_time = None
if last_record:
waiting_time = round(
last_record.waiting_time.total_seconds() / 60)
return schemas.WaitingTime(status=True, waiting_time=waiting_time)
closure = db.query(
models.Closure).filter(
models.Closure.place == place,
models.Closure.beginning_date <= datetime.combine(date, time_slot.open_time),
models.Closure.end_date >= datetime.combine(date, time_slot.open_time)).order_by(
models.Closure.beginning_date).first()
if not closure:
if current_time < time_slot.open_time:
return schemas.WaitingTime(next_timetable=time_slot.open_time.strftime('%Hh%M'))
elif current_time <= time_slot.close_time:
limit = datetime.combine(date, time_slot.open_time)
last_record = db.query(
models.Records.waiting_time).filter(
models.Records.place == place).filter(
models.Records.date >= limit).order_by(
models.Records.date.desc()).first()
waiting_time = None
if last_record:
waiting_time = round(
last_record.waiting_time.total_seconds() / 60)
return schemas.WaitingTime(status=True, waiting_time=waiting_time)
return schemas.WaitingTime()
@@ -46,11 +56,10 @@ def shift_time(t: time, delta: timedelta):
def add_slot(slots_list, start_time, end_time, function):
average_waiting_time = function(start_time, end_time)
if average_waiting_time:
waiting_time = function(start_time, end_time)
if waiting_time:
name = 60 * start_time.hour + start_time.minute
slots_list.append(schemas.RecordRead(
name=name, time=average_waiting_time))
slots_list.append(schemas.RecordRead(name=name, time=waiting_time))
def get_avg_graph_points(place: str, weekday: int, min_time: time,
@@ -88,12 +97,21 @@ def get_avg_graph(place: str, db: Session):
for the current or next available timeslot"""
current_date = datetime.now(tz=pytz.timezone("Europe/Paris"))
weekday, current_time = current_date.weekday(), current_date.time()
opening_hours = db.query(models.OpeningHours.open_time, models.OpeningHours.close_time).filter(
models.OpeningHours.place == place, models.OpeningHours.day == weekday).order_by(models.OpeningHours.open_time).all()
for time_slot in opening_hours:
if current_time <= time_slot.close_time:
return get_avg_graph_points(place, weekday, time_slot.open_time, time_slot.close_time, timedelta(minutes=5), db)
opening_hours = db.query(
models.OpeningHours.open_time,
models.OpeningHours.close_time).filter(
models.OpeningHours.place == place,
models.OpeningHours.day == weekday).order_by(
models.OpeningHours.open_time).all()
closure = db.query(
models.Closure).filter(
models.Closure.place == place,
models.Closure.beginning_date <= current_date,
models.Closure.end_date >= current_date).first()
if not closure:
for time_slot in opening_hours:
if current_time <= time_slot.close_time:
return get_avg_graph_points(place, weekday, time_slot.open_time, time_slot.close_time, timedelta(minutes=5), db)
return []
@@ -130,18 +148,25 @@ def get_current_graph_points(place: str, current_date: date,
def get_current_graph(place: str, db: Session):
""" Get the waiting_time_graph for the current timeslot"""
current_date = datetime.now(tz=pytz.timezone("Europe/Paris"))
weekday, day, current_time = current_date.weekday(
), current_date.date(), current_date.time()
opening_hours = db.query(models.OpeningHours.open_time, models.OpeningHours.close_time).filter(
models.OpeningHours.place == place, models.OpeningHours.day == weekday).all()
for time_slot in opening_hours:
if time_slot.open_time <= current_time <= time_slot.close_time:
points = get_current_graph_points(
place, day, time_slot.open_time, current_time, timedelta(minutes=5), db)
start_time = 60 * time_slot.open_time.hour + time_slot.open_time.minute
end_time = 60 * time_slot.close_time.hour + time_slot.close_time.minute
return schemas.Graph(data=points, start=start_time, end=end_time)
weekday, day, current_time = current_date.weekday(), current_date.date(), current_date.time()
opening_hours = db.query(
models.OpeningHours.open_time,
models.OpeningHours.close_time).filter(
models.OpeningHours.place == place,
models.OpeningHours.day == weekday).all()
closure = db.query(
models.Closure).filter(
models.Closure.place == place,
models.Closure.beginning_date <= current_date,
models.Closure.end_date >= current_date).first()
if not closure:
for time_slot in opening_hours:
if time_slot.open_time <= current_time <= time_slot.close_time:
points = get_current_graph_points(
place, day, time_slot.open_time, current_time, timedelta(minutes=5), db)
start_time = 60 * time_slot.open_time.hour + time_slot.open_time.minute
end_time = 60 * time_slot.close_time.hour + time_slot.close_time.minute
return schemas.Graph(data=points, start=start_time, end=end_time)
return schemas.Graph(data=[])
@@ -155,10 +180,15 @@ def get_comments(place: str, page: int, db: Session):
models.Comments.published_at.desc(),
models.Comments.id.desc()).all()
else:
comments = db.query(models.Comments, models.Users.username).join(models.Users).filter(models.Comments.place == place).order_by(
models.Comments.published_at.desc(), models.Comments.id.desc()).slice((page - 1) * 20, page * 20).all()
comments_list = list(schemas.Comment(
**comment.__dict__, username=username) for comment, username in comments)
comments = db.query(
models.Comments,
models.Users.username).join(
models.Users).filter(
models.Comments.place == place).order_by(
models.Comments.published_at.desc(),
models.Comments.id.desc()).slice(
(page - 1) * 20, page * 20).all()
comments_list = [schemas.Comment(**comment.__dict__, username=username) for comment, username in comments]
comments_list.reverse()
return comments_list
@@ -166,8 +196,7 @@ def get_comments(place: str, page: int, db: Session):
def create_comment(user: schemas.User, place: str, new_comments: schemas.CommentBase, db: Session):
""" Add a new comment to the database """
date = datetime.now(tz=pytz.timezone("Europe/Paris"))
db_comment = models.Comments(
**new_comments.dict(), published_at=date, place=place, user_id=user.id)
db_comment = models.Comments(**new_comments.dict(), published_at=date, place=place, user_id=user.id)
db.add(db_comment)
db.commit()
db.refresh(db_comment)
@@ -187,10 +216,43 @@ def delete_comment(id: int, db: Session):
def get_news(place: str, db: Session):
""" Get the news for the given place """
current_date = datetime.now(tz=pytz.timezone("Europe/Paris"))
news = db.query(
models.News).filter(
models.News.place == place).order_by(
models.News.published_at.desc()).all()
models.News.place == place,
models.News.end_date >= current_date).order_by(
models.News.published_at.desc()).all()
opening_hours = db.query(
models.OpeningHours.open_time,
models.OpeningHours.close_time).filter(
models.OpeningHours.place == place,
models.OpeningHours.day == current_date.weekday()).order_by(
models.OpeningHours.open_time).all()
next_timetable = None
for time_slot in opening_hours:
if current_date.time() < time_slot.open_time:
next_timetable=time_slot.open_time
break
if not next_timetable:
closure = db.query(
models.Closure).filter(
models.Closure.place == place,
models.Closure.beginning_date <= current_date,
models.Closure.end_date >= current_date).first()
else:
closure = db.query(
models.Closure).filter(
models.Closure.place == place,
models.Closure.beginning_date <= datetime.combine(current_date.date(), next_timetable),
models.Closure.end_date >= datetime.combine(current_date.date(), next_timetable)).first()
if closure:
closure_news = schemas.News(
title="Fermeture exceptionnelle",
content=f"{place} est exceptionnellement hors service jusqu'au {closure.end_date.strftime('%d/%m/%y à %Hh%M')}",
end_date=closure.end_date,
place=place,
published_at=closure.beginning_date)
news.append(closure_news)
return news
@@ -450,3 +512,32 @@ def delete_collaborative_record(id: int, db: Session):
models.CollaborativeRecords.id == id).delete()
db.commit()
return
# Define CRUD operation for exceptional closure
def get_closure(place: str, db: Session):
current_date = datetime.now(tz=pytz.timezone("Europe/Paris"))
closures = db.query(
models.Closure).filter(
models.Closure.place == place,
models.Closure.end_date >= current_date).order_by(
models.Closure.beginning_date).all()
return [schemas.Closure(**closure.__dict__) for closure in closures]
def create_closure(closure: schemas.Closure, db: Session):
db_closure = models.Closure(**closure.dict())
db.add(db_closure)
db.commit()
db.refresh(db_closure)
return schemas.Closure(**closure.dict())
def delete_closure(id: int, db: Session):
if id == 0:
db.query(models.Closure).delete()
else:
db.query(models.Closure).filter(models.Closure.id == id).delete()
db.commit()
return
Loading