src/dp_rrdtool.c: Adapt to new callback prototype.
[collection4.git] / src / utils_search.c
index 648c2ec..1a1be7d 100644 (file)
@@ -28,6 +28,8 @@
 #include <errno.h>
 
 #include "utils_search.h"
+#include "graph_ident.h"
+#include "graph_instance.h"
 #include "utils_array.h"
 
 #include <fcgiapp.h>
@@ -44,6 +46,9 @@ struct search_info_s
   str_array_t *terms;
 };
 
+/*
+ * Private functions
+ */
 static char *read_quoted_string (const char **buffer) /* {{{ */
 {
   const char *ptr = *buffer;
@@ -151,7 +156,7 @@ static char *next_token (const char **buffer) /* {{{ */
   return (ret);
 } /* }}} char *next_token */
 
-static int store_token_field (char **field, const char *token)
+static int store_token_field (char **field, const char *token) /* {{{ */
 {
   char *copy;
 
@@ -168,7 +173,7 @@ static int store_token_field (char **field, const char *token)
   return (0);
 } /* }}} int store_token_field */
 
-static int store_token (search_info_t *si, const char *token)
+static int store_token (search_info_t *si, const char *token) /* {{{ */
 {
   if (strncmp ("host:", token, strlen ("host:")) == 0)
     return (store_token_field (&si->host, token + strlen ("host:")));
@@ -184,6 +189,9 @@ static int store_token (search_info_t *si, const char *token)
   return (array_append (si->terms, token));
 } /* }}} int store_token */
 
+/*
+ * Public functions
+ */
 search_info_t *search_parse (const char *search) /* {{{ */
 {
   const char *ptr;
@@ -227,4 +235,129 @@ void search_destroy (search_info_t *si) /* {{{ */
   array_destroy (si->terms);
 } /* }}} void search_destroy */
 
+_Bool search_has_selector (search_info_t *si) /* {{{ */
+{
+  if (si == NULL)
+    return (0);
+
+  if ((si->host != NULL)
+      || (si->plugin != NULL) || (si->plugin_instance != NULL)
+      || (si->type != NULL) || (si->type_instance != NULL))
+    return (1);
+
+  return (0);
+} /* }}} _Bool search_has_selector */
+
+graph_ident_t *search_to_ident (search_info_t *si) /* {{{ */
+{
+  if (si == NULL)
+    return (NULL);
+
+  return (ident_create ((si->host == NULL) ? ANY_TOKEN : si->host,
+        (si->plugin == NULL) ? ANY_TOKEN : si->plugin,
+        (si->plugin_instance == NULL) ? ANY_TOKEN : si->plugin_instance,
+        (si->type == NULL) ? ANY_TOKEN : si->type,
+        (si->type_instance == NULL) ? ANY_TOKEN : si->type_instance));
+} /* }}} graph_ident_t *search_to_ident */
+
+search_info_t *search_from_ident (const graph_ident_t *ident) /* {{{ */
+{
+  search_info_t *si;
+
+  if (ident == NULL)
+    return (NULL);
+
+  si = malloc (sizeof (*si));
+  if (si == NULL)
+    return (NULL);
+  memset (si, 0, sizeof (*si));
+  si->terms = NULL;
+
+#define COPY_FIELD(f) do {                                                   \
+  const char *tmp = ident_get_##f (ident);                                   \
+  if (tmp == NULL)                                                           \
+    si->f = NULL;                                                            \
+  else                                                                       \
+    si->f = strdup (tmp);                                                    \
+} while (0)
+
+  COPY_FIELD(host);
+  COPY_FIELD(plugin);
+  COPY_FIELD(plugin_instance);
+  COPY_FIELD(type);
+  COPY_FIELD(type_instance);
+
+#undef COPY_FIELD
+
+  return (si);
+} /* }}} search_info_t *search_from_ident */
+
+_Bool search_graph_title_matches (search_info_t *si, /* {{{ */
+    const char *title)
+{
+  char **argv;
+  int argc;
+  int i;
+
+  if ((si == NULL) || (title == NULL))
+    return (0);
+
+  if (si->terms == NULL)
+    return (1);
+
+  argc = array_argc (si->terms);
+  argv = array_argv (si->terms);
+  for (i = 0; i < argc; i++)
+    if (strstr (title, argv[i]) == NULL)
+      return (0);
+
+  return (1);
+} /* }}} _Bool search_graph_title_matches */
+
+_Bool search_graph_inst_matches (search_info_t *si, /* {{{ */
+    graph_config_t *cfg, graph_instance_t *inst,
+    const char *title)
+{
+  char **argv;
+  int argc;
+  int i;
+
+  if ((si == NULL) || (cfg == NULL) || (inst == NULL))
+    return (0);
+
+  if ((si->host != NULL)
+      && !inst_matches_field (inst, GIF_HOST, si->host))
+    return (0);
+  else if ((si->plugin != NULL)
+      && !inst_matches_field (inst, GIF_PLUGIN, si->plugin))
+    return (0);
+  else if ((si->plugin_instance != NULL)
+      && !inst_matches_field (inst, GIF_PLUGIN_INSTANCE, si->plugin_instance))
+    return (0);
+  else if ((si->type != NULL)
+      && !inst_matches_field (inst, GIF_TYPE, si->type))
+    return (0);
+  else if ((si->type_instance != NULL)
+      && !inst_matches_field (inst, GIF_TYPE_INSTANCE, si->type_instance))
+    return (0);
+
+  if (si->terms == NULL)
+    return (1);
+
+  argc = array_argc (si->terms);
+  argv = array_argv (si->terms);
+  for (i = 0; i < argc; i++)
+  {
+    if (inst_matches_string (cfg, inst, argv[i]))
+      continue;
+
+    if ((title != NULL) && (strstr (title, argv[i]) != NULL))
+      continue;
+
+    return (0);
+  }
+
+  return (1);
+} /* }}} _Bool search_graph_inst_matches */
+
 /* vim: set sw=2 sts=2 et fdm=marker : */