Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 460 remember selected calendar period within session #467

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
4 changes: 3 additions & 1 deletion flexmeasures/ui/crud/assets.py
@@ -1,7 +1,7 @@
from typing import Union, Optional, List, Tuple
import copy

from flask import url_for, current_app
from flask import url_for, current_app, session
from flask_classful import FlaskView
from flask_wtf import FlaskForm
from flask_security import login_required, current_user
Expand Down Expand Up @@ -237,6 +237,8 @@ def get(self, id: str):
asset=asset,
asset_form=asset_form,
msg="",
event_starts_after=session.get("event_starts_after"),
event_ends_before=session.get("event_ends_before"),
latest_measurement_time_str=latest_measurement_time_str,
asset_plot_html=asset_plot_html,
mapboxAccessToken=current_app.config.get("MAPBOX_ACCESS_TOKEN", ""),
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
2 changes: 2 additions & 0 deletions flexmeasures/ui/utils/view_utils.py
Expand Up @@ -129,6 +129,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
4 changes: 3 additions & 1 deletion flexmeasures/ui/views/sensors.py
@@ -1,7 +1,7 @@
import json

from altair.utils.html import spec_to_html
from flask import current_app
from flask import current_app, session
from flask_classful import FlaskView, route
from flask_security import auth_required, login_required
from marshmallow import fields
Expand Down Expand Up @@ -69,5 +69,7 @@ def get(self, id: int):
return render_flexmeasures_template(
"views/sensors.html",
sensor_id=id,
event_starts_after=session.get("event_starts_after"),
nhoening marked this conversation as resolved.
Show resolved Hide resolved
event_ends_before=session.get("event_ends_before"),
msg="",
)