From: Michael Tremer Date: Fri, 5 Jun 2026 15:37:30 +0000 (+0000) Subject: graphs: Add function to check if we have a source w/ object X-Git-Url: http://git.ipfire.org/gitweb/?a=commitdiff_plain;h=bbb4a7f17b289169e83e09eb74152fb003a908b2;p=telemetry.git graphs: Add function to check if we have a source w/ object Signed-off-by: Michael Tremer --- diff --git a/src/daemon/graph.c b/src/daemon/graph.c index 51caa46..5cfeff4 100644 --- a/src/daemon/graph.c +++ b/src/daemon/graph.c @@ -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; diff --git a/src/daemon/graph.h b/src/daemon/graph.h index 0a86d84..59300eb 100644 --- a/src/daemon/graph.h +++ b/src/daemon/graph.h @@ -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,