Skip to content

Commit

Permalink
Tijdsrange live grafieken zelf kunnen instellen #947
Browse files Browse the repository at this point in the history
  • Loading branch information
dennissiemensma committed May 3, 2020
1 parent a73b3ee commit fe37858
Show file tree
Hide file tree
Showing 17 changed files with 52 additions and 50 deletions.
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Changelog

v3.9.0 - 2020-xx-xx

- [`#947 <https://github.com/dennissiemensma/dsmr-reader/issues/947>`_] Tijdsrange live grafieken zelf kunnen instellen
- [`#969 <https://github.com/dennissiemensma/dsmr-reader/issues/969>`_] In- en uitknijpen van de grafieken werkt niet meer
- [`#966 <https://github.com/dennissiemensma/dsmr-reader/issues/966>`_] Error in dsmr_backup_create --compact

Expand Down
2 changes: 1 addition & 1 deletion dsmr_frontend/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ class FrontendSettingsAdmin(SingletonModelAdmin):
(
_('Graphs'), {
'fields': [
'live_graphs_hours_range',
'electricity_graph_style',
'stack_electricity_graphs',
'gas_graph_style',
'dashboard_graph_width',
],
}
),
Expand Down
23 changes: 23 additions & 0 deletions dsmr_frontend/migrations/0035_graphs_range.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 3.0.5 on 2020-05-02 22:23

import django.core.validators
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('dsmr_frontend', '0034_mysql_timezone_support'),
]

