11from uuid import UUID
22from typing import Optional
33from ninja .errors import HttpError
4+ from django .db .utils import DataError , DatabaseError
45from sta .models import Datastream
56from sensorthings .components .datastreams .engine import DatastreamBaseEngine
67from sensorthings .components .datastreams .schemas import (Datastream as DatastreamSchema , DatastreamPostBody ,
@@ -60,26 +61,19 @@ def get_datastreams(
6061 count = None
6162
6263 if thing_ids :
63- datastreams = self .apply_rank (
64- component = DatastreamSchema ,
64+ datastreams = self .apply_window (
6565 queryset = datastreams ,
6666 partition_field = "thing_id" ,
67- filter_ids = thing_ids ,
68- max_records = 1
6967 )
7068 elif sensor_ids :
71- datastreams = self .apply_rank (
72- component = DatastreamSchema ,
69+ datastreams = self .apply_window (
7370 queryset = datastreams ,
7471 partition_field = "sensor_id" ,
75- filter_ids = sensor_ids
7672 )
7773 elif observed_property_ids :
78- datastreams = self .apply_rank (
79- component = DatastreamSchema ,
74+ datastreams = self .apply_window (
8075 queryset = datastreams ,
8176 partition_field = "observed_property_id" ,
82- filter_ids = observed_property_ids
8377 )
8478 else :
8579 if pagination :
@@ -90,47 +84,54 @@ def get_datastreams(
9084 )
9185 datastreams = datastreams .all ()
9286
93- return {
94- datastream .id : {
95- "id" : datastream .id ,
96- "name" : str (datastream .name ),
97- "description" : datastream .description ,
98- "thing_id" : datastream .thing_id ,
99- "sensor_id" : datastream .sensor_id ,
100- "observed_property_id" : datastream .observed_property_id ,
101- "unit_of_measurement" : {
102- "name" : datastream .unit .name ,
103- "symbol" : datastream .unit .symbol ,
104- "definition" : datastream .unit .definition .split (";" )[0 ]
105- },
106- "observation_type" : datastream .observation_type ,
107- "phenomenon_time" : getattr (self , "iso_time_interval" )(
108- datastream .phenomenon_begin_time , datastream .phenomenon_end_time
109- ),
110- "result_time" : getattr (self , "iso_time_interval" )(
111- datastream .result_begin_time , datastream .result_end_time
112- ),
113- "properties" : {
114- "result_type" : datastream .result_type ,
115- "status" : datastream .status ,
116- "sampled_medium" : datastream .sampled_medium ,
117- "value_count" : datastream .value_count ,
118- "no_data_value" : datastream .no_data_value ,
119- "processing_level_code" : datastream .processing_level .code ,
120- "intended_time_spacing" : datastream .intended_time_spacing ,
121- "intended_time_spacing_unit_of_measurement" : datastream .intended_time_spacing_unit ,
122- "aggregation_statistic" : datastream .aggregation_statistic ,
123- "time_aggregation_interval" : datastream .time_aggregation_interval ,
124- "time_aggregation_interval_unit_of_measurement" : datastream .time_aggregation_interval_unit ,
125- "workspace" : {
126- "id" : datastream .thing .workspace .id ,
127- "name" : datastream .thing .workspace .name ,
128- "link" : datastream .thing .workspace .link ,
129- "is_private" : datastream .thing .workspace .is_private
87+ try :
88+ return {
89+ datastream .id : {
90+ "id" : datastream .id ,
91+ "name" : str (datastream .name ),
92+ "description" : datastream .description ,
93+ "thing_id" : datastream .thing_id ,
94+ "sensor_id" : datastream .sensor_id ,
95+ "observed_property_id" : datastream .observed_property_id ,
96+ "unit_of_measurement" : {
97+ "name" : datastream .unit .name ,
98+ "symbol" : datastream .unit .symbol ,
99+ "definition" : datastream .unit .definition .split (";" )[0 ]
100+ },
101+ "observation_type" : datastream .observation_type ,
102+ "phenomenon_time" : getattr (self , "iso_time_interval" )(
103+ datastream .phenomenon_begin_time , datastream .phenomenon_end_time
104+ ),
105+ "result_time" : getattr (self , "iso_time_interval" )(
106+ datastream .result_begin_time , datastream .result_end_time
107+ ),
108+ "properties" : {
109+ "result_type" : datastream .result_type ,
110+ "status" : datastream .status ,
111+ "sampled_medium" : datastream .sampled_medium ,
112+ "value_count" : datastream .value_count ,
113+ "no_data_value" : datastream .no_data_value ,
114+ "processing_level_code" : datastream .processing_level .code ,
115+ "processing_level_id" : datastream .processing_level .id ,
116+ "unit_id" : datastream .unit .id ,
117+ "intended_time_spacing" : datastream .intended_time_spacing ,
118+ "intended_time_spacing_unit_of_measurement" : datastream .intended_time_spacing_unit ,
119+ "aggregation_statistic" : datastream .aggregation_statistic ,
120+ "time_aggregation_interval" : datastream .time_aggregation_interval ,
121+ "time_aggregation_interval_unit_of_measurement" : datastream .time_aggregation_interval_unit ,
122+ "is_private" : datastream .is_private ,
123+ "is_visible" : datastream .is_visible ,
124+ "workspace" : {
125+ "id" : datastream .thing .workspace .id ,
126+ "name" : datastream .thing .workspace .name ,
127+ "link" : datastream .thing .workspace .link ,
128+ "is_private" : datastream .thing .workspace .is_private
129+ }
130130 }
131- }
132- } for datastream in datastreams
133- }, count
131+ } for datastream in datastreams
132+ }, count
133+ except (DatabaseError , DataError ,) as e :
134+ raise HttpError (400 , str (e ))
134135
135136 def create_datastream (
136137 self ,
0 commit comments