]> git.ipfire.org Git - collecty.git/commitdiff
daemon: Initialize all graphs when the daemon starts
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 29 Sep 2025 18:05:27 +0000 (18:05 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 29 Sep 2025 18:05:27 +0000 (18:05 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/daemon/graph.c
src/daemon/graph.h
src/daemon/graphs.c

index ea0049b1ad6884b319f3388280309b12be260cf4..8a2065cebf4b8fe0479f79a824eb26371923e24f 100644 (file)
@@ -31,6 +31,9 @@ struct collecty_graph {
 
        // Daemon
        collecty_daemon* daemon;
+
+       // Implementation
+       const collecty_graph_impl* impl;
 };
 
 static void collecty_graph_free(collecty_graph* self) {
@@ -42,7 +45,7 @@ static void collecty_graph_free(collecty_graph* self) {
 }
 
 int collecty_graph_create(collecty_graph** graph,
-               collecty_ctx* ctx, collecty_daemon* daemon) {
+               collecty_ctx* ctx, collecty_daemon* daemon, const collecty_graph_impl* impl) {
        collecty_graph* self = NULL;
 
        // Allocate some memory
@@ -59,6 +62,9 @@ int collecty_graph_create(collecty_graph** graph,
        // Store a reference to the daemon
        self->daemon = collecty_daemon_ref(daemon);
 
+       // Store the implementation
+       self->impl = impl;
+
        // Return the pointer
        *graph = self;
        return 0;
index 0cf37b0470fb1aa988f02c1227f2d6f9f8fb8260..f0828d86b9a4c2e576d0417c333ed8bc2338ba58 100644 (file)
@@ -26,7 +26,15 @@ typedef struct collecty_graph collecty_graph;
 #include "ctx.h"
 #include "daemon.h"
 
-int collecty_graph_create(collecty_graph** graph, collecty_ctx* ctx, collecty_daemon* daemon);
+typedef struct collecty_graph_impl {
+       const char* name;
+
+       // Available
+       int (*available)(collecty_ctx* ctx, collecty_daemon* daemon);
+} collecty_graph_impl;
+
+int collecty_graph_create(collecty_graph** graph,
+               collecty_ctx* ctx, collecty_daemon* daemon, const collecty_graph_impl* impl);
 
 collecty_graph* collecty_graph_ref(collecty_graph* self);
 collecty_graph* collecty_graph_unref(collecty_graph* self);
index 9daf43af72a7be343e850ca352b3fe51075778a7..137849ef90885b3dc2232ff20104013f36d1d701 100644 (file)
 
 #include "ctx.h"
 #include "daemon.h"
+#include "graph.h"
 #include "graphs.h"
 
+// Register all graphs
+static const collecty_graph_impl* graph_impls[] = {
+       NULL,
+};
+
 struct collecty_graphs {
        collecty_ctx* ctx;
        int nrefs;
 
        // Daemon
        collecty_daemon* daemon;
+
+       // Graphs
+       collecty_graph** graphs;
+       unsigned int num_graphs;
 };
 
+static int collecty_graphs_init(collecty_graphs* self) {
+       collecty_graph** graphs = NULL;
+       collecty_graph* graph = NULL;
+       int r;
+
+       // Initialize all graphs
+       for (const collecty_graph_impl** impl = graph_impls; *impl; impl++) {
+               r = collecty_graph_create(&graph, self->ctx, self->daemon, *impl);
+               if (r < 0)
+                       return r;
+
+               // Make space to store the graph
+               graphs = reallocarray(self->graphs, self->num_graphs + 1, sizeof(*self->graphs));
+               if (!graphs) {
+                       r = -errno;
+                       goto ERROR;
+               }
+
+               // Store a reference to the graph in the array
+               graphs[self->num_graphs++] = collecty_graph_ref(graph);
+
+               // Replace the array
+               self->graphs = graphs;
+
+               // Unref the graph
+               collecty_graph_unref(graph);
+               graph = NULL;
+       }
+
+       return 0;
+
+ERROR:
+       if (graph)
+               collecty_graph_unref(graph);
+
+       return r;
+}
+
 static void collecty_graphs_free(collecty_graphs* self) {
+       if (self->graphs) {
+               for (unsigned int i = 0; i < self->num_graphs; i++)
+                       collecty_graph_unref(self->graphs[i]);
+               free(self->graphs);
+       }
        if (self->daemon)
                collecty_daemon_unref(self->daemon);
        if (self->ctx)
@@ -44,6 +97,7 @@ static void collecty_graphs_free(collecty_graphs* self) {
 int collecty_graphs_create(collecty_graphs** graphs,
                collecty_ctx* ctx, collecty_daemon* daemon) {
        collecty_graphs* self = NULL;
+       int r;
 
        // Allocate some memory
        self = calloc(1, sizeof(*self));
@@ -59,9 +113,20 @@ int collecty_graphs_create(collecty_graphs** graphs,
        // Store a reference to the daemon
        self->daemon = collecty_daemon_ref(daemon);
 
+       // Setup all graphs
+       r = collecty_graphs_init(self);
+       if (r < 0)
+               goto ERROR;
+
        // Return the pointer
        *graphs = self;
        return 0;
+
+ERROR:
+       if (self)
+               collecty_graphs_unref(self);
+
+       return r;
 }
 
 collecty_graphs* collecty_graphs_ref(collecty_graphs* self) {