Tweak display options for tempmon probe readings graph

This commit is contained in:
Lance Edgar 2023-07-07 17:56:45 -05:00
parent 4729785b05
commit a84bcf688b
2 changed files with 14 additions and 18 deletions

View file

@ -39,7 +39,13 @@
</b-field> </b-field>
<b-field horizontal label="Showing"> <b-field horizontal label="Showing">
${time_range} <b-select v-model="currentTimeRange"
@input="timeRangeChanged">
<option value="last hour">Last Hour</option>
<option value="last 6 hours">Last 6 Hours</option>
<option value="last day">Last Day</option>
<option value="last week">Last Week</option>
</b-select>
</b-field> </b-field>
</div> </div>
@ -86,7 +92,9 @@
this.chart.destroy() this.chart.destroy()
} }
this.$http.get('${url('{}.graph_readings'.format(route_prefix), uuid=probe.uuid)}', {params: {'time-range': timeRange}}).then(({ data }) => { let url = '${url(f'{route_prefix}.graph_readings', uuid=probe.uuid)}'
let params = {'time-range': timeRange}
this.$http.get(url, {params: params}).then(({ data }) => {
this.chart = new Chart(this.$refs.tempchart, { this.chart = new Chart(this.$refs.tempchart, {
type: 'scatter', type: 'scatter',

View file

@ -26,12 +26,11 @@ Views for tempmon probes
import datetime import datetime
from rattail.time import make_utc, localtime
from rattail_tempmon.db import model as tempmon from rattail_tempmon.db import model as tempmon
import colander import colander
from deform import widget as dfwidget from deform import widget as dfwidget
from webhelpers2.html import tags, HTML from webhelpers2.html import tags
from tailbone import forms, grids from tailbone import forms, grids
from tailbone.views.tempmon import MasterView from tailbone.views.tempmon import MasterView
@ -258,27 +257,16 @@ class TempmonProbeView(MasterView):
selected = self.request.session.get(key, 'last hour') selected = self.request.session.get(key, 'last hour')
self.request.session[key] = selected self.request.session[key] = selected
range_options = tags.Options([
tags.Option("Last Hour", 'last hour'),
tags.Option("Last 6 Hours", 'last 6 hours'),
tags.Option("Last Day", 'last day'),
tags.Option("Last Week", 'last week'),
])
time_range = HTML.tag('b-select', c=[range_options.render()],
**{'v-model': 'currentTimeRange',
'@input': 'timeRangeChanged'})
context = { context = {
'probe': probe, 'probe': probe,
'parent_title': str(probe), 'parent_title': str(probe),
'parent_url': self.get_action_url('view', probe), 'parent_url': self.get_action_url('view', probe),
'time_range': time_range,
'current_time_range': selected, 'current_time_range': selected,
} }
return self.render_to_response('graph', context) return self.render_to_response('graph', context)
def graph_readings(self): def graph_readings(self):
app = self.get_rattail_app()
probe = self.get_instance() probe = self.get_instance()
key = 'tempmon.probe.{}.graph_time_range'.format(probe.uuid) key = 'tempmon.probe.{}.graph_time_range'.format(probe.uuid)
@ -299,7 +287,7 @@ class TempmonProbeView(MasterView):
raise NotImplementedError("Unknown time range: {}".format(selected)) raise NotImplementedError("Unknown time range: {}".format(selected))
# figure out which readings we need to graph # figure out which readings we need to graph
cutoff = make_utc() - datetime.timedelta(seconds=cutoff) cutoff = app.make_utc() - datetime.timedelta(seconds=cutoff)
readings = self.Session.query(tempmon.Reading)\ readings = self.Session.query(tempmon.Reading)\
.filter(tempmon.Reading.probe == probe)\ .filter(tempmon.Reading.probe == probe)\
.filter(tempmon.Reading.taken >= cutoff)\ .filter(tempmon.Reading.taken >= cutoff)\
@ -308,7 +296,7 @@ class TempmonProbeView(MasterView):
# convert readings to data for scatter plot # convert readings to data for scatter plot
data = [{ data = [{
'x': localtime(self.rattail_config, reading.taken, from_utc=True).isoformat(), 'x': app.localtime(reading.taken, from_utc=True).isoformat(),
'y': float(reading.degrees_f), 'y': float(reading.degrees_f),
} for reading in readings] } for reading in readings]
return data return data