Skip to content

Commit

Permalink
Merge pull request #148 from hydroserver2/29-sensorthings-bug-fixes
Browse files Browse the repository at this point in the history
29 sensorthings bug fixes
  • Loading branch information
kjlippold authored Oct 30, 2023
2 parents 60210cc + d0e666b commit 4b3ffb6
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 35 deletions.
2 changes: 1 addition & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ dependencies:

- pip:
- Authlib==1.2.1
- hydroserver-sensorthings==0.2.2
- hydroserver-sensorthings==0.2.3
- django-ninja-jwt==5.2.5
- hydroloader==0.1.15
- sqlalchemy-json==0.5.0
Expand Down
1 change: 1 addition & 0 deletions hydroserver/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@
# SensorThings Configuration

ST_API_PREFIX = 'api/sensorthings'
ST_API_ID_QUALIFIER = "'"


# # We need to patch Django Ninja's OpenAPISchema "methods" method to create a unique operationId for endpoints
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
django==4.1
django-ninja==0.20.0
django-ninja-jwt==5.2.5
hydroserver-sensorthings==0.2.2
hydroserver-sensorthings==0.2.3
hydroloader==0.1.15
dj-database-url==1.3.0
python-decouple==3.7
Expand Down
1 change: 0 additions & 1 deletion stapi/engine/components/datastreams.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ def get_datastreams(
'definition': datastream.unit.definition.split(';')[0]
},
'observation_type': datastream.observation_type,
'observed_area': {},
'phenomenon_time': getattr(self, 'iso_time_interval')(
datastream.phenomenon_begin_time, datastream.phenomenon_end_time
),
Expand Down
24 changes: 23 additions & 1 deletion stapi/engine/components/observations.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import List
from core.endpoints.observations.utils import query_observations
from core.endpoints.resultqualifier.utils import query_result_qualifiers, check_result_qualifier_by_id
from core.endpoints.datastream.utils import check_datastream_by_id
from core.endpoints.datastream.utils import check_datastream_by_id, get_datastream_by_id
from core.models import Observation
from sensorthings.components.observations.engine import ObservationBaseEngine
from stapi.engine.utils import SensorThingsUtils
Expand Down Expand Up @@ -142,6 +142,8 @@ def create_observation(
result_qualifiers=observation.result_quality.result_qualifiers if observation.result_quality else []
)

self.update_value_count(datastream_id=observation.datastream.id)

return new_observation.id

