Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions backend/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ def handle_unsubscribe_cells(data):
from .resources.cell_tags import CellTags, CellTagDetail, CellsByTag
from .resources.cell_users import CellUsers, CellUserDetail, CellByUser, CellShare
from .resources.logger import Logger
from .resources.cell_sensors import CellSensors

from .auth.routes import auth

Expand All @@ -183,6 +184,7 @@ def handle_unsubscribe_cells(data):
api.add_resource(SensorData, "/sensor/")
api.add_resource(SensorData_Json, "/sensor_json/")
api.add_resource(DataAvailability, "/data-availability/")
api.add_resource(CellSensors, "/cell-sensors/")
api.add_resource(Session_r, "/session")
api.add_resource(User_Data, "/user")
api.add_resource(Status, "/status/<string:id>")
Expand Down
42 changes: 42 additions & 0 deletions backend/api/resources/cell_sensors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from flask import request
from flask_restful import Resource
from ..models.sensor import Sensor


class CellSensors(Resource):
def get(self):
"""Get distinct sensor names that exist for specified cells

Query Parameters:
- cell_ids: Comma-separated list of cell IDs

Returns:
- Dict mapping cell_id (string) to list of sensor names that have data
"""
cell_ids_param = request.args.get("cell_ids")

if cell_ids_param is None:
return {"error": "cell_ids parameter is required"}, 400

try:
cell_ids = [
int(id.strip()) for id in cell_ids_param.split(",") if id.strip()
]
except ValueError:
return {"error": "Invalid cell_ids format"}, 400

if not cell_ids:
return {"error": "At least one valid cell_id is required"}, 400

rows = (
Sensor.query.filter(Sensor.cell_id.in_(cell_ids))
.with_entities(Sensor.cell_id, Sensor.name)
.distinct()
.all()
)

result = {}
for cell_id, name in rows:
result.setdefault(str(cell_id), []).append(name)

return result, 200
32 changes: 30 additions & 2 deletions backend/api/resources/sensor_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,42 @@ class SensorData(Resource):
get_sensor_data_schema = GetSensorDataSchema()

def get(self):
"""Gets specified sensor data"""
"""Gets specified sensor data

Supports two modes:
- Single cell: pass cellId (int) — returns sensor data object directly
- Batch cells: pass cellIds (comma-separated ints) — returns {cell_id: data_obj}
"""

# get args
v_args = self.get_sensor_data_schema.load(request.args)
stream = v_args.get("stream", False)
resample = v_args.get("resample", "hour")

# get data
cell_ids_raw = v_args.get("cellIds")
if cell_ids_raw:
# Batch mode: return data for all requested cells in one response
try:
cell_ids = [
int(x.strip()) for x in cell_ids_raw.split(",") if x.strip()
]
except ValueError:
return {"error": "Invalid cellIds format"}, 400

result = {}
for cid in cell_ids:
result[str(cid)] = Sensor.get_sensor_data_obj(
name=v_args["name"],
cell_id=cid,
measurement=v_args["measurement"],
resample=resample,
start_time=v_args.get("startTime"),
end_time=v_args.get("endTime"),
stream=stream,
)
return jsonify(result)

# Single cell mode (existing behaviour)
sensor_data_obj = Sensor.get_sensor_data_obj(
name=v_args["name"],
cell_id=v_args["cellId"],
Expand Down
3 changes: 2 additions & 1 deletion backend/api/schemas/get_sensor_data_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
class GetSensorDataSchema(ma.SQLAlchemySchema):
"""validates get request for sensor data"""

cellId = ma.Int()
cellId = ma.Int(required=False)
cellIds = ma.String(required=False, load_default=None)
name = ma.String()
measurement = ma.String()
resample = ma.String(required=False)
Expand Down
Loading