aboutsummaryrefslogtreecommitdiff
path: root/rrd/models.py
diff options
context:
space:
mode:
authorElena ``of Valhalla'' Grandi <valhalla@trueelena.org>2023-12-09 15:26:02 +0100
committerElena ``of Valhalla'' Grandi <valhalla@trueelena.org>2023-12-29 07:29:15 +0100
commit27bc8de7c7f8e29ba10324b56fef9fc4e80a9fd6 (patch)
tree4616e597b77995c7870c1b803999616394c14b9e /rrd/models.py
parentf2916ba1e749dc3aca47a256e1472e5032e2c9c8 (diff)
Start recording mqtt data in rss.
Diffstat (limited to 'rrd/models.py')
-rw-r--r--rrd/models.py53
1 files changed, 50 insertions, 3 deletions
diff --git a/rrd/models.py b/rrd/models.py
index 6adc17f..8d995c8 100644
--- a/rrd/models.py
+++ b/rrd/models.py
@@ -1,7 +1,13 @@
+import logging
+import os
+
import django.contrib.auth.models as amodels
+import rrdtool
from django.conf import settings
from django.db import models
+log = logging.getLogger(__name__)
+
class ModelWithPerms(models.Model):
users_read = models.ManyToManyField(
@@ -30,29 +36,70 @@ class DataSource(ModelWithPerms):
# which is probably too much for a sensible db
topic = models.CharField(max_length=512)
path = models.FilePathField(
- path=settings.RRD_DB_PATH,
+ path=settings.RRD_DB_PATH.as_posix(),
recursive=True,
max_length=512,
)
- rrd_config = models.TextField()
+ rrd_config = models.TextField(
+ default=settings.RRD_DS_CONFIG
+ )
active = models.BooleanField(
default=True,
)
+ def __str__(self):
+ return self.topic
+
+ @property
+ def lastupdate(self):
+ try:
+ last = rrdtool.lastupdate(os.path.join(
+ settings.RRD_DB_PATH, self.path
+ ))
+ except rrdtool.OperationalError as e:
+ log.warning("Failure reading from ds: %s", e)
+ return (None, None)
+ else:
+ return last["date"], last["ds"][self.topic.split("/")[-1]]
+
+ def update(self, ts, value):
+ rrd_path = os.path.join(settings.RRD_DB_PATH, self.path)
+ if not os.path.isfile(rrd_path):
+ rrdtool.create(
+ os.path.join(settings.RRD_DB_PATH, self.path),
+ "--no-overwrite",
+ self.rrd_config.format(
+ ds_name=self.topic.split("/")[-1]
+ ).strip().split('\n'),
+ )
+ try:
+ rrdtool.update(
+ os.path.join(settings.RRD_DB_PATH, self.path),
+ str(ts) + ":" + str(value)
+ )
+ except ValueError as e:
+ log.warning("Could not update ds: %s", e)
+
class Graph(ModelWithPerms):
title = models.CharField(max_length=64)
data_sources = models.ManyToManyField(DataSource)
path = models.FilePathField(
- path=settings.RRD_GRAPH_PATH,
+ path=settings.RRD_GRAPH_PATH.as_posix(),
recursive=True,
max_length=512,
)
rrd_config = models.TextField()
+ def __str__(self):
+ return self.title
+
class Dashboard(ModelWithPerms):
title = models.CharField(max_length=64)
graphs = models.ManyToManyField(Graph)
data_sources = models.ManyToManyField(DataSource)
template = models.TextField()
+
+ def __str__(self):
+ return self.title