]> git.ipfire.org Git - collecty.git/commitdiff
graph: Add title and vertical label
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 3 Oct 2025 11:02:04 +0000 (11:02 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 3 Oct 2025 11:02:04 +0000 (11:02 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/daemon/graph.c
src/daemon/graph.h
src/daemon/graphs/loadavg.c
src/daemon/util.c
src/daemon/util.h

index 46a9994b724fa50b89dd8a38b0475010d8a60d73..5cbb5e38eeebb7ae1d30f76b2d2cac82cfcf70c7 100644 (file)
@@ -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)
index 7e12e4a2fc35e49e8638c1f1f77c40259ee8f6cc..f9352149e622dce81e12c696024e4a8dfcd39911 100644 (file)
@@ -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,
index 9e5f791fa6eb452851160aeed06f4f9e56693149..c258ff55a1a0ef7f51cdd066783dbdc29a94d115 100644 (file)
@@ -23,6 +23,7 @@
 #include "../colors.h"
 #include "../graph.h"
 #include "../i18n.h"
+#include "../util.h"
 #include "loadavg.h"
 
 // Set some colors
 #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,
index 34e0de9555c7149f0045f19bbe2c2ea6b1e8bcd6..093ed1d920541d17b91cd743b6c8de6aae8e68e4 100644 (file)
@@ -19,6 +19,7 @@
 #############################################################################*/
 
 #include <errno.h>
+#include <stdarg.h>
 #include <stdint.h>
 #include <stdio.h>
 
@@ -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;
+}
index 8b8542cb3e51cb1ff1c5d9729941cb0fe1327c93..bbe075bf11a5623a9120c112cdd41f7a3d9dddaf 100644 (file)
@@ -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 */