1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <errno.h>
6 #include "action_search_json.h"
7 #include "common.h"
8 #include "graph.h"
9 #include "graph_ident.h"
10 #include "graph_instance.h"
11 #include "graph_list.h"
12 #include "utils_cgi.h"
14 #include <fcgiapp.h>
15 #include <fcgi_stdio.h>
17 #define RESULT_LIMIT 10
19 struct callback_data_s
20 {
21 graph_config_t *cfg;
22 int limit;
23 _Bool first;
24 };
25 typedef struct callback_data_s callback_data_t;
27 static int json_begin_graph (graph_config_t *cfg) /* {{{ */
28 {
29 char desc[1024];
31 if (cfg == NULL)
32 return (EINVAL);
34 graph_get_title (cfg, desc, sizeof (desc));
36 printf ("{\"title\":\"%s\",\"instances\":[", desc);
38 return (0);
39 } /* }}} int json_begin_graph */
41 static int json_end_graph (void) /* {{{ */
42 {
43 printf ("]}");
45 return (0);
46 } /* }}} int json_end_graph */
48 static int json_print_instance (graph_config_t *cfg, /* {{{ */
49 graph_instance_t *inst)
50 {
51 char params[1024];
52 char desc[1024];
54 if ((cfg == NULL) || (inst == NULL))
55 return (EINVAL);
57 memset (desc, 0, sizeof (desc));
58 inst_describe (cfg, inst, desc, sizeof (desc));
60 memset (params, 0, sizeof (params));
61 inst_get_params (cfg, inst, params, sizeof (params));
63 printf ("{\"description\":\"%s\",\"params\":\"%s\"}",
64 desc, params);
66 return (0);
67 } /* }}} int json_print_instance */
69 static int json_print_graph_instance (graph_config_t *cfg, /* {{{ */
70 graph_instance_t *inst,
71 void *user_data)
72 {
73 callback_data_t *data = user_data;
75 if (data->cfg != cfg)
76 {
77 if (!data->first)
78 {
79 json_end_graph ();
80 printf (",\n");
81 }
82 json_begin_graph (cfg);
84 data->cfg = cfg;
85 data->first = 0;
86 }
87 else /* if (not first instance) */
88 {
89 printf (",\n");
90 }
92 json_print_instance (cfg, inst);
94 if (data->limit > 0)
95 data->limit--;
97 if (data->limit == 0)
98 return (1);
100 return (0);
101 } /* }}} int json_print_graph_instance */
103 static int list_graphs_json (const char *term) /* {{{ */
104 {
105 callback_data_t data;
107 time_t now;
108 char time_buffer[128];
109 int status;
111 printf ("Content-Type: application/json\n");
113 now = time (NULL);
114 status = time_to_rfc1123 (now + 300, time_buffer, sizeof (time_buffer));
115 if (status == 0)
116 printf ("Expires: %s\n"
117 "Cache-Control: public\n",
118 time_buffer);
119 printf ("\n");
121 data.cfg = NULL;
122 data.limit = RESULT_LIMIT;
123 data.first = 1;
125 printf ("[\n");
126 if (term == NULL)
127 gl_instance_get_all (json_print_graph_instance, /* user_data = */ &data);
128 else
129 gl_search (term, json_print_graph_instance, /* user_data = */ &data);
131 if (!data.first)
132 json_end_graph ();
134 printf ("\n]");
136 return (0);
137 } /* }}} int list_graphs_json */
139 int action_search_json (void) /* {{{ */
140 {
141 char *search;
142 int status;
144 gl_update ();
146 search = strtolower_copy (param ("q"));
148 status = list_graphs_json (search);
150 free (search);
152 return (status);
153 } /* }}} int action_search_json */
155 /* vim: set sw=2 sts=2 et fdm=marker : */