]> git.ipfire.org Git - telemetry.git/commitdiff
graphs: Add function to check if we have a source w/ object
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 5 Jun 2026 15:37:30 +0000 (15:37 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 5 Jun 2026 15:37:30 +0000 (15:37 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/daemon/graph.c
src/daemon/graph.h

index 51caa462b8df218adb16a294e5466f699911c8f5..5cfeff44f494b4fc711a4bef396c4eb1b9d76301 100644 (file)
@@ -195,6 +195,67 @@ char* td_graph_get_bus_path(td_graph* self) {
        return path;
 }
 
+/*
+       This is a test function that will check if a certain source
+       and optionally an object are available.
+*/
+int td_graph_requires_source(td_graph* self, const char* name, const char* object) {
+       td_sources* sources = NULL;
+       td_source* source = NULL;
+       int r = 0;
+
+       // Fetch all sources
+       sources = td_daemon_get_sources(self->daemon);
+       if (!sources) {
+               r = -ENOTSUP;
+               goto ERROR;
+       }
+
+       // Fetch the source by its name
+       source = td_sources_get_by_name(sources, name);
+       if (!source) {
+               DEBUG(self->ctx, "Cannot render %s because source '%s' is not available\n",
+                               td_graph_get_name(self), name);
+               r = -ENOENT;
+               goto ERROR;
+       }
+
+       // Cannot render if the source has been disabled
+       if (td_source_is_disabled(source)) {
+               DEBUG(self->ctx, "Cannot render %s because source '%s' is disabled\n",
+                               td_graph_get_name(self), name);
+               r = -ENODATA;
+               goto ERROR;
+       }
+
+       // Check the object
+       if (object)     {
+               r = td_source_has_object(source, object);
+               switch (r) {
+                       // No match
+                       case 0:
+                               r = -ENODATA;
+                               goto ERROR;
+
+                       // Match
+                       case 1:
+                               break;
+
+                       // Raise any other errors
+                       default:
+                               goto ERROR;
+               }
+       }
+
+ERROR:
+       if (sources)
+               td_sources_unref(sources);
+       if (source)
+               td_source_unref(source);
+
+       return r;
+}
+
 int td_graph_load_source(td_graph* self,
                td_args* args, const char* name, const char* object) {
        td_sources* sources = NULL;
index 0a86d8400df5cfcba1b9052516af39f1b457a2b6..59300eb46ac8996e42e1221ddd0f279a786854f1 100644 (file)
@@ -98,6 +98,8 @@ td_graph* td_graph_unref(td_graph* self);
 const char* td_graph_get_name(td_graph* self);
 char* td_graph_get_bus_path(td_graph* self);
 
+int td_graph_requires_source(td_graph* self, const char* name, const char* object);
+
 int td_graph_load_source(td_graph* self,
        td_args* args, const char* name, const char* object);
 int td_graph_load_sources(td_graph* self,