From: Michael Tremer Date: Fri, 3 Oct 2025 11:02:04 +0000 (+0000) Subject: graph: Add title and vertical label X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=33831aa06d97d06eb8fdfa91736b463cafb10f7f;p=collecty.git graph: Add title and vertical label Signed-off-by: Michael Tremer --- diff --git a/src/daemon/graph.c b/src/daemon/graph.c index 46a9994..5cbb5e3 100644 --- a/src/daemon/graph.c +++ b/src/daemon/graph.c @@ -203,6 +203,8 @@ ERROR: int collecty_graph_render(collecty_graph* self, const char* object, const collecty_graph_render_options* options, char** buffer, size_t* length) { collecty_args* args = NULL; + char* vlabel = NULL; + char* title = NULL; char** data = NULL; FILE* f = NULL; int w = 0; @@ -269,6 +271,40 @@ int collecty_graph_render(collecty_graph* self, const char* object, goto ERROR; } + // Set the title + if (self->impl->title) { + // Fetch the title + r = self->impl->title(self->ctx, self, object, &title); + if (r < 0) { + ERROR(self->ctx, "Failed to render the title: %s\n", strerror(-r)); + goto ERROR; + } + + // Add the title to the command line + if (title) { + r = collecty_args_push(args, "--title=%s", title); + if (r < 0) + goto ERROR; + } + } + + // Set the vertical label + if (self->impl->vlabel) { + // Fetch the vertical label + r = self->impl->vlabel(self->ctx, self, object, &vlabel); + if (r < 0) { + ERROR(self->ctx, "Failed to render the vertical label: %s\n", strerror(-r)); + goto ERROR; + } + + // Add the vertical label to the command line + if (vlabel) { + r = collecty_args_push(args, "--vertical-label=%s", vlabel); + if (r < 0) + goto ERROR; + } + } + // Write the graph to the output stream r = collecty_args_push(args, "-"); if (r < 0) @@ -316,6 +352,10 @@ ERROR: free(data[i]); free(data); } + if (vlabel) + free(vlabel); + if (title) + free(title); if (args) collecty_args_unref(args); if (f) diff --git a/src/daemon/graph.h b/src/daemon/graph.h index 7e12e4a..f935214 100644 --- a/src/daemon/graph.h +++ b/src/daemon/graph.h @@ -42,6 +42,14 @@ typedef struct collecty_graph_impl { // Render! int (*render)(collecty_ctx* ctx, collecty_graph* graph, collecty_args* args, const char* object); + + // Title + int (*title)(collecty_ctx* ctx, collecty_graph* graph, + const char* object, char** title); + + // Vertical Label + int (*vlabel)(collecty_ctx* ctx, collecty_graph* graph, + const char* object, char** title); } collecty_graph_impl; int collecty_graph_create(collecty_graph** graph, diff --git a/src/daemon/graphs/loadavg.c b/src/daemon/graphs/loadavg.c index 9e5f791..c258ff5 100644 --- a/src/daemon/graphs/loadavg.c +++ b/src/daemon/graphs/loadavg.c @@ -23,6 +23,7 @@ #include "../colors.h" #include "../graph.h" #include "../i18n.h" +#include "../util.h" #include "loadavg.h" // Set some colors @@ -30,6 +31,11 @@ #define COLOR_LOAD5 ORANGE #define COLOR_LOAD1 YELLOW +static int loadavg_title(collecty_ctx* ctx, collecty_graph* graph, + const char* object, char** title) { + return collecty_format_title(title, "%s", _("Load Average")); +} + static int loadavg_render(collecty_ctx* ctx, collecty_graph* graph, collecty_args* args, const char* object) { int r; @@ -151,6 +157,8 @@ static int loadavg_render(collecty_ctx* ctx, const collecty_graph_impl loadavg_graph = { .name = "LoadAverage", .render = loadavg_render, + .title = loadavg_title, + .vlabel = loadavg_title, // Limits .lower_limit = 0, diff --git a/src/daemon/util.c b/src/daemon/util.c index 34e0de9..093ed1d 100644 --- a/src/daemon/util.c +++ b/src/daemon/util.c @@ -19,6 +19,7 @@ #############################################################################*/ #include +#include #include #include @@ -69,3 +70,20 @@ ERROR: return r; } + +// Helper function to set the title +int collecty_format_title(char** title, const char* format, ...) { + va_list args; + int r; + + // Format the title + va_start(args, format); + r = vasprintf(title, format, args); + va_end(args); + + // Abort on errors + if (r < 0) + return -errno; + + return 0; +} diff --git a/src/daemon/util.h b/src/daemon/util.h index 8b8542c..bbe075b 100644 --- a/src/daemon/util.h +++ b/src/daemon/util.h @@ -39,4 +39,8 @@ int __collecty_format_number(char* buffer, size_t length, int number); int collecty_file_read_uint64(const char* path, uint64_t* number); +// Helper function to set the title +int collecty_format_title(char** title, const char* format, ...) + __attribute__((format(printf, 2, 3))); + #endif /* COLLECTY_UTIL_H */