Skip to content

Commit

Permalink
Remember the selected time period within the user's session.
Browse files Browse the repository at this point in the history
I chose to set the session data with the /chart instead of the /chart_data endpoints (for sensors and assets), because the former endpoint can be called without the need to call the latter (using include_data=true). Right now both the sensor and asset page are calling the /chart endpoint with each date pick.
  • Loading branch information
Flix6x committed Jul 23, 2022
1 parent ed890c2 commit 327b6eb
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 17 deletions.
2 changes: 2 additions & 0 deletions flexmeasures/api/dev/sensors.py
Expand Up @@ -18,6 +18,7 @@
from flexmeasures.data.models.generic_assets import GenericAsset
from flexmeasures.data.models.time_series import Sensor
from flexmeasures.data.services.annotations import prepare_annotations_for_chart
from flexmeasures.ui.utils.view_utils import set_time_range_for_session


class SensorAPI(FlaskView):
Expand Down Expand Up @@ -55,6 +56,7 @@ def get_chart(self, id: int, sensor: Sensor, **kwargs):
.. :quickref: Chart; Download a chart with time series
"""
set_time_range_for_session()
return json.dumps(sensor.chart(**kwargs))

@route("/<id>/chart_data/")
Expand Down
2 changes: 2 additions & 0 deletions flexmeasures/api/v3_0/assets.py
Expand Up @@ -15,6 +15,7 @@
from flexmeasures.data.schemas.generic_assets import GenericAssetSchema as AssetSchema
from flexmeasures.api.common.schemas.generic_assets import AssetIdField
from flexmeasures.api.common.schemas.users import AccountIdField
from flexmeasures.ui.utils.view_utils import set_time_range_for_session


asset_schema = AssetSchema()
Expand Down Expand Up @@ -282,6 +283,7 @@ def get_chart(self, id: int, asset: GenericAsset, **kwargs):
.. :quickref: Chart; Download a chart with time series
"""
set_time_range_for_session()
return json.dumps(asset.chart(**kwargs))

@route("/<id>/chart_data/")
Expand Down
27 changes: 20 additions & 7 deletions flexmeasures/ui/templates/crud/asset.html
Expand Up @@ -279,13 +279,26 @@ <h3>All sensors for {{ asset.name }}</h3>
start.setHours(0,0,0,0) // get start of first day
end.setHours(0,0,0,0) // get start of last day

// Initialize picker to the last 2 days of sensor data
var nearEnd = new Date(end)//.setDate(end.getDate() - 1)
nearEnd.setDate(nearEnd.getDate() - 1)
picker.setDateRange(
nearEnd,
end,
);
{% if event_starts_after and event_ends_before %}
// Initialize picker to the date selection specified in the session
var sessionStart = new Date('{{ event_starts_after }}');
var sessionEnd = new Date('{{ event_ends_before }}')
sessionEnd.setSeconds(sessionEnd.getSeconds() - 1); // -1 second in case most recent event ends at midnight
sessionStart.setHours(0,0,0,0) // get start of first day
sessionEnd.setHours(0,0,0,0) // get start of last day
picker.setDateRange(
sessionStart,
sessionEnd,
);
{% else %}
// Initialize picker to the last 2 days of sensor data
var nearEnd = new Date(end)//.setDate(end.getDate() - 1)
nearEnd.setDate(nearEnd.getDate() - 1)
picker.setDateRange(
nearEnd,
end,
);
{% endif %}

// No use looking for data in years outside timerange of sensor data
picker.setOptions({
Expand Down
27 changes: 20 additions & 7 deletions flexmeasures/ui/templates/views/sensors.html
Expand Up @@ -154,13 +154,26 @@
start.setHours(0,0,0,0) // get start of first day
end.setHours(0,0,0,0) // get start of last day

// Initialize picker to the last 2 days of sensor data
var nearEnd = new Date(end)//.setDate(end.getDate() - 1)
nearEnd.setDate(nearEnd.getDate() - 1)
picker.setDateRange(
nearEnd,
end,
);
{% if event_starts_after and event_ends_before %}
// Initialize picker to the date selection specified in the session
var sessionStart = new Date('{{ event_starts_after }}');
var sessionEnd = new Date('{{ event_ends_before }}')
sessionEnd.setSeconds(sessionEnd.getSeconds() - 1); // -1 second in case most recent event ends at midnight
sessionStart.setHours(0,0,0,0) // get start of first day
sessionEnd.setHours(0,0,0,0) // get start of last day
picker.setDateRange(
sessionStart,
sessionEnd,
);
{% else %}
// Initialize picker to the last 2 days of sensor data
var nearEnd = new Date(end)//.setDate(end.getDate() - 1)
nearEnd.setDate(nearEnd.getDate() - 1)
picker.setDateRange(
nearEnd,
end,
);
{% endif %}

// No use looking for data in years outside timerange of sensor data
picker.setOptions({
Expand Down
18 changes: 15 additions & 3 deletions flexmeasures/ui/utils/view_utils.py
Expand Up @@ -47,6 +47,9 @@ def render_flexmeasures_template(html_filename: str, **variables):
if "end_time" in session:
variables["end_time"] = session["end_time"]

variables["event_starts_after"] = session.get("event_starts_after")
variables["event_ends_before"] = session.get("event_ends_before")

variables["page"] = html_filename.split("/")[-1].replace(".html", "")
if "show_datepicker" not in variables:
variables["show_datepicker"] = variables["page"] in ("analytics", "portfolio")
Expand Down Expand Up @@ -113,10 +116,17 @@ def clear_session():


def set_time_range_for_session():
"""Set period (start_date, end_date and resolution) on session if they are not yet set.
The datepicker sends times as tz-aware UTC strings.
"""Set period on session if they are not yet set.
The daterangepicker sends times as tz-aware UTC strings.
We re-interpret them as being in the server's timezone.
Also set the forecast horizon, if given."""
Also set the forecast horizon, if given.
TODO: event_[stars|ends]_before are used on the new asset and sensor pages.
We simply store the UTC strings.
It might be that the other settings & logic can be deprecated when we clean house.
Tip: grep for timerangeEnd, where end_time is used in our base template,
and then used in the daterangepicker. We seem to use litepicker now.
"""
if "start_time" in request.values:
session["start_time"] = time_utils.localized_datetime(
iso8601.parse_date(request.values.get("start_time"))
Expand All @@ -129,6 +139,8 @@ def set_time_range_for_session():
): # session storage seems to lose tz info and becomes UTC
session["start_time"] = time_utils.as_server_time(session["start_time"])

session["event_starts_after"] = request.values.get("event_starts_after")
session["event_ends_before"] = request.values.get("event_ends_before")
if "end_time" in request.values:
session["end_time"] = time_utils.localized_datetime(
iso8601.parse_date(request.values.get("end_time"))
Expand Down

0 comments on commit 327b6eb

Please sign in to comment.