operations = [
migrations.RemoveField(
model_name='frontendsettings',
name='dashboard_graph_width',
),
migrations.AddField(
model_name='frontendsettings',
name='live_graphs_hours_range',
field=models.IntegerField(default=24, help_text='The range of the data displayed in live graphs (increasing it may degrade rendering performance!)', validators=[django.core.validators.MinValueValidator(1), django.core.validators.MaxValueValidator(168)], verbose_name='Live graphs hours range'),
),
]
10 changes: 5 additions & 5 deletions dsmr_frontend/models/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ class FrontendSettings(ModelUpdateMixin, SingletonModel):
verbose_name=_('Temperature color'),
help_text=_("Graph color for temperatures read")
)
dashboard_graph_width = models.IntegerField(
default=30,
validators=[MinValueValidator(30), MaxValueValidator(60 * 60 * 24)],
verbose_name=_('Dashboard graph width'),
help_text=_("The number of items displayed on the X-axis of the dashboard graphs")
live_graphs_hours_range = models.IntegerField(
default=24,
validators=[MinValueValidator(1), MaxValueValidator(7 * 24)],
verbose_name=_('Live graphs hours range'),
help_text=_("The range of the data displayed in live graphs (increasing it may degrade rendering performance!)")
)
gas_graph_style = models.CharField(
max_length=4,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,6 @@ $(document).ready(function () {
/* Init graph. */
$.get(echarts_electricity_graph_url, function (xhr_data) {
echarts_electricity_graph.hideLoading();

/* Adjust default zooming to the number of default items we want to display. */
var zoom_percent = 100 - (dashboard_graph_width / xhr_data.read_at.length * 100);
echarts_electricity_initial_options.dataZoom[0].start = zoom_percent;
echarts_electricity_graph.setOption(echarts_electricity_initial_options);

/* Different set of options, to prevent the dataZoom being reset on each update. */
Expand Down
3 changes: 0 additions & 3 deletions dsmr_frontend/static/dsmr_frontend/js/dsmr-reader/live/gas.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ $(document).ready(function () {
$.get(echarts_gas_graph_url, function (xhr_data) {
echarts_gas_graph.hideLoading();

/* Adjust default zooming to the number of default items we want to display. */
var zoom_percent = 100 - (dashboard_graph_width / xhr_data.read_at.length * 100);

var option = {
color: [
gas_delivered_color
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,6 @@ $(document).ready(function () {
/* Init graph. */
$.get(echarts_phases_graph_url, function (xhr_data) {
echarts_phases_graph.hideLoading();

/* Adjust default zooming to the number of default items we want to display. */
var zoom_percent = 100 - (dashboard_graph_width / xhr_data.read_at.length * 100);
echarts_phases_initial_options.dataZoom[0].start = zoom_percent;
echarts_phases_graph.setOption(echarts_phases_initial_options);

/* Different set of options, to prevent the dataZoom being reset on each update. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,6 @@ $(document).ready(function () {
];
}

/* Adjust default zooming to the number of default items we want to display. */
var zoom_percent = 100 - (dashboard_graph_width / xhr_data.read_at.length * 100);
echarts_power_current_initial_options.dataZoom[0].start = zoom_percent;
echarts_power_current_graph.setOption(echarts_power_current_initial_options);

/* Different set of options, to prevent the dataZoom being reset on each update. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ $(document).ready(function () {
$.get(echarts_temperature_graph_url, function (xhr_data) {
echarts_temperature_graph.hideLoading();

/* Adjust default zooming to the number of default items we want to display. */
var zoom_percent = 100 - (dashboard_graph_width / xhr_data.read_at.length * 100);

var option = {
color: [
temperature_color
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,6 @@ $(document).ready(function () {
];
}

/* Adjust default zooming to the number of default items we want to display. */
var zoom_percent = 100 - (dashboard_graph_width / xhr_data.read_at.length * 100);
echarts_voltage_initial_options.dataZoom[0].start = zoom_percent;
echarts_voltage_graph.setOption(echarts_voltage_initial_options);

/* Different set of options, to prevent the dataZoom being reset on each update. */
Expand Down
1 change: 0 additions & 1 deletion dsmr_frontend/templates/dsmr_frontend/live-graphs.html
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@
<script type="text/javascript">
{% localize off %}
var is_multi_phase = {{ capabilities.multi_phases|yesno:'true,false' }};
var dashboard_graph_width = {{ frontend_settings.dashboard_graph_width }};
var echarts_loading_options = {text: '{% blocktrans %}Loading...{% endblocktrans %}'};
{% endlocalize %}
</script>
Expand Down
4 changes: 2 additions & 2 deletions dsmr_frontend/tests/models/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@ def test_phase_delivered_l3_color(self):
def test_temperature_color(self):
self.assertEqual(self.instance.temperature_color, '#0073B7')

def test_dashboard_graph_width(self):
self.assertEqual(self.instance.dashboard_graph_width, 30)
def test_live_graphs_hours_range(self):
self.assertEqual(self.instance.live_graphs_hours_range, 24)
5 changes: 0 additions & 5 deletions dsmr_frontend/tests/webinterface/test_dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from dsmr_consumption.models.energysupplier import EnergySupplierPrice
from dsmr_weather.models.settings import WeatherSettings
from dsmr_stats.models.statistics import DayStatistics
from dsmr_frontend.models.settings import FrontendSettings


class TestViews(TestCase):
Expand Down Expand Up @@ -46,10 +45,6 @@ def test_dashboard(self, now_mock):
self.assertIn('datalogger_settings', response.context)
self.assertIn('frontend_settings', response.context)
self.assertIn('notification_count', response.context)
self.assertEqual(
response.context['frontend_settings'].dashboard_graph_width,
FrontendSettings.get_solo().dashboard_graph_width
)


class TestViewsWithoutData(TestViews):
Expand Down
5 changes: 0 additions & 5 deletions dsmr_frontend/tests/webinterface/test_live_graphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from dsmr_consumption.models.energysupplier import EnergySupplierPrice
from dsmr_weather.models.settings import WeatherSettings
from dsmr_stats.models.statistics import DayStatistics
from dsmr_frontend.models.settings import FrontendSettings
from dsmr_weather.models.reading import TemperatureReading


Expand Down Expand Up @@ -44,10 +43,6 @@ def test_live_graphs(self, now_mock):
)
self.assertEqual(response.status_code, 200, response.content)
self.assertIn('frontend_settings', response.context)
self.assertEqual(
response.context['frontend_settings'].dashboard_graph_width,
FrontendSettings.get_solo().dashboard_graph_width
)

@mock.patch('django.utils.timezone.now')
def test_live_xhr_electricity(self, now_mock):
Expand Down
17 changes: 10 additions & 7 deletions dsmr_frontend/views/live_graphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
import dsmr_stats.services


XHR_RECENT_CONSUMPTION_HOURS_AGO = 24


class LiveGraphs(TemplateView):
template_name = 'dsmr_frontend/live-graphs.html'

Expand Down Expand Up @@ -73,8 +70,10 @@ def get(self, request): # noqa: C901
# Optional delta.
latest_delta_id = form.cleaned_data.get('latest_delta_id')

# Optimize queries for large datasets by restricting the data to the last week in the first place.
base_timestamp = timezone.now() - timezone.timedelta(hours=XHR_RECENT_CONSUMPTION_HOURS_AGO)
# Optimize queries for large datasets by restricting the data (when using the installation default).
base_timestamp = timezone.now() - timezone.timedelta(
hours=FrontendSettings.get_solo().live_graphs_hours_range
)
electricity = ElectricityConsumption.objects.filter(read_at__gt=base_timestamp).order_by('read_at')

if latest_delta_id:
Expand Down Expand Up @@ -133,7 +132,9 @@ def get(self, request): # noqa: C901
}

# Optimize queries for large datasets by restricting the data to the last week in the first place.
base_timestamp = timezone.now() - timezone.timedelta(hours=XHR_RECENT_CONSUMPTION_HOURS_AGO)
base_timestamp = timezone.now() - timezone.timedelta(
hours=FrontendSettings.get_solo().live_graphs_hours_range
)
gas = GasConsumption.objects.filter(read_at__gt=base_timestamp).order_by('read_at')

for current in gas:
Expand All @@ -153,7 +154,9 @@ def get(self, request): # noqa: C901
}

# Optimize queries for large datasets by restricting the data to the last week in the first place.
base_timestamp = timezone.now() - timezone.timedelta(hours=XHR_RECENT_CONSUMPTION_HOURS_AGO)
base_timestamp = timezone.now() - timezone.timedelta(
hours=FrontendSettings.get_solo().live_graphs_hours_range
)
temperature = TemperatureReading.objects.filter(read_at__gt=base_timestamp).order_by('read_at')

for current in temperature:
Expand Down
Binary file modified dsmrreader/locales/nl/LC_MESSAGES/django.mo
Binary file not shown.
14 changes: 10 additions & 4 deletions dsmrreader/locales/nl/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -854,11 +854,11 @@ msgstr "Temperatuurkleur"
msgid "Graph color for temperatures read"
msgstr "Grafiekkleur voor temperatuurmetingen"

msgid "Dashboard graph width"
msgstr "Dashboard grafiek breedte"
msgid "Live graphs hours range"
msgstr "Live grafieken urenlimiet"

msgid "The number of items displayed on the X-axis of the dashboard graphs"
msgstr "Het aantal punten dat getoond wordt op de X-as van de Dashboardgrafieken"
msgid "The range of the data displayed in live graphs (increasing it may degrade rendering performance!)"
msgstr "Het bereik van de gegevens in de live grafieken (het verhogen van deze waarde kan resulteren in verslechterde performance van de grafieken!)"

msgid "Gas graph style"
msgstr "Grafiekstijl voor gas"
Expand Down Expand Up @@ -2564,6 +2564,12 @@ msgstr "Nederlands"
msgid "English"
msgstr "Engels"

#~ msgid "Dashboard graph width"
#~ msgstr "Dashboard grafiek breedte"

#~ msgid "The number of items displayed on the X-axis of the dashboard graphs"
#~ msgstr "Het aantal punten dat getoond wordt op de X-as van de Dashboardgrafieken"

#~ msgid "The lifetime of readings, before discarding them. Please note that retention is applied during night time currently, between midnight and six in the morning."
#~ msgstr "De levenduur van metingen, voordat ze opgeschoond worden. N.B.: Momenteel wordt retentie alleen doorgevoerd tijdens de nachturen, tussen middernacht en zes uur 's ochtends."

Expand Down

0 comments on commit fe37858

Please sign in to comment.