From: Florian Forster Date: Wed, 22 Dec 2010 16:53:10 +0000 (+0100) Subject: "graph_def_json" action: Make this work with ad-hoc DEFs, too. X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=13fbd841132a9bb8ddf83f614662113b3b36653c;p=collection4.git "graph_def_json" action: Make this work with ad-hoc DEFs, too. --- diff --git a/src/action_graph_def_json.c b/src/action_graph_def_json.c index 1ba0678..2003d2f 100644 --- a/src/action_graph_def_json.c +++ b/src/action_graph_def_json.c @@ -32,6 +32,7 @@ #include "action_graph_def_json.h" #include "common.h" #include "graph.h" +#include "graph_instance.h" #include "graph_list.h" #include "utils_cgi.h" @@ -50,6 +51,7 @@ static void write_callback (__attribute__((unused)) void *ctx, /* {{{ */ int action_graph_def_json (void) /* {{{ */ { graph_config_t *cfg; + graph_instance_t *inst; yajl_gen_config handler_config; yajl_gen handler; @@ -62,6 +64,10 @@ int action_graph_def_json (void) /* {{{ */ if (cfg == NULL) return (ENOMEM); + inst = inst_get_selected (cfg); + if (inst == NULL) + return (EINVAL); + memset (&handler_config, 0, sizeof (handler_config)); handler_config.beautify = 1; handler_config.indentString = " "; @@ -83,7 +89,7 @@ int action_graph_def_json (void) /* {{{ */ time_buffer); printf ("\n"); - status = graph_def_to_json (cfg, handler); + status = graph_def_to_json (cfg, inst, handler); yajl_gen_free (handler); diff --git a/src/graph.c b/src/graph.c index deeb05a..713d45f 100644 --- a/src/graph.c +++ b/src/graph.c @@ -649,7 +649,8 @@ int graph_to_json (const graph_config_t *cfg, /* {{{ */ return (0); } /* }}} int graph_to_json */ -int graph_def_to_json (const graph_config_t *cfg, /* {{{ */ +int graph_def_to_json (graph_config_t *cfg, /* {{{ */ + graph_instance_t *inst, yajl_gen handler) { #define yajl_gen_string_cast(h,p,l) \ @@ -676,7 +677,18 @@ int graph_def_to_json (const graph_config_t *cfg, /* {{{ */ yajl_gen_bool (handler, cfg->show_zero); yajl_gen_string_cast (handler, "defs", strlen ("defs")); - def_to_json (cfg->defs, handler); + if (cfg->defs == NULL) + { + graph_def_t *defs; + + defs = inst_get_default_defs (cfg, inst); + def_to_json (defs, handler); + def_destroy (defs); + } + else + { + def_to_json (cfg->defs, handler); + } yajl_gen_map_close (handler); diff --git a/src/graph.h b/src/graph.h index 2873103..46964e5 100644 --- a/src/graph.h +++ b/src/graph.h @@ -112,7 +112,9 @@ int graph_inst_search_field (graph_config_t *cfg, int graph_compare (graph_config_t *cfg, const graph_ident_t *ident); int graph_to_json (const graph_config_t *cfg, yajl_gen handler); -int graph_def_to_json (const graph_config_t *cfg, yajl_gen handler); +int graph_def_to_json (graph_config_t *cfg, + graph_instance_t *inst, + yajl_gen handler); size_t graph_num_instances (graph_config_t *cfg); diff --git a/src/graph_instance.c b/src/graph_instance.c index a834950..2249cc0 100644 --- a/src/graph_instance.c +++ b/src/graph_instance.c @@ -56,6 +56,33 @@ typedef struct def_callback_data_s def_callback_data_t; /* * Private functions */ +struct ident_get_default_defs__data_s +{ + char **dses; + size_t dses_num; +}; +typedef struct ident_get_default_defs__data_s ident_get_default_defs__data_t; + +static int ident_get_default_defs__callback (__attribute__((unused)) + graph_ident_t *ident, + const char *ds_name, void *user_data) +{ + ident_get_default_defs__data_t *data = user_data; + char **tmp; + + tmp = realloc (data->dses, (data->dses_num + 1) * sizeof (data->dses)); + if (tmp == NULL) + return (ENOMEM); + data->dses = tmp; + + data->dses[data->dses_num] = strdup (ds_name); + if (data->dses[data->dses_num] == NULL) + return (ENOMEM); + + data->dses_num++; + return (0); +} /* }}} int ident_get_default_defs__callback */ + /* Create one DEF for each data source in the file. Called by * "inst_get_default_defs" for each file. */ static graph_def_t *ident_get_default_defs (graph_config_t *cfg, /* {{{ */ @@ -63,11 +90,13 @@ static graph_def_t *ident_get_default_defs (graph_config_t *cfg, /* {{{ */ { graph_def_t *defs = NULL; char *file; - char **dses = NULL; - size_t dses_num = 0; + ident_get_default_defs__data_t ds_data; int status; size_t i; + ds_data.dses = NULL; + ds_data.dses_num = 0; + if ((cfg == NULL) || (ident == NULL)) return (def_head); @@ -78,22 +107,23 @@ static graph_def_t *ident_get_default_defs (graph_config_t *cfg, /* {{{ */ return (def_head); } - status = ds_list_from_rrd_file (file, &dses_num, &dses); + status = data_provider_get_ident_ds_names (ident, + ident_get_default_defs__callback, &ds_data); if (status != 0) { free (file); return (def_head); } - for (i = 0; i < dses_num; i++) + for (i = 0; i < ds_data.dses_num; i++) { graph_def_t *def; - def = def_search (def_head, ident, dses[i]); + def = def_search (def_head, ident, ds_data.dses[i]); if (def != NULL) continue; - def = def_create (cfg, ident, dses[i]); + def = def_create (cfg, ident, ds_data.dses[i]); if (def == NULL) continue; @@ -102,45 +132,15 @@ static graph_def_t *ident_get_default_defs (graph_config_t *cfg, /* {{{ */ else def_append (defs, def); - free (dses[i]); + free (ds_data.dses[i]); } - free (dses); + free (ds_data.dses); free (file); return (defs); } /* }}} int ident_get_default_defs */ -/* Create one or more DEFs for each file in the graph instance. The number - * depends on the number of data sources in each of the files. Called from - * "inst_get_rrdargs" if no DEFs are available from the configuration. - * */ -static graph_def_t *inst_get_default_defs (graph_config_t *cfg, /* {{{ */ - graph_instance_t *inst) -{ - graph_def_t *defs = NULL; - size_t i; - - if ((cfg == NULL) || (inst == NULL)) - return (NULL); - - for (i = 0; i < inst->files_num; i++) - { - graph_def_t *def; - - def = ident_get_default_defs (cfg, inst->files[i], defs); - if (def == NULL) - continue; - - if (defs == NULL) - defs = def; - else - def_append (defs, def); - } - - return (defs); -} /* }}} graph_def_t *inst_get_default_defs */ - /* Called with each DEF in turn. Calls "def_get_rrdargs" with every appropriate * file / DEF pair. */ static int gl_instance_get_rrdargs_cb (graph_def_t *def, void *user_data) /* {{{ */ @@ -374,6 +374,36 @@ int inst_get_rrdargs (graph_config_t *cfg, /* {{{ */ return (status); } /* }}} int inst_get_rrdargs */ +/* Create one or more DEFs for each file in the graph instance. The number + * depends on the number of data sources in each of the files. Called from + * "inst_get_rrdargs" if no DEFs are available from the configuration. + * */ +graph_def_t *inst_get_default_defs (graph_config_t *cfg, /* {{{ */ + graph_instance_t *inst) +{ + graph_def_t *defs = NULL; + size_t i; + + if ((cfg == NULL) || (inst == NULL)) + return (NULL); + + for (i = 0; i < inst->files_num; i++) + { + graph_def_t *def; + + def = ident_get_default_defs (cfg, inst->files[i], defs); + if (def == NULL) + continue; + + if (defs == NULL) + defs = def; + else + def_append (defs, def); + } + + return (defs); +} /* }}} graph_def_t *inst_get_default_defs */ + graph_ident_t *inst_get_selector (graph_instance_t *inst) /* {{{ */ { if (inst == NULL) diff --git a/src/graph_instance.h b/src/graph_instance.h index ebf6bc6..5685853 100644 --- a/src/graph_instance.h +++ b/src/graph_instance.h @@ -55,6 +55,9 @@ int inst_get_params (graph_config_t *cfg, graph_instance_t *inst, int inst_get_rrdargs (graph_config_t *cfg, graph_instance_t *inst, rrd_args_t *args); +graph_def_t *inst_get_default_defs (graph_config_t *cfg, + graph_instance_t *inst); + /* Returns a copy of the selector which must be freed by the caller. */ graph_ident_t *inst_get_selector (graph_instance_t *inst);