]> git.ipfire.org Git - collecty.git/commitdiff
graphs: Export them over dbus
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 30 Sep 2025 15:51:12 +0000 (15:51 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 30 Sep 2025 15:51:12 +0000 (15:51 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/daemon/daemon.c
src/daemon/daemon.h
src/daemon/graph-bus.c
src/daemon/graph.c
src/daemon/graph.h
src/daemon/graphs.c
src/daemon/graphs.h

index 92715b09842f28c8497d0dcf0fbaca01f6496503..436db95a7cf4cba16b554cfe3768547fd71600e5 100644 (file)
@@ -250,6 +250,13 @@ sd_event* collecty_daemon_loop(collecty_daemon* self) {
        return sd_event_ref(self->loop);
 }
 
+collecty_graphs* collecty_daemon_get_graphs(collecty_daemon* self) {
+       if (self->graphs)
+               return collecty_graphs_ref(self->graphs);
+
+       return NULL;
+}
+
 int collecty_daemon_run(collecty_daemon* self) {
        int r;
 
index 5d965e786243eecbfe7d76d6c134ab61abc96772..ffb42172e7615f0a03a5c50b30c90e688307dae3 100644 (file)
@@ -27,6 +27,7 @@ typedef struct collecty_daemon collecty_daemon;
 
 #include "bus.h"
 #include "ctx.h"
+#include "graphs.h"
 #include "module.h"
 
 int collecty_daemon_create(collecty_daemon** daemon, collecty_ctx* ctx);
@@ -35,6 +36,7 @@ collecty_daemon* collecty_daemon_ref(collecty_daemon* daemon);
 collecty_daemon* collecty_daemon_unref(collecty_daemon* daemon);
 
 sd_event* collecty_daemon_loop(collecty_daemon* self);
+collecty_graphs* collecty_daemon_get_graphs(collecty_daemon* self);
 
 int collecty_daemon_run(collecty_daemon* self);
 
index 8e5139c09162ac5ecff47385dee3d0ebf346f484..f2820ece1aa066400a068ad7f82b816ae02ace15 100644 (file)
 
 static int collecty_graph_node_enumerator(sd_bus* bus,
                const char* path, void* data, char*** nodes, sd_bus_error* error) {
-       //collecty_daemon* daemon = data;
+       collecty_daemon* daemon = data;
+       collecty_graphs* graphs = NULL;
+       int r = 1;
+
+       // Fetch all graphs
+       graphs = collecty_daemon_get_graphs(daemon);
+       if (!graphs)
+               goto ERROR;
+
+       // Fetch the bus paths
+       *nodes = collecty_graphs_get_bus_paths(graphs);
+       if (*nodes)
+               goto ERROR;
+
+       // Success
+       r = 0;
 
-       // XXX TODO
+ERROR:
+       if (graphs)
+               collecty_graphs_unref(graphs);
 
-       return 0;
+       return r;
 }
 
 static int collecty_graph_object_find(sd_bus* bus, const char* path,
                const char* interface, void* data, void** found, sd_bus_error* error) {
        collecty_daemon* daemon = data;
+       collecty_graphs* graphs = NULL;
        collecty_graph* graph = NULL;
        char* name = NULL;
        int r;
 
        // Decode the path of the requested object
        r = sd_bus_path_decode(path, "/org/ipfire/collecty1/graph", &name);
-       if (r <= 0)
-               return 0;
+       if (r <= 0) {
+               r = 0;
+               goto ERROR;
+       }
+
+       // Fetch all graphs
+       graphs = collecty_daemon_get_graphs(daemon);
+       if (!graphs) {
+               r = 0;
+               goto ERROR;
+       }
 
-#if 0
        // Find the graph
-       graph = collecty_daemon_get_graph_by_name(daemon, name);
-       if (!graph)
-               return 0;
-#endif
+       graph = collecty_graphs_get_by_name(graphs, name);
+       if (!graph) {
+               r = 0;
+               goto ERROR;
+       }
 
        // Match!
-       *found = collecty_graph_unref(graph);
+       *found = graph;
+
+       // Success
+       r = 1;
+
+ERROR:
+       if (graphs)
+               collecty_graphs_unref(graphs);
+       if (graph)
+               collecty_graph_unref(graph);
 
-       return 1;
+       return r;
 }
 
 static const sd_bus_vtable collecty_graph_vtable[] = {
index 8a2065cebf4b8fe0479f79a824eb26371923e24f..d03f8f1c370eed5b98dbd48020f2437905e85ce5 100644 (file)
@@ -82,3 +82,25 @@ collecty_graph* collecty_graph_unref(collecty_graph* self) {
        collecty_graph_free(self);
        return NULL;
 }
+
+const char* collecty_graph_get_name(collecty_graph* self) {
+       return self->impl->name;
+}
+
+char* collecty_graph_get_bus_path(collecty_graph* self) {
+       const char* name = NULL;
+       char* path = NULL;
+       int r;
+
+       // Fetch the name
+       name = collecty_graph_get_name(self);
+       if (!name)
+               return NULL;
+
+       // Format the bus path
+       r = sd_bus_path_encode("/org/ipfire/collecty1/graph", name, &path);
+       if (r < 0)
+               return NULL;
+
+       return path;
+}
index f0828d86b9a4c2e576d0417c333ed8bc2338ba58..8330c1e7fb451ebcc292881c9e9b7dab3532bfe7 100644 (file)
@@ -39,4 +39,7 @@ int collecty_graph_create(collecty_graph** graph,
 collecty_graph* collecty_graph_ref(collecty_graph* self);
 collecty_graph* collecty_graph_unref(collecty_graph* self);
 
+const char* collecty_graph_get_name(collecty_graph* self);
+char* collecty_graph_get_bus_path(collecty_graph* self);
+
 #endif /* COLLECTY_GRAPH_H */
index 5f96e95baae87b8f366fd56bd34593580be1a938..ebd9c61ba5227f5136b909daf2c6a50b5604458d 100644 (file)
@@ -145,3 +145,54 @@ collecty_graphs* collecty_graphs_unref(collecty_graphs* self) {
        collecty_graphs_free(self);
        return NULL;
 }
+
+collecty_graph* collecty_graphs_get_by_name(collecty_graphs* self, const char* name) {
+       const char* n = NULL;
+
+       // Iterate over all graphs to find a match
+       for (unsigned int i = 0; i < self->num_graphs; i++) {
+               // Fetch the name
+               n = collecty_graph_get_name(self->graphs[i]);
+               if (!n)
+                       continue;
+
+               // Return the object if the name matches
+               if (strcmp(name, n) == 0)
+                       return collecty_graph_ref(self->graphs[i]);
+       }
+
+       return NULL;
+}
+
+char** collecty_graphs_get_bus_paths(collecty_graphs* self) {
+       char** paths = NULL;
+       char* path = NULL;
+
+       // Allocate an array to store all the strings
+       paths = calloc(self->num_graphs + 1, sizeof(*path));
+       if (!paths)
+               goto ERROR;
+
+       // Add all paths for all graphs
+       for (unsigned int i = 0; i < self->num_graphs; i++) {
+               // Fetch the bus path
+               path = collecty_graph_get_bus_path(self->graphs[i]);
+               if (!path)
+                       continue;
+
+               // Assign to the output array
+               paths[i] = path;
+       }
+
+       return paths;
+
+ERROR:
+       if (paths) {
+               for (unsigned int i = 0; i < self->num_graphs; i++)
+                       if (paths[i])
+                               free(paths[i]);
+               free(paths);
+       }
+
+       return NULL;
+}
index 92c2f0cea45e53fa69a5f3a401743831c012a766..aa07b65b6e56248227cbfba9c08d0cfe0772018c 100644 (file)
@@ -25,10 +25,14 @@ typedef struct collecty_graphs collecty_graphs;
 
 #include "ctx.h"
 #include "daemon.h"
+#include "graph.h"
 
 int collecty_graphs_create(collecty_graphs** graphs, collecty_ctx* ctx, collecty_daemon* daemon);
 
 collecty_graphs* collecty_graphs_ref(collecty_graphs* self);
 collecty_graphs* collecty_graphs_unref(collecty_graphs* self);
 
+collecty_graph* collecty_graphs_get_by_name(collecty_graphs* self, const char* name);
+char** collecty_graphs_get_bus_paths(collecty_graphs* self);
+
 #endif /* COLLECTY_GRAPHS_H */