From eaff87d6917ff4744d5ed6fdd5c791a724211835 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Fri, 3 Oct 2025 10:40:29 +0000 Subject: [PATCH] graph: Add upper and lower limits Signed-off-by: Michael Tremer --- src/daemon/graph.c | 37 +++++++++++++++++++++++++++++++++++++ src/daemon/graph.h | 5 +++++ src/daemon/graphs/loadavg.c | 6 ++++++ 3 files changed, 48 insertions(+) diff --git a/src/daemon/graph.c b/src/daemon/graph.c index cb04642..1073db4 100644 --- a/src/daemon/graph.c +++ b/src/daemon/graph.c @@ -19,6 +19,7 @@ #############################################################################*/ #include +#include #include #include @@ -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) diff --git a/src/daemon/graph.h b/src/daemon/graph.h index 16d6c16..7e12e4a 100644 --- a/src/daemon/graph.h +++ b/src/daemon/graph.h @@ -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); diff --git a/src/daemon/graphs/loadavg.c b/src/daemon/graphs/loadavg.c index eb5a8ab..9e5f791 100644 --- a/src/daemon/graphs/loadavg.c +++ b/src/daemon/graphs/loadavg.c @@ -18,6 +18,8 @@ # # #############################################################################*/ +#include + #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, }; -- 2.47.3