]> git.ipfire.org Git - collecty.git/commitdiff
graph: Add upper and lower limits
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 3 Oct 2025 10:40:29 +0000 (10:40 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 3 Oct 2025 10:42:03 +0000 (10:42 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/daemon/graph.c
src/daemon/graph.h
src/daemon/graphs/loadavg.c

index cb04642ed46d1ce9313cce4edcb12aaaf463799f..1073db46a14b2d776705f38bf6cb6b66e3c81944 100644 (file)
@@ -19,6 +19,7 @@
 #############################################################################*/
 
 #include <errno.h>
+#include <limits.h>
 #include <stdlib.h>
 
 #include <rrd.h>
@@ -70,6 +71,16 @@ const char* DEFAULT_RENDER_ARGS[] = {
 const unsigned int DEFAULT_HEIGHT = 480;
 const unsigned int DEFAULT_WIDTH  = 960;
 
+static int collecty_graph_check(collecty_graph* self) {
+       // Check if upper and lower limits are set (at least one must be set)
+       if (!self->impl->lower_limit && !self->impl->upper_limit) {
+               ERROR(self->ctx, "lower_limit and upper_limit are not set\n");
+               return -EINVAL;
+       }
+
+       return 0;
+}
+
 static void collecty_graph_free(collecty_graph* self) {
        if (self->daemon)
                collecty_daemon_unref(self->daemon);
@@ -81,6 +92,7 @@ static void collecty_graph_free(collecty_graph* self) {
 int collecty_graph_create(collecty_graph** graph,
                collecty_ctx* ctx, collecty_daemon* daemon, const collecty_graph_impl* impl) {
        collecty_graph* self = NULL;
+       int r;
 
        // Allocate some memory
        self = calloc(1, sizeof(*self));
@@ -99,9 +111,20 @@ int collecty_graph_create(collecty_graph** graph,
        // Store the implementation
        self->impl = impl;
 
+       // Perform some basic checks
+       r = collecty_graph_check(self);
+       if (r < 0)
+               goto ERROR;
+
        // Return the pointer
        *graph = self;
        return 0;
+
+ERROR:
+       if (self)
+               collecty_graph_unref(self);
+
+       return r;
 }
 
 collecty_graph* collecty_graph_ref(collecty_graph* self) {
@@ -229,6 +252,20 @@ int collecty_graph_render(collecty_graph* self, const char* object,
        if (r < 0)
                goto ERROR;
 
+       // Set lower limit
+       if (self->impl->lower_limit > -LONG_MAX) {
+               r = collecty_args_push(args, "--lower-limit=%ld", self->impl->lower_limit);
+               if (r < 0)
+                       goto ERROR;
+       }
+
+       // Set upper limit
+       if (self->impl->upper_limit < LONG_MAX) {
+               r = collecty_args_push(args, "--upper-limit=%ld", self->impl->upper_limit);
+               if (r < 0)
+                       goto ERROR;
+       }
+
        // Write the graph to the output stream
        r = collecty_args_push(args, "-");
        if (r < 0)
index 16d6c162b3720de34dad057e05e6ee040910c784..7e12e4a2fc35e49e8638c1f1f77c40259ee8f6cc 100644 (file)
@@ -29,8 +29,13 @@ typedef struct collecty_graph collecty_graph;
 #include "graph.h"
 
 typedef struct collecty_graph_impl {
+       // Name
        const char* name;
 
+       // Limits
+       long lower_limit;
+       long upper_limit;
+
        // Available
        int (*available)(collecty_ctx* ctx, collecty_daemon* daemon);
 
index eb5a8ab0ce6fa39b517528b1bd3023f64c6cf39d..9e5f791fa6eb452851160aeed06f4f9e56693149 100644 (file)
@@ -18,6 +18,8 @@
 #                                                                             #
 #############################################################################*/
 
+#include <limits.h>
+
 #include "../colors.h"
 #include "../graph.h"
 #include "../i18n.h"
@@ -149,4 +151,8 @@ static int loadavg_render(collecty_ctx* ctx,
 const collecty_graph_impl loadavg_graph = {
        .name    = "LoadAverage",
        .render  = loadavg_render,
+
+       // Limits
+       .lower_limit = 0,
+       .upper_limit = LONG_MAX,
 };