X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Faction_show_graph_json.c;h=0820fb025614997ae39aca7737ded1406c82a75f;hb=a9177c62be39c0b47777f1d3547fca2612c73003;hp=402627ce36eb7e9a810ebd1e5698bca6de057473;hpb=d746867e2a940a8f752e781c866243f775777338;p=collection4.git diff --git a/src/action_show_graph_json.c b/src/action_show_graph_json.c index 402627c..0820fb0 100644 --- a/src/action_show_graph_json.c +++ b/src/action_show_graph_json.c @@ -40,72 +40,42 @@ #include #include -struct show_graph_data_s +static void write_callback (__attribute__((unused)) void *ctx, /* {{{ */ + const char *str, unsigned int len) { - graph_config_t *cfg; - _Bool first; -}; -typedef struct show_graph_data_s show_graph_data_t; - -static int show_instance_cb (graph_instance_t *inst, /* {{{ */ - void *user_data) -{ - show_graph_data_t *data = user_data; - graph_ident_t *ident; - char *ident_json; - - ident = inst_get_selector (inst); - if (ident == NULL) - return (ENOMEM); - - ident_json = ident_to_json (ident); - if (ident_json == NULL) - { - ident_destroy (ident); - return (ENOMEM); - } - - if (!data->first) - printf (",\n"); - data->first = 0; - - printf (" %s", ident_json); - - free (ident_json); - ident_destroy (ident); - return (0); -} /* }}} int show_instance_cb */ + fwrite ((void *) str, /* size = */ len, /* nmemb = */ 1, stdout); +} /* }}} void write_callback */ int action_show_graph_json (void) /* {{{ */ { - show_graph_data_t data; + graph_config_t *cfg; + + yajl_gen_config handler_config; + yajl_gen handler; time_t now; char time_buffer[128]; int status; - char title[1024]; - graph_ident_t *ident; - char *ident_json; - - memset (&data, 0, sizeof (data)); - data.first = 1; - data.cfg = gl_graph_get_selected (); - if (data.cfg == NULL) + cfg = gl_graph_get_selected (); + if (cfg == NULL) return (ENOMEM); - ident = graph_get_selector (data.cfg); - if (ident == NULL) - return (ENOMEM); + memset (&handler_config, 0, sizeof (handler_config)); + handler_config.beautify = 1; + handler_config.indentString = " "; - ident_json = ident_to_json (ident); - if (ident_json == NULL) + handler = yajl_gen_alloc2 (write_callback, + &handler_config, + /* alloc functions = */ NULL, + /* context = */ NULL); + if (handler == NULL) { - ident_destroy (ident); - return (ENOMEM); + graph_destroy (cfg); + return (-1); } - printf ("Content-Type: text/plain\n"); + printf ("Content-Type: application/json\n"); now = time (NULL); status = time_to_rfc1123 (now + 300, time_buffer, sizeof (time_buffer)); @@ -115,21 +85,12 @@ int action_show_graph_json (void) /* {{{ */ time_buffer); printf ("\n"); - memset (title, 0, sizeof (title)); - graph_get_title (data.cfg, title, sizeof (title)); - json_escape_buffer (title, sizeof (title)); - - printf ("{\n"); - printf (" \"title\": \"%s\",\n", title); - printf (" \"selector\": %s,\n", ident_json); - printf (" \"instances\":\n"); - printf (" [\n"); - graph_inst_foreach (data.cfg, show_instance_cb, &data); - printf ("\n ]\n}\n"); - - free (ident_json); - ident_destroy (ident); - return (0); + status = graph_to_json (cfg, handler); + + graph_destroy (cfg); + yajl_gen_free (handler); + + return (status); } /* }}} int action_show_graph_json */ /* vim: set sw=2 sts=2 et fdm=marker : */