Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
StorageLeaf
Manage
Activity
Members
Labels
Plan
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
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
ProjectLeaf
StorageLeaf
Commits
e44f1e1e
Commit
e44f1e1e
authored
4 years ago
by
Robert Goldmann
Browse files
Options
Downloads
Patches
Plain Diff
added route to retrieve latest measurement of a specific sensor
parent
e7e15dd4
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/logic/database/MeasurementAccess.py
+1
-44
1 addition, 44 deletions
src/logic/database/MeasurementAccess.py
src/logic/databaseNew/Crud.py
+7
-0
7 additions, 0 deletions
src/logic/databaseNew/Crud.py
src/routers/SensorRouter.py
+10
-0
10 additions, 0 deletions
src/routers/SensorRouter.py
with
18 additions
and
44 deletions
src/logic/database/MeasurementAccess.py
+
1
−
44
View file @
e44f1e1e
import
logging
from
datetime
import
datetime
from
typing
import
Dict
,
List
from
typing
import
Dict
from
logic
import
Constants
from
logic.database.DatabaseAccess
import
DatabaseAccess
,
FetchType
...
...
@@ -9,50 +8,8 @@ LOGGER = logging.getLogger(Constants.APP_NAME)
class
MeasurementAccess
(
DatabaseAccess
):
TABLE_NAME
=
'
measurement
'
def
create_table
(
self
):
self
.
_query
(
f
'''
CREATE TABLE IF NOT EXISTS
{
self
.
TABLE_NAME
}
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
sensor_id INTEGER,
value TEXT NOT NULL,
timestamp TEXT NOT NULL)
'''
,
fetch_type
=
FetchType
.
CREATE
)
def
__get_current_datetime
(
self
):
return
datetime
.
strftime
(
datetime
.
now
(),
self
.
DATE_FORMAT
)
def
get_all_measurements
(
self
,
startDateTime
:
str
,
endDateTime
:
str
)
->
List
[
Dict
[
str
,
str
]]:
if
startDateTime
and
endDateTime
:
return
self
.
_query
(
f
'
SELECT * FROM
{
self
.
TABLE_NAME
}
WHERE
'
f
'
DATETIME(timestamp) BETWEEN DATETIME(?) AND DATETIME(?)
'
f
'
ORDER BY sensor_id ASC, datetime(timestamp) DESC
'
,
startDateTime
,
endDateTime
,
fetch_type
=
FetchType
.
ALL
)
return
self
.
_query
(
f
'
SELECT * FROM
{
self
.
TABLE_NAME
}
ORDER BY sensor_id ASC,
'
f
'
datetime(timestamp) DESC
'
,
fetch_type
=
FetchType
.
ALL
)
def
get_all_measurements_for_sensor
(
self
,
sensorID
:
int
,
startDateTime
:
str
,
endDateTime
:
str
)
->
List
[
Dict
[
str
,
str
]]:
if
startDateTime
and
endDateTime
:
return
self
.
_query
(
f
'
SELECT * FROM
{
self
.
TABLE_NAME
}
WHERE sensor_id = ?
'
f
'
AND DATETIME(timestamp) BETWEEN DATETIME(?) AND DATETIME(?)
'
f
'
ORDER BY datetime(timestamp) DESC
'
,
sensorID
,
startDateTime
,
endDateTime
,
fetch_type
=
FetchType
.
ALL
)
return
self
.
_query
(
f
'
SELECT * FROM
{
self
.
TABLE_NAME
}
WHERE sensor_id = ? ORDER BY datetime(timestamp) DESC
'
,
sensorID
,
fetch_type
=
FetchType
.
ALL
)
def
get_latest_measurements_for_sensor
(
self
,
sensorID
:
int
)
->
Dict
[
str
,
str
]
or
None
:
return
self
.
_query
(
f
'
SELECT * FROM
{
self
.
TABLE_NAME
}
WHERE sensor_id = ?
'
f
'
ORDER BY datetime(timestamp) DESC LIMIT 1
'
,
sensorID
,
fetch_type
=
FetchType
.
ONE
)
This diff is collapsed.
Click to expand it.
src/logic/databaseNew/Crud.py
+
7
−
0
View file @
e44f1e1e
...
...
@@ -83,6 +83,13 @@ def get_measurements_for_sensor(db: Session, startDateTime: str, endDateTime: st
return
db
.
query
(
Models
.
Measurement
).
filter
(
Models
.
Measurement
.
sensorId
==
sensorId
).
all
()
def
get_latest_measurement_for_sensor
(
db
:
Session
,
sensorId
:
int
):
return
db
.
query
(
Models
.
Measurement
)
\
.
filter
(
Models
.
Measurement
.
sensorId
==
sensorId
)
\
.
order_by
(
Models
.
Measurement
.
timestamp
.
desc
())
\
.
first
()
def
get_measurement
(
db
:
Session
,
measurementId
:
int
):
return
db
.
query
(
Models
.
Measurement
).
filter
(
Models
.
Measurement
.
id
==
measurementId
).
first
()
...
...
This diff is collapsed.
Click to expand it.
src/routers/SensorRouter.py
+
10
−
0
View file @
e44f1e1e
...
...
@@ -75,3 +75,13 @@ async def get_sensor_measurements(sensorId: int,
if
sensor
is
None
:
raise
HTTPException
(
status_code
=
404
,
detail
=
'
Sensor not found
'
)
return
Crud
.
get_measurements_for_sensor
(
db
,
startDateTime
,
endDateTime
,
sensorId
)
@router.get
(
'
/{sensorId}/measurements/latest
'
,
response_model
=
Schemas
.
Measurement
,
summary
=
'
Gets the latest measurement for a specific sensor
'
,
responses
=
{
404
:
{
'
description
'
:
'
Sensor not found
'
}})
async
def
get_latest_measurements_for_sensor
(
sensorId
:
int
,
db
:
Session
=
Depends
(
get_database
)):
sensor
=
Crud
.
get_sensor
(
db
,
sensorId
=
sensorId
)
if
sensor
is
None
:
raise
HTTPException
(
status_code
=
404
,
detail
=
'
Sensor not found
'
)
return
Crud
.
get_latest_measurement_for_sensor
(
db
,
sensorId
)
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