def create_observation_bulk(
Expand Down Expand Up @@ -185,6 +187,8 @@ def create_observation_bulk(

new_observations.extend(new_observations_for_datastream)

self.update_value_count(datastream_id=datastream_id)

return [
observation.id for observation in new_observations
]
Expand All @@ -201,3 +205,21 @@ def delete_observation(
observation_id: str
) -> None:
pass

def update_value_count(
self,
datastream_id: UUID
) -> None:

observation_query, _ = query_observations(
user=getattr(getattr(self, 'request', None), 'authenticated_user', None),
datastream_ids=[datastream_id]
)

datastream = get_datastream_by_id(
user=getattr(getattr(self, 'request', None), 'authenticated_user', None),
datastream_id=datastream_id
)

datastream.value_count = int(observation_query.count())
datastream.save()
62 changes: 31 additions & 31 deletions tests/test_sensorthings_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ def base_url():

@pytest.mark.parametrize('endpoint, query_params', [
('Things', {}),
('Things(9344a3d4-a45a-4529-b731-b51149b4d1b8)/Datastreams', {}),
('Things(9344a3d4-a45a-4529-b731-b51149b4d1b8)/Locations', {}),
('Things(\'9344a3d4-a45a-4529-b731-b51149b4d1b8\')/Datastreams', {}),
('Things(\'9344a3d4-a45a-4529-b731-b51149b4d1b8\')/Locations', {}),
('Things', {'$count': True}),
('Things', {'$filter': 'name eq \'test\''}),
('Things', {'$skip': 10}),
('Things', {'$top': 10}),
('Things', {'$expand': 'Locations,Datastreams/Sensor'}),
('Locations', {}),
('Locations(1796a56f-2cdf-42c6-8cc7-3da2f757e9a0)/Things', {}),
('Locations(1796a56f-2cdf-42c6-8cc7-3da2f757e9a0)/HistoricalLocations', {}),
('Locations(\'1796a56f-2cdf-42c6-8cc7-3da2f757e9a0\')/Things', {}),
('Locations(\'1796a56f-2cdf-42c6-8cc7-3da2f757e9a0\')/HistoricalLocations', {}),
('Locations', {'$count': True}),
('Locations', {'$filter': 'name eq \'test\''}),
('Locations', {'$skip': 10}),
Expand All @@ -39,21 +39,21 @@ def base_url():
('HistoricalLocations', {'$skip': 10}),
('HistoricalLocations', {'$top': 10}),
('Sensors', {}),
('Sensors(90d7f4a5-2042-4840-9bb4-b991f49cb8ed)/Datastreams', {}),
('Sensors(\'90d7f4a5-2042-4840-9bb4-b991f49cb8ed\')/Datastreams', {}),
('Sensors', {'$count': True}),
('Sensors', {'$filter': 'name eq \'test\''}),
('Sensors', {'$skip': 10}),
('Sensors', {'$top': 10}),
('Sensors', {'$expand': 'Datastreams/Thing'}),
('ObservedProperties', {}),
('ObservedProperties(97f5e0b8-e1e9-4c65-9b98-0438cdfb4a19)/Datastreams', {}),
('ObservedProperties(\'97f5e0b8-e1e9-4c65-9b98-0438cdfb4a19\')/Datastreams', {}),
('ObservedProperties', {'$count': True}),
('ObservedProperties', {'$filter': 'name eq \'test\''}),
('ObservedProperties', {'$skip': 10}),
('ObservedProperties', {'$top': 10}),
('ObservedProperties', {'$expand': 'Datastreams/Thing'}),
('Datastreams', {}),
('Datastreams(ca999458-d644-44b0-b678-09a892fd54ac)/Observations', {}),
('Datastreams(\'ca999458-d644-44b0-b678-09a892fd54ac\')/Observations', {}),
('Datastreams', {'$count': True}),
('Datastreams', {'$filter': 'name eq \'ca999458-d644-44b0-b678-09a892fd54ac\''}),
('Datastreams', {'$skip': 10}),
Expand Down Expand Up @@ -87,32 +87,32 @@ def test_sensorthings_list_endpoints(auth_headers, base_url, endpoint, query_par

@pytest.mark.parametrize('endpoint, query_params, status_code', [
('', {}, 200),
('Things(9344a3d4-a45a-4529-b731-b51149b4d1b8)', {}, 200),
('Things(9344a3d4-a45a-4529-b731-b51149b4d1b8)', {'$expand': 'Locations,Datastreams/Sensor'}, 200),
('Things(00000000-0000-0000-0000-000000000000)', {}, 404),
('Locations(1796a56f-2cdf-42c6-8cc7-3da2f757e9a0)', {}, 200),
('Locations(1796a56f-2cdf-42c6-8cc7-3da2f757e9a0)', {'$expand': 'Things/Datastreams/ObservedProperty'}, 200),
('Locations(00000000-0000-0000-0000-000000000000)', {}, 404),
('HistoricalLocations(00000000-0000-0000-0000-000000000000)', {}, 404),
('Sensors(90d7f4a5-2042-4840-9bb4-b991f49cb8ed)', {}, 200),
('Sensors(90d7f4a5-2042-4840-9bb4-b991f49cb8ed)', {'$expand': 'Datastreams/Thing'}, 200),
('Sensors(00000000-0000-0000-0000-000000000000)', {}, 404),
('ObservedProperties(97f5e0b8-e1e9-4c65-9b98-0438cdfb4a19)', {}, 200),
('ObservedProperties(97f5e0b8-e1e9-4c65-9b98-0438cdfb4a19)', {'$expand': 'Datastreams/Thing'}, 200),
('ObservedProperties(00000000-0000-0000-0000-000000000000)', {}, 404),
('Datastreams(376be82c-b3a1-4d96-821b-c7954b931f94)', {}, 200),
('Datastreams(376be82c-b3a1-4d96-821b-c7954b931f94)', {
('Things(\'9344a3d4-a45a-4529-b731-b51149b4d1b8\')', {}, 200),
('Things(\'9344a3d4-a45a-4529-b731-b51149b4d1b8\')', {'$expand': 'Locations,Datastreams/Sensor'}, 200),
('Things(\'00000000-0000-0000-0000-000000000000\')', {}, 404),
('Locations(\'1796a56f-2cdf-42c6-8cc7-3da2f757e9a0\')', {}, 200),
('Locations(\'1796a56f-2cdf-42c6-8cc7-3da2f757e9a0\')', {'$expand': 'Things/Datastreams/ObservedProperty'}, 200),
('Locations(\'00000000-0000-0000-0000-000000000000\')', {}, 404),
('HistoricalLocations(\'00000000-0000-0000-0000-000000000000\')', {}, 404),
('Sensors(\'90d7f4a5-2042-4840-9bb4-b991f49cb8ed\')', {}, 200),
('Sensors(\'90d7f4a5-2042-4840-9bb4-b991f49cb8ed\')', {'$expand': 'Datastreams/Thing'}, 200),
('Sensors(\'00000000-0000-0000-0000-000000000000\')', {}, 404),
('ObservedProperties(\'97f5e0b8-e1e9-4c65-9b98-0438cdfb4a19\')', {}, 200),
('ObservedProperties(\'97f5e0b8-e1e9-4c65-9b98-0438cdfb4a19\')', {'$expand': 'Datastreams/Thing'}, 200),
('ObservedProperties(\'00000000-0000-0000-0000-000000000000\')', {}, 404),
('Datastreams(\'376be82c-b3a1-4d96-821b-c7954b931f94\')', {}, 200),
('Datastreams(\'376be82c-b3a1-4d96-821b-c7954b931f94\')', {
'$expand': 'Observations,Thing/Locations,Sensor,ObservedProperty'
}, 200),
('Datastreams(00000000-0000-0000-0000-000000000000)', {}, 404),
('Datastreams(ca999458-d644-44b0-b678-09a892fd54ac)/Sensor', {}, 200),
('Datastreams(ca999458-d644-44b0-b678-09a892fd54ac)/ObservedProperty', {}, 200),
('Datastreams(ca999458-d644-44b0-b678-09a892fd54ac)/Thing', {}, 200),
('Observations(002bc4e1-3b3e-448b-a91f-346f968b988c)', {}, 200),
('Observations(002bc4e1-3b3e-448b-a91f-346f968b988c)', {'$expand': 'Datastream/Thing'}, 200),
('Observations(00000000-0000-0000-0000-000000000000)', {}, 404),
('Observations(002bc4e1-3b3e-448b-a91f-346f968b988c)/Datastream', {}, 200),
('FeaturesOfInterest(00000000-0000-0000-0000-000000000000)', {}, 404),
('Datastreams(\'00000000-0000-0000-0000-000000000000\')', {}, 404),
('Datastreams(\'ca999458-d644-44b0-b678-09a892fd54ac\')/Sensor', {}, 200),
('Datastreams(\'ca999458-d644-44b0-b678-09a892fd54ac\')/ObservedProperty', {}, 200),
('Datastreams(\'ca999458-d644-44b0-b678-09a892fd54ac\')/Thing', {}, 200),
('Observations(\'002bc4e1-3b3e-448b-a91f-346f968b988c\')', {}, 200),
('Observations(\'002bc4e1-3b3e-448b-a91f-346f968b988c\')', {'$expand': 'Datastream/Thing'}, 200),
('Observations(\'00000000-0000-0000-0000-000000000000\')', {}, 404),
('Observations(\'002bc4e1-3b3e-448b-a91f-346f968b988c\')/Datastream', {}, 200),
('FeaturesOfInterest(\'00000000-0000-0000-0000-000000000000\')', {}, 404),
])
@pytest.mark.django_db()
def test_sensorthings_get_endpoints(
Expand Down

0 comments on commit 4b3ffb6

Please sign in to comment.