Skip to content

Commit 8b254ad

Browse files
Reapply "feat: query sensors only if they exist, batch sensor data requests"
This reverts commit 684417b.
1 parent 3c62f4c commit 8b254ad

8 files changed

Lines changed: 412 additions & 263 deletions

File tree

backend/api/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ def handle_unsubscribe_cells(data):
170170
from .resources.cell_tags import CellTags, CellTagDetail, CellsByTag
171171
from .resources.cell_users import CellUsers, CellUserDetail, CellByUser, CellShare
172172
from .resources.logger import Logger
173+
from .resources.cell_sensors import CellSensors
173174

174175
from .auth.routes import auth
175176

@@ -183,6 +184,7 @@ def handle_unsubscribe_cells(data):
183184
api.add_resource(SensorData, "/sensor/")
184185
api.add_resource(SensorData_Json, "/sensor_json/")
185186
api.add_resource(DataAvailability, "/data-availability/")
187+
api.add_resource(CellSensors, "/cell-sensors/")
186188
api.add_resource(Session_r, "/session")
187189
api.add_resource(User_Data, "/user")
188190
api.add_resource(Status, "/status/<string:id>")
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from flask import request
2+
from flask_restful import Resource
3+
from ..models.sensor import Sensor
4+
5+
6+
class CellSensors(Resource):
7+
def get(self):
8+
"""Get distinct sensor names that exist for specified cells
9+
10+
Query Parameters:
11+
- cell_ids: Comma-separated list of cell IDs
12+
13+
Returns:
14+
- Dict mapping cell_id (string) to list of sensor names that have data
15+
"""
16+
cell_ids_param = request.args.get("cell_ids")
17+
18+
if cell_ids_param is None:
19+
return {"error": "cell_ids parameter is required"}, 400
20+
21+
try:
22+
cell_ids = [
23+
int(id.strip()) for id in cell_ids_param.split(",") if id.strip()
24+
]
25+
except ValueError:
26+
return {"error": "Invalid cell_ids format"}, 400
27+
28+
if not cell_ids:
29+
return {"error": "At least one valid cell_id is required"}, 400
30+
31+
rows = (
32+
Sensor.query.filter(Sensor.cell_id.in_(cell_ids))
33+
.with_entities(Sensor.cell_id, Sensor.name)
34+
.distinct()
35+
.all()
36+
)
37+
38+
result = {}
39+
for cell_id, name in rows:
40+
result.setdefault(str(cell_id), []).append(name)
41+
42+
return result, 200

backend/api/resources/sensor_data.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,42 @@ class SensorData(Resource):
4141
get_sensor_data_schema = GetSensorDataSchema()
4242

4343
def get(self):
44-
"""Gets specified sensor data"""
44+
"""Gets specified sensor data
45+
46+
Supports two modes:
47+
- Single cell: pass cellId (int) — returns sensor data object directly
48+
- Batch cells: pass cellIds (comma-separated ints) — returns {cell_id: data_obj}
49+
"""
4550

4651
# get args
4752
v_args = self.get_sensor_data_schema.load(request.args)
4853
stream = v_args.get("stream", False)
4954
resample = v_args.get("resample", "hour")
5055

51-
# get data
56+
cell_ids_raw = v_args.get("cellIds")
57+
if cell_ids_raw:
58+
# Batch mode: return data for all requested cells in one response
59+
try:
60+
cell_ids = [
61+
int(x.strip()) for x in cell_ids_raw.split(",") if x.strip()
62+
]
63+
except ValueError:
64+
return {"error": "Invalid cellIds format"}, 400
65+
66+
result = {}
67+
for cid in cell_ids:
68+
result[str(cid)] = Sensor.get_sensor_data_obj(
69+
name=v_args["name"],
70+
cell_id=cid,
71+
measurement=v_args["measurement"],
72+
resample=resample,
73+
start_time=v_args.get("startTime"),
74+
end_time=v_args.get("endTime"),
75+
stream=stream,
76+
)
77+
return jsonify(result)
78+
79+
# Single cell mode (existing behaviour)
5280
sensor_data_obj = Sensor.get_sensor_data_obj(
5381
name=v_args["name"],
5482
cell_id=v_args["cellId"],

backend/api/schemas/get_sensor_data_schema.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
class GetSensorDataSchema(ma.SQLAlchemySchema):
55
"""validates get request for sensor data"""
66

7-
cellId = ma.Int()
7+
cellId = ma.Int(required=False)
8+
cellIds = ma.String(required=False, load_default=None)
89
name = ma.String()
910
measurement = ma.String()
1011
resample = ma.String(required=False)

0 commit comments

Comments
 (0)