Skip to content

Commit

Permalink
refactor: changes to asset sensors API and ISO formatter logic on for…
Browse files Browse the repository at this point in the history
…ntend

Signed-off-by: joshuaunity <[email protected]>
  • Loading branch information
joshuaunity committed Sep 27, 2024
1 parent dc3e8df commit 807f0d0
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 37 deletions.
12 changes: 11 additions & 1 deletion flexmeasures/api/v3_0/assets.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import json
from humanize import naturaldelta

from flask import current_app
from flask_classful import FlaskView, route
Expand Down Expand Up @@ -36,6 +37,7 @@

asset_schema = AssetSchema()
assets_schema = AssetSchema(many=True)
sensor_schema = SensorSchema()
sensors_schema = SensorSchema(many=True)
partial_asset_schema = AssetSchema(partial=True, exclude=["account_id"])

Expand Down Expand Up @@ -273,8 +275,16 @@ def asset_sensors(
select(func.count(Sensor.id)).where(query_statement)
)

sensors_response: list = [
{
**sensor_schema.dump(sensor),
"event_resolution": naturaldelta(sensor.event_resolution),
}
for sensor in select_pagination.items
]

response = {
"data": sensors_schema.dump(select_pagination.items, many=True),
"data": sensors_response,
"num-records": num_records,
"filtered-records": select_pagination.total,
}
Expand Down
2 changes: 1 addition & 1 deletion flexmeasures/api/v3_0/sensors.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def index(
if page is None:
return sensors_response, 200
else:
num_records = db.session.execute(sensor_query).scalars().count()
num_records = len(db.session.execute(sensor_query).scalars().all())
select_pagination = db.paginate(sensor_query, per_page=per_page, page=page)
response = {
"data": sensors_response,
Expand Down
3 changes: 0 additions & 3 deletions flexmeasures/api/v3_0/tests/test_assets_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,6 @@ def test_fetch_asset_sensors(client, setup_api_test_data, requesting_user):
"""
Retrieve all sensors associated with a specific asset.
The response will include metadata such as the total number of records and
filtered records when pagination is applied.
This test checks for these metadata fields and the number of sensors returned, as well as
confirming that the response is a list of dictionaries, each containing a valid unit.
"""
Expand Down
5 changes: 4 additions & 1 deletion flexmeasures/data/services/sensors.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ def get_sensors(
:param sensor_name_allowlist: optionally, allow only sensors whose name is in this list
"""
sensor_query = sa.select(Sensor)
accounts: list = [account] if account else []
if isinstance(account, list):
accounts = account
else:
accounts: list = [account] if account else []
account_ids: list = [acc.id for acc in accounts]

sensor_query = sensor_query.join(
Expand Down
16 changes: 0 additions & 16 deletions flexmeasures/ui/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -806,22 +806,6 @@
</script>

<script>
// Format ISO 8601 duration using luxon
function formatResolution(isoDuration) {
const duration = Duration.fromISO(isoDuration);

// Convert to a human-readable string
let result = "";
if (duration.hours) {
result += `${duration.hours} hour${duration.hours > 1 ? 's' : ''}`;
}
if (duration.minutes) {
if (result) result += " and ";
result += `${duration.minutes} minute${duration.minutes > 1 ? 's' : ''}`;
}
return result || isoDuration;
}

function User(
id,
username,
Expand Down
16 changes: 1 addition & 15 deletions flexmeasures/ui/templates/crud/asset.html
Original file line number Diff line number Diff line change
Expand Up @@ -278,14 +278,6 @@ <h3>All sensors for {{ asset.name }}</h3>
<div class="table-responsive">

<table class="table table-striped paginate nav-on-click" title="View data" id="sensorsTable">
<div class="col-2 col-auto ms-auto">
<select class="form-select form-select-sm" id="unitFilterOptions">
<option selected value="">Filter Unit</option>
<option value="m/s">m/s</option>
<option value="°C">°C</option>
<option value="kW/m²">kW/m²</option>
</select>
</div>
</table>
</div>
</div>
Expand Down Expand Up @@ -401,11 +393,7 @@ <h3>All child assets for {{ asset.name }}</h3>
});
</script>

<script src="https://cdn.jsdelivr.net/npm/luxon/build/global/luxon.min.js"></script>
<script>
// Import luxon
const { Duration } = luxon;

function Sensor(
id,
name,
Expand All @@ -417,9 +405,7 @@ <h3>All child assets for {{ asset.name }}</h3>
this.id = id;
this.name = name;
this.unit = unit;

this.resolution = formatResolution(resolution);

this.resolution = resolution
this.entity_address = entity_address;
this.url = `/sensors/${id}`;
}
Expand Down

0 comments on commit 807f0d0

Please sign in to comment.