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
aa575cf4
Commit
aa575cf4
authored
3 years ago
by
Aymeric Chaumont
Browse files
Options
Downloads
Patches
Plain Diff
add route to get waiting time stats
parent
261e066c
No related branches found
No related tags found
1 merge request
!23
Extract stats
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
backend/db/crud.py
+48
-0
48 additions, 0 deletions
backend/db/crud.py
backend/main.py
+6
-1
6 additions, 1 deletion
backend/main.py
with
54 additions
and
1 deletion
backend/db/crud.py
+
48
−
0
View file @
aa575cf4
"""
"""
Module to interact with the database
Module to interact with the database
"""
"""
from
datetime
import
date
,
datetime
,
time
,
timedelta
from
numpy
import
average
from
sqlalchemy.orm
import
Session
from
sqlalchemy.orm
import
Session
from
sqlalchemy.sql
import
func
from
db
import
models
,
schemas
from
db
import
models
,
schemas
...
@@ -24,3 +28,47 @@ def get_waiting_time(place: str, db: Session):
...
@@ -24,3 +28,47 @@ def get_waiting_time(place: str, db: Session):
"""
Get the last estimated waiting time for the given place
"""
"""
Get the last estimated waiting time for the given place
"""
db_record
=
db
.
query
(
models
.
Records
).
filter
(
models
.
Records
.
place
==
place
).
order_by
(
models
.
Records
.
date
.
desc
()).
one
()
db_record
=
db
.
query
(
models
.
Records
).
filter
(
models
.
Records
.
place
==
place
).
order_by
(
models
.
Records
.
date
.
desc
()).
one
()
return
db_record
.
waiting_time
return
db_record
.
waiting_time
def
get_stats
(
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
):
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
.
weekday
(
models
.
Records
.
date
)
==
weekday
,
(
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
(
dic
,
start_time
,
end_time
):
average_waiting_time
=
avg_time_query
(
start_time
,
end_time
)
if
average_waiting_time
:
key
=
f
'
{
start_time
.
hour
:
02
}
-
{
start_time
.
minute
:
02
}
-
{
end_time
.
hour
:
02
}
-
{
end_time
.
minute
:
02
}
'
dic
[
key
]
=
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
This diff is collapsed.
Click to expand it.
backend/main.py
+
6
−
1
View file @
aa575cf4
from
datetime
import
timedelta
from
datetime
import
datetime
,
time
,
timedelta
from
typing
import
List
from
typing
import
List
from
fastapi
import
Body
,
Depends
,
FastAPI
from
fastapi
import
Body
,
Depends
,
FastAPI
from
fastapi.middleware.cors
import
CORSMiddleware
from
fastapi.middleware.cors
import
CORSMiddleware
...
@@ -57,6 +57,11 @@ async def waiting_time(place: str, db: Session = Depends(get_db)):
...
@@ -57,6 +57,11 @@ async def waiting_time(place: str, db: Session = Depends(get_db)):
return
crud
.
get_waiting_time
(
place
,
db
)
return
crud
.
get_waiting_time
(
place
,
db
)
@app.get
(
'
/api/{place}/stats/{day}/{start_time}/{end_time}/{interval}
'
,
response_model
=
int
)
async
def
stats
(
place
:
str
,
day
:
int
,
start_time
:
time
,
end_time
:
time
,
interval
:
timedelta
,
db
:
Session
=
Depends
(
get_db
)):
return
crud
.
get_stats
(
place
,
day
,
start_time
,
end_time
,
interval
,
db
)
"""
"""
import cv2
import cv2
import numpy as np
import numpy as np
...
...
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