]> git.ipfire.org Git - collecty.git/commitdiff
graph: Create the basic to render a graph
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 1 Oct 2025 17:06:09 +0000 (17:06 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 1 Oct 2025 17:06:51 +0000 (17:06 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/daemon/graph.c

index 696ad4b414d9618ebe0f21083e9e0471814c8b8c..39d8069236fb77c7381ac699ea2c68679f79e7d3 100644 (file)
@@ -21,6 +21,9 @@
 #include <errno.h>
 #include <stdlib.h>
 
+#include <rrd.h>
+
+#include "args.h"
 #include "ctx.h"
 #include "daemon.h"
 #include "graph.h"
@@ -107,15 +110,55 @@ char* collecty_graph_get_bus_path(collecty_graph* self) {
 
 int collecty_graph_render(collecty_graph* self,
                const char* object, char** buffer, size_t* length) {
-       char* p = NULL;
+       collecty_args* args = NULL;
+       FILE* f = NULL;
+       int w = 0;
+       int h = 0;
        int r;
 
-       r = asprintf(&p, "GRAPH");
+       // Log action
+       DEBUG(self->ctx, "Rendering %s...\n", collecty_graph_get_name(self));
+
+       // Open a file handle to the output buffer
+       f = open_memstream(buffer, length);
+       if (!f) {
+               ERROR(self->ctx, "Failed to open memory stream: %m\n");
+               r = -errno;
+               goto ERROR;
+       }
+
+       // Allocate a new argument list
+       r = collecty_args_create(&args, self->ctx);
        if (r < 0)
-               return r;
+               goto ERROR;
 
-       *buffer = p;
-       *length = r;
+       // XXX TODO Fill this with content
 
-       return 0;
+       // Dump the command
+       r = collecty_args_dump(args);
+       if (r < 0)
+               goto ERROR;
+
+       // Render the graph
+       r = rrd_graph(collecty_args_argc(args), (char**)collecty_args_argv(args),
+                       NULL, &w, &h, f, NULL, NULL);
+       if (r < 0) {
+               ERROR(self->ctx, "Failed to generate the graph: %s\n", rrd_get_error());
+               rrd_clear_error();
+               r = -ENOTSUP;
+               goto ERROR;
+       }
+
+       // Log action
+       DEBUG(self->ctx, "Rendered graph %s:\n", collecty_graph_get_name(self));
+       DEBUG(self->ctx, "    Width  : %d\n", w);
+       DEBUG(self->ctx, "    Height : %d\n", h);
+
+ERROR:
+       if (args)
+               collecty_args_unref(args);
+       if (f)
+               fclose(f);
+
+       return r;
 }