From 1f8507508ab182cb079b2fd44f04d8d440c99f5b Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Sat, 20 Oct 2018 04:17:42 -0500 Subject: [PATCH] Make dummy probe use tighter pattern for random readings to make for a more reasonable-looking graph --- rattail_tempmon/client.py | 13 ++++++++++++- rattail_tempmon/db/model.py | 11 +++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/rattail_tempmon/client.py b/rattail_tempmon/client.py index 2b154cb..ffcc721 100644 --- a/rattail_tempmon/client.py +++ b/rattail_tempmon/client.py @@ -181,7 +181,18 @@ class TempmonClient(Daemon): return therm_file.readlines() def random_temp(self, probe): - temp = random.uniform(probe.critical_temp_min - 5, probe.critical_temp_max + 5) + last_reading = probe.last_reading() + if last_reading: + volatility = 2 + # try to keep somewhat of a tight pattern, so graphs look reasonable + last_degrees = float(last_reading.degrees_f) + temp = random.uniform(last_degrees - volatility * 3, last_degrees + volatility * 3) + if temp > (probe.critical_temp_max + volatility * 2): + temp -= volatility + elif temp < (probe.critical_temp_min - volatility * 2): + temp += volatility + else: + temp = random.uniform(probe.critical_temp_min - 5, probe.critical_temp_max + 5) return round(temp, 4) diff --git a/rattail_tempmon/db/model.py b/rattail_tempmon/db/model.py index 4abbd29..181f064 100644 --- a/rattail_tempmon/db/model.py +++ b/rattail_tempmon/db/model.py @@ -297,6 +297,16 @@ class Probe(Base): def __str__(self): return self.description + def last_reading(self): + """ + Returns the reading which was taken most recently for this probe. + """ + session = orm.object_session(self) + return session.query(Reading)\ + .filter(Reading.probe == self)\ + .order_by(Reading.taken.desc())\ + .first() + def start_status(self, status, time): """ Update the "started" timestamp field for the given status. This is @@ -413,6 +423,7 @@ class Reading(Base): """, backref=orm.backref( 'readings', + order_by='Reading.taken', cascade='all, delete-orphan')) taken = sa.Column(sa.DateTime(), nullable=False, default=datetime.datetime.utcnow)