Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
V
ViaResto-website
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Aymeric Chaumont
ViaResto-website
Commits
928b2238
Commit
928b2238
authored
3 years ago
by
Aymeric Chaumont
Browse files
Options
Downloads
Patches
Plain Diff
update routes for average and current graphs
parent
a5339688
No related branches found
No related tags found
2 merge requests
!29
Time dependence
,
!28
improve front
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
backend/db/crud.py
+79
-3
79 additions, 3 deletions
backend/db/crud.py
backend/routers/stats.py
+3
-4
3 additions, 4 deletions
backend/routers/stats.py
with
82 additions
and
7 deletions
backend/db/crud.py
+
79
−
3
View file @
928b2238
...
...
@@ -13,8 +13,8 @@ from db import models, schemas
def
get_waiting_time
(
place
:
str
,
db
:
Session
):
"""
Get the last estimated waiting time for the given place
"""
date
=
datetime
.
now
(
tz
=
pytz
.
timezone
(
"
Europe/Paris
"
))
weekday
,
current_time
=
date
.
weekday
(),
date
.
time
()
current_
date
=
datetime
.
now
(
tz
=
pytz
.
timezone
(
"
Europe/Paris
"
))
weekday
,
current_time
=
current_
date
.
weekday
(),
date
.
time
()
first_timeslot
=
get_timeslot
(
place
,
weekday
,
True
,
db
)
if
first_timeslot
and
current_time
<
first_timeslot
[
0
]:
return
first_timeslot
[
0
].
hour
,
first_timeslot
[
0
].
minute
...
...
@@ -44,7 +44,7 @@ def get_waiting_time(place: str, db: Session):
return
None
,
None
def
get_
sta
ts
(
place
:
str
,
weekday
:
int
,
min_time
:
time
,
max_time
:
time
,
interval
:
timedelta
,
db
:
Session
):
def
get_
avg_graph_poin
ts
(
place
:
str
,
weekday
:
int
,
min_time
:
time
,
max_time
:
time
,
interval
:
timedelta
,
db
:
Session
):
"""
Get the average waiting time for each interval between two time steps
"""
def
shift_time
(
t
:
time
,
delta
:
timedelta
):
...
...
@@ -87,6 +87,82 @@ def get_stats(place: str, weekday: int, min_time: time, max_time: time, interval
return
stats
def
get_next_avg_graph
(
place
:
str
,
db
:
Session
):
"""
Get the average waiting time for each interval between two time steps,
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
()
first_timeslot
=
get_timeslot
(
place
,
weekday
,
True
,
db
)
if
first_timeslot
and
current_time
<=
first_timeslot
[
1
]:
return
get_avg_graph_points
(
place
,
weekday
,
first_timeslot
[
0
],
first_timeslot
[
1
],
timedelta
(
minutes
=
5
),
db
)
second_timeslot
=
get_timeslot
(
place
,
weekday
,
False
,
db
)
if
second_timeslot
and
current_time
<=
second_timeslot
[
1
]:
return
get_avg_graph_points
(
place
,
weekday
,
second_timeslot
[
0
],
second_timeslot
[
1
],
timedelta
(
minutes
=
5
),
db
)
return
None
def
get_current_graph_points
(
place
:
str
,
current_date
:
date
,
min_time
:
time
,
max_time
:
time
,
interval
:
timedelta
,
db
:
Session
):
"""
Get the waiting time for each interval between two time steps for the current timeslot
"""
def
shift_time
(
t
:
time
,
delta
:
timedelta
):
return
(
datetime
.
combine
(
date
(
1
,
1
,
1
),
t
)
+
delta
).
time
()
def
avg_time_query
(
start_time
,
end_time
):
records
=
db
.
query
(
(
func
.
round
(
func
.
avg
(
3600
*
func
.
extract
(
'
HOUR
'
,
models
.
Records
.
waiting_time
)
+
60
*
func
.
extract
(
'
MINUTE
'
,
models
.
Records
.
waiting_time
)
+
func
.
extract
(
'
SECOND
'
,
models
.
Records
.
waiting_time
))
))
/
60
).
filter
(
models
.
Records
.
place
==
place
,
func
.
extract
(
'
YEAR
'
,
models
.
Records
.
date
)
==
current_date
.
year
,
func
.
extract
(
'
MONTH
'
,
models
.
Records
.
date
)
==
current_date
.
month
,
func
.
extract
(
'
DAY
'
,
models
.
Records
.
date
)
==
current_date
.
day
,
(
func
.
extract
(
'
HOUR
'
,
models
.
Records
.
date
)
>
start_time
.
hour
)
|
((
func
.
extract
(
'
HOUR
'
,
models
.
Records
.
date
)
==
start_time
.
hour
)
&
(
func
.
extract
(
'
MINUTE
'
,
models
.
Records
.
date
)
>=
start_time
.
minute
)),
(
func
.
extract
(
'
HOUR
'
,
models
.
Records
.
date
)
<
end_time
.
hour
)
|
((
func
.
extract
(
'
HOUR
'
,
models
.
Records
.
date
)
==
end_time
.
hour
)
&
(
func
.
extract
(
'
MINUTE
'
,
models
.
Records
.
date
)
<
end_time
.
minute
)),
).
one
()
if
records
[
0
]:
return
int
(
records
[
0
])
return
None
def
add_slot
(
slots_list
,
start_time
,
end_time
):
average_waiting_time
=
avg_time_query
(
start_time
,
end_time
)
if
average_waiting_time
:
name
=
f
'
{
start_time
.
hour
:
02
}
h
{
start_time
.
minute
:
02
}
'
slots_list
.
append
({
'
name
'
:
name
,
'
time
'
:
average_waiting_time
})
stats
=
[]
start_time
,
end_time
=
min_time
,
shift_time
(
min_time
,
interval
)
while
start_time
<
max_time
:
add_slot
(
stats
,
start_time
,
end_time
)
start_time
,
end_time
=
end_time
,
shift_time
(
end_time
,
interval
)
return
stats
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
()
first_timeslot
=
get_timeslot
(
place
,
weekday
,
True
,
db
)
if
first_timeslot
and
current_time
<=
first_timeslot
[
0
]:
return
None
elif
first_timeslot
and
current_time
<=
first_timeslot
[
1
]:
return
get_current_graph_points
(
place
,
day
,
first_timeslot
[
0
],
current_time
,
timedelta
(
minutes
=
5
),
db
)
second_timeslot
=
get_timeslot
(
place
,
weekday
,
False
,
db
)
if
second_timeslot
and
current_time
<=
second_timeslot
[
0
]:
return
None
elif
second_timeslot
and
current_time
<=
second_timeslot
[
1
]:
return
get_current_graph_points
(
place
,
day
,
second_timeslot
[
0
],
current_time
,
timedelta
(
minutes
=
5
),
db
)
return
None
# Define CRUD operation for the comments
def
get_comments
(
place
:
str
,
page
:
int
,
db
:
Session
):
...
...
This diff is collapsed.
Click to expand it.
backend/routers/stats.py
+
3
−
4
View file @
928b2238
from
fastapi
import
APIRouter
,
Depends
from
sqlalchemy.orm
import
Session
from
datetime
import
timedelta
,
time
from
db
import
crud
from
db.database
import
get_db
...
...
@@ -14,6 +13,6 @@ async def waiting_time(place: str, db: Session = Depends(get_db)):
return
crud
.
get_waiting_time
(
place
,
db
)
@router.get
(
'
/{place}/stats/
{day}/{min_time}/{max_time}/{interval}
'
,
response_model
=
list
)
async
def
stats
(
place
:
str
,
day
:
int
,
min_time
:
time
,
max_time
:
time
,
interval
:
timedelta
,
db
:
Session
=
Depends
(
get_db
)):
return
crud
.
get_
stats
(
place
,
day
,
min_time
,
max_time
,
interval
,
db
)
@router.get
(
'
/{place}/stats/
avg_graph
'
,
response_model
=
list
)
async
def
stats
(
place
:
str
,
db
:
Session
=
Depends
(
get_db
)):
return
crud
.
get_
avg_graph
(
place
,
db
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment