2 * collection4 - action_search_json.c
3 * Copyright (C) 2010 Florian octo Forster
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301 USA
21 * Florian octo Forster <ff at octo.it>
29 #include "action_search_json.h"
32 #include "graph_ident.h"
33 #include "graph_instance.h"
34 #include "graph_list.h"
35 #include "utils_cgi.h"
38 #include <fcgi_stdio.h>
40 #define RESULT_LIMIT 10
42 struct callback_data_s
48 typedef struct callback_data_s callback_data_t;
50 static int json_begin_graph (graph_config_t *cfg) /* {{{ */
57 graph_get_title (cfg, desc, sizeof (desc));
59 printf ("{\"title\":\"%s\",\"instances\":[", desc);
62 } /* }}} int json_begin_graph */
64 static int json_end_graph (void) /* {{{ */
69 } /* }}} int json_end_graph */
71 static int json_print_instance (graph_config_t *cfg, /* {{{ */
72 graph_instance_t *inst)
77 if ((cfg == NULL) || (inst == NULL))
80 memset (desc, 0, sizeof (desc));
81 inst_describe (cfg, inst, desc, sizeof (desc));
83 memset (params, 0, sizeof (params));
84 inst_get_params (cfg, inst, params, sizeof (params));
86 printf ("{\"description\":\"%s\",\"params\":\"%s\"}",
90 } /* }}} int json_print_instance */
92 static int json_print_graph_instance (graph_config_t *cfg, /* {{{ */
93 graph_instance_t *inst,
96 callback_data_t *data = user_data;
105 json_begin_graph (cfg);
110 else /* if (not first instance) */
115 json_print_instance (cfg, inst);
120 if (data->limit == 0)
124 } /* }}} int json_print_graph_instance */
126 static int list_graphs_json (const char *term) /* {{{ */
128 callback_data_t data;
131 char time_buffer[128];
134 printf ("Content-Type: application/json\n");
137 status = time_to_rfc1123 (now + 300, time_buffer, sizeof (time_buffer));
139 printf ("Expires: %s\n"
140 "Cache-Control: public\n",
145 data.limit = RESULT_LIMIT;
150 gl_instance_get_all (json_print_graph_instance, /* user_data = */ &data);
152 gl_search (term, json_print_graph_instance, /* user_data = */ &data);
160 } /* }}} int list_graphs_json */
162 int action_search_json (void) /* {{{ */
169 search = strtolower_copy (param ("q"));
171 status = list_graphs_json (search);
176 } /* }}} int action_search_json */
178 /* vim: set sw=2 sts=2 et fdm=marker : */