diff options
author | Elena ``of Valhalla'' Grandi <valhalla@trueelena.org> | 2023-12-27 13:10:01 +0100 |
---|---|---|
committer | Elena ``of Valhalla'' Grandi <valhalla@trueelena.org> | 2023-12-29 07:29:22 +0100 |
commit | e9c843a4eb817daf249167b79ba3c60237bb93e9 (patch) | |
tree | ea60527374dc05f333dd6122fc1517e68558b8bf /rrd/models.py | |
parent | 27bc8de7c7f8e29ba10324b56fef9fc4e80a9fd6 (diff) |
Generate graphs when data is updated.
Diffstat (limited to 'rrd/models.py')
-rw-r--r-- | rrd/models.py | 29 |
1 files changed, 28 insertions, 1 deletions
diff --git a/rrd/models.py b/rrd/models.py index 8d995c8..1d1e5e9 100644 --- a/rrd/models.py +++ b/rrd/models.py @@ -80,6 +80,9 @@ class DataSource(ModelWithPerms): except ValueError as e: log.warning("Could not update ds: %s", e) + for graph in self.graph_set.all(): + graph.update() + class Graph(ModelWithPerms): title = models.CharField(max_length=64) @@ -89,11 +92,35 @@ class Graph(ModelWithPerms): recursive=True, max_length=512, ) - rrd_config = models.TextField() + rrd_config = models.TextField( + default=settings.RRD_GRAPH_CONFIG + ) def __str__(self): return self.title + def update(self): + graph_path = os.path.join(settings.RRD_GRAPH_PATH, self.path) + os.makedirs(os.path.dirname(graph_path), exist_ok=True) + rrd_paths = [] + rrd_topics = [] + rrd_ds_names = [] + for ds in self.data_sources.all(): + rrd_paths.append(os.path.join(settings.RRD_DB_PATH, ds.path)) + rrd_topics.append(ds.topic) + rrd_ds_names.append(ds.topic.split("/")[-1]) + opts = self.rrd_config.format( + topics=rrd_topics, + ds_names=rrd_ds_names, + ds_paths=rrd_paths, + title=self.title, + ).strip().split('\n') + opts = [o.strip() for o in opts] + rrdtool.graph( + graph_path, + * opts + ) + class Dashboard(ModelWithPerms): title = models.CharField(max_length=64) |