Skip to content

Commit d8499f7

Browse files
committed
feat: add selectable fields to measurement query
1 parent 9d17bb6 commit d8499f7

2 files changed

Lines changed: 58 additions & 18 deletions

File tree

rest_api/main.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ def get_measurements(
157157
offset=None,
158158
start_time=None,
159159
end_time=None,
160+
fields=[],
160161
conn=Depends(partial(get_db_connection, connection_pool)),
161162
user: UserInfo = Depends(get_current_user),
162163
):
@@ -165,13 +166,17 @@ def get_measurements(
165166
limit = int(limit)
166167
if offset is not None:
167168
offset = int(offset)
169+
if fields is not None:
170+
fields = [f.strip() for f in fields.split(",")]
168171

169172
measurements = fetch_measurements_for_sensor(
170-
conn, sensor_name, start_time, end_time
173+
conn, sensor_name, start_time, end_time, fields
171174
)
172175

173176
if len(measurements) == 0:
174-
return ORJSONResponse(content={"error": "Sensor not found"}, status_code=404)
177+
return ORJSONResponse(
178+
content={"error": "Sensor not found or data not available"}, status_code=404
179+
)
175180
# Calculate pagination details
176181
total_measurements = len(measurements)
177182

rest_api/src/handlers.py

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def fetch_table_names_from_app_id(conn, app_id: int) -> list | None:
7575

7676

7777
def fetch_measurements_for_sensor(
78-
conn, sensor_name: str, start_time: str | None, end_time: str | None
78+
conn, sensor_name: str, start_time: str | None, end_time: str | None, fields: list
7979
) -> list:
8080
"""Fetch all measurements for a given sensor_name from all relevant tables."""
8181
with conn.cursor() as cur:
@@ -120,22 +120,57 @@ def fetch_measurements_for_sensor(
120120

121121
# Step 3: Query each table for its measurement_ids
122122
for table_name, ids in table_to_ids.items():
123-
query = sql.SQL("SELECT * FROM {} WHERE id = ANY(%s)").format(
124-
sql.Identifier(table_name)
125-
)
126-
cur.execute(query, (ids,))
127-
columns = [desc[0] for desc in cur.description]
128-
for row in cur.fetchall():
129-
data = {col: val for col, val in zip(columns[3:], row[3:])}
130-
filtered_data = {k: v for k, v in data.items() if v is not None}
131-
measurements.append(
132-
Measurement(
133-
# sensor_id=row[1],
134-
table_name=table_name,
135-
timestamp=str(row[2]),
136-
data=filtered_data,
137-
)
123+
if fields:
124+
select_fields = sql.SQL(", ").join(
125+
[
126+
sql.Identifier(f)
127+
for f in ["id", "sensor_id", "timestamp"] + fields
128+
]
138129
)
130+
else:
131+
select_fields = sql.SQL("*")
132+
query = sql.SQL("SELECT {} FROM {} WHERE id = ANY(%s)").format(
133+
select_fields, sql.Identifier(table_name)
134+
)
135+
print(f"Executing query on table {table_name} for IDs: {ids}")
136+
print(f"Query: {query.as_string(conn)}")
137+
138+
try:
139+
cur.execute(query, (ids,))
140+
if fields:
141+
columns = ["id", "sensor_id", "timestamp"] + fields
142+
else:
143+
columns = [desc[0] for desc in cur.description]
144+
for row in cur.fetchall():
145+
data = {col: val for col, val in zip(columns[3:], row[3:])}
146+
filtered_data = {k: v for k, v in data.items() if v is not None}
147+
148+
if filtered_data:
149+
measurements.append(
150+
Measurement(
151+
table_name=table_name,
152+
timestamp=str(row[2]),
153+
data=filtered_data,
154+
)
155+
)
156+
except Exception as e:
157+
print(f"Error executing query on table {table_name}: {e}")
158+
# query = sql.SQL("SELECT * FROM {} WHERE id = ANY(%s)").format(
159+
# sql.Identifier(table_name)
160+
# )
161+
# cur.execute(query, (ids,))
162+
# columns = [desc[0] for desc in cur.description]
163+
# for row in cur.fetchall():
164+
# data = {col: val for col, val in zip(columns[3:], row[3:])}
165+
# filtered_data = {k: v for k, v in data.items() if v is not None}
166+
# measurements.append(
167+
# Measurement(
168+
# # sensor_id=row[1],
169+
# table_name=table_name,
170+
# timestamp=str(row[2]),
171+
# data=filtered_data,
172+
# )
173+
# )
139174

140175
# Now sort all measurements by timestamp descending
141176
measurements.sort(key=lambda x: x.timestamp, reverse=True)

0 commit comments

Comments
 (